keepcalculator

06 Numbers and Powers

Git Revert Merge: How to Undo a Merged Branch

Card No. 06·48866 · Updated · Standard formula, shown below

To revert a merge commit in Git, use git revert -m 1 <merge-commit-hash>. This creates a new commit that undoes all the changes introduced by the merge while preserving the history of both branches. The -m 1 option tells Git which parent of the merge to treat as the “main line” (the branch you were on when you ran git merge).

Why Reverting a Merge Is Different

A normal commit has one parent; a merge commit has two (or more). When you run git revert on a regular commit, Git simply applies the inverse of that commit’s diff. But for a merge, there is no single diff to invert—the merge brought together two divergent histories. Without specifying a parent, Git would not know which side to revert against, so it refuses and asks for -m. The -m option selects which parent is the “mainline”—the branch you want to keep as the baseline. Changes from the other parent are then undone relative to that baseline.

How to Revert a Merge: Step-by-Step

  1. Find the merge commit hash: Run git log --oneline --graph to see the commit history. Merge commits often have a message like “Merge branch ‘feature’ into main”. Note the full or short hash (e.g., a1b2c3d).
  2. Identify the mainline parent number: Run git show <merge-hash> and look at the Merge: line. It shows two hashes, e.g., Merge: 1234abc 5678def. The first one is parent 1 (the branch you were on), the second is parent 2 (the branch you merged in). For most cases, you want -m 1.
  3. Execute the revert: Run git revert -m 1 <merge-hash>. Git will open your editor for a commit message (default: “Revert \”Merge branch …\””). Save and close.
  4. Resolve any conflicts: If the revert causes conflicts (because later commits changed the same lines), resolve them manually, then git add the files and git commit.
  5. Push the revert: git push to share the revert with your team.

When to Use -m 1 vs -m 2

Choose the parent that represents the branch you want to keep. If you merged feature into main and you want to undo the feature’s changes while keeping all of main’s history, use -m 1. If you merged main into feature (e.g., to update the feature branch) and you want to undo that update, use -m 1 as well—because the first parent is the branch you were on (feature). The second parent is the branch that was merged in. In short: -m 1 keeps the branch you were on; -m 2 keeps the other branch. For a standard feature merge into main, -m 1 is almost always correct.

What Happens After the Revert?

The revert creates a new commit that is the exact opposite of the merge. The original merge commit remains in history, and the branch that was merged in still has all its commits. This is important: if you later try to merge that same branch again, Git will see that the branch’s commits are already in the history (via the original merge) and will not re-introduce them. To re-apply the changes later, you must revert the revert (i.e., git revert <revert-commit-hash>). This is a common pitfall—many developers expect a simple merge to work after a revert, but it won’t.

Common Problems and Misconceptions

  • “Reverting a merge deletes the branch?” No. The branch and its commits remain intact. The revert only creates a new commit that undoes the changes on the current branch.
  • “Can I use git reset instead?” Yes, but only if you haven’t pushed. git reset --hard <commit-before-merge> will remove the merge from history entirely, but rewriting shared history causes problems for collaborators. Revert is safe for pushed commits.
  • “Why does my revert fail with a conflict?” If other commits were made after the merge that touched the same lines, Git cannot automatically undo the merge. You must resolve conflicts manually.
  • “What if I don’t know which parent is which?” Run git show --summary <merge-hash> and look at the Merge: line. The first hash is parent 1. You can also use git log <merge-hash>^1 --oneline -1 to see the first parent’s latest commit.

Alternatives to Reverting a Merge

  • git reset (before push): If the merge has not been pushed, git reset --hard HEAD~1 will undo it cleanly. Never do this after pushing if others have pulled.
  • git revert with -m 2: If you accidentally chose the wrong parent, you can revert the revert and then revert with the correct parent.
  • git checkout of specific files: If you only want to undo parts of the merge, you can checkout files from the parent commit: git checkout <parent-hash> -- <file>.
  • Rebase and drop the merge: For local cleanup, you can use interactive rebase (git rebase -i) and remove the merge line, but this rewrites history and is not recommended for shared branches.

Frequently Asked Questions

How do I revert a merge that has already been pushed?

Use git revert -m 1 <merge-hash> and push the new revert commit. This is the standard safe way to undo a merge on a shared branch because it doesn’t rewrite history.

What does “-m 1” mean exactly?

It tells Git to treat the first parent of the merge as the mainline. The revert will undo all changes that came from the second parent, relative to the first parent. The first parent is the branch you were on when you ran git merge.

Why can’t I just revert the merge without -m?

Because a merge commit has multiple parents, Git doesn’t know which side to compare against. The -m option removes that ambiguity by specifying the mainline parent.

After reverting a merge, can I merge the same branch again?

No, not directly. Git will consider the branch’s commits already merged. To re-introduce the changes, you must first revert the revert (i.e., git revert <revert-commit>), then merge again.

What if I get a merge conflict during revert?

Resolve the conflicts manually, then git add the resolved files and run git commit to finish the revert. The revert commit will still be created.

Is there a way to undo a merge without creating a revert commit?

Yes, use git reset --hard <commit-before-merge>, but only if the merge hasn’t been pushed. If it has been pushed, resetting creates divergent histories and can break your team’s repository.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →