How to Undo Git Merge

Sometimes, you want to undo the merge operation no matter it has happened because of the merge command, the pull command or something else. Here, we will consider several options of undoing merge in Git.

How to Undo a Merge with Conflicts

In case conflicts occurred after calling the merge command, then you can undo the whole process by using the command below:

git merge --abort

How to Undo an Unpushed Merge Commit

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

git reset --merge ORIG_HEAD

As an alternative, you can find the appropriate hash of the merge commit by running the git log command. After that, you need to put the hash into the following command:

git reset --hard <merge-commit-hash>

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 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.

How to Undo a Pushed Merge Commit

Undoing with the git reset command

In the framework of this approach, you need to reset the merge commit as it is mentioned in the section above, then run the command below:

git push origin HEAD --force
Instead of --force, you can use a safer method: --force-with-lease, which will not overwrite the work of other developers.

Undoing with the git revert command

If you don't want to make difficulties for the developers working in parallel, then you can use the git revert command. To do that, you need to find the appropriate hash of the merge commit using the git log command. Then, you should put the hash into the command below:

git revert -m 1 <merge-commit-hash>

With -m 1 you order Git reverting to the 1st parent of the merge commit. If you want to revert to the 2nd parent, you should use the -m 2 option.

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.