Stories From The Git Frontline (or, how to revert / undo a merge that's already pushed)

7 November 2014

So, this happened:

  • was working on a different branch
  • did a merge back into master too early
  • mistook git revert for "take me back to this point" instead of what it actually does (undo one specific commit)

The tree then ended up looking like:

  • a revert i don't actually mean to have
  • a merge of a bunch of stuff i dont want
  • a previous good merge before my merge
  • the commit which i mistakenly reverted
  • good stuff from before that needs to go out in a release very soon

I tried a lot of things: hard reset, reverting each individual commit of my bad merge, checkout master to a previous point and force push (which, by the way, it looks like Visual Studio Online doesn't really let you do)...

But finally I got it back to exactly where I wanted, before I did the merge, and it turns out to be pretty easy. All I needed to do was:

  • revert my revert
  • revert my merge

The key points to takeaway is that when you revert a merge, make sure you select the right mainline (normally 1 or 2). My first attempt I didn't really understand or read the documentation for the -m parameter, and just assumed that reverting merges was stupid and broken, where actually I had just selected the wrong branch to consider as the one to keep.

The command is pretty easy, and looks like:

git revert -m 1 commit_hash

So if using a mainline of 1 doesn't give you what you expect, try 2.

While this might not be as easy as "rollback and push-override", this has less of an effect on other people working from the same shared repository, and will leave a nice reminder in the history that I made a mistake, forever :)

Tags: git, merge, revert

Add a Comment

No Comments