my-git

Revert Merge Commit

English 中文

When reverting a merge commit, you need to specify the mainline parent.

For normal commits, you can directly:

git revert <commit-sha>

But a merge commit has two or more parent commits, and Git needs to know which side you want to keep as the mainline.

Check Merge Commit First

git show --summary <merge-commit-sha>
git log --oneline --graph --decorate -20

Confirm which branch this merge commit was merged from.

Common Usage

git revert -m 1 <merge-commit-sha>

-m 1 usually indicates keeping the target branch perspective, undoing the changes brought by the merged branch.

Why -m is Needed

A merge commit has multiple parents.

For example:

parent 1: Position of main before merge
parent 2: Position of feature branch

-m 1 means taking parent 1 as the mainline, reverting the changes brought by the other branch.

Risks

After reverting a merge commit, if you want to merge the same branch again in the future, Git may consider part of the changes already handled.

A safer approach is:

Further Reading