How to Undo or revert a Merge in Git

Guide of Undo or revert a Merge in Git – Merging is the process of combining two or more branches into one HEAD branch. (If you’re interested in learning more about merge in general, see our git merge Command Overview.)

We’ll go over how to undo or revert a merge with Git in this brief tutorial.

Why undo merge

You may find yourself in a situation where you make a merge, commit it, and possibly even push it to your remote server before recognizing there’s a problem. You’ll need a mechanism to restore your main branch’s former state if this happens.

You may run git merge — if you were still in the middle of the merge operation.

abort if you want to cancel the merge – Git cleans everything up nicely, and you’ll be back in the same state as your main branch.

If you’ve already completed your merging, though, you won’t be able to do so. Here’s what you’ll need to do instead: First, double-check the main branch into which you merged your changes. The next steps should have an impact on this branch.


$ git checkout master

How does reverting a merge in git works?

Git makes merge commits with a commit hash that represents the moment in history where the other branch was merge in, much as it does with regular commits. Using this information, we can use the git revert command to restore your branch to its prior state.

git reverse generates a new commit with a set of changes that, when applied, provide the exact inverse of the commit you supply. This implies that both the original commit and the new inverse commit will be save in history. Retaining all of the information about what happened but leaving your branch in the same condition as before the initial commit.

However, due to the way merges work, you will have to put in some extra effort. Normally, you can input git revert the hash of the commit you wish to reverse, and Git will decide which modifications to revert by looking at that commit’s pointer to its parent commit.

Due to the fact that a merge commit contains two parent commits (one from each branch you’re merging), you’ll need to identify which parent is the “mainline” — the branch you merge into

That’s where the -m flag comes in: specifying a 1 refers to the branch you were working on when you start the merge (a 2 refers to the branch that was merge in, which isn’t useful in our instance). The’revert’ documentation explains how this works.

Using git reset to Undo a Merge

One of the most appealing features of Git is the ability to undo almost anything. And, thankfully, a merger isn’t one of them! The git reset command can be use to go back to the revision preceding the merge, thus undoing it:


$ git reset --hard <commit-before-merge>

You can also use the following form of the command if you don’t have the hash of the commit before the merging on hand:


$ git reset --hard HEAD~1

Using “HEAD~1,” you’re telling Git to go back to the commit that came before the current HEAD revision — which should be the commit that came before the merging!

Please note that we must use the “–hard” option in both circumstances. Any local modifications in your Working Copy will be discarded as a result; if you have important uncommitted changes, make sure to use git stash first.

How to undo a pushed merge

When you haven’t yet pushed the merge to a remote repository, the preceding example is useful. You should consider a different strategy in this scenario, especially if you’ve already shared the merge commit with your remote colleagues.


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

Let’s look at what this command does in more detail:

  • git revert will ensure that a new commit is made to undo the undesirable merge’s consequences. git reset, on the other hand, effectively “removes” a commit from the history. That’s also why, in circumstances when you’ve already pushed to a remote, git revert is a superior option.
  • The -m 1 option informs Git that we want to keep the merge’s parent side (which is the branch we had merged into).
  • Finally, make sure you offer the correct commit hash: in the git reset example above, we had to provide the commit before to the merge; but, when using git revert, we must provide the hash of the actual merge commit.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Leave a Comment