如何恢复错误的 git merge 提交

发布于 2024-09-30 05:25:27 字数 566 浏览 2 评论 0原文

因此,我的一位同事不小心进行了一次合并,实际上只保留了树的一侧。所以他开始合并,删除合并引入的所有更改,然后提交合并。

这是我在测试仓库上做的一个简单的测试用例。这个仓库有三个分支。 idx是不小心合并的主题分支,master是主线。 dev 只是对 revert -m 工作方式的测试,因此您可以忽略它。

alt text

我想要做的是恢复错误的合并。因此,我尝试从 master 运行 git revert -m 1 但随后 git 响应为:

# On branch master                               
nothing to commit (working directory clean)

所以它实际上并没有创建撤消合并的恢复提交。我相信发生这种情况是因为合并实际上并未包含任何真正的更改。

这是一个 git bug,还是我遗漏了什么?

So one of my coworkers accidentally made a merge that actually only kept one side of the tree. So he started a merge, deleted all the changes introduced by the merge, and then committed the merge.

Here is a simple test case I did on a test repo. This repo has three branches. idx is the topic branch that was accidentally merged, and master is the mainline. dev is just a test of how revert -m works so you can ignore it.

alt text

What I want to do is revert the faulty merge. So from master I try to run git revert -m 1 <sha1sum of faulty merge> but then git responds with:

# On branch master                               
nothing to commit (working directory clean)

So it doesn't actually create a revert commit that undoes the merge. I believe that this happens because the merge didn't actually contain any real changes.

Is this a git bug, or am I missing something?

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

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

发布评论

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

评论(2

对你而言 2024-10-07 05:25:27

git 文档说明了有关恢复合并的信息: git/Documentation/howto/revert-a-faulty-merge.txt
尽管这主要是关于恢复带来不良更改的合并,而不是恢复错误完成的合并。

基本上,恢复合并将撤消数据更改,但不会撤消历史(图表)更改。因此,预计恢复错误的合并不会产生任何作用。

处理这个问题的一种方法是再次进行合并,然后将结果合并到 master 中。在你的例子中,这可能是这样的:

git checkout -b temp/merge-fixup ead364653b601f48159bca5cb59d6a204a426168
git merge 2fce9bfe8f721c45ea1ed5f93176322cac60a1d9
git checkout master
git merge temp/merge-fixup
git branch -d temp/merge-fixup

The git documentation says this about reverting merges: git/Documentation/howto/revert-a-faulty-merge.txt
although this is mostly about reverting merges that brought in bad changes, rather than reverting merges that were done incorrectly.

Basically, reverting a merge will undo the data changes, but not the history (graph) changes. Therefore it is expected that reverting your faulty merge does nothing.

One way you can deal with this is to do the merge again, then merge the result of that into master. In your example this could be like:

git checkout -b temp/merge-fixup ead364653b601f48159bca5cb59d6a204a426168
git merge 2fce9bfe8f721c45ea1ed5f93176322cac60a1d9
git checkout master
git merge temp/merge-fixup
git branch -d temp/merge-fixup
执着的年纪 2024-10-07 05:25:27

这里最好的经验法则是在你的存储库公开后永远不要改变它的历史。这真的把事情搞砸了。但我认为这个案子无法避免。

我认为这里有几个选择,但最简单的是进行变基。使用您的存储库,这些是我使用的命令:

git rebase -i ead3646

然后,当交互式 shell 出现时,删除错误提交的整行。保存下来,你应该会得到一个新的历史记录,如下所示:

* f0ab6d5 more normal work on dev
* ebb5103 idx commit
* ead3646 master change
* 582c38c dev commits
* f4b8bc6 initial commit

让你们两个(和其他人)恢复同步需要进行一些分支、推送、发送电子邮件和购买午餐。

The best rule of thumb here is to never ever change the history of your repo after it has been made public. It really screws things up. I don't think this case can avoid it though.

I think there are a couple of options here but the simplest is to do a rebase. Using your repo, these are the commands I used:

git rebase -i ead3646

Then, when the interactive shell comes up, remove the entire line of the faulty commit. Save that and you should wind up with a new history like this:

* f0ab6d5 more normal work on dev
* ebb5103 idx commit
* ead3646 master change
* 582c38c dev commits
* f4b8bc6 initial commit

Getting you two (and others) back in sync is going to take some branching, pushing, emailing and lunch buying.

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