git rebase 合并同一分支的commit
背景
开发过程中,本地时长有无数次的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
作者:试试是是
链接:https://juejin.cn/post/7035158894099824648