三分钟教你学Git(二十二) - 互动式改写历史

我们之前讲过可以使用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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值