Undoing a local Git merge

December 10, 2015, 10:34 pm
Author: James Griffiths

Git is a fantastic tool for managing project code within a team context but, as with any tool, there'll always be occasions when you run into problems.

One such problem might occur where you want to undo a local branch merge that hasn't been pushed to the remote repository. Sounds tricky right?

The following are ways in which this can be accomplished relatively quickly...

Hard reset

Check your git log to determine which commit preceded the merge and then reset your local repository to that specific commit sha:

git log
git reset --hard COMMIT_SHA_REFERENCE

Alternatively you can opt to roll back by a specified number of commits like so:

git log

Don't forget the tilde character in the above method!

The major downside of these approaches? You won't retain any local changes you've made so use with caution!

Merge reset

The following approach should retain your local changes while pointing to the last commit made before the merge took place:

git reset --merge ORIG_HEAD

This makes it an ideal method for cleaning up a merge while preserving local changes but, as you might have guessed, there are downsides to using this approach - if your local master branch is behind the remote branch by any number of commits then this method may not give the expected results.

There's also the possibility that this method could rewrite the Git history making it confusing/difficult for other developers in the team to determine the flow of changes made to the remote repository when the commit is finally pushed. We're not sure how true this is and it may only apply when reverting changes that have already been pushed to the remote repository.

So there you have it, a number of ways to accomplish undoing local merges in Git.

Whichever option you choose be aware of the consequences and, as with any tool, use wisely!


« Return to Posts

Post a comment

All comments are welcome and the rules are simple - be nice and do NOT engage in trolling, spamming, abusiveness or illegal behaviour. If you fail to observe these rules you will be permanently banned from being able to comment.