git实用操作:git rebase -i 合并多个 commit

我们开发的过程中,可能会有多次的修补提交,就会出现多条提交记录和备注信息,此时我们可以使用 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 try
        git rebase (--continue | --abort | --skip)
If that is not the case, please
        rm -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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

森林老虎

混口饭吃嘿嘿

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值