跳过正文
Background Image
  1. Posts/

Git 提交合并(Squash)

·296 字·1 分钟· ·
沈显鹏
作者
沈显鹏
目录

本地提交尚未推送到远程
#

如果要将本地的多个提交合并为一个,可以按以下流程操作。

这里有一个 3 分钟短视频 讲解了 git rebase -i 的用法。

  1. 查看本地仓库的日志:
    git log --oneline
    

``

list your logs in oneline

  1. 假设要合并最近的 3 个提交(add61523650100396a652):

    git rebase -i HEAD~3
    

    list last three commits

  2. 将需要合并到前一个提交的记录改为 ssquash

    combine three commits to one

  3. 保存退出(ESC:wq!)。

  4. 在提交信息编辑界面中,注释掉不需要保留的提交信息:

    comment out some commits message you don’t need
    comment out some commits message you don’t need

  5. 查看日志,确认已合并为一个提交:

    comment out some commits message you don’t need


提交已推送到远程
#

如果提交已推送到远程,建议新建分支进行 squash 以避免影响已有分支历史。

  1. 查看日志:

    list your logs in oneline

  2. 新建分支:

    git checkout -b bugfix/UNV-1234-for-squash
    
  3. 合并最近 2 个提交:

    git rebase -i HEAD~2
    

    select a commit you want to squash

  4. 修改提交信息,例如:

    UNV-1234 combine all commit to one commit
    

    comment out commit message you don’t want to display

  5. 推送新分支到远程:

    git push -u origin bugfix/UNV-1234-for-squash
    

相关文章

Error: Permission denied (publickey)
·276 字·1 分钟
本文介绍了如何在配置多个 SSH Git Key 时解决 “Permission denied (publickey)” 错误,确保 GitHub 和 Bitbucket 的 SSH 连接正常工作。
Git 分支策略
·772 字·2 分钟
介绍大型项目的 Git 分支策略,包括 master、develop、release 和 hotfix 分支的作用和使用方法,帮助团队更好地管理代码和版本发布。
通过 Jenkins 来提交修改的代码 git push by Jenkins
·439 字·1 分钟
如何通过 Jenkins Pipeline 脚本来提交修改的代码到 Git 仓库,包括克隆仓库、修改代码和推送更改等步骤。
Git 管理
·324 字·1 分钟
本文介绍了 Git 的常见管理操作,包括分支管理、提交规范、代码审查等,帮助开发者更好地使用 Git 进行版本控制。
Git 命令备忘
·188 字·1 分钟
本文总结了 Git 的常用命令和技巧,帮助开发者快速查找和使用 Git 命令,提高工作效率。
Git remove and add remote repository
·63 字·1 分钟
本文介绍了如何在 Git 中移除和添加远程仓库,帮助开发者管理代码仓库的远程连接。