5

I had a bad merge, I did not know it was a bad merge until I pushed it to GitHub and now I do not know how to undo/revert it.

The story goes like this:

I opened two distinct branches and committed several changes to those branches. After a while, I decided to merge those branches back to master branch and I use $ git merge branch_name for this purpose. I merged the first branch and checked out the log of master branch afterwards: all commits on that branch were on master. Then I proceeded with the merging of second branch by the same command. Then I checked the log of master again, where I saw the commits on the branch I merged including a commit with the following comment: "merge branch branch_name". A comment like this was not included when I merged the other branch.

Long story short, the problem is that git merged the commits of the first branch to master like they are the commits of the master already. When you examine the network graph from 'GitHub' or the graph from 'gitg', there is no trace of the first branch left, but its commits are shown as the commits of master branch. There is no problem with the second branch: it is shown separately and it is merged as expected. Also note that, after running the merge commands, I went on deleting the branches by $ git branch -d branch_name, like it was the most urgent thing to do, unfortunately.

Now I want to go back to the position just before I ran two consecutive merges. Any help is appreciated. If you comment on why this has happened, it will be pleasing as well.

2
  • 1
    I can at least tell you that what happened was that the first branch was a fast-forward-merge, and this is not a bad thing to happen at all. If you really need to keep the history, in the future, you have to call merge with the --no-ff option. Commented Feb 21, 2013 at 9:10
  • Until you get an answer, I can add two more things: first, git reflog might be of help here, second, whatever you do, if you undo and redo the merge with a non-ff merge, you'll end up having a non-fast-forward push, meaning you have to force-push to github. This is generally a bad thing, because it will cause serious trouble for all people who have pulled the repository in the meantime. Commented Feb 21, 2013 at 9:11

1 Answer 1

4
  1. Find the commits that were the heads of the old topic branches that you deleted. They should be the two parents of the merge commit. Make them into branches again (git branch or git checkout -b will do the trick nicely).

  2. Move master back to its old position with git reset. At this point, you're back where you started before you merged.

  3. Next time when you merge, use git merge --no-ff. This will create a merge commit even if Git doesn't need to.

I almost always use git merge with either --ff-only or --no-ff.

12
  • There is no merge commit for the first merge, so no parents. Commented Feb 21, 2013 at 9:13
  • @JonasWielicki: There is a merge commit. It has two parents. Those are the parents I'm talking about. Commented Feb 21, 2013 at 9:14
  • Aaah, nevermind, I got it now. If you now add git reflog or something else useful as a hint on how to find the old masters HEAD, you get a +1 :) Commented Feb 21, 2013 at 9:15
  • @JonasWielicki: It should be obvious what the old master head was. You find where the branches meet up. Commented Feb 21, 2013 at 9:17
  • Hmm. I'm not sure about that, but I'll better believe you before I had my first coffee ;) Commented Feb 21, 2013 at 9:17

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