Lesson Completion
Back to course

Undoing a Merge Commit

Intermediate
9 minutes4.7Git

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:

ScenarioMethodCommandSafe for shared?
Merge NOT pushedresetgit reset --hard HEAD~1N/A (local only)
Merge IS pushedrevertgit 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

flowchart TD MERGE["❌ Bad Merge"] --> PUSHED{"Pushed?"} PUSHED -->|"No"| RESET["git reset --hard HEAD~1<br/>Merge erased"] PUSHED -->|"Yes"| REVERT["git revert -m 1 MERGE_SHA<br/>New undo commit"] style MERGE fill:#2d1b1b,stroke:#e94560,color:#e94560 style RESET fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style REVERT fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Merge reverts are common: Bad merges happen in every team — knowing the fix is essential.
  • The -m flag: 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, revert is the only safe option.

Code

bash
# ─── 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~1 to erase the merge entirely.
  • Already pushed: Use git revert -m 1 <merge-sha> to create an undo commit.
  • The -m 1 flag 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 -m flag? A: Because merge commits have two parents. Git doesn't know which parent to keep and which to undo. -m 1 specifies keeping the first parent (typically main) 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 reset vs revert for undoing a merge? A: Use reset if the merge hasn't been pushed — it cleanly erases the merge. Use revert if the merge has been pushed to a shared branch — it creates a new undo commit without rewriting history.

Topics Covered

Git HistoryGit Merge

Tags

#git#merge#undo#revert

Last Updated

2026-02-13