在这里插入图片描述

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

01 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

02 合并步骤


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

2.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.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信息,编辑完后退出就好了。

2.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

2.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

03 GitLens


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

如下图所示:

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

在这里插入图片描述


如果文章对你有帮助,欢迎一键三连 👍 ⭐️ 💬 。如果还能够点击关注,那真的是对我最大的鼓励 🔥 🔥 🔥 。


参考资料

https://cloud.tencent.com/developer/article/1690638

https://zhuanlan.zhihu.com/p/139321091

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐