Rollback to an old Git commit in a public repo
Solution 1
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.
Solution 2
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?
Solution 3
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.
Solution 4
Step 1: fetch list of commits:
git log
You'll get list like in this example:
[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <[email protected]>
Date: Fri Jul 8 23:42:22 2016 +0300
This is last commit message
commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <[email protected]>
Date: Fri Jun 24 20:20:24 2016 +0300
This is previous commit message
commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <[email protected]>
Date: Thu Jun 23 00:41:55 2016 +0300
This is previous previous commit message
...
Step 2: copy needed commit hash and paste it for checkout:
git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f
That's all.
Solution 5
Want HEAD detached mode?
If you wish to rollback X time to a certain commit with a DETACHED HEAD (meaning you can't mess up anything), then by all means, use the following:
(replace X with how many commits you wish to go back)
git checkout HEAD~X
I.E. to go back one commit:
git checkout HEAD~1
David
Updated on July 11, 2022Comments
-
David almost 2 years
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.