Git 在執(zhí)行 rebase 操作時,有時候會需要把當前分支中的若干次提交合并在一起,作為一次提交合并到主分支當中,這時候就要用到 squash
醉拓。顧名思義伟姐,squash 的用途是合并多次提交。
流程
初始狀態(tài)
開始 Rebase
在 Rebase 操作的同時進行 Squash亿卤。
完成 Rebase
操作
如圖一所示愤兵,要合并 C5
、 C6
和 C7
三次提交排吴,先切換到 C5
秆乳、 C6
和 C7
所在的分支,然后執(zhí)行
git rebase -i HEAD~3
之后會出現(xiàn)如下提示信息
pick 708fa0a commit message A
pick 8baaa26 commit message B
pick dba177a commit message C
# Rebase 2d2fb07..bfa62a9 onto 2d2fb07 (3 command(s))
#
# 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
# d, drop = remove commit
#
# 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
這個時候把它改成如下形式
pick 708fa0a commit message A
squash 8baaa26 commit message B
squash dba177a commit message C
# ...
之后保存退出钻哩,出現(xiàn)
# This is a combination of 3 commits.
# The first commit's message is:
commit message A
# This is the 2nd commit message:
commit message B
# This is the 3rd commit message:
commit message C
再保存屹堰,就會顯示合并成功的提示信息。
[detached HEAD bfa62a9] commit message A
Date: Fri Jun 17 16:32:31 2016 +0800
2 files changed, 3 insertions(+)
Successfully rebased and updated refs/heads/dev.
這時候街氢,Rebase 合并成功扯键,并且 C5
、 C6
和 C7
三次提交也合并在一起了珊肃。
補充
對于合并提示信息
pick 708fa0a commit message A
squash 8baaa26 commit message B
squash dba177a commit message C
squash
也可以改為 fixup
pick 708fa0a commit message A
squash 8baaa26 commit message B
fixup dba177a commit message C
它們二者的區(qū)別就在于 fixup
后面的提交信息會被忽略荣刑。
最后,合并提示信息也可以簡寫成這樣
p 708fa0a commit message A
s 8baaa26 commit message B
f dba177a commit message C