chocotakaの日記

日々技術的な事で学んだ小さな事をを自分なりに書いていきます

git rebaseコマンドを使うようになって

今までgitでのコミットログをまとめる事をやっていなかったため、rebaseコマンドを使ってきませんでした。

最近使うようになったので書き溜めておきます。

git rebase -i HEAD~~

~ の数でどこまでのコミットを扱うかの範囲指定します。

上記のコマンドだとHEAD から1つ前のコミットログまで指定していることになります。

そしてgit rebase -i HEAD~~を打つと

pick 9a54fd4 commitの説明を追加
pick 0d4a808 pullの説明を追加

# Rebase 326fc9f..0d4a808 onto d286baa
#
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

上記のように、2つのコミットログが表示されます。

例えば pick 0d4a808 pullの説明を追加fixup 0d4a808 pullの説明を追加と書き換えると

pick 0d4a808 pullの説明を追加の内容がpick 9a54fd4 commitの説明を追加に取り込まれ1つのコミットとして扱われるようになり、コミットログがpick 9a54fd4 commitの説明を追加だけになります。

よく使いそうなコマンドの説明
(p)pick: コミットをそのまま残す。
(r)reword: コミットメッセージを変更。
(e)edit: コミット自体の内容を編集。
(s)squash: 直前のpickを指定したコミットに統合しメッセージも統合。
(f)fixup :直前のpickを指定したコミットに統合しメッセージは破棄。

pick reword edut squash fixup それぞれ頭文字だけで書くことも可能です。