王清欢Randy 王清欢Randy
首页
  • 编程语言

    • C/C++ 学习笔记
    • Golang 学习笔记
  • 算法分析

    • LeetCode 刷题笔记
  • 操作系统

    • Linux 基础
    • Vim 实用技巧
    • Shell 脚本编程
    • GDB 学习笔记
  • 开发工具

    • Git 学习笔记
  • 分布式理论

    • 共识算法
    • 分布式事务
  • 数据库内核

    • PostgreSQL
    • Postgres-XL
  • hidb
  • pgproxy
  • 实用技巧
  • 学习方法
  • 资源分享
GitHub (opens new window)
首页
  • 编程语言

    • C/C++ 学习笔记
    • Golang 学习笔记
  • 算法分析

    • LeetCode 刷题笔记
  • 操作系统

    • Linux 基础
    • Vim 实用技巧
    • Shell 脚本编程
    • GDB 学习笔记
  • 开发工具

    • Git 学习笔记
  • 分布式理论

    • 共识算法
    • 分布式事务
  • 数据库内核

    • PostgreSQL
    • Postgres-XL
  • hidb
  • pgproxy
  • 实用技巧
  • 学习方法
  • 资源分享
GitHub (opens new window)
  • Git 常用技巧

    • 常用Git命令清单
    • Git使用手册
    • Git变基合并
    • Git命令思维导图
    • Git修改分支名
    • Markdown使用教程
    • Git提交规范
    • Git合并多个提交
      • 使用场景
      • git rebase
      • 合并步骤
        • 1. 查看 log
        • 2. 交互页面编辑合并版本
        • 3. 查看合并后的 log
        • 4. 推送远程
      • GitLens
  • GitHub 高级技巧

    • GitHub高级搜索技巧
    • GitHub Actions 实现自动部署静态博客
    • GitHub Actions 定时运行代码:每天定时百度链接推送
    • GitHub加速下载项目的方法
    • GitHub提交MR分支合并请求
  • Git 官方文档笔记

    • Git基础与命令
    • Git分支-分支原理
    • Git分支的新建与合并-分支操作
    • Git分支管理-查看分支
    • Git分支开发工作流
    • Git分支-远程分支
    • Git分支-变基
    • Git工具-查看修订版本
    • Git工具-交互式暂存
    • Git工具-重写历史
    • Git工具-重置揭密
  • Git 学习笔记
  • Git 常用技巧
王清欢
2023-07-07
目录

Git合并多个提交

# 使用场景

开发过程中,本地通常会有无数次 commit ,可以合并“相同功能”的多个 commit,以保持历史的简洁。

# git rebase

命令使用

git rebase --help

# 从HEAD版本开始往过去数3个版本
$ git rebase -i HEAD~3

# 合并指定版本号(不包含此版本)
$ git rebase -i [commitid]

说明:

  • -i(--interactive):弹出交互式的界面进行编辑合并
  • [commitid]:要合并多个版本之前的版本号,注意:[commitid] 本身不参与合并

例如,如下例子中你想合并前 5 个 commit, 那么命令指定的 commitid 为 1d795e6,即 git rebase -i 1d795e6

$ git log --oneline
291e427 update website
8c8f3f4 update website
1693a6f update clear-logs.sh version
3759b84 update clear-logs.sh
fc36a2a add links
1d795e6 fix && update clear-logs.sh 0.0.2
9536dab add dingtalk script
3a51aaa fix shellcheck problem
2db6ad3 add clear logs scripts
e57b0e6 fix && add batch del
17cb931 fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit

使用该命令之后会进入一个交互式的界面进行编辑合并,该页面中的第一列表示指令:

  • 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

# 合并步骤

  1. 查看 log 记录,使用 git rebase -i [commitid] 选择要合并的 commit
  2. 编辑要合并的版本信息,保存提交,多条合并会出现多次(可能会出现冲突)
  3. 修改注释信息后,保存提交,多条合并会出现多次
  4. 推送远程仓库或合并到主干分支、

# 1. 查看 log

$ git log --oneline
291e427 update website
8c8f3f4 update website
1693a6f update clear-logs.sh version
3759b84 update clear-logs.sh
fc36a2a add links
1d795e6 fix && update clear-logs.sh 0.0.2
9536dab add dingtalk script
3a51aaa fix shellcheck problem
2db6ad3 add clear logs scripts
e57b0e6 fix && add batch del
17cb931 fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit

# 2. 交互页面编辑合并版本

例如,执行 git rebase -i [commitid] 命令之后会跳出如下内容,注意显示的内容是倒序的最老的提交在最上面,最新的在最下面

# 指定要合并版本号,cf7e875 不参与合并,进入 vi 编辑器
$ git rebase -i cf7e875 
pick 17cb931 fix && add batch del
pick e57b0e6 fix && add batch del
pick 2db6ad3 add clear logs scripts
pick 3a51aaa fix shellcheck problem
pick 9536dab add dingtalk script
pick 1d795e6 fix && update clear-logs.sh 0.0.2
pick fc36a2a add links
pick 3759b84 update clear-logs.sh
pick 1693a6f update clear-logs.sh version
pick 8c8f3f4 update website

# Rebase cf7e875..291e427 onto cf7e875 (10 commands)
#
# Commands:
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

根据需求修改第一列的指令,对版本进行合并

pick 17cb931 fix && add batch del
f e57b0e6 fix && add batch del # 把 e57b0e6 合并到 17cb931,且不保留注释
pick 2db6ad3 add clear logs scripts
pick 3a51aaa fix shellcheck problem
pick 9536dab add dingtalk script
pick 1d795e6 fix && update clear-logs.sh 0.0.2
pick fc36a2a add linkss
pick 3759b84 update clear-logs.sh
s 1693a6f update clear-logs.sh version
s 8c8f3f4 update website # 把 8c8f3f4 和 1693a6f 合并到 3759b84

修改完成之后,:wq 保持退出。保存退出后,又弹出一个新的框,让我们更改commit信息,编辑完后退出就好了。

# 3. 查看合并后的 log

4c2316c update clear-logs.sh
73f082e add links
56adcf2 fix && update clear-logs.sh 0.0.2
ebf3786 add dingtalk script
6e81ea7 fix shellcheck problem
64ca58f add clear logs scripts
9327def fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit

# 4. 推送远程

首先,git pull 拉取与远程仓库同步,养成先pull最新代码再修改的好习惯;在修改本地代码前,先使用git pull拉取远程最新代码,然后再进行修改(推荐--rebase)

git pull 远程仓库名 远程分支名 --rebase

然后,git push 推送,合并后 commitid 发生了变化,git 不允许 push 改变提交历史的操作,可以新增或者减少 commit 但不能改变原来的commit 历史,因此会报冲突。

在确认代码无误的情况下,直接使用 --force 强制推送

git push 远程仓库名 远程分支名 --force

在 git rebase 过程中,可能会存在冲突,此时就需要解决冲突。

# 查看冲突
$ git status

# 解决冲突之后,本地提交
$ git add .

# rebase 继续
$ git rebase --continue

# GitLens

如果你的 VSCode 安装了 GitLens 插件,这一切都可以变得很简单

如下图所示:

  • 首先点击侧边栏的版本控制按钮,
  • 然后,打开 Commits 栏,在该栏目中可以选择 Undo Commit 撤销提交
  • 接着,在本地暂存区 Stage 中就可以看到撤销的提交;选择需要合并冲突或者要修改的文件点击 Unstage,这些文件就会从暂存区撤回到工作区 Changes,这样就可以进行修改了;
  • 最后,修改完成之后,将不同的 Commit 修改合并成一个 Commit 提交
gitlens

reference:

  • https://cloud.tencent.com/developer/article/1690638
  • https://zhuanlan.zhihu.com/p/139321091
上次更新: 2023/11/19, 12:55:48
Git提交规范
GitHub高级搜索技巧

← Git提交规范 GitHub高级搜索技巧→

Theme by Vdoing | Copyright © 2023-2024 Wang Qinghuan | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式