我们之前讲过可以使用merge squash将多个commit合并成一个commit,其实还有另外的思路可以帮你完成任务,那就是互动式rebase。
比如,用git rebase -i HEAD^^可以对当前分支的最近两个commit进行改写(注意是开区间,因为git在实现层面需要知道父亲节点)。然后git会将你带入到一个编辑器里,按照历史commit从老到新的顺序从上到下依次列出,每一个commit前边都会有一个字段叫做pick,如下:
pick f31768b commit 1
pick cec3eb7 commit 2
你可以将第二个pick改成squash或者s,意思是你要把这个commit squash到前一个commit上。改完之后保存退出,这时git会自动将两个commit的信息合并,你需要改动这些commit message,最后保存,然后你就看到两个commit变成一个了。
是不是很方便呢,其实rebase -i还有很多其它的用途,比如变换commit历史顺序,删除其中一个commit等,非常的方便。下边列出官方文档:
# 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
原文:http://blog.csdn.net/hongchangfirst/article/details/54406912
作者:hongchangfirst
hongchangfirst的主页:http://blog.csdn.net/hongchangfirst