171

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?

1
  • 10
    Not a duplicate, a push is different from a commit. Commented Jul 11, 2011 at 19:08

2 Answers 2

259

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

Sign up to request clarification or add additional context in comments.

4 Comments

+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.
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.
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)
Why do you have the second git reset --hard HEAD@{1} after the git push -f?
119

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 

4 Comments

Note that using --hard will erase your local work if you have anything stashed.
Simple way to revert local and remote repos to sertain commit by hach id. Ty.
This should be on top.
This is the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.