原文链接:http://www.jianshu.com/p/964de879904a
在使用 Git 作为版本控制的时候,我们可能会由于各种各样的原因提交了许多临时的 commit,而这些 commit 拼接起来才是完整的任务。那么我们为了避免太多的 commit 而造成版本控制的混乱,通常我们推荐将这些 commit 合并成一个。
首先假设我们有3个 commit
![](https://i-blog.csdnimg.cn/blog_migrate/9b1c57a16a73873622e9d4fbe4658bd2.webp?x-image-process=image/format,png)
我们需要将 2dfbc7e8
和 c4e858b5
合并成一个 commit,那么我们输入如下命令:
![](https://i-blog.csdnimg.cn/blog_migrate/a7c1cc3cb37e81844173b6258d7574c0.webp?x-image-process=image/format,png)
其中,-i
的参数是不需要合并的 commit 的 hash 值(也就是我们要rebase到的commit对象),这里指的是第一条 commit, 接着我们就进入到 vi
的编辑模式:
![](http://upload-images.jianshu.io/upload_images/228805-fce11005e6ee8e2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到其中分为两个部分,上方未注释的部分是填写要执行的指令,而下方注释的部分则是指令的提示说明。指令部分中由前方的命令名称、commit hash 和 commit message 组成。
当前我们只要知道 pick
和 squash
这两个命令即可。
pick
的意思是要会执行这个 commitsquash
的意思是这个 commit 会被合并到前一个commit
我们将 c4e858b5
这个 commit 前方的命令改成 squash
或 s
,然后输入:wq
以保存并退出
![](https://i-blog.csdnimg.cn/blog_migrate/420ec69b4384d05d304b543d8b9dc208.webp?x-image-process=image/format,png)
这是我们会看到 commit message 的编辑界面
![](https://i-blog.csdnimg.cn/blog_migrate/f151ce960472dd4a081761744503e9fd.webp?x-image-process=image/format,png)
其中, 非注释部分就是两次的 commit message, 你要做的就是将这两个修改成新的 commit message。
![](https://i-blog.csdnimg.cn/blog_migrate/299f2a785dc0b4fe6193de80222b5ce5.webp?x-image-process=image/format,png)
输入wq
保存并推出, 再次输入git log
查看 commit 历史信息,你会发现这两个 commit 已经合并了。
![](https://i-blog.csdnimg.cn/blog_migrate/88b392d90e313817746372b53738ebd0.webp?x-image-process=image/format,png)
注意事项:如果这个过程中有操作错误,可以使用 git rebase --abort
来撤销修改,回到没有开始操作合并之前的状态。
作者:_Ryeeeeee
链接:http://www.jianshu.com/p/964de879904a
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。