你如何恢复错误的 git merge 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4084013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:42:34  来源:igfitidea点击:

How do you revert a faulty git merge commit

gitversion-controlmerge

提问by Jason Axelson

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.

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

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:

我想要做的是恢复错误的合并。所以从 master 我尝试运行,git revert -m 1 <sha1sum of faulty merge>但 git 响应:

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

所以它实际上并没有创建一个撤销合并的还原提交。我相信发生这种情况是因为合并实际上并不包含任何真正的更改。

If you want to play around with my test repo you can download it from here

如果你想玩我的测试仓库,你可以从这里下载

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

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

采纳答案by brycemcd

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:

我认为这里有几个选项,但最简单的是做一个 rebase。使用你的 repo,这些是我使用的命令:

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 historylike this:

然后,当交互式 shell 出现时,删除错误提交的整行。保存它,你应该会得到一个像这样的新历史

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

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

回答by jilles

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

git 文档说这是关于恢复合并的:git/Documentation/howto/revert-a-faulty-merge.txt虽然这主要是关于恢复带来不良更改的合并,而不是恢复不正确的合并。

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:

解决这个问题的一种方法是再次进行合并,然后将结果合并到 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