10

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.

2 Answers 2

6

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.

4
  • Yes, sort of, but "feature", "master", and "bugfix" are not actually branches, I suspect that's what's confusing. That is, 3 ppl committed to their own changes of the master branch. One guy did a bad merge and pushed. I pulled the bad merge. Now I want a way to exclude the bad merge while still keeping all the individual changes...
    – aberrant80
    Commented Jan 19, 2011 at 15:36
  • So feature and bugfix are no branches? Surely the author of feature has a branch in his own repo. What you should do is to reset your master to M2, and ask the author of feature to do the same (but make sure he does not lose the branch feature) and redo the merge, correctly this time. See my update if you want to do that yourself.
    – Gauthier
    Commented Jan 19, 2011 at 15:43
  • Do you really have a repo without a master branch?
    – Gauthier
    Commented Jan 19, 2011 at 15:48
  • We do have a master branch. Ok, now I see what you mean about getting the other guy to reset this branch. Thanks!
    – aberrant80
    Commented Jan 19, 2011 at 16:30
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.