Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert a range of commits in git

Tags:

git

range

revert

People also ask

How do I revert multiple commits?

The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use git revert in conjunction with git rev-list . The latter one will provide you with a list of commits, the former will do the revert itself.

How do I revert a git commit?

The revert command You can find the name of the commit you want to revert using git log . The first commit that's described there is the last commit created. Then you can copy from there the alphanumerical name and use that in the revert command. In this image, each circe represents a commit.

How do I revert multiple commits in Intellij?

Revert uncommitted changesIn the Commit tool window Alt+0 , select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z .


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"

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


Doing git revert OLDER_COMMIT^..NEWER_COMMIT didn't work for me.

I used git revert -n OLDER_COMMIT^..NEWER_COMMIT and everything is good. I'm using git version 1.7.9.6.