LoginSignup
57
53

More than 5 years have passed since last update.

git rebase -i のための rebase.autosquash オプション

Last updated at Posted at 2013-05-12

rebase.autosquash オプションって何?

git rebase -i hoge を実行した時に、特定のcommitが指定したcommitの後に移動した状態で編集用テキストを開くオプション
コミットログがfoo fooのcommitが存在する場合、squash! foo foo というコミットログでcommitを行うと、git rebase -i hoge を行った時に、下記の状態で編集用テキストが開く

pick a7f1c68 foo foo
squash d5be25c squash! foo foo

わかりにくいかもしれないので実例

autosquash[1-6].txt を二つづつ順番にgitにcommitしていくときに、autosquash4.txtのcommitをとばしちゃったのを修正したい!

touch autosquash{1,2}.txt && git add . && git commit -m "test1,2"
touch autosquash3.txt     && git add . && git commit -m "test3"
touch autosquash{5,6}.txt && git add . && git commit -m "test5,6"

と実行したところで4がないことに気がついた。

autosquashを使わない場合

touch autosquash4.txt && git add . && git commit -m "test4"
git rebase -i HEAD~3

を実行。下記の状態で編集用テキストが開く。

pick 0457afa test3
pick 001d634 test5,6
pick 9c3263d test4

下記のように、"test4"のcommitを"test3"の後ろに持って行って、先頭をsquash に変更してから編集用テキストを閉じてrebaseを続行。コミットログを適切に入力し完了させる

pick 0457afa test3
squash 9c3263d test4
pick 001d634 test5,6

autosquashを使う場合

#"squash! test3" の部分が重要!
touch autosquash4.txt && git add . && git commit -m "squash! test3"
git rebase -i HEAD~3

を実行すると下記の状態で編集用テキストが開くのでそのまま編集用テキストを閉じてrebaseを続行。コミットログを適切に入力し完了させる

pick 0457afa test3
squash 73236b3 squash! test3
pick 001d634 test5,6

まとめ

autosquashを使うと後々のrebase操作がちょっと便利になる

おまけ

  • squash!の他にfixup!も使えます

  • 1.7以降で使えるらしいです

  • autosquashでどのコミットの後ろにコミットを持っていくかは、"squash! ..."のメッセージ部分の前方一致で先頭から順番に検索をかけているようです

極端な話、下記のように1文字でも動作するのでこっちのほうがよく使うかも。

touch autosquash4.txt && git add . && git commit -m "squash! t"
git rebase -i HEAD~3

とすると

pick 0457afa test3
squash 28a19f0 squash! t
pick 001d634 test5,6

の状態でrebase編集用テキストが開く

参考

57
53
1

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
57
53