如何恢复错误的 git merge 提交
因此,我的一位同事不小心进行了一次合并,实际上只保留了树的一侧。所以他开始合并,删除合并引入的所有更改,然后提交合并。
这是我在测试仓库上做的一个简单的测试用例。这个仓库有三个分支。 idx是不小心合并的主题分支,master是主线。 dev 只是对 revert -m 工作方式的测试,因此您可以忽略它。
我想要做的是恢复错误的合并。因此,我尝试从 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
git 文档说明了有关恢复合并的信息: git/Documentation/howto/revert-a-faulty-merge.txt
尽管这主要是关于恢复带来不良更改的合并,而不是恢复错误完成的合并。
基本上,恢复合并将撤消数据更改,但不会撤消历史(图表)更改。因此,预计恢复错误的合并不会产生任何作用。
处理这个问题的一种方法是再次进行合并,然后将结果合并到 master 中。在你的例子中,这可能是这样的:
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:
这里最好的经验法则是在你的存储库公开后永远不要改变它的历史。这真的把事情搞砸了。但我认为这个案子无法避免。
我认为这里有几个选择,但最简单的是进行变基。使用您的存储库,这些是我使用的命令:
然后,当交互式 shell 出现时,删除错误提交的整行。保存下来,你应该会得到一个新的历史记录,如下所示:
让你们两个(和其他人)恢复同步需要进行一些分支、推送、发送电子邮件和购买午餐。
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:
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:
Getting you two (and others) back in sync is going to take some branching, pushing, emailing and lunch buying.