Frequently asked questions around Git and Version Control.
Git FAQ featured image

How to Undo a Merge in Git

One of the best aspects about Git is that you can undo virtually anything. And, luckily, a merge is no exception!

Perhaps you merged the wrong branch, encountered conflicts during the merge process, or realized that the changes introduced are unnecessary. Whatever the case may be, undoing a merge is simply a matter of executing a few commands.

In this short article, we'll discuss how to undo a merge:

  1. With the "git reset" command: for merges that have only occured in your local repository.
  2. With the "git revert" command: for those situations where the merge has already been pushed to the remote repository.

The Git Cheat Sheet

No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!

Using git reset to Undo a Merge in Your Local Repository

You can use the git reset command to return to the revision before the merge, thereby effectively undoing it:

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

You will need to replace <commit-before-merge> with the hash of the commit that occurred before the merge.

To find this information, you can type git log --oneline or git reflog to see a list of recent operations and the corresponding hash values. A commit hash should look something like this: ee1a4f2.

If you don't have the hash of the commit and the merge was the most recent operation, you can also use the following command:

$ git reset --hard HEAD~1

This command can be useful if you've just completed a merge and realize that it was a mistake. By typing "HEAD~1", you're telling Git to go back to the commit before the current HEAD revision — which should be the commit before the merge!

As you may have noticed, in both cases, we are using the "--hard" option. This means that any local changes in your Working Copy will be discarded.

If you have important uncommitted changes you wish to keep, you have two options:

  1. You can use the git stash command to save those changes.

  2. Alternatively, you can use the "--merge" flag with the git reset command to reset the current branch to the state of the last commit while preserving any uncommitted changes that have not been staged.

For the latter method, simply replace --hard with --merge, as demonstrated below:

  $ git reset --merge <commit-before-merge>
  $ git reset --merge HEAD~1

Undoing a Merge in Tower

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z (or CTRL+Z on Windows) afterwards and Tower will undo the merge for you!

In Tower, you can easily revert nearly everything using this convenient keyboard shortcut.

Using git revert to Undo a Pushed Merge

The explanation above is useful if you HAVEN'T already pushed the merge to a remote repository. If you've already shared the merge commit with your colleagues on a remote, git revert is your friend.

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

It's important to note that git revert does not delete the merge history; instead, it creates a new commit that reverts the changes. This is in contrast to git reset, where we effectively "remove" a commit from the history. This is also the reason why git revert is a better solution in cases where you've already pushed to a remote.

One other important aspect to note is that in the git reset example provided earlier, we needed to specify the commit before the merge. In contrast, when using git revert, we have to specify the actual hash of the merge commit.

Let's summarize what this command is doing:

  1. git revert will make sure that a new commit is created to revert the effects of that unwanted merge.
  2. The -m 1 option tells Git that we want to keep the parent side of the merge (which is the branch we had merged into).
  3. Finally, we provide the correct merge commit hash, which can be obtained using the git log or git reflog commands.

Learn More

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.