How to revert a merge in git (git revert -m 1) and why re-merging does nothing
A pull request was merged, something broke, and you want it gone. If the merge created a merge commit, a plain git revert will not work. This is a short guide to doing it correctly and to the trap that catches people afterwards.
Why a plain revert fails
A merge commit has two parents. Git cannot tell which side you want to keep, so git revert stops with an error that the commit is a merge but no -m option was given.
The fix: -m 1
Run git revert -m 1 followed by the merge commit hash. The -m option names the mainline parent. Parent 1 is the branch you merged into (usually main) and parent 2 is the branch that was merged in. With -m 1, git keeps the main side and undoes what the branch brought in.
- Find the merge commit with git log --merges.
- Revert it with git revert -m 1 <hash>.
- Push the revert through a pull request if your branch is protected.
- Squash and rebase merges do not create merge commits. A squash is one ordinary commit, so a plain git revert works.
On GitHub
A merged pull request has a Revert button. It opens a new pull request that undoes the merge, with the right parent chosen for you. Merge it like any other pull request, and your normal deploy runs again with the old code.
The re-merge trap
After you revert a merge, git remembers that the branch was merged. If you fix the branch and merge it again, git compares the history, sees that the branch's old commits are already part of main, and brings in only the new commits. The changes that the revert removed do not come back. The merge looks successful and the feature is still missing.
- Option 1: revert the revert. This puts the original changes back, and you add your fix on top of the branch and merge again.
- Option 2: rebuild the branch. Create a new branch from main and apply the original changes again, with cherry-pick or by re-creating the commits, then add the fix and open a new pull request.
- Git's own documentation on reverting a faulty merge describes both. The second is cleaner when the original branch was messy.
Check before and after
- Before: confirm which commit is the merge, and that nobody pushed on top of it that depends on it.
- After: run the build and tests on the revert, and compare the files with the version from before the merge.
- Database migrations and other side effects do not revert with code. Check those separately.
How Merge Scheduler handles it
If a deploy fails after a scheduled merge, Merge Scheduler opens a revert pull request, merges it, and watches the redeploy. It uses GitHub's own revert, so the right parent is chosen for you. If the revert would conflict, or the redeploy also fails, it stops and tells you which step failed, so a person decides.
Do this in Merge Scheduler: Set up automatic revert for a failed deploy
Related: Merge commit vs squash vs rebase: what each GitHub merge actually does · How to automatically revert a failed deploy (and when not to) · Why your auto-rollback job can't push the revert to main