How to Undo Git Merge

Have you ever done a git merge, then realize that you haven’t meant it? Such kind of problems can happen anytime with any developer. In this short tutorial, you will find out how to act in such circumstances.

How to Undo an Unpushed Git Merge

Sometimes undoing a git merge, that has not been pushed can cause problems for developers. Finding the right solutions can take a lot of time and energy. Here are the methods that will help you save your time, and work more productively, as well.

Merge Resetting

One of the useful ways of undoing a git merge when you have not pushed it yet is to run git reset. Using this method will help you keep all the local changes you have made. It points to the last commit you made before the merge took place. To run this command, you should act like this:

git reset --merge ORIG_HEAD

In case merge conflicts occur, you need to invoke the merge abort command:

git merge --abort

As you can see, this is a perfect method to clean up your merge, saving the local changes at the same time. Anyway, it is not free of drawbacks. In case your local master branch is behind the remote branch with any commit quantity, don’t expect absolute results from this method. While using this method, you should consider another scenario, as well. For example, your Git history might be rewritten, and it can become hard for other programmers of your team to track the changes of the remote repository.

Hard Resetting

First of all, start from checking your git log, to learn by which commit the merge was preceded. Afterward, go on with resetting your local repository to the specific commit sha. For this purpose, you should run:

git log
git reset --hard COMMIT_SHA_REFERENCE

How to Undo a Pushed Merge

For undoing a merge, which has already been pushed, you can use the git revert command. Start from checking out the master branch using git checkout, like this:

git checkout master

Then, you need to run git log getting the id of the merge commit. To do that, act as follows:

git log

Next step is reverting to that commit using the git revert command:

git revert -m 1 <merge-commit>

With “ ‘-m 1’ ” you order Git reverting to the 1st parent of the master branch merge commit. For example, ‘-m 2’ will make Git will revert to the 1st parent, from where the merge initially came from.

Returning the Reverted Changes

This will change the data, not the history to look like before merging. So, it’s not totally like undoing. If you merge develop into the master once more, the changes that were reverted to it, can’t be applied. If you want to return the changes, you need to revert the 1st revert.

As you can see, there are different methods for undoing git merge. Before choosing one of the above-given methods, you should consider all the consequences it can bring.

Primary usage of Git Merge

Git merge is primarily aimed at combining two branches. You can also use it for merging several commits into a single history. Merge commits are considered unique for having two parent commits. Separate histories are merged automatically by Git anytime a new merge commit is produced. But it will not be combining the data that was changed in the histories.


Do you find this helpful?

Related articles