The original poster states:
The best answer someone could give me was to use git revert
X times until I
reach the desired commit.
So let's say I want to revert back to a commit that's 20 commits old, I'd have
to run it 20 times.
Is there an easier way to do this?
I can't use reset cause this repo is public.
It's not necessary to use git revert
X times. git revert
can accept a
commit range as an argument, so you only need to use it once to revert a range
of commits. For example, if you want to revert the last 20 commits:
git revert --no-edit HEAD~20..
The commit range HEAD~20..
is short for HEAD~20..HEAD
, and means "start from the 20th parent of the HEAD commit, and revert all commits after it up to HEAD".
That will revert that last 20 commits, assuming that none of those are merge
commits. If there are merge commits, then you cannot revert them all in one command, you'll need to revert them individually with
git revert -m 1 <merge-commit>
Note also that I've tested using a range with git revert
using git version 1.9.0. If you're using an older version of git, using a range with git revert
may or may not work.
In this case, git revert
is preferred over git checkout
.
Note that unlike this answer that says to use git checkout
, git revert
will actually remove any files that were added in any of the commits that you're
reverting, which makes this the correct way to revert a range of revisions.
Documentation