Reset your Git repository back to a previous commit

Reset your Git repository back to a previous push

Earlier today I accidentally deleted quite a lot of code from a Git repo, quickly realising I had made a catastrophic error, I scoured Stack Overflow for a way to reset by entire repository back to a previous commit. Here’s what I found:

First things first make sure your current checkout has no local changes, if it does stash them or just create another clone of the branch you are using.

git stash -u save “My Wicked Cool Feature to stash”

or

git clone repo_url

Reset your working copy, so that it matches exactly what was in your branch during the commit you are reseting to.

git reset --hard sha1_hash

Although you won’t be able to see any changes when you run “git status”, running the below command, will publish the old revision back to HEAD.

git push -f

That’s all, your online repository will now be reset back to the previous commit.

4 Love This

Leave a Reply

Your email address will not be published.