Branches and rebase

Git undo merge: undoing a merge with reset or revert

Git undo merge in three cases: abort a merge in progress, reset your branch if the merge is local, or revert a pushed merge commit with git revert -m 1.

On this page

Want to undo a merge in Git? Check if it’s pushed first

To undo a merge in Git, first check whether you pushed it. If not, git reset moves your branch back to the commit before the merge. If you did, git revert -m 1 adds a commit that undoes the merge without rewriting shared history.

Three cases, one command each:

  1. The merge is still in progress, stopped on conflicts: git merge --abort.
  2. The merge commit is only local: git reset --hard ORIG_HEAD (the branch tip from before the merge), or git reset --merge ORIG_HEAD to keep your local changes.
  3. The merge is in the remote repository: git revert -m 1 <merge commit hash>.

git status tells you which case you’re in. Here the branch is ahead of origin/main, so the merge is not pushed yet:

On branch main
Your branch is ahead of 'origin/main' by 3 commits.
  (use "git push" to publish your local commits)

Undo a merge commit on your branch before you push it

In this throwaway repository, main merged a feature branch with two commits (Git 2.54.0; your IDs will differ):

*   fb6ace6 (HEAD -> main) Merge branch 'feature'
|\
| * 40f385b (feature) Add search tests
| * 6379d7c Add search
* | b4692ec Rename project title
|/
* 12f78f6 Add default config

Right after the merge, ORIG_HEAD points to the old tip of main: Git’s merge documentation says it is set to the branch tip when the operation starts. Check it, then reset:

git log --oneline -1 ORIG_HEAD
git reset --hard ORIG_HEAD
b4692ec Rename project title
HEAD is now at b4692ec Rename project title

The merge is gone from main, and feature keeps its two commits. Use ORIG_HEAD right away, because the next reset or merge moves it.

--hard overwrites tracked files: in my test, an uncommitted edit to config.txt was lost. With git reset --merge ORIG_HEAD, shown in the reset documentation for this case, the edit stayed and the merge still went away.

Undoing a merge after it’s pushed with git revert

Once you’ve pushed, teammates may already have the merge: use git revert, which adds a commit instead of rewriting history. Without options, Git refuses to revert a merge commit:

error: commit fb6ace61dd7eaeaa689d9023a559a7cac2e60b17 is a merge but no -m option was given.
fatal: revert failed

Git cannot tell which side of the merge is the mainline. -m gives the number of that parent, starting from 1. git show lists the parents in order, here Merge: b4692ec 40f385b: parent 1 is the old tip of main, parent 2 the tip of feature.

git revert --no-edit -m 1 fb6ace6
[main fc8b98a] Revert "Merge branch 'feature'"
 2 files changed, 2 deletions(-)
 delete mode 100644 search.txt
 delete mode 100644 tests.txt

The revert removed the two files feature brought in. It’s a new commit on top of main, so a plain git push publishes it.

The git-revert documentation adds a catch: reverting a merge declares that you will never want its tree changes. When I fixed feature and merged it again, tests.txt did not come back.

The official “Revert a faulty merge” how-to says to revert the revert first if you fix the branch with new commits, and to merge again without that step if you rebuild the branch from scratch. Here, git revert fc8b98a created Reapply "Merge branch 'feature'", and the next merge brought both files back.

Handling merge conflicts when undoing, and recovering from a bad reset

A merge stopped on a conflict has no merge commit yet, so git merge --abort puts the branch back. Git’s merge documentation recommends a git commit or git stash before any merge, because the abort cannot always rebuild uncommitted changes.

If git reset --merge refuses with Entry 'search.txt' not uptodate, that file has edits the merge also touched. Run git stash first, or accept that --hard discards them.

A reset to the wrong commit is not final. git reflog lists where HEAD has been:

b4692ec HEAD@{0}: reset: moving to ORIG_HEAD
fb6ace6 HEAD@{1}: merge feature: Merge made by the 'ort' strategy.
b4692ec HEAD@{2}: commit: Rename project title

The merge is one move back, so git reset --hard HEAD@{1} brought it back. In GitMini, a merge made in the app ends with an Undo toast, and the reflog panel shows the same list.

Sources