The Hook (The "Byte-Sized" Intro)
You merged a feature branch into main and tests started failing. Now what? You can't just git revert <hash> like a normal commit — merge commits have two parents, and Git doesn't know which side to undo. You need the -m flag. Or, if you haven't pushed yet, git reset can erase the merge entirely. Two paths, two scenarios — here's when to use each.
📖 What is Undoing a Merge Commit?
Undoing a merge means removing the effects of a merged branch. The method depends on whether the merge has been pushed to a shared branch.
Conceptual Clarity
Two strategies:
| Scenario | Method | Command | Safe for shared? |
|---|---|---|---|
| Merge NOT pushed | reset | git reset --hard HEAD~1 | N/A (local only) |
| Merge IS pushed | revert | git revert -m 1 <merge-sha> | ✅ Yes |
Understanding -m (mainline parent):
A merge commit has two parents. -m 1 keeps parent 1 (usually main). -m 2 keeps parent 2 (the feature branch). Almost always you want -m 1.
Parent 1 (main) Parent 2 (feature)
\ /
Merge Commit (M)
Real-Life Analogy
- Reset = Ripping the merged chapter out of the book before publishing (local only)
- Revert = Publishing a correction page that removes the chapter's content (safe for shared)
Visual Architecture
Why It Matters
- Merge reverts are common: Bad merges happen in every team — knowing the fix is essential.
- The
-mflag: This is the critical detail most developers miss when trying to revert a merge. - Re-merging after revert: If you revert a merge, you can't simply re-merge the same branch later without reverting the revert first.
- Production safety: On shared branches,
revertis the only safe option.
Code
# ─── Method 1: Reset (if NOT pushed) ───
git reset --hard HEAD~1
# The merge commit is gone; branch is back to pre-merge state
# ─── Method 2: Revert (if pushed) ───
git revert -m 1 abc1234 --no-edit
# Creates a new commit undoing the merge's changes
# -m 1 = keep main's side, undo the feature's changes
# ─── Later: re-merge the same branch ───
# You must revert the revert first!
git revert <revert-commit-hash>
# Then merge again:
git merge feature/login
# ─── Check parent order of a merge commit ───
git log --oneline --graph -5
# or:
git cat-file -p abc1234
# Shows: parent abc... (1st/main), parent def... (2nd/feature)Key Takeaways
- Not pushed: Use
git reset --hard HEAD~1to erase the merge entirely. - Already pushed: Use
git revert -m 1 <merge-sha>to create an undo commit. - The
-m 1flag tells Git to keep the first parent (main) and undo the second (feature). - To re-merge after a revert, you must first revert the revert.
Interview Prep
-
Q: Why does reverting a merge commit require the
-mflag? A: Because merge commits have two parents. Git doesn't know which parent to keep and which to undo.-m 1specifies keeping the first parent (typicallymain) and undoing the changes from the second parent (the feature branch). -
Q: What happens if you try to re-merge a branch that was previously reverted? A: Git will say "Already up to date" because the merge history already includes those commits. You must first revert the revert commit (
git revert <revert-hash>) to restore the original changes, then you can merge new commits from the branch. -
Q: When should you use
resetvsrevertfor undoing a merge? A: Useresetif the merge hasn't been pushed — it cleanly erases the merge. Userevertif the merge has been pushed to a shared branch — it creates a new undo commit without rewriting history.