Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing a git merge

Tags:

git

merge

I'm not that experienced with Git and now I have a big problem falling into my lap.

Here's how my current branch look like:

feature       /---F1-----F2----\
             /                  \
master -----M0-----M1-----M2-----M3-----M4
             \                        /
bugfix        \--B1-----B2-----------/

The situation:

Someone did a very bad thing and pushed a really bad merge (M3). I only noticed the bad merge when our models (not source code) wouldn't load after I merged B1 and B2 into M4. Luckily, I haven't pushed M4 yet.

The problem:

How do I set things right again? I want M0, M1, M2, F1, F2, B1, and B2. But I don't want M3 and M4 (since M4 is obviously broken). If I have to abandon changes, then F1 and F2 can be sacrificed :)

I looked at git revert but I'm not confident that I fully understand how it works. So... I'm really hoping for help on how to resolve this.

Thanks in advance.

like image 467
aberrant80 Avatar asked Jan 19 '11 11:01

aberrant80


2 Answers

So to clarify, what you want is this, right?

feature       /---F1-----F2
             /                 
master -----M0-----M1-----M2
             \            
bugfix        \--B1-----B2

Make sure that the HEADs feature and bugfix are still on F2 and B2 respectively.

Edit: if feature and bugfix are no branches, make them branches (stash your work if it is uncommited):

git checkout F2
git branch feature
git checkout B2
git branch bugfix

Edit end

Then reset the master to M2:

git checkout master
git reset M2 --hard

(reset will make you lose your local changes, stash them if you don't want that.)

M3 and M4 are not destroyed right away, but will be eventually. They should not show up in git log or gitk.

Then it's time to merge feature and make a M3' that is correct.

like image 159
Gauthier Avatar answered Sep 19 '22 06:09

Gauthier


If you didn't push (publish) the merge commits yet, use git reset. E.g. on the master branch, assuming your working tree is clean, do git reset --hard .

More info: Undo a Git merge that hasn't been pushed yet

If you did publish your changes you might want to consider making a revert commit instead. More info here: http://progit.org/2010/03/02/undoing-merges.html

like image 37
Lasse Avatar answered Sep 18 '22 06:09

Lasse