[Git] How to revert a merge commit

Tadashi Shigeoka ·  Fri, July 25, 2014

I’ll show you how to revert a topic branch merge in git.

Git

Conclusion: git revert -m 1

You just need to use git revert with -m 1.

git revert -m 1 063d4352e5c09c1981e8495a1932656735ea9c4e

Explanation of git revert -m

Let’s say the merge commit hash is 063d4352e5c09c1981e8495a1932656735ea9c4e.

$ git show 063d4352e5c09c1981e8495a1932656735ea9c4e

commit 063d4352e5c09c1981e8495a1932656735ea9c4e
Merge: a8c77d3 6a66237
Author: Tadashi Shigeoka 
Date:   Wed Jul 23 16:03:57 2014 +0900

    Merge pull request #7421 from codenote/7379-example
    
    7379 sample

If you try git revert as is, you’ll get an error saying the -m option is required.

$ git revert 063d4352e5c09c1981e8495a1932656735ea9c4e
error: Commit 063d4352e5c09c1981e8495a1932656735ea9c4e is a merge but no -m option was given.
fatal: revert failed

When you try git revert with the -m option, you need to specify the parent number as an argument to the option.

✗ git revert 063d4352e5c09c1981e8495a1932656735ea9c4e -m
error: switch `m' requires a value
usage: git revert [options] ...
   or: git revert 

    --quit                end revert or cherry-pick sequence
    --continue            resume revert or cherry-pick sequence
    --abort               cancel revert or cherry-pick sequence
    -n, --no-commit       don't automatically commit
    -e, --edit            edit the commit message
    -s, --signoff         add Signed-off-by:
    -m, --mainline     parent number
    --rerere-autoupdate   update the index with reused conflict resolution if possible
    --strategy 
                          merge strategy
    -X, --strategy-option 

You should use git revert with -m 1.

$ git revert -m 1 063d4352e5c09c1981e8495a1932656735ea9c4e

That’s all from the Gemba on how to revert a branch merge in git.

Reference Information