Git Revert 然后在修复后重新合并

Posted

技术标签:

【中文标题】Git Revert 然后在修复后重新合并【英文标题】:Git Revert and then Remerge after Fix 【发布时间】:2021-11-05 20:10:49 【问题描述】:

我有以下 git 分支

功能分支 生产分部

我将我的 Feature 分支与 Production 分支合并,合并后,Production 出现了一些问题,因此我使用了 GitHub Revert 功能并创建了 Revert-Feature-Branch,然后将其与 Production 合并。

现在,我已经修复了我的 Feature Branch 中的错误,我正在尝试将该分支与 Production 合并,但我没有得到我所做的所有更改,我只得到了为修复。 (这是可以接受的,因为提交已经与生产分支合并。)

有没有办法让我的原始代码从 Feature Branch 合并到 Production 分支中作为新的提交而不重置生产分支?

感谢您的帮助。

注意,我已经做了一些研究并阅读了以下答案:How to revert a merge commit that's already pushed to remote branch?

这没有帮助。

【问题讨论】:

您可能需要revert the revert 是的,这就是我所做的,它创建了Revert-Revert-Feature-Branch 分支,然后我将Feature Branch 与修复合并到Revert-Revert-Feature-Branch 中。请将此作为答案发布,以便我接受并关闭问题。谢谢!!! @JanWilamowski 这能回答你的问题吗? Re-doing a reverted merge in Git 【参考方案1】:

如果特性分支是一条直线,那么你需要让 git 相信最初的修订从未被合并。这似乎很难做到,但事实并非如此。首先,找到两个分支之间的最后一个共同祖先。然后查看此修订版。然后重新应用(cherry-pick)功能分支的所有修订。对 git 来说,就好像这些新修订从未应用于分支。

git merge-base the-target-brsnch the-feature-branch
git checkout -b temp that-id #using a new branch temp
git cherry-pick HEAD..the-feature-branch

如果你尝试将生成的分支合并到目标分支中,单元这次应该携带所有的变化。

【讨论】:

【参考方案2】:

一个简单的 revert-the-revert 可能不起作用,因为您在顶部进行了修复,因此您可能必须在恢复 revert 后重新设置基准

feature: branch that you merge into production
feature-fixes: branch that contains the fixes
feature-reverted: contrains the reverted feature
feature-reverted-revert: contains the reverted revert

所以你可能不得不这样做

git checkout feature-fixes
git rebase feature-reverted-revert
git checkout feature-reverted-revert
git merge feature-fixes
git checkout feature
git merge feature-reverted-revert

现在您可以在验证一切都正确后将功能分支合并到生产中

【讨论】:

以上是关于Git Revert 然后在修复后重新合并的主要内容,如果未能解决你的问题,请参考以下文章

如何修复还原的 git 提交?

Git 还原一些提交(包括合并),然后稍后重新提交

git revert 后,合并显示没有更改并且已经更新

git —— bug分支

如何执行 git revert 并删除合并历史记录?

在 git 中如何将 Feature 分支合并到 Main 分支然后重新同步