开发者

How do you revert a faulty git merge commit

开发者 https://www.devze.com 2023-01-23 19:44 出处:网络
So one of my coworkers accidentally made a merge开发者_C百科 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 th

So one of my coworkers accidentally made a merge开发者_C百科 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.

How do you revert a faulty git merge commit

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消