如何避免 git rebase 杀死合并提交?
给定以下 git 历史记录:
C-I origin/master
/
A-B-F-G-H master
\ /
D-E branch-b
我想在 origin/master
之上重新调整本地 master
分支,但我想保留合并提交G
。当我在 master
尝试简单地执行 git rebase origin/master
时,它会将 D..E
压缩为 G
并使用 E
提交消息提交,因此合并的历史记录丢失了。有没有某种方法可以保留此合并,同时仍然获得变基?为了清楚起见,我的预期结果是:
A-B-C-I-F-G-H master
\ /
D-----E branch-b
Given the following git
history:
C-I origin/master
/
A-B-F-G-H master
\ /
D-E branch-b
I want to rebase my local master
branch on top of origin/master
, but I want to preserve the merge commit G
. When I tried simply doing a git rebase origin/master
while at master
it squashed D..E
as G
and committed that with the commit message of E
, so the history that there was a merge was lost. Is there some way of preserving this merge while still getting the rebase? For clarity, my intended result is:
A-B-C-I-F-G-H master
\ /
D-----E branch-b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
--preserve-merges
添加到您的 rebase 命令中。如果合并中存在冲突解决方案,请添加“递归他们的”策略作为参数。编辑:
--preserve-merges
现已弃用,请使用--rebase-merges
代替Add
--preserve-merges
to your rebase command. In case there were conflict resolutions in your merge, add 'recursive theirs' strategy as a parameter as well.EDIT:
--preserve-merges
is now deprecated, use--rebase-merges
instead这不会很漂亮,但我认为你可以做到。
将 F 重新设置为 origin/master 作为新的主分支:
将分支 b 合并到新分支中:
精挑细选剩余的 H 提交到新的主分支上:
删除旧的主分支:
不幸的是,您还必须再次进行合并(希望它不需要任何手动合并)。
我实际上并没有尝试过这一点,所以我会首先对存储库进行备份,但我非常有信心您会得到您想要的。我还建议打开 gitk --all 并在每个命令后使用“F5”刷新树,以便您可以看到发生了什么变化。
如果其他人知道更优雅的方法,他们仍然应该发帖。
This isn't going to be very pretty but I think you can do it.
Rebase F onto the origin/master as your new master branch:
Merge branch-b into your new branch:
Cherry pick the remaining H commit onto your new master branch:
Delete your old master branch:
Unfortunately you will also have to do the merge again (hopefully it doesn't take any manual merging).
I didn't actually try this out, so I would make a backup of the repository first, but I am pretty confident that you will get what you want. I also suggest opening up gitk --all and refreshing the tree with "F5" after each command so you can see what is changing.
Someone else should still post if they know of a more elegant way to do it.