0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gitについて勉強する。第七弾 (リベースでコミットの履歴を書き換えよう 編)

Last updated at Posted at 2019-10-06

リベースとは

履歴をきれいにしつつ、変更を統合するのがリベースです。

コマンド

  • git rebase "ブランチ名"

リベース元の履歴がリベース先のブランチの後ろに追加される形で一本化されます。

仮にブランチが「master」と「feature」の2つがあったとします。
(改修などを行う作業ブランチはfeatureと名付けるところが多いよ!)

featureで作業していてmasterに変更がありました。
featureにmasterの変更を取り込みたい場合、通常はmasterをfeatureにマージして取り込みます。
その場合、コミット状況が以下のようになります。

スクリーンショット 2019-10-06 22.24.05.png

これをgit rebase masterで行うと以下のように一本化された状態になります。

スクリーンショット 2019-10-06 22.24.26.png

mergeであれば、featureコミット3の親コミットはmasterコミット1ですが、rebaseを使用する事によって、featureコミット3の親コミットが、masterコミット2となります。

コミット履歴を変更する方法

GitHubにPushする前に、コミット履歴をきれいにする場合に使用します。

  • git rebase -i "コミットID"
  • git rebase -i HEAD~3 (直前のコミットから3つ前を変更)

修正する際の流れ

  1. git rebase -i で修正対象を選択する。修正するものに関しては、pick→editのように書き換える。
  2. git commit --amend で直前のコミットを修正する。
  3. git rebase --continueで次のコミットに移動する。移動先がない場合、完了。

コミットの順番を入れ替える

git rebase -i "コミットID"を使用すると、エディタが立ち上がり、履歴が表示されます。 ファイルを編集して、入れ替えればOKです。

コミットをまとめる

git rebase -i を使用して表示されたファイルに、まとめたいものに対して、pick→squashを使用すれば、指定したものをすべてまとめることができます。

コミットを分割する

  1. git rebase -i "コミットID"
  2. 対象のコミットをeditに変更
  3. git reset HEAD^ でコミットを取り消す
  4. git add と git commitで分割したい単位でコミットし直す。
  5. git rebase --continueで終了

次・第八弾

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?