LoginSignup
72
72

More than 3 years have passed since last update.

Gitでpush済のcommitをsqushでまとめる方法

Last updated at Posted at 2018-06-29

概要

表題のとおり。やリかたよく忘れるので備忘録も兼ねて。

対象者

push済のcommit変えちゃダメとかそういうのいいから今すぐpush済のcommitをsqushでまとめたい人。

rebaseコマンドを使う

rebaseコマンド

HEADから遡って、まとめたいcommit数 + 1を指定する。
以下は、HEADから遡って4commitをまとめる例。
(HEADも含めて4commit)

$ git rebase -i HEAD~5

まとめる対象commitを選択

すると以下のような画面が開く。
This is a just commit以外のpicksもしくはsquashに書き換えて、保存する。

なぜ1行目は書き換えないのかというと、こうすることで、This is a just commitに他のcommitが集約されるからだ。そのために、4commitまとめるのに、HEAD~5を指定したのだ。

ちなみに全部sにすると、Cannot 'squash' without a previous commitって怒られる。

pick 2008349 This is a just commit
-pick 20381e5 This is the first commit
-pick ca67ac9 This is the second commit
-pick 34e8939 This is the third commit
-pick bc0eb17 This is the fourth commit
+s 20381e5 This is the first commit
+s ca67ac9 This is the second commit
+s 34e8939 This is the third commit
+s bc0eb17 This is the fourth commit

# Rebase 36b1d53..bc0eb17 onto 36b1d53
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

push

あとは自己責任でpush。

$ git push origin +master

参考

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