いろんなのが上がっているが写経する。
[Git] 複数のコミットを一つにまとめるメモ - Qiita
Gitの複数コミットをrebaseとsquashでまとめる方法 | iwb.jp
Gitで複数のcommitを一つにまとめる方法 - yachibit.log
やってみる
まず適当にコミットを作ります。
$ git init
$ touch sample.txt
$ for i in a b c d e ; do echo $i >> sample.txt ; git add sample.txt; git commit -m "add $i"; done
$ git log --oneline
0cc983e add e
974341b add d
54c160b add c
3eed8a1 add b
d3b1aaa add a
c, d のコミットをbにまとめたいとします。
rebase -iにaのコミットを指定するとそこまでの履歴が出てきます。
git log とは逆順になっているので一つ上にまとめるイメージでc,dにsquashを指定します。
$ git rebase -i d3b1aaa
pick 3eed8a1 add b
s 54c160b add c
s 974341b add d
pick 0cc983e add e
# Rebase d3b1aaa..0cc983e onto d3b1aaa
#
# 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
コミットログにどう残すか聞いてきています。
# This is a combination of 3 commits.
# The first commit's message is:
add b
# This is the 2nd commit message:
add c
# This is the 3rd commit message:
add d
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached from 3eed8a1
# You are currently editing a commit while rebasing branch 'master' on 'd3b1aaa'.
#
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# modified: sample.txt
bにc,dをまとめた記載をし、それ以外は
いらないので削除します。
# This is a combination of 3 commits.
# The first commit's message is:
add b c d
# This is the 2nd commit message:
# This is the 3rd commit message:
まとまりました。
[ymko@www1352ue rebase]$ git log --oneline
3eb4a28 add e
f3694ac add b c d
d3b1aaa add a
注意点
ほんとうは怖いリベース の通り、禁止事項が一つあります。
公開リポジトリにプッシュしたコミットをリベースしてはいけない
git push -f
でプッシュする方法もありますが、基本的にpushしたコミットを書き換えてはいけません。