🐿️
【git】rebaseでプルリク作る前のコミット整理を効率化したい
背景
プルリクを出す前、作業中に作ったコミットを整理する必要があります。
しかし整理している途中、"この変更、前のコミットに含めるべきやったな"って感じでミスがあった場合、いちいち reset するのは手間だしストレスたまります。
git rebase を使えばコミットを一気に整理できるので、その手順を備忘録としてまとめます。
対象
- git rebase になじみが薄い人
複数のコミットを一つにまとめたい
ケース
すでにコミット済みの commit2 に commit3 をまとめればよかった....(直前の commit だとリセットすりゃ楽ですが例ということで勘弁。。。)
手順
# commit番号を取得
git log --oneline
commit2~3 をまとめたいので commit1 に移動
# git rebase --i {{コミット番号}}
$ git rebase --i 6904133
このような画面になります。
pick 7dfbbc5 commit2
pick c5e4e0a commit3
# コメントでその後のコマンドの詳細が載っています。
# Rebase 6904133..c5e4e0a onto 6904133 (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
......
今回のケースでは commit2 と commit3 をまとめたいので、squash を使います。
squash とはコメントにも書かれている通りで
# s, squash <commit> = use commit, but meld into previous commit
"指定したコミットをそのまま直前のコミット"に取り込んでくれます。
今回のケースでは commmit3 を commit2 に取り込みたいので、次のように編集します。
pick 7dfbbc5 commit2
s c5e4e0a
※vim での編集に慣れていない人はこちらが参考になります。
コマンド結果が表示されます。
因みにこのタイミングで commit メッセージを変更できます。
そのままでよければ上書き保存します。
# This is a combination of 2 commits.
# This is the 1st commit message:
commit2
# This is the commit message #2:
commit3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Apr 12 10:30:07 2024 +0900
#
# interactive rebase in progress; onto 6904133
これで commit3 の内容が commit2 にまとめられます。
【補足】2つ以上の commit をまとめる場合
ケース
commit3 と commit4 って commit2 にまとめるべきやったな...
手順
上述の git reset までは同じ
pick 7dfbbc5 commit2
pick c5e4e0a commit3
pick c8129f1 commit4
commit2 以外はすべて squash します。
pick 7dfbbc5 commit2
s c5e4e0a
s c8129f1
まとめられました。
今までは Source Tree 等の GUI を使って reset ばかりやっていましたが、作業効率を上げるにはコマンドの方が早いですね。
Discussion