当前位置: 首页 > news >正文

git 合并多次提交 commit

在工作中,有时候在反复修改代码中(比如处理MR的检视意见,或者为了推送到测试环境,先 commit到自己的远程分支上)不免会有多次 commit,这样发起 MR 的时候,就会有一堆 commit 信息,看起来不清爽,如果想把多次提交合并成一个提交,可参考如下操作:

1)执行如下命令:

git log --oneline

 查看历史提交信息:

最新提交为 56f3fec 因此,该提交为 HEAD

HEAD 指向最新放入仓库的版本。

2)合并 a84e379(最旧提交)-56f3fec(最新提交)

可执行如下操作:

git rebase -i HEAD~4

 HEAD~4 会包含 HEAD、HEAD~1、HEAD~2、HEAD~3、HEAD~4(共 5 个提交)。

3)执行完操作后,会弹出命令编辑窗口:

pick a84e379 revise review comments
s 5542cd0 revise review comments 2
s e5b712f revise review comments 3
s 614f74a revise review comments 3
s 56f3fec revise review comments 5

第一个提交保留为 pick,后续提交改为 squash 或 s。

编辑完成后,:wq 保留并且退出。

4)会再弹出新的命令编辑窗口,示例如下:

# This is a combination of 5 commits.
# This is the 1st commit message:
revise review comments

# This is the commit message #2:
revise review comments 2

# This is the commit message #3:
revise review comments 3

# This is the commit message #4:
revise review comments 3

# This is the commit message #5:
revise review comments 5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      <原始提交的时间戳>
#
# interactive rebase in progress; onto <父提交哈希>
# Last commands done (5 commands done):
#    pick a84e379 revise review comments
#    squash 5542cd0 revise review comments 2
#    squash e5b712f revise review comments 3
#    squash 614f74a revise review comments 3
#    squash 56f3fec revise review comments 5
# No commands remaining.
# You are currently editing a commit while rebasing branch 'main' on '<父提交哈希>'.
#
# Changes to be committed:
#       modified:   file1.txt
#       modified:   file2.txt

 这时候,只需要删除该窗口中的所有信息,保留最后合并完后想保留的提交信息,然后 :wq 保存并且退出即可:

revise review comments

 5)操作完成后,可再次执行查看提交日志操作,查看多次提交是否成功合并成了一次提交。

git log --oneline 

 

 可以看到,多次提交已经合并成了一次提交。

6)执行 git push --force,更新远程代码仓库的 commit 信息。

git push origin loustest --force

登录远端代码仓,可发现已经成功合并了多次提交:

相关文章:

  • Problem: 73. 矩阵置零
  • Unity网络开发快速回顾
  • 人工智能在现代科技中的应用和未来发展趋势
  • Java问题小记——入职心得
  • 风控笔记4——市场风险管理
  • 【Python入门速学】2.4. 逻辑运算符
  • SQL Server查询计划操作符(7.3)——查询计划相关操作符(11)
  • 人工智能之数学基础:瑞利商与特征值的关系
  • JAVA组件的利用漏洞 Shlr搜索引擎 shiro身份 Log4j日志
  • 常见中间件漏洞(tomcat)
  • 深入解析 Spring IOC AOP:原理、源码与实战
  • 屏幕刷新机制(一):机制
  • 用tensorboard支持pytorch训练可视化
  • JAVA学习*Object类
  • gbase8s cdc协议解析
  • 多源最短路径算法(竞赛)
  • 第45章:配置更新与应用热重载策略
  • VMware上对CentOS7虚拟机进行磁盘扩容、缩容
  • Uthana,AI 3D角色动画生成平台
  • Python第六章08:元组操作练习题
  • 网站运营改进的点/美国最新新闻头条
  • 网站院长信箱怎么做/爱站网seo培训
  • 承德网站开发/网络营销公司怎么注册
  • 浪起科技做的网站怎么样/百度关键词热搜
  • 个人博客页面设计图/优化关键词的方法
  • 自己电脑 wordpress/seo课程培训班费用