Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Possible Duplicates:
Undoing a 'git push'

I have pushed some bad code, and I am the only user of the repository. How can I rollback my last commit?

share|improve this question

migrated from programmers.stackexchange.com Jul 11 '11 at 18:58

This question came from our site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly.

marked as duplicate by Adam Lear, Felix Kling, manojlds, Paŭlo Ebermann, Ken Bloom Jul 12 '11 at 0:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5  
Not a duplicate, a push is different from a commit. – static_rtti Jul 11 '11 at 19:08
    
@manojlds you were suggesting that all and every new question, starting from July 2011, should be marked as a duplicate. – user234736 Aug 4 '15 at 7:49
up vote 110 down vote accepted

Since you are the only user:

git reset --hard HEAD@{1}
git push -f
git reset --hard HEAD@{1}

( basically, go back one commit, force push to the repo, then go back again - remove the last step if you don't care about the commit )

Without doing any changes to your local repo, you can also do something like:

git push -f origin <sha_of_previous_commit>:master

Generally, in published repos, it is safer to do git revert and then git push

share|improve this answer
1  
+1 for "Generally, in published repos, it is safer to do git revert and then git push". The only reason I'd recommend rewinding history in a remote is because the OP is the only user. If you're sharing with others, don't go backwards, only ever go forwards. – Dan Ray Jul 11 '11 at 19:21
    
very practical thanks! – Zafer Fatih Koyuncu Sep 6 at 8:50
    
Nice one! Ass saver! :p – Vin.X Oct 20 at 3:35

First you need to determine the revision ID of the last known commit. You can use HEAD^ or HEAD~{1} if you know you need to reverse exactly one commit.

git reset --hard <revision_id_of_last_known_good_commit>
git push --force
share|improve this answer
    
Thanks a lot :) – Kanishka Panamaldeniya May 10 at 7:07
    
Note that using --hard will erase your local work if you have anything stashed. – Will Lovett Aug 25 at 2:46
    
Simple way to revert local and remote repos to sertain commit by hach id. Ty. – Santiago Battaglino Sep 2 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.