Terminus by Warp
Undo a Git Merge

Undo a Git Merge

Philip Wilkinson
Philip Wilkinson
Software Engineer, Amazon

A key feature of Git is the ability to merge branches together to bring in new features or resolve bugs. Sometimes, mistakes will happen and you may need to undo the effects of a `git merge` operation. This may be because you merged in the wrong branch, the merge has led to conflicts, or the changes introduced are not desirable or no longer necessary.

The short answer 

The easiest way to undo a merge is using [.inline-code]git revert[.inline-code]. The [.inline-code]git revert[.inline-code] command is used to create a new commit that undoes the changes introduced by a specific commit. In this case, this is the merge commit.

To undo a merge using the [.inline-code]git revert[.inline-code] command you need to:

  1. Find the commit hash of the merge commit using [.inline-code]git log[.inline-code].
  2. Revert the merge commit by running [.inline-code]git revert -m 1 <merge-commit-hash>[.inline-code]

Adding the option [.inline-code]-m 1[.inline-code] to the [.inline-code]git revert[.inline-code] command tells Git that you want to keep the parent side of the merge (the branch you merged into). If you want to keep the side of the branch merged, you change the [.inline-code]1[.inline-code] to a [.inline-code]2[.inline-code] instead. 

Keep in mind that [.inline-code]git revert[.inline-code] does not erase the history of the merge; it creates a new commit that effectively undoes the change. This is the best method for undoing a merge, especially when you have already pushed the changes to a remote branch.

[#easily-recall-with-ai]Easily retrieve this command using Warp’s AI Command Search[#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code]git revert merge[.inline-code] in the AI command search will prompt a [.inline-code]git-revert[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#undoing-with-git-reset]Undoing a merge with [.inline-code]git reset[.inline-code][#undoing-with-git-reset]

Another way to undo a merge is by using [.inline-code]git reset[.inline-code]. The [.inline-code]git reset[.inline-code] command allows you to move the branch pointer to a previous commit, effectively rolling back the changes.

This can be done using either the '--merge' or '--hard' flag. The main difference being that the '--merge' flag will not reset unstaged changes, while the '--hard' flag will reset all files to the target commit. If you use the '--merge' flag and a file has unstaged changes but has had commited changes since the target commit, the reset will be aborted.

[#using-the-merge-flag]Using the [.inline-code]--merge[.inline-code] flag[#using-the-merge-flag]

The safest way to undo a merge is using the [.inline-code]--merge[.inline-code] flag. To undo a merge with the [.inline-code]--merge[.inline-code] flag you need to:

  1. Find the commit hash of the branch before the merge using [.inline-code]git reflog[.inline-code]
  2. Reset the branch using [.inline-code]git reset --merge <commit-before-merge>[.inline-code] to move the branch pointer to that commit and discard the changes introduced by the merge.

Using the [.inline-code]--merge[.inline-code] flag keeps uncommitted changes part of the branch and only resets the merge.

[#using-the-hard-flag]Using the [.inline-code]--hard[.inline-code] flag[#using-the-hard-flag]

The other way of doing this is using the [.inline-code]--hard[.inline-code] flag which will remove all changes after the merge. To do this you need to:

  1. Find the commit hash of the merge commit using [.inline-code]git log[.inline-code]
  2. Reset the branch using [.inline-code]git reset --hard <commit-before-merge>[.inline-code] to move the branch pointer to that commit and discard the changes introduced by the merge

It is important to note that unlike [.inline-code]git revert[.inline-code], [.inline-code]git reset[.inline-code] rewrites the history of the branch so you need to use this with caution, especially if the branch has already been pushed to a remote repository.

[#finding-the-merge-commit]Finding the commit before the merge[#finding-the-merge-commit]

You can also replace [.inline-code]<commit-before-merge>[.inline-code] in the above commands with:

  • [.inline-code]HEAD~1[.inline-code] which will find the commit before the current head.
  • [.inline-code]ORIG_HEAD[.inline-code] which finds the commit of the branch before the last merge.

[#undoing-local-vs-remote]Undoing local vs remote merges[#undoing-local-vs-remote]

Undoing a local merge as opposed to one that has already been pushed to a remote repository differs in the implications for the history of the repository. To undo a local merge you can use any of the methods discussed above as long as you are okay with changing the history of your repository. 

However, if the merge has already been pushed to a remote repository, you should be cautious about using [.inline-code]git reset[.inline-code] and [.inline-code]git push --force[.inline-code] as this can cause issues for collaborators who have already pulled the changes. In such cases it is generally recommended to use [.inline-code]git revert[.inline-code] to create a new commit that undoes the changes without altering the history.

[#undoing-in-github]Undoing a merge in GitHub[#undoing-in-github]

Unfortunately, Git itself does not have a built-in mechanism to directly undo a merge using the GitHub UI. To undo a merge that happened on a remote repository, you would need to perform the undo locally using one of the methods mentioned above ([.inline-code]git revert[.inline-code] or [.inline-code]git reset[.inline-code]) and then push the changes back to the remote repository.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.