git rebaseが捗るオプション紹介
git rebaseでfixupやsquashをよくつかうわたしが出会って感動したオプションたちを紹介してこうと思います。
rebase.autostash
git rebase
を行うとき、編集差分がある状態では実行できないため、stashして下さいてきなコメントが出てやり直しとなってしまうのが、なんとも面倒臭いと思うのです。
これを払拭するのがautostash
です。
git rebase を実行した際に自動でgit stash
を行い、rebaseの作業に移ります。そして、rebase作業が終わった際にはstashしたものを戻すgit stash pop
までを自動でやってくれるというものです。ぜひ、おすすめしたい、、!
rebase.abbreviateCommands
git rebase -i で作業するとき、毎度出てくるこの画面。
pick 5f818fa edit file pick cd4e4ab delete file # Rebase 24029a2..cd4e4ab onto 24029a2 (2 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST.
わたしの場合、fixupをよく使いますが、これを行うためにはpick
の文字を消してから、f
を入力しなければなりません。これがちょっと面倒臭いと思うのです。
そこでこのabbreviateCommands
を使います。
これにより、pick が p となり編集がより楽になります。
pにカーソルを合わせたらrf
を入力してpをfに変更して終了!とやるのが私流です。
p 5f818fa edit file
p cd4e4ab delete file
# Rebase 24029a2..cd4e4ab onto 24029a2 (2 commands)
・・・
rebase.autosquash
fixupやsquashをよく使う人におすすめなオプションです。
このオプションはコミットメッセージの文頭にfixup!
またはsquash!
がついたものコミットを対象にgit rebase -i
画面においてあらかじめfixupやsquashにしてくれます。
$git rebase -i head~~ --autosquash p 5f818fa edit file f f540190 fixup! edit file # Rebase 24029a2..f540190 onto 24029a2 (2 commands) ・・・
autosqaushのオプション追加によりこの画面におけるデフォルトのpick(p)ではなくfixup(f)の状態で画面がでてきたのがわかります。
ここまでautosqaushの説明をしてきましたが、コミットの際にわざわざfixup!とつけたコミットメッセージを書かなければ効果を発揮しないのが難点です。。。
ここでfixup!やsquash!を簡単につける方法があるのでぜひ紹介したいと思います。
git commit --fixup <コミットの指定>
です。
コミットの指定についてはコミットidや、head~などが使えます。
$ git commit --fixup head [master f540190] fixup! edit file 1 file changed, 1 insertion(+), 1 deletion(-) $ git log --oneline 2285367 (HEAD -> master) fixup! edit file ab0d701 edit file 24029a2 create file
デフォルトに設定しよう
このオプションたちはデフォルト設定にしておくととても便利。
$ git config --global rebase.autostash true $ git config --global rebase.abbreviateCommands true $ git config --global rebase.autosqaush true
これらを実行するとhome下の.gitconfigファイルの中身に下記が追加されます。
[rebase] autosqaush = true abbreviateCommands = true autostash = true
まとめ
gitはもどかしいと思うポイントは大体対策されてるんだなとrebaseのoptionたちから感じることができました。もどかしいと思ったらとりあえずオプションを探しにいく、これを続けてより効率の良い開発ができるようになりたいです。
もともとわたしはgit commit に関してはIDEのIntelliJさんに頼ってるのですが、3番目に書いたgit commit --fixup
に関してはサポートされてないなーと思ったら、今年でた2019.1で追加されたみたいです。うれしい。