前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Git 合并多个 commit,保持历史简洁

Git 合并多个 commit,保持历史简洁

作者头像
叨叨软件测试
发布2020-09-01 23:29:31
125.2K0
发布2020-09-01 23:29:31
举报
文章被收录于专栏:叨叨软件测试叨叨软件测试

背景

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

git rebase

代码语言:javascript
复制
# 从HEAD版本开始往过去数3个版本
$ git rebase -i HEAD~3

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

说明:

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

指令解释(交互编辑时使用):

  • 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选择要合并的 commit
  2. 编辑要合并的版本信息,保存提交,多条合并会出现多次(可能会出现冲突)
  3. 修改注释信息后,保存提交,多条合并会出现多次
  4. 推送远程仓库或合并到主干分支

查看 log

代码语言:javascript
复制
$ 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

编辑要合并版本

代码语言:javascript
复制
# 指定要合并版本号,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

将 commit 内容编辑如下:

代码语言:javascript
复制
# 把 e57b0e6 合并到 17cb931,不保留注释;把 1693a6f 合并到 3759b84
pick 17cb931 fix && add batch del
f 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
s 1693a6f update clear-logs.sh version
pick 8c8f3f4 update website

然后:wq保存退出后是注释界面:

代码语言:javascript
复制
# This is a combination of 2 commits.
# This is the 1st commit message:

update clear-logs.sh

# This is the commit message #2:

update clear-logs.sh version

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jul 28 18:25:57 2020 +0800
#
# interactive rebase in progress; onto cf7e875
# Last commands done (9 commands done):
#    pick 3759b84 update clear-logs.sh
#    s 1693a6f update clear-logs.sh version
# Next command to do (1 remaining command):
#    pick 8c8f3f4 update website
# You are currently editing a commit while rebasing branch 'master' on 'cf7e875'.
#
# Changes to be committed:
# modified:   logs/README.md
# modified:   logs/clear-logs.sh
#

编辑注释信息,保存退出:wq即可完成 commit 的合并:

代码语言:javascript
复制
update clear-logs.sh

查看合并后的 log

代码语言:javascript
复制
$ git log --oneline
47e7751 update website
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

推送远程

代码语言:javascript
复制
# 由于我是一个人开发,故强制推送到远程仓库
$ git push --force origin master

冲突解决

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

错误提示信息:git rebase -i resumeerror: could not apply ...

代码语言:javascript
复制
# 查看冲突
$ git status

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

# rebase 继续
$ git rebase --continue
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 叨叨软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • git rebase
  • 合并步骤
    • 查看 log
      • 编辑要合并版本
        • 查看合并后的 log
          • 推送远程
          • 冲突解决
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档