Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 240
Jason Axelson Avatar asked Nov 03 '10 03:11

Jason Axelson


People also ask

How do I undo a faulty merge with revert git?

simply run git reset --hard to revert all those changes.

How do I revert a merge commit in git?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do you undo a git merge after pushing the changes?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu.


2 Answers

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
like image 182
jilles Avatar answered Oct 24 '22 01:10

jilles


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.

like image 39
brycemcd Avatar answered Oct 24 '22 00:10

brycemcd