Sub topic 12
Reverting a merge commit with -m
This chapter assumes you know how git revert works on an ordinary commit and what a merge commit is. It covers the extra step Git needs when the commit you want to undo is a merge.
Why a merge needs -m
An ordinary commit has one parent, so Git can compute its change by comparing it with that parent. A merge commit has two parents. Git can't tell which side to compare against, so it refuses:
git revert 5f3c9a1error: commit 5f3c9a1 is a merge but no -m option was given.The -m option (short for --mainline) takes a parent number. It tells Git which parent is the mainline, meaning the line of history you want to keep. Git then reverses everything the merge brought in from the other parent.
Finding the parent numbers
Parents are numbered in the order Git recorded them. Parent 1 is the branch you were on when you ran git merge. Parent 2 is the branch you merged in. List them like this:
git show --no-patch --format="%H%nparents: %P" 5f3c9a1The output shows two hashes after parents:. The first is parent 1.
In this graph, M has parent 1 D (on main) and parent 2 C (on feature).
Reverting the merge
To remove the feature's changes and keep main's history, choose parent 1 as the mainline:
git revert -m 1 <merge-commit-hash>
git push origin mainGit creates a new commit that reverses the changes made by commits B and C. History is not rewritten, so the push is a normal fast-forward and is safe on a shared branch.
Using -m 2 would treat the feature branch as the mainline. The new commit would then undo what main did relative to the feature. That is almost never what you want.
The gotcha: merging the branch again
A revert undoes the changes, but it doesn't undo the fact that the merge happened. Git still records B and C as part of main's history. If you fix the feature branch and merge it again, Git sees those commits as already merged and brings in only the new commits. The original work stays missing.
You have two ways to deal with this:
- Revert the revert commit before merging again. This restores the original changes, and the new commits then merge on top of them.
- Create a new branch with the fixes and merge that instead. The fixes must include the original work, for example by cherry-picking the original commits and adding your corrections.
git revert <revert-commit-hash>
git merge featureCommon errors
| Symptom | Cause | Fix |
|---|---|---|
| `is a merge but no -m option was given` | Git can't choose a mainline | Add `-m 1` after checking parents |
| Revert removes main's changes instead | Wrong parent number, such as `-m 2` | Revert the revert, then redo it with `-m 1` |
| Re-merging the branch brings in nothing | The earlier revert is still in history | Revert the revert, then merge |
Exercise
You merged branch login into main as commit M, and it broke production. M's parent 1 is the previous tip of main. Which command undoes the merge without rewriting history, and why can't you simply run git revert M?
Show answer
Run git revert -m 1 M. A merge has two parents, so Git needs -m to know which one is the mainline to keep. Parent 1 is the main side, so the revert removes the changes that login brought in. Plain git revert M fails with "is a merge but no -m option was given".
Exercise
After reverting the merge of login, you fix the bug on login and merge it into main again. The original login code is still missing. Why, and what is one fix?
Show answer
The first merge is still in history, so Git treats the original login commits as already merged and brings in only the new fixes. Revert the revert commit to restore the original changes, then merge login again.