先に結論
- 過去のcommitを修正したい時は、
git commit --fixup=#{commit番号}
してから、git rebase --autofixup
--autofixup
オプションは、git commit --fixup
した commit を自動的にfixup
してくれるgit rebase
のfixup
は、対象の commit を上の commit と結合させる命令- 詳しくは 前回
例えばこんな状態
* 8a66664 (HEAD -> refs/heads/branch) Add 02.txt * c85bd12 Add aaa on 01.txt * 7b24435 Add 01.txt * a8bc429 (refs/heads/master) first commit
branch
ブランチで、01.txt を作って、中身に 'aaa' を入れて、その後 02.txt を作った
ここで、01.txt に入れる内容は 'bbb' であったことに気づく!
さあどうする
ここで、 git commit --fixup
の登場!
- 引数に、修正対象のcommit番号を入れる
g ci --fixup=c85bd12 [branch 63cb5d4] fixup! Add aaa on 01.txt 1 file changed, 1 insertion(+), 1 deletion(-)
すると、自動的にコミットメッセージが生成されて commit される
* 63cb5d4 (HEAD -> refs/heads/branch) fixup! Add aaa on 01.txt * 8a66664 Add 02.txt * c85bd12 Add aaa on 01.txt * 7b24435 Add 01.txt * a8bc429 (refs/heads/master) first commit
git rebase --autofixup
git rebase --autofixup
すると、--fixup
した commit をこんな風にいい感じにしてくれる
git rebase master -i --autofixup
pick 7b24435 Add 01.txt pick c85bd12 Add aaa on 01.txt fixup 63cb5d4 fixup! Add aaa on 01.txt pick 8a66664 Add 02.txt
最終的なcommitログ
* 0423c38 (HEAD -> refs/heads/branch) Add 02.txt * 362238a Add aaa on 01.txt * 7b24435 Add 01.txt * a8bc429 (refs/heads/master) first commit
c85bd12
と 63cb5d4
が統合された!!
便利
2-3個 commit を重ねた後に、間違い気付いた場合にさっと修正できるのが気持ち良い!
追記
今回のような修正の場合は git commit --squash
と、 git rebase --autosquash
の方がよいかも
- squash なので、圧縮後にコミットメッセージを修正できる
追記2
autofixup
と、autosquash
オプションを on にしておくと楽。
- rebase 時に勝手に、
--autosquash
--autofixup
をつけてくれる
.gitconfig
[rebase] autofixup = true autosquash = true
参考
追記
first commit を rebase したい場合
git rebase -i --root