LoginSignup
5
5

More than 5 years have passed since last update.

細かく刻んでしまったコミットをまとめたいときの便利コマンド

Last updated at Posted at 2015-08-27

確認したのは下記のバージョン。
$ git --version
git version 2.4.4

細かく刻んでしまったコミットを、まとめたいときは、「git rebase -i」オプションを使ってブランチ上のコミットを修正すると便利です。

$ git rebase -i HEAD~{n}

(例)HEAD~{n} :n個前のコミットを意味する。10個前から表示したいときは{n}=10

$ git rebase -i HEAD~5

コマンドを実行すると、

テキストエディタが開かれHEADから5つ分のコミットが表示されます。

pick be989ca [commit comment1]
pick 310154e [commit comment2]
pick a554a0d [commit comment3]
pick 310154e [commit comment4]
pick a73546d [commit comment5]

使うコミットを p:pick(use commit)、統合して消したいコミットを f(use commit, but melt into previous commit, and discard this commit’s log message)にします。

pick be989ca [commit comment1]
f 310154e [commit comment2]
f a554a0d [commit comment3]
f 310154e [commit comment4]
f a73546d [commit comment5]

これを保存してエディタを終了

git log で確認。

ちなみに「-i」オプションだけでは、マージコミットは表示されません。
マージコミットが含まれる場合は「-p」オプションをつけて実行します。

$ git rebase -i -p HEAD~~

リモートリポジトリに push する際、コミットハッシュが変わるので「-f」が必要です。

$ git push -f

以上です。

【補足】
メッセージの中に、手順が出力されています。
これを参考にコミットメッセージのeditなどもできます。

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

5
5
0

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
5
5