在 Git 中重新进行恢复合并

发布于 2024-07-26 22:01:23 字数 400 浏览 11 评论 0原文

我在这里遇到了一些问题:我在 Git 中有一个特定于问题的分支 28s,我将其合并到通用 develop 分支中。 事实证明我做得太快了,所以我使用 git-revert 来撤消合并。 然而,现在是时候将 28s 合并到 develop 中了,但是 git-merge 命令看到了原始合并,并高兴地宣布一切都很好,分支已经存在合并了。 现在我该怎么做? 创建一个'Revert'Revert'28s-> 开发“”'提交? 这似乎不是一个好方法,但我目前无法想象其他方法。

树结构如下:

Git log output

I have run into a bit of a problem here: I had a problem-specific branch 28s in Git, that I merged in the general develop branch. Turns out I had done it too fast, so I used git-revert to undo the merge. Now, however, the time has come to merge 28s into develop, but git-merge command sees the original merge, and happily announces that all is well and branches have been already merged. What do I do now? Create a 'Revert "Revert "28s -> develop"" ' commit? Doesn't seem to be a good way to do it, but I can't imagine any other at the moment.

What the tree structure looks like:

Git log output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

寄意 2024-08-02 22:01:23

你必须“恢复恢复”。 根据您如何恢复原始状态,它可能不像听起来那么容易。 查看关于此的官方文档主题

---o---o---o---M---x---x---W---x---Y
              /
      ---A---B-------------------C---D

允许:

---o---o---o---M---x---x-------x-------*
              /                       /
      ---A---B-------------------C---D

但这一切都有效吗? 当然可以。 您可以恢复合并,并从
纯粹技术角度,git 做得很自然,没有真正的
麻烦。
它只是认为这是从“合并前的状态”到
“合并后的状态”,就是这样。
没有什么复杂的,没有什么奇怪的,
没有什么真正危险的。 Git 会不假思索地做到这一点。

所以从技术角度来看,恢复合并没有任何问题,
但是从工作流程的角度来看,您通常应该尝试这样做
避免

如果可能的话,例如,如果您发现一个已合并的问题
进入主树,不要恢复合并,而是真正努力

  • 将问题平分到您合并的分支中,然后修复它,
  • 或尝试恢复导致该问题的单个提交。

是的,它更复杂,而且不,它并不总是有效(有时
答案是:“哎呀,我真的不应该合并它,因为它不是
准备好了,我真的需要撤消所有合并”)。那么你
确实应该恢复合并,但是当您想重新进行合并时,您
现在需要通过恢复恢复来做到这一点。

You have to "revert the revert". Depending on you how did the original revert, it may not be as easy as it sounds. Look at the official document on this topic.

---o---o---o---M---x---x---W---x---Y
              /
      ---A---B-------------------C---D

to allow:

---o---o---o---M---x---x-------x-------*
              /                       /
      ---A---B-------------------C---D

But does it all work? Sure it does. You can revert a merge, and from a
purely technical angle, git did it very naturally and had no real
troubles.
It just considered it a change from "state before merge" to
"state after merge", and that was it.
Nothing complicated, nothing odd,
nothing really dangerous. Git will do it without even thinking about it.

So from a technical angle, there's nothing wrong with reverting a merge,
but from a workflow angle it's something that you generally should try to
avoid
.

If at all possible, for example, if you find a problem that got merged
into the main tree, rather than revert the merge, try really hard to:

  • bisect the problem down into the branch you merged, and just fix it,
  • or try to revert the individual commit that caused it.

Yes, it's more complex, and no, it's not always going to work (sometimes
the answer is: "oops, I really shouldn't have merged it, because it wasn't
ready yet, and I really need to undo all of the merge"). So then you
really should revert the merge, but when you want to re-do the merge, you
now need to do it by reverting the revert.

╄→承喏 2024-08-02 22:01:23

假设您有这样的历史记录

---o---o---o---M---W---x-------x-------*
              /                      
      ---A---B

,其中 A、B 提交失败,而 W - 是 M 的恢复

,因此在开始修复发现的问题之前,我会择优挑选 W 提交到我的分支

git cherry-pick -x W

然后我在我的分支上恢复 W 提交

git revert W 

之后我可以继续修复。

最终的历史记录可能如下所示:

---o---o---o---M---W---x-------x-------*
              /                       /     
      ---A---B---W---W`----------C---D

当我发送 PR 时,它将清楚地显示 PR 已撤消恢复并添加了一些新的提交。

Let's assume you have such history

---o---o---o---M---W---x-------x-------*
              /                      
      ---A---B

Where A, B failed commits and W - is revert of M

So before I start fixing found problems I do cherry-pick of W commit to my branch

git cherry-pick -x W

Then I revert W commit on my branch

git revert W 

After I can continue fixing.

The final history could look like:

---o---o---o---M---W---x-------x-------*
              /                       /     
      ---A---B---W---W`----------C---D

When I send a PR it will clearly shows that PR is undo revert and adds some new commits.

安穩 2024-08-02 22:01:23

要在不破坏工作流程的情况下恢复恢复,请执行以下操作:

  • 创建开发的本地垃圾副本
  • 在开发的本地副本上恢复恢复
  • 提交 将副本合并到功能分支中,然后将功能分支推送到 git 服务器。

当您准备好时,您的功能分支现在应该能够正常合并。 这里唯一的缺点是您的历史记录中会有一些额外的合并/恢复提交。

To revert the revert without screwing up your workflow too much:

  • Create a local trash copy of develop
  • Revert the revert commit on the local copy of develop
  • Merge that copy into your feature branch, and push your feature branch to your git server.

Your feature branch should now be able to be merged as normal when you're ready for it. The only downside here is that you'll a have a few extra merge/revert commits in your history.

李不 2024-08-02 22:01:23

要在 GIT 中恢复还原:

git revert <commit-hash-of-previous-revert>

To revert a revert in GIT:

git revert <commit-hash-of-previous-revert>
揽清风入怀 2024-08-02 22:01:23

您可以在 devel 分支中使用此命令来丢弃(撤消)错误的合并提交(而不是使用 git-revert),而不是使用 git-revert只是恢复它)。

git checkout devel
git reset --hard COMMIT_BEFORE_WRONG_MERGE

这也将相应地调整工作目录的内容。 小心

  • 在开发分支中保存您的更改(因为错误的合并),因为它们
    也会被 git-reset 删除。 您指定的提交之后的所有提交
    git reset 参数将会消失!
  • 另外,如果您的更改已从其他存储库中提取,请不要执行此操作
    因为重置会改写历史。

我建议在尝试此操作之前仔细研究 git-reset 手册页。

现在,重置后,您可以重新应用 devel 中的更改,然后执行

git checkout devel
git merge 28s

这将是从 28sdevel 的真正合并,就像最初的一个(现在是
从 git 的历史记录中删除)。

Instead of using git-revert you could have used this command in the devel branch to throw away (undo) the wrong merge commit (instead of just reverting it).

git checkout devel
git reset --hard COMMIT_BEFORE_WRONG_MERGE

This will also adjust the contents of the working directory accordingly. Be careful:

  • Save your changes in the develop branch (since the wrong merge) because they
    too will be erased by the git-reset. All commits after the one you specify as
    the git reset argument will be gone!
  • Also, don't do this if your changes were already pulled from other repositories
    because the reset will rewrite history.

I recommend to study the git-reset man-page carefully before trying this.

Now, after the reset you can re-apply your changes in devel and then do

git checkout devel
git merge 28s

This will be a real merge from 28s into devel like the initial one (which is now
erased from git's history).

尹雨沫 2024-08-02 22:01:23

我建议您按照以下步骤来恢复恢复,例如 SHA1。

git checkout develop #go to develop branch
git pull             #get the latest from remote/develop branch
git branch users/yourname/revertOfSHA1 #having HEAD referring to develop
git checkout users/yourname/revertOfSHA1 #checkout the newly created branch
git log --oneline --graph --decorate #find the SHA of the revert in the history, say SHA1
git revert SHA1
git push --set-upstream origin users/yourname/revertOfSHA1 #push the changes to remote

现在为分支 users/yourname/revertOfSHA1 创建 PR

I would suggest you to follow below steps to revert a revert, say SHA1.

git checkout develop #go to develop branch
git pull             #get the latest from remote/develop branch
git branch users/yourname/revertOfSHA1 #having HEAD referring to develop
git checkout users/yourname/revertOfSHA1 #checkout the newly created branch
git log --oneline --graph --decorate #find the SHA of the revert in the history, say SHA1
git revert SHA1
git push --set-upstream origin users/yourname/revertOfSHA1 #push the changes to remote

Now create PR for the branch users/yourname/revertOfSHA1

背叛残局 2024-08-02 22:01:23
  1. 在原始合并之前提交时创建新分支 - 将其称为“develop-base”
  2. 在“develop-base”之上执行“develop”的交互式变基(即使它已经在顶部)。 在交互式变基期间,您将有机会删除合并提交和反转合并的提交,即从 git 历史记录中删除这两个事件

此时您将拥有一个可以合并的干净的“开发”分支像往常一样,您的功能分支。

  1. create new branch at commit prior to the original merge - call it it 'develop-base'
  2. perform interactive rebase of 'develop' on top of 'develop-base' (even though it's already on top). During interactive rebase, you'll have the opportunity to remove both the merge commit, and the commit that reversed the merge, i.e. remove both events from git history

At this point you'll have a clean 'develop' branch to which you can merge your feature brach as you regularly do.

-柠檬树下少年和吉他 2024-08-02 22:01:23

我在遇到同样的问题时刚刚发现了这篇文章。 我发现上面的方法很难进行重置等操作。我最终会删除一些我不想要的东西,并且无法将其恢复。

相反,我检查了我希望分支返回到例如 git checkout 123466t7632723 的提交。 然后转换为分支git checkout my-new-branch。 然后我删除了我不再想要的分支。 当然,只有当你能够扔掉弄乱的树枝时,这才有效。

I just found this post when facing the same problem. I find above wayyy to scary to do reset hards etc. I'll end up deleting something I don't want to, and won't be able to get it back.

Instead I checked out the commit I wanted the branch to go back to e.g. git checkout 123466t7632723. Then converted to a branch git checkout my-new-branch. I then deleted the branch I didn't want any more. Of course this will only work if you are able to throw away the branch you messed up.

才能让你更想念 2024-08-02 22:01:23

如果需要,您可以创建一个新分支,并从恢复的拉取请求中cherry-pick提交(用空格分隔提交 SH,这样它将成为一个命令),这样您就可以合并再次不会丢失文件/更改。

If needed you can create a new branch and cherry-pick the commits (separate the commit SH with space so it will be one command) from the reverted pull request, that way you'll be able to merge again with out losing files/changes.

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