在 Rebase 期间压扁提交

提交可以在 git rebase 期间被压扁。在尝试以这种方式压缩提交之前,建议你了解变基。

  1. 确定要重新绑定的提交,并记下其提交哈希值。

  2. git rebase -i [commit hash]

    或者,你可以键入 HEAD~4 而不是提交哈希,以查看最新提交以及最新提交之前的 4 次提交。

  3. 在运行此命令时打开的编辑器中,确定要压缩的提交。用 squash 替换这些行开头的 pick,将它们压缩到上一次提交中。

  4. 选择要压缩的提交后,系统将提示你编写提交消息。

记录提交以确定重新定位的位置

> git log --oneline
612f2f7 This commit should not be squashed
d84b05d This commit should be squashed
ac60234 Yet another commit
36d15de Rebase from here
17692d1 Did some more stuff
e647334 Another Commit
2e30df6 Initial commit

> git rebase -i 36d15de

此时,你选择的编辑器会弹出,你可以在其中描述你要对提交执行的操作。Git 在评论中提供帮助。如果你原样保留它,那么什么都不会发生,因为每次提交都将保留,并且它们的顺序将与 rebase 之前的顺序相同。在此示例中,我们应用以下命令:

pick ac60234 Yet another commit
squash d84b05d This commit should be squashed
pick 612f2f7 This commit should not be squashed

# Rebase 36d15de..612f2f7 onto 36d15de (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
#
# 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

写提交消息后的 Git 日志

> git log --oneline
77393eb This commit should not be squashed
e090a8c Yet another commit
36d15de Rebase from here
17692d1 Did some more stuff
e647334 Another Commit
2e30df6 Initial commit