Rollback to an old Git commit in a public repo
How can I go about rolling back to a specific commit in git?
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 because this repository is public.
Try this:
git checkout [revision] .
where [revision]
is the commit hash (for example: 12345678901234567890123456789012345678ab
).
Don't forget the .
at the end, very important. This will apply changes to the whole tree. You should execute this command in the git project root. If you are in any sub directory, then this command only changes the files in the current directory. Then commit and you should be good.
You can undo this by
git reset --hard
that will delete all modifications from the working directory and staging area.
To rollback to a specific commit:
git reset --hard commit_sha
To rollback 10 commits back:
git reset --hard HEAD~10
You can use "git revert" as in the following post if you don't want to rewrite the history
How to revert Git repository to a previous commit?
Well, I guess the question is, what do you mean by 'roll back'? If you can't reset
because it's public and you want to keep the commit history intact, do you mean you just want your working copy to reflect a specific commit? Use git checkout
and the commit hash.
Edit: As was pointed out in the comments, using git checkout
without specifying a branch will leave you in a "no branch" state. Use git checkout <commit> -b <branchname>
to checkout into a branch, or git checkout <commit> .
to checkout into the current branch.
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
- git-revert(1) Manual Page.
- Commit Ranges.