如何避免 git rebase 杀死合并提交?

发布于 2024-11-14 03:35:11 字数 491 浏览 2 评论 0原文

给定以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-11-21 03:35:11

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

时光匆匆的小流年 2024-11-21 03:35:11

这不会很漂亮,但我认为你可以做到。

将 F 重新设置为 origin/master 作为新的主分支:

git checkout F
git checkout -b new_master
git rebase origin/master

将分支 b 合并到新分支中:

git merge branch-b

精挑细选剩余的 H 提交到新的主分支上:

git cherry-pick master

删除旧的主分支:

git branch -D master

不幸的是,您还必须再次进行合并(希望它不需要任何手动合并)。

我实际上并没有尝试过这一点,所以我会首先对存储库进行备份,但我非常有信心您会得到您想要的。我还建议打开 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:

git checkout F
git checkout -b new_master
git rebase origin/master

Merge branch-b into your new branch:

git merge branch-b

Cherry pick the remaining H commit onto your new master branch:

git cherry-pick master

Delete your old master branch:

git branch -D master

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文