リベースのinteractiveモード( git rebase -i ) を利用すると、コミット履歴を綺麗に整理することができます。ここでは、rebaseを利用して、コミットの「順序入れ替え」「再編集」「統合」「メッセージ変更」をしてみます。
検証準備
ここでは、下記のコミット履歴を調整していきます。
echo "Hello World" > test1.txt
git add test1.txt
git commit -m "1st commit"
echo "Hello World" > test2.txt
git add test2.txt
git commit -m "2nd commit"
echo "AAAAA" >> test1.txt
git add test1.txt
git commit -m "3rd commit"
echo "BBBBB" >> test2.txt
git add test2.txt
git commit -m "4th commit"$ git log --oneline --reverse
d2bb6ce 1st commit
6d68bc5 2nd commit
d65800f 3rd commit
4a2135d 4th commitrebaseでコミットログを調整
順序入れ替え
コミット順序を入れ替える方法を解説します。
$ git log --oneline --reverse
d2bb6ce 1st commit
6d68bc5 2nd commit
d65800f 3rd commit
4a2135d 4th commit2nd commit と 3rd commit の順序を入れ替えてみます。
1st commit より後のコミット履歴を修正したいので、以下コマンドを実行します。
$ git rebase -i d2bb6ceviが立ち上がり、以下内容が表示されました。
pick 6d68bc5 2nd commit
pick d65800f 3rd commit
pick 4a2135d 4th commit
# Rebase d2bb6ce..4a2135d onto d2bb6ce (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented outコミット順序を入れ替えたいので、 3rd commit を 2nd commit の前に記述します。
pick d65800f 3rd commit
pick 6d68bc5 2nd commit
pick 4a2135d 4th commitこの状態で保存します。
以下のようにコミット順序が入れ替わったことを確認できました。
$ git log --oneline --reverse
d2bb6ce 1st commit
0d94c48 3rd commit
2a44d0a 2nd commit
68b52f6 4th commit再編集 ( e )
すでにコミット済みの内容を変更する方法を解説します。1つ前のコミットであれば amend を活用できますが、 2つ以上前のコミットの場合 rebase を利用します。
$ git log --oneline --reverse
d2bb6ce 1st commit
0d94c48 3rd commit
2a44d0a 2nd commit
68b52f6 4th commit3rd commit の変更内容を再編集してみます。
$ git diff d2bb6ce 0d94c48
diff --git a/test1.txt b/test1.txt
index 557db03..7e482bb 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1,2 @@
Hello World
+AAAAA現状ですと、 3rd commit では、 test1.txt に AAAAA の文字列を追記しています。
以下コマンドを実行します。
$ git rebase -i d2bb6ce3rd commit を編集したいので、以下のようにします。
pick 0d94c48 3rd commit
pick 2a44d0a 2nd commit
pick 68b52f6 4th commite 0d94c48 3rd commit
pick 2a44d0a 2nd commit
pick 68b52f6 4th commitこの状態で保存すると以下メッセージが表示されました。
Stopped at 0d94c48... 3rd commit
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continueコミット履歴は以下のようになっています。
$ git log --oneline --reverse
d2bb6ce 1st commit
0d94c48 3rd committest1.txt に rebase edit という文字列を追記して、コミットをやり直します。
$ echo "rebase edit" >> test1.txt
$ git add test1.txt
$ git commit --amend3rd commit の再編集が完了したので、rebase作業を終了させます。
終了させるには、 git rebase --continue を実行します。
$ git rebase --continuerebase作業を終了させたので、 3rd commit の後続コミットが反映された状態になりました。
$ git log --oneline --reverse
d2bb6ce 1st commit
147a5d6 3rd commit
6574c68 2nd commit
6c97d16 4th commit再度、 3rd commit の変更内容を確認してみます。
$ git diff d2bb6ce 147a5d6
diff --git a/test1.txt b/test1.txt
index 557db03..36585a9 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1,3 @@
Hello World
+AAAAA
+rebase edit3rd commit で、 test1.txt に rebase edit の文字列も追記された状態になりました。
統合 ( s )
複数のコミットを1つに統合する方法を解説します。
$ git log --oneline --reverse
d2bb6ce 1st commit
147a5d6 3rd commit
6574c68 2nd commit
6c97d16 4th commit1st commit と 3rd commit を統合します。
一番最初のコミットから修正するので、以下コマンドを実行します。
git rebase -i --root3rd commit を 1st commit に統合したいので、以下のようにします。
pick d2bb6ce 1st commit
pick 147a5d6 3rd commit
pick 6574c68 2nd commit
pick 6c97d16 4th commitpick d2bb6ce 1st commit
s 147a5d6 3rd commit
pick 6574c68 2nd commit
pick 6c97d16 4th commitこの状態で保存すると、統合したコミットのメッセージ編集状態になります。
# This is a combination of 2 commits.
# This is the 1st commit message:
1st commit
# This is the commit message #2:
3rd commitここでは、以下コミットメッセージにします。
1st commit & 3rd commitこの状態で保存して、コミットログを確認します。
$ git log --oneline --reverse
670e063 1st commit & 3rd commit
85248fa 2nd commit
6d9e93a 4th commit$ git log -p 670e063
commit 670e06338471a7aa92f68fdde4a6ea187925a726
Author: wakuwaku_bank <wakuwakubank@gmail.com>
Date: Sun Nov 4 13:56:43 2018 +0900
1st commit & 3rd commit
diff --git a/test1.txt b/test1.txt
new file mode 100644
index 0000000..36585a9
--- /dev/null
+++ b/test1.txt
@@ -0,0 +1,3 @@
+Hello World
+AAAAA
+rebase edit1st commit と 3rd commit で行った変更内容が1つのコミットに統合されました。
コミットメッセージ変更( r )
2つ以上前のコミットメッセージを変更する方法を解説します。
$ git log --oneline --reverse
670e063 1st commit & 3rd commit
85248fa 2nd commit
6d9e93a 4th commit85248fa のコミットメッセージを編集してみます。
$ git rebase -i HEAD~2以下のように修正します。
pick 85248fa 2nd commit
pick 6d9e93a 4th commitr 85248fa 2nd commit
pick 6d9e93a 4th commitこの状態で保存すると、 85248fa のコミットメッセージを編集できるようになります。2nd commit reword というメッセージに変更して、保存します。
2nd commit2nd commit reword以下のように、コミットメッセージが変更されたことを確認できます。
$ git log --oneline --reverse
670e063 1st commit & 3rd commit
4ce8573 2nd commit reword
dfcefcc 4th commit