简体   繁体   English

你如何恢复错误的git合并提交

[英]How do you revert a faulty git merge commit

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. idx是意外合并的主题分支,master是主线。 dev is just a test of how revert -m works so you can ignore it. dev只是测试revert -m如何工作,所以你可以忽略它。

替代文字

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: 所以从主人我尝试运行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 bug,还是我错过了什么?

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. 我认为这里有几个选项,但最简单的是做一个rebase。 Using your repo, these are the commands I used: 使用你的repo,这些是我使用的命令:

git rebase -i ead3646

Then, when the interactive shell comes up, remove the entire line of the faulty commit. 然后,当交互式shell出现时,删除错误提交的整行。 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. 让你们两个(和其他人)重新同步将需要一些分支,推送,电子邮件和午餐购买。

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. 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. 处理此问题的一种方法是再次进行合并,然后将其结果合并到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM