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:

  • Why use 'git rm' to remove a file instead of 'rm'?
  • git: publish from certain commit onward, keeping earlier history private, but provable
  • Drop old commit: `git rebase` causes merge conflicts
  • Does every git branch of an NPM project have different node_modules dependencies?
  • Repull changes in git
  • Git excludesfile for a branch
  • 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.

  • Stuck on git rebase while merging diverged branches
  • Confused on git workflow (PHP/MySQL)
  • Jenkins cannot read Github password since upgrading to Git client plugin 2.1.0
  • Using git commands in a TeamCity Build Step
  • question about git diff
  • How do I get local parent branch commit update
  • 4 Solutions collect form web for “Revert a range of commits in git”

    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 
    

    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"
    

    doing git revert OLDER_COMMIT^..NEWER_COMMIT didn’t work for me, I used git revert -n OLDER_COMMIT^..NEWER_COMMIT and everything good, I’m using git version 1.7.9.6.

    If you want to revert commit range B to D (at least in git version 2) in a single commit, you can do

     git revert -n B^..D
    

    This revert the changes done by commits from B’s parent commit (excluded) to the D commit (included), but doesn’t create any commit with the reverted changes. The revert only modifies the working tree and the index.

    Don’t forgot to commit the changes after

     git commit -m "revert commit range B to D"
    

    You can also revert multiple unrelated commits in a single commit, using same method. for example to revert B and D but not C

     git revert -n B D
     git commit -m "Revert commits B and D"
    

    Reference: https://www.kernel.org/pub/software/scm/git/docs/git-revert.html

    Thanks Honza Haering for the correction

    Use git rebase -i to squash the relevant commits into one. Then you just have one commit to revert.

    Git Baby is a git and github fan, let's start git clone.