gitrevertrange

Revert a range of commits in git


How can I revert a range of commits in git? From looking at the gitrevisions documentation, I cannot see how to specify the range I need. For example:

A -> B -> C -> D -> E -> HEAD

I want to do the equivalent of:

git revert B-D

where the result would be:

A -> B -> C -> D -> E -> F -> HEAD

where F contains the reverse of B-D inclusive.


Solution

  • What version of Git are you using?

    Reverting multiple commits in only supported in Git1.7.2+: see "Rollback to an old commit using revert multiple times." for more details.
    The current git revert man page is only for the current Git version (1.7.4+).


    As the OP Alex Spurling reports in the comments:

    Upgrading to 1.7.4 works fine.
    To answer my own question, this is the syntax I was looking for:

    git revert B^..D 
    

    B^ means "the first parent commit of B": that allows to include B in the revert.
    See "git rev-parse SPECIFYING REVISIONS section" which include the <rev>^, e.g. HEAD^ syntax: see more at "What does the caret (^) character mean?")

    Note that each reverted commit is committed separately.

    Henrik N clarifies in the comments:

    git revert OLDER_COMMIT^..NEWER_COMMIT
    

    As shown below, you can revert without committing right away:

    git revert -n OLDER_COMMIT^..NEWER_COMMIT
    git commit -m "revert OLDER_COMMIT to NEWER_COMMIT"