美文网首页杂七杂八
git rebase合并同一个分支的多个提交

git rebase合并同一个分支的多个提交

作者: 前端小白的摸爬滚打 | 来源:发表于2021-11-27 16:04 被阅读0次

背景

开发过程中,本地时长有无数次的commit,有时候我们想合并功能相同的commit,保持历史记录的干净整洁

git rebase

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

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

参数说明:

  • -i进入交互模式
  • commitid 指定一个commitid,则交互界面中只会列出该commitid之后的所有提交,不包含它本身
    交互界面命令解释:
# 命令:
# p, pick <提交> = 使用提交
# r, reword <提交> = 使用提交,但修改提交说明
# e, edit <提交> = 使用提交,进入 shell 以便进行提交修补
# s, squash <提交> = 使用提交,但融合到前一个提交
# f, fixup <提交> = 类似于 "squash",但丢弃提交说明日志
# x, exec <命令> = 使用 shell 运行命令(此行剩余部分)
# b, break = 在此处停止(使用 'git rebase --continue' 继续变基)
# d, drop <提交> = 删除提交

操作步骤

查看log

git的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

编辑要合并的版本

# 指定要合并版本号,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信息

# 把 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退出vim编辑器,此是就是注释界面

# 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

可以编辑commit message,保存并退出(:wq),即可完成commit合并

update clear-logs.sh

查看合并后的log

$ 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

然后就可以将整理过后的commit推送到远程了

冲突解决

git rebase 的时候可能会出现冲突,此时就需要解决冲突
错误提示信息:git rebase -i resumeerror: could not apply ...

# 查看冲突
$ git status

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

# rebase 继续
$ git rebase --continue

相关文章

  • Git 常用命令

    Git 初始化 Git 提交 Git 分支操作 git rebase作用 合并多个commit 为一个完整的com...

  • GIT常用命令备忘

    Git配置 Git常用命令 查看文件diff 查看提交记录 Git 本地分支管理 分支合并和rebase Git补...

  • 如何正确rebase

    git rebase -i origin/分支 合并冲突 git status git rebase --cont...

  • 图解4种Git合并分支方法

    原文---图解4种git合并分支方法 1. 补充 git rebase 改命令会把"dev"分支里的每个提交(c...

  • git合并分支和提交步骤

    git 合并分支和提交的步骤 Git 问题 :一个 master, 多个新功能分支, 怎样有序地合并和提交(ht...

  • git本地合并分支

    git 本地合并分支 一、git rebase 二、git merge

  • Git 基本操作收集

    rebase : git rebase用于把一个分支的修改合并到当前分支。详情

  • Git merge 和 Git rebase 区别

    git merge 和 git rebase 均具有合并分支的效果,但合并分支的机理不同 Git merge gi...

  • git整体学习

    基础 1. git ... 3. git rebase 第二种合并分支的方法是 git rebase。Rebase...

  • git常用命令

    git常用命令 git rebase 合并提交

网友评论

    本文标题:git rebase合并同一个分支的多个提交

    本文链接:https://www.haomeiwen.com/subject/jgfqxrtx.html