Undoing Merges: Aborting, Resetting and Reverting a Merge Commit
Undo a merge at every stage: abort one in progress, remove an unpushed merge or fast-forward with ORIG_HEAD, revert a pushed merge with -m 1, and avoid the trap when the reverted branch is merged again, by reverting the revert. Also undoing a pull and choosing between the options.
kvi5hnuCode Sprout team member7 min readOn this page
Merges are undone differently from ordinary commits, for two reasons: a merge commit has two parents, so "undo its changes" needs a direction, and a merge brings in a whole branch, so undoing it affects how Git sees that branch later. This article covers each stage: a merge still in progress, a merge only you have, and a merge that's already shared.
| Situation | Command |
|---|---|
| merge in progress, conflicts showing | git merge --abort |
| merge finished, not pushed | git reset --hard ORIG_HEAD (or HEAD~1 for a merge commit) |
| merge pushed and shared | git revert -m 1 <merge-commit> |
| re-merging a branch whose merge was reverted | revert the revert first |
A merge in progress: abort
When a merge stops with conflicts, nothing has been committed yet. git merge --abort puts everything back as it was before git merge:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c metric; echo "Lake loop: 5.0 km" > lake.txt; git commit -q -am "Use one decimal"
git switch -q main; echo "Lake loop: 3.1 miles" > lake.txt; git commit -q -am "Use miles"
git merge metric > /dev/null
git status --short
git merge --abort
echo "=== after --abort"
git status --short; cat lake.txt
git log --oneline -1
UU lake.txt
=== after --abort
Lake loop: 3.1 miles
df24e24 Use miles
--abort also discards any conflict resolution you'd started. If you had uncommitted changes before the merge, Git may not be able to restore them exactly, which is one reason to commit or stash before merging. git merge --quit is the rarely needed alternative: it forgets the merge is in progress but leaves the files as they are.
The same pattern applies to the other multi-step commands: git rebase --abort, git cherry-pick --abort and git revert --abort.
A finished merge you haven't pushed: reset
If the merge is committed but only in your repository, simply move the branch back to where it was. Git records the pre-merge position in ORIG_HEAD:
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c ridge; echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git switch -q main; echo "Notes" > notes.txt; git add .; git commit -q -m "Add notes"
git merge -q --no-edit ridge
echo "=== after the merge"; git log --oneline --graph
git reset -q --hard ORIG_HEAD
echo "=== after reset --hard ORIG_HEAD"; git log --oneline --graph
echo "=== a fast-forward merge is undone the same way"
git switch -q -c ff-test; git reset -q --hard ridge~1 # a branch behind ridge
git merge -q --ff-only ridge
git log --oneline -1
git reset -q --hard ORIG_HEAD
git log --oneline -1
=== after the merge
* a7c2db1 Merge branch 'ridge'
|\
| * ad382c1 Add ridge page
* | 832a784 Add notes
|/
* cf8c779 Add lake page
=== after reset --hard ORIG_HEAD
* 832a784 Add notes
* cf8c779 Add lake page
=== a fast-forward merge is undone the same way
ad382c1 Add ridge page
cf8c779 Add lake page
- After a merge commit,
HEAD~1is the first parent, the commit your branch was on, sogit reset --hard HEAD~1works too. For a fast-forward, there's no merge commit andHEAD~1would be wrong, butORIG_HEADis still right. ORIG_HEADis overwritten by the next merge, reset, rebase or similar command. If you've done something else since, find the pre-merge position in the reflog (article 26):git reflogshows an entry likemerge ridge: Merge made by the 'ort' strategy., and the entry below it is where you were.git reset --merge ORIG_HEADis like--hard, but keeps uncommitted changes in files the merge didn't touch.
This also undoes an unwanted git pull that made a merge: git reset --hard ORIG_HEAD. If the pull rebased instead, ORIG_HEAD is the branch before the rebase, so the same command works.
A pushed merge: revert -m 1
Once others have the merge, don't reset: revert it. A merge commit has two parents, so tell Git which parent is the mainline, the side to keep. That's almost always the first parent, the branch you merged into:
git revert -m 1 <merge-commit>
The revert commit undoes every change the merge brought in from the other branch. Without -m, Git refuses: error: commit ... is a merge but no -m option was given.
The trap: merging the branch again
A reverted merge has a consequence that surprises almost everyone. The revert undoes the merge's changes, but the merge itself is still in history, so Git considers the branch's commits already merged. Later, when the branch is fixed and merged again, only the new commits come in. The original work stays reverted:
git init -q trail-guide && cd trail-guide
echo "Home page" > index.txt; git add .; git commit -q -m "Add home page"
git switch -q -c maps
echo "Lake map" > lake-map.txt; git add .; git commit -q -m "Add lake map"
echo "Ridge map" > ridge-map.txt; git add .; git commit -q -m "Add ridge map"
git switch -q main
echo "News" > news.txt; git add .; git commit -q -m "Add news"
git merge -q --no-ff --no-edit maps
echo "=== merged; then a problem is found, and the merge is reverted"
git revert --no-edit -m 1 HEAD > /dev/null
ls
git switch -q maps
echo "Scale 1:25000" > legend.txt; git add .; git commit -q -m "Add missing map legend"
git switch -q main
git merge -q --no-edit maps
echo "=== after merging the fixed branch: where are the maps?"
ls
=== merged; then a problem is found, and the merge is reverted
index.txt
news.txt
=== after merging the fixed branch: where are the maps?
index.txt
legend.txt
news.txt
The second merge brought only "Add missing map legend". Both maps are still missing, because their commits count as merged, and their removal by the revert is newer. The fix is to revert the revert before or after merging again:
git init -q trail-guide && cd trail-guide
echo "Home page" > index.txt; git add .; git commit -q -m "Add home page"
git switch -q -c maps
echo "Lake map" > lake-map.txt; git add .; git commit -q -m "Add lake map"
echo "Ridge map" > ridge-map.txt; git add .; git commit -q -m "Add ridge map"
git switch -q main
echo "News" > news.txt; git add .; git commit -q -m "Add news"
git merge -q --no-ff --no-edit maps
git revert --no-edit -m 1 HEAD > /dev/null
git switch -q maps
echo "Scale 1:25000" > legend.txt; git add .; git commit -q -m "Add missing map legend"
git switch -q main
git revert --no-edit HEAD > /dev/null # revert the revert: the maps work is back
git merge -q --no-edit maps # now bring in the fix
ls
git log --oneline --first-parent
index.txt
lake-map.txt
legend.txt
news.txt
ridge-map.txt
a60141b Merge branch 'maps'
cc659b6 Reapply "Merge branch 'maps'"
2d620dc Revert "Merge branch 'maps'"
98b6ae1 Merge branch 'maps'
0a3e47e Add news
9675943 Add home page
Notice the message Reapply "Merge branch 'maps'", Git's name for reverting a revert since 2.43.
If the re-merge conflicts, a variation helps: on the feature branch, merge main and revert the revert there, then fix, then merge the branch into main. The conflicts are then resolved on the branch, where the fix is being written.
Another alternative is to recreate the branch's commits with new IDs, for example with git rebase --force-rebase <commit-the-branch-started-from> on the feature branch. The replayed commits get new IDs, so Git no longer sees them as merged. This avoids the revert-of-a-revert but rewrites the branch, so it only suits branches nobody else uses.
Choosing
- Not finished yet:
git merge --abort. - Only in your repository:
git reset --hard ORIG_HEAD. History looks as if the merge never happened. - Shared:
git revert -m 1 <merge>. Remember that re-merging the branch later needs the revert reverted. - Pull request merged on a hosting service: most services offer a "Revert" button, which creates a pull request containing the same
git revert -m 1, so the same trap applies when the original branch is merged again, if the pull request was merged with a merge commit. After a squash or rebase merge, the branch's own commits never became part ofmain.
What's current
git merge --abort(andrebase,cherry-pickandrevert --abort) is the universal "get me out of here" for operations in progress.- Merges use the ort strategy (the default since Git 2.34). Since Git 2.50, asking for
recursivealso uses ort. - Since Git 2.43, reverting a revert is labelled
Reapply "...".
Common questions
Why does git revert <merge> say "is a merge but no -m option was given"?
A merge commit has several parents, so Git can't know which side to undo. -m 1 keeps the first parent, the branch you merged into, and undoes what came from the other branch.
I reverted a merge, fixed the branch and merged it again, but most of the feature is still missing. Why?
The original merge is still in history, so its commits count as merged, and the revert's removal of them stays in effect. Revert the revert commit (git revert <revert-commit>), then merge the branch's new commits.
How do I undo a git pull?
If you haven't done anything since, git reset --hard ORIG_HEAD. Otherwise find the position before the pull in git reflog and reset to it. Make sure you have no uncommitted changes you want, as --hard discards them.
Is it OK to reset a merge that I already pushed?
Only if nobody else has pulled it, and with git push --force-with-lease. On a shared branch, revert instead.
Summary
git merge --abortcancels a merge that stopped on conflicts.git reset --hard ORIG_HEADundoes an unpushed merge, including fast-forwards and pulls.git revert -m 1 <merge>undoes a pushed merge with a new commit, keeping the first parent's side.- After reverting a merge, merging the same branch again brings only new commits. Revert the revert to restore the original work.
- The reflog finds the pre-merge position when
ORIG_HEADhas moved on.
Practice questions
You merged feature into main locally, ran the tests, and they fail badly. Nothing is pushed. How do you get main back to before the merge?
Answer
git reset --hard ORIG_HEAD, provided you haven't run another merge, reset or rebase since. For a merge commit, git reset --hard HEAD~1 also works. If ORIG_HEAD has moved on, find the commit before the merge in git reflog.
A teammate merged payments into main and pushed. It breaks production. What do you run, and why -m 1?
Answer
git revert -m 1 <merge-commit>, then push. The merge commit has two parents: -m 1 tells Git to keep the first, the previous main, and undo everything that came from payments. History isn't rewritten, so everyone simply pulls the revert.
Later, payments is fixed and merged into main again, but only the fix shows up. What happened, and how do you fix it?
Answer
The first merge is still in history, so Git considers the earlier payments commits merged, and the revert's removal of their changes remains. Revert the revert commit on main (git revert <revert-commit-id>), which reapplies the original changes. Together with the new merge, main then has everything.
Previous: Undoing Changes: restore, reset, revert and Detached HEAD
Next: Recovering Lost Work With the Reflog: Commits, Branches, Rebases and Stashes

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.