技術ブログ

(技術系中心)基本自分用備忘録なので、あくまで参考程度でお願いします。

gitコミットをまとめる方法

gitコミットをまとめる方法

コミットメッセージを修正する場合

コミットID_4 (HEAD -> master)コミットメッセージ_4
コミットID_3 コミットメッセージ_3
コミットID_2 コミットメッセージ_2
コミットID_1 コミットメッセージ_1

1, git rebase -i <コミット ID>

コミットID_2 〜 コミットID_4 をまとめる場合は コミットID_1

git rebase -i HEAD^^^ HEAD^^^^で3つ分のコミットを編集します。

2, Edit モードで rebase 指示書を修正する

r コミットID_2 コミットメッセージ_2
f コミットID_3 コミットメッセージ_3
f コミットID_4 コミットメッセージ_4

# Rebase xxxxx..yyyyy onto zzzzzz
#
# 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# 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>]

3. コミットメッセージの修正

Edit モードを :wq でセーブして抜けるとコミットメッセージの修正が求められるので修正する

ミスったときの対象方法

error: cannot 'fixup' without a previous commit
You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
Or you can abort the rebase with 'git rebase --abort'.

こうなった時は、git rebase --abortで元に戻せます

参考リンク

基本これみればOK

コミットをまとめる方法(git rebase -i)

gitで細かくコミットしたのを1つにまとめる|十 taro 十|note

【Git】離れた複数のcommit(コミット)をまとめる方法|Webエンジニア研究室