Reverting a merge commit is one of those Git tasks that looks simple until Git asks you to choose a mainline parent. But unlike a normal commit, a merge commit has two parents, so the command git revert <commit> is not enough on its own. Consider this: to revert a merge commit safely, you need to tell Git which parent represents the branch you want to keep and which side of the merge you want to undo. This guide walks through the exact commands, explains why -m 1 is usually the right choice, and covers conflicts, reverting a revert, and when a reset might be a better option Simple, but easy to overlook..
What a Merge Commit Actually Contains
A merge commit records the joining of two histories. If you merge a feature branch into main, the resulting merge commit has:
- First parent: the tip of
mainbefore the merge. - Second parent: the tip of the feature branch that was merged in.
Git stores both parents in the commit object. On top of that, it cannot guess whether you want to undo the changes brought in from the feature branch or undo the changes on main. Which means when you run git revert on a regular commit, Git compares that commit with its single parent and creates a new commit that applies the inverse changes. With a merge commit, Git sees two possible comparisons. That is why Git requires the -m flag.
The -m flag stands for mainline parent. It tells Git which parent should be treated as the main line of development. The number you pass is the parent number, starting at 1.
Why git revert -m 1 Is Usually Correct
In most real-world cases, you merge a feature branch into main and later discover that the merge introduced a bug. You want to undo the feature branch changes while keeping everything else that happened on main. In that situation, the first parent is main, so you run:
git revert -m 1
This creates a new commit that reverses the changes introduced by the merge relative to the first parent. Worth adding: the merge commit itself remains in history. Nothing is rewritten. That is the key advantage of revert over reset: it is safe for shared branches because it adds a new commit instead of deleting existing commits.
If you merged in the other direction — for example, you merged main into a long-running release branch and now want to undo the changes that came from main — you might need -m 2. The correct number depends on which parent you consider the mainline. Always inspect the commit before choosing.
Step-by-Step: How to Revert a Merge Commit
1. Find the Merge Commit
Start by locating the merge commit you want to undo. A graph view makes merge commits easy to spot:
git log --oneline --graph --decorate
Merge commits often have a message such as Merge branch 'feature/login' into main. Copy the commit hash. You can also use:
git log --merges --oneline
This lists only merge commits, which is useful in a busy repository Simple as that..
2. Inspect the Parents
Before running the revert, confirm the parent order:
git show --no-patch --pretty=raw
Look for the parent lines. The first parent line is parent 1, the second is parent 2. You can also use:
git cat-file -p
The order matters. If parent 1 is the branch you want to preserve, use -m 1. If parent 2 is the branch you want to preserve, use -m 2.
3. Run the Revert
For the common case of undoing a feature branch merge into main:
git revert -m 1
Git will open an editor with a default revert message. Here's the thing — you can keep it or customize it. A clear message helps future readers understand why the merge was undone Most people skip this — try not to..
Revert "Merge branch 'feature/login' into main"
This reverts commit 1a2b3c4d, reversing changes made to 5e6f7a8b.
4. Resolve Conflicts If They Appear
A revert can conflict, especially if later commits touched the same lines. Git will pause and mark the conflicted files. Resolve them as you would any merge conflict, then stage the resolved files:
git add
git revert --continue
If you want to abandon the revert entirely, run:
git revert --abort
Do not commit half-resolved conflicts. A revert commit should represent a clean inverse of the merge.
5. Push the Revert Commit
Once the revert commit is created, push it to the shared branch:
git push origin main
Because revert adds a new commit, other collaborators can pull it normally. No one needs to force-push or rewrite local history.
What Happens to the Reverted Changes?
The revert commit does not delete the original merge commit or the feature branch commits. Here's the thing — it creates a new commit whose changes cancel out the merge’s effect. The history now shows that the merge happened and was later reverted.
That is usually desirable for auditability, but it means the changes are not truly gone—they are simply inverted by a new commit. Also, if you later decide the feature should come back, you cannot simply merge the same branch again and expect Git to reapply everything. Because the original merge is still an ancestor, Git may treat those changes as already present in the branch’s history.
Quick note before moving on Simple, but easy to overlook..
To restore the work, you generally have two practical options:
-
Revert the revert. Locate the revert commit you just created and run:
git revertThis reapplies the changes from the original merge. Afterward, you can merge the feature branch again if needed.
-
Start fresh on a new branch. If the feature has evolved significantly or the original merge caused problems, it is often cleaner to fix the issues on a new branch and merge that instead of fighting the old revert history.
Never use git reset or git rebase to erase a merge commit that has already been pushed to a shared branch. Rewriting public history forces every collaborator to deal