我们开发的过程中,可能会有多次的修补提交,就会出现多条提交记录和备注信息,此时我们可以使用 git rebase -i来合并多个commit,以简化提交记录
1.合并最近的 4 次提交纪录,执行:
git rebase -i HEAD~4
2.自动进入 vi 编辑模式:
有几个命令需要注意一下:
- 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
修改完成后,按Esc键回到最下方命令行,输入:wq 完成保存并退出
3.弹出修改备注版面
完成编辑后,按Esc键回到最下方命令行,输入:wq 完成保存并退出:
待程序运行,最后得到Successfully的结果,即完成合并:
4.最后再使用 git log
最后再使用 git log 查看,之前的提交信息就都合并成了我们编辑的最后一条
合并之后再推送的远程仓库,信息条目就清晰很多,条数也会相对较少
5.异常情况处理
在上面第2步如果出现不正常的操作,导致退出了vim编辑页面,这时候如果还想使用git rebase -i HEAD~4进入vim编辑页面是不行的,会提示:
It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase. If that is the
case, please trygit rebase (--continue | --abort | --skip)
If that is not the case, pleaserm -fr "/Users/qinwenjing/Documents/Projects/Test/.git/rebase-merge"
and run me again. I am stopping in case you still have something
valuable there.
解释的还算清楚,所以不要慌!
使用 git rebase --edit-todo 会再次进入刚才编辑错误退出前的vim状态,这时候可以修改你的编辑。
使用git rebase --abort 表明退出当前的合并请求( 又回到原来的4个commit的状态)
参考文章:
1.https://blog.csdn.net/qinwenjng120/article/details/105735243
2.https://blog.csdn.net/AS_TS/article/details/106983974