1 查看日志
查看需要汇总几个commit的,记下最旧的id或者倒数第几个,然后选择reset 或者rebase方法
例如下面有5个提交,想要把从3到5的提交汇总在一起作为一次提交,得到 1 2 6{3 4 5的汇总}
git reflog
05a3854 (HEAD -> main) HEAD@{4}: commit: 5
e125d52 HEAD@{5}: commit: 4
ec70c06 HEAD@{6}: commit: 3
a4621de HEAD@{7}: commit: 2
065dc62 HEAD@{8}: commit (initial): 1
2 git rebase -i HEAD~n 或者 git rebase -i commitid
Commitid 为选定的提交,后续的提交合并到此基础上
git rebase -i a4621de
此时进入vi编辑,将后前个commit的pick修改为drop,保留最后一个pick。:wq保存退出
pick:使用commit。
reword:使用commit,修改commit信息。
squash:使用commit,将commit信息合入上一个commit。
drop:丢弃commit信息。
执行结果:
git rebase -i a4621de
Auto-merging aaa.txt
CONFLICT (content): Merge conflict in aaa.txt
error: could not apply 05a3854... 5
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 05a3854... 5
% git status
interactive rebase in progress; onto a4621de
Last commands done (3 commands done):
drop e125d52 4
pick 05a3854 5
(see more in file .git/rebase-merge/done)
No commands remaining.
You are currently rebasing branch 'main' on 'a4621de'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: aaa.txt
解决冲突
% vi aaa.txt
% git add .
% git commit -m "rebase"
[detached HEAD 9d52fd9] rebase
1 file changed, 3 insertions(+), 1 deletion(-)
执行rebase时,在选定的提交上创建并切换到临时分支,然后将最新的多个提交merge到临时分支,merge会产生冲突,解决冲突后作为一次提交进行提交。
3、操作完之后,中间提交日志没有了
git log
commit 9d52fd9b0da0bed7fecd13944a81541913330a52 (HEAD)
Date: Thu Dec 5 20:36:34 2024 +0800
rebase
commit a4621dec940e28d3b498c0f04f6b580a84d1ba3c
Date: Thu Dec 5 20:16:25 2024 +0800
2
commit 065dc62c96d5a2b0cfc042985db5603e2f006c1e
Date: Thu Dec 5 20:15:56 2024 +0800
1
4、注意:此时rebase并未结束
此时还在临时分支上,执行git rebase –continue 会自动回到操作前的分支
% git status
interactive rebase in progress; onto a4621de
Last commands done (3 commands done):
drop e125d52 4
pick 05a3854 5
(see more in file .git/rebase-merge/done)
No commands remaining.
You are currently editing a commit while rebasing branch 'main' on 'a4621de'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
% git rebase --continue
Successfully rebased and updated refs/heads/main.