170

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?

Community
  • 1
  • 1
OpenCoderX
  • 6,180
  • 7
  • 34
  • 61

2 Answers2

255

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

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 4
    +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
  • I think with the 2nd one you remove indeed the bad push in the remote repository BUT the history remains in your local repo. and when you do new changes, commit and push again, you will have again the bad push in the remote repo. – karlihnos Jul 14 '17 at 14:27
  • Hi, I do only `git push -f origin 99157a1b1b27820dfba48c5e9d3c4f075670670c:master ` as the "most cited recipe", but **nothing ocurr**... How to use your recipe? It is the *simplest case*: delete the last commit from master. Delete=remove from commit history. (not evident in your text that `git revert` will take off from history) – Peter Krauss Feb 16 '18 at 13:48
  • 2
    Why do you have the second `git reset --hard HEAD@{1}` after the `git push -f`? – kas Apr 19 '18 at 04:14
115

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
Vegar
  • 12,828
  • 16
  • 85
  • 151
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168