作業ノート

様々なまとめ、雑感など

gitでマージコミットが含まれるブランチをrebaseする

確認したのは以下のバージョン。

$ git --version
git version 1.7.12.4

例えば以下のように、マージコミットを含むブランチのコメントを編集したいとき。

$ git log --graph --pretty=format:'%h -%d %s' --abbrev-commit --date=relative
*   cd00264 - (HEAD, master) Merge branch 'branch-b'
|\
| * 8f7b4c7 - add b.txt
|/
*   521e66f - Merge branch 'branch-a'
|\
| * f5622e4 - add a.txt
|/
* 0d8bbff - new

この状態でrebaseを行うときに

$ git rebase -i HEAD~~

-iオプションだけでは

pick f5622e4 add a.txt
pick 8f7b4c7 add b.txt

# Rebase 0d8bbff..cd00264 onto 0d8bbff (       2 TODO item(s))
#
# 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
#
...

マージコミットは含まれない。マージコミットがあるときは

git rebase -i -p HEAD~~

-pオプションをつけて実行すると

pick f5622e4 add a.txt
pick 521e66f Merge branch 'branch-a'
pick 8f7b4c7 add b.txt
pick cd00264 Merge branch 'branch-b'

# Rebase 0d8bbff..cd00264 onto 0d8bbff (       4 TODO item(s))
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
...

指定した範囲のマージコミットが含まれる。

これで該当コミットのコマンドを

$ git rebase -i -p HEAD~~

pick f5622e4 add a.txt
edit 521e66f Merge branch 'branch-a'
pick 8f7b4c7 add b.txt
pick cd00264 Merge branch 'branch-b'

# Rebase 0d8bbff..cd00264 onto 0d8bbff (       4 TODO item(s))
#
# 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
#
...

editに変更し

$ git commit --amend -m 'changed commit message'
[detached HEAD e3571bb] changed commit message
 Date: Tue Mar 24 22:27:59 2015 +0900

commitコマンドに--amendオプションをつけ、コメントを編集する。

そして

$ git rebase --continue
Successfully rebased and updated refs/heads/master.

rebaseを進めると

$ git log --graph --pretty=format:'%h -%d %s' --abbrev-commit --date=relative
*   b8b15b8 - (HEAD, master) Merge branch 'branch-b'
|\
| * c5ca283 - add b.txt
|/
*   e3571bb - changed commit message
|\
| * f5622e4 - add a.txt
|/
* 0d8bbff - new

該当のマージコミットのコメントが変更される。

参考