菜鸟笔记
提升您的技术认知

git rebase 合并多个commit为一个commit-ag真人游戏

需求

当前状态:

$ git log --oneline --all --graph
* 69edf65 (head -> main, origin/main) update update-markdown-to-wordpress.py
* a6f3b81 update readme.md
* bf4d1f0 update readme.md
* 50c405f update readme.md
* 4a5004d add readme.md

现在我想要合并三个update readme.md,然后再提交update update-markdown-to-wordpress.py。

操作

首先将head指向最近的update readme.md:git reset --soft head~1

暂存当前修改:git stash

用rebase修改从4a5004d的提交到当前head的提交:git rebase -i 4a5004d,进入vim修改界面:

pick 50c405f update readme.md
pick bf4d1f0 update readme.md
pick a6f3b81 update readme.md

将下面两个pick改成s,如下:

pick 50c405f update readme.md
s bf4d1f0 update readme.md
s a6f3b81 update readme.md

输入 :wq 保存退出。

进入以下界面:

# this is a combination of 3 commits.
# this is the 1st commit message:
update readme.md
# this is the commit message #2:
update readme.md
# this is the commit message #3:
update readme.md
# please enter the commit message for your changes. lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# date:      sun nov 21 18:17:10 2021  0800
#
# interactive rebase in progress; onto 4a5004d
# last commands done (3 commands done):
#    squash bf4d1f0 update readme.md
#    squash a6f3b81 update readme.md
# no commands remaining.
# you are currently rebasing branch 'main' on '4a5004d'.
#
# changes to be committed:
#       modified:   readme.md
#

不用改,直接输入 :wq 保存退出。

当前状态:

$ git log --oneline --all --graph
* 9075fcc (head -> main) update readme.md
| *   abfbc6b (refs/stash) wip on main: a6f3b81 update readme.md
| |\
| | * fa40b4c index on main: a6f3b81 update readme.md
| |/
| | * 69edf65 (origin/main) update update-markdown-to-wordpress.py
| |/
| * a6f3b81 update readme.md
| * bf4d1f0 update readme.md
| * 50c405f update readme.md
|/
* 4a5004d add readme.md
$ git log --oneline --graph
* 9075fcc (head -> main) update readme.md
* 4a5004d add readme.md
* 487cb78 initial commit

然后git stash pop还原一下,然后再提交update update-markdown-to-wordpress.py即可。

总结

总结一下全过程:

git reset --soft  #为rebase的"终点"提交
git stash #如果当前有未提交的修改
git rebase -i  #为rebase的"起点"提交的前一个
# 进入vim界面,最靠前的那个pick不要改,之后的所有pick全部改成s,保存退出;进入vim界面,再次保存退出
git stash pop

注意:

  • 进入vim界面,最靠前的一次pick不要改。

  • git rebase -i 中的是真正要合并的commit的起点的前一个

网站地图