LoginSignup
6
5

push済みのコミットをまとめる方法

Posted at

はじめに

開発現場ではレビューしてもらった対応などで1ファイルしか編集してないのにcommitが増えてしまったりします。
別にこれは問題ではないのですが、一度に複数のブランチの修正をデプロイするときにどのcommitが何のタスクの何を実装しているのかが追いづらくなり、現場のリーダー格に嫌がられることがあります。
そんな時にはコミットをまとめてあげると喜ばれます。

スクリーンショット 2023-05-26 12.48.14.png

1. commitの取得

$ git rebase -i HEAD~3

  • 3は取得するcommit数です。状況に応じて変更してください
  • まとめる基準となるコミットから下のコミットのpick -> s(sqush)に変更
  • まとめるコミット分のコミットメッセージが表示されるので1つに絞る

以下のような表示になると思います。
今回は0108a6c 1つ目のcommitのコミットに他の2つをまとめて1つのコミットにします。

pick 0108a6c 1つ目のcommit
pick 4426df8 commit 2回目
pick 3446912 commit 3回目

# Rebase 6b8f1c2..3446912 onto 6b8f1c2 (3 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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell

2. 変更したいcommitをsquashに変更

まとめたい2つのコミットpick -> squashに変更します。(*iで編集モードに)
編集が完了したらesc+ :wqで編集を抜けます。
色々書いてあるコメントのこの部分です。
# s, squash <commit> = use commit, but meld into previous commit

pick 0108a6c 1つ目のcommit
s 4426df8 commit 2回目   
s 3446912 commit 3回目   

# Rebase 6b8f1c2..3446912 onto 6b8f1c2 (3 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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case

3. commitメッセージの調整

編集を抜けると以下のような形でcommitまとめるのは良いけど、commitメッセージどうする?って聞かれます。

# This is a combination of 3 commits.
# This is the 1st commit message:

1つ目のcommit

# This is the commit message #2:

commit 2回目

# This is the commit message #3:

commit 3回目

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

なので2つ目3つ目のコミットメッセージは削除します。
ついでに1つ目のコミットメッセージも編集してみます。
編集が完了したらesc+ :wqで編集を抜けます。
*行の削除はddでできます。

# This is a combination of 3 commits.
# This is the 1st commit message:

commit1回目

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri May 26 12:31:09 2023 +0900
#
# interactive rebase in progress; onto 6b8f1c2
# Last commands done (3 commands done):
#    squash 4426df8 commit 2回目
#    squash 3446912 commit 3回目
# No commands remaining.

4. 最後にpush

$ git push origin -f ${branch_name}
*-fで強制的にpushするので他の方のcommitが含まれているブランチでは注意してください。自分の作業ブランチでだけ実行することを推奨します。

pushすると3つあったコミットが一つに纏まりました!
$ git log --oneline
ログを確認してもコミットが一つになっていることが確認できると思います。

スクリーンショット 2023-05-26 12.53.52.png

参考

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