Git reset to previous commit

I have three commits I made which I attempted to clean up some code. Anyways I managed to completely destroy what I was working on. And I want to delete the past three commits and return to a specific commit SHA1.

How can I restore to that previous commit and delete the history of those 3 commits? (The history deleting part is not a big deal). These commits are pushed already, so I am a little lost.

Thanks!


Find the commit you want to reset to:

git log

Once you have the hash:

git reset --hard <hash>

And to push onto the remote:

git push -f <remote> <branch>

Since your commits are pushed remotely you need to remove them. I'll assume your branch is master and it's pushed over origin.

You first need to remove master from origin:

git push origin :master (note the colon)

Then you need to get master to the status you want, I'll assume the commit hash is ABCDE:

git reset --hard ABCDE

Lastly, push master again:

git push origin master

That's it! Note that if someone already downloaded your changes from origin this will screw them pretty much leaving their local repos unstable.