« 「git commit --amend」と「git reset」の挙動 | メイン | 「git rebase --onto newbase upstream branch」の意味 »

「git rebase」はコミットの移動ではない

「git rebase」はコミットの移動だと思っていたがコミットの再適用だった。以下実験。
$ git init
$ touch aaaaa
$ git add aaaaa
$ git commit -m "a"
$ touch bbbbb
$ git add bbbbb
$ git commit -m "b"
$ touch ccccc
$ git add ccccc
$ git commit -m "c"
$ git branch branch1
$ git branch branch2 HEAD^^
$ git checkout branch2
$ touch ddddd
$ git add ddddd
$ git commit -m "d"
これで次のような状態になる。「[]」は現在のブランチ、「.」に続くのはブランチ名。
 [d.branch2]
 /
a---b---c.branch1.master
次にbranch1をbranch2の先端にリベースしてみる。
$ git checkout branch1
$ git rebase branch2
結果は次のようになる。
  d.branch2---b---c.branch1
 /
a---b---c.master
ここでそれぞれのブランチの履歴を見てみる。
$ git checkout master 
$ git log --pretty="oneline"
7693a9a91c96c7650de8115545e254b58c822b3d c
fe9f674ad0b724fe995ccc94240d673ea5bde02e b
5652af90e342454863003be1218a7cf7851ac9b8 a
$ git checkout branch1
$ git log --pretty="oneline"
3466d40431341822e9acaed3837b5c0b3f9fc478 c
de4ffa617d694be1b23de94b1062c0e97fe1e7eb b
6c7d5011324965a2ec5a198f089d30e6437aee38 d
5652af90e342454863003be1218a7cf7851ac9b8 a
$ git checkout branch2
$ git log --pretty="oneline"
6c7d5011324965a2ec5a198f089d30e6437aee38 d
5652af90e342454863003be1218a7cf7851ac9b8 a
注目したいのはbranch1とmasterそれぞれのコミットのbとcのコミットIDが違うこと。つまりリベースは、コミットを単純に移動するのではなく、コミット内に格納している変更内容を移動先に順番に適用していくということらしい。根本的な考え方は、前回のエントリーに書いた「git commit -amend」や「git reset」と同じようだ。

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)

Google

タグ クラウド