Search code examples
gitmergerevert

Git: How to revert a merge and two other adjacent commits?


We have this git log:

D:\blah>git log -n 5 --oneline    
fc19dec My change #2
68fa9db Merge branch 
4bc1ac7 My change #1
ec345d9 [maven-release-plugin] prepare for next development iteration

I'd like to revert to ec345d9 but am getting an error:

D:\blah>git revert --no-commit ec345d9..HEAD
error: a cherry-pick or revert is already in progress
hint: try "git cherry-pick (--continue | --quit | --abort)"
fatal: revert failed

How can I do this revert?

I am aware of the issue with reverting a merge and needing to specify the -m option. When I've used that in the past I am just reverting the one single merge commit. But in this case the merge commit is sandwiched between two other commits I'm trying to revert.


Solution

  • First, make sure that your merge does not include the changes from the commit below it, when you specify the -m 1 (or other mainline number) option.

    (Well, first first, finish or abort the cherry-pick or revert you're in the middle of now. Then check whether the merge includes one of those commits' changes.)

    Now that you're sure which commits to revert, and which ones need a -m option and which ones require not using a -m option, do them one at a time, most-recent first:

    git revert fc19dec
    git revert -m 1 68fa9db
    git revert 4bc1ac7
    

    for instance. Note that this produces three separate new commits, each one doing one revert. (This is what you would get with the .. notation as well.) If you want to get one big revert commit, use git revert -n each time, and commit the final result separately once done.