If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:
git reset --hard HEAD^
git push -f
(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)
Warning: git reset --hard discards local changes
As dremodaris correctly warns in the comments:
WARNING: the
--hardoption will also make you lose your changes in your local working directory!
Note, as commented by alien-technology in the comments below, on Windows (CMD session), you would need ^^:
git reset --hard HEAD^^
git push -f
And, as noted in the comments by Jon Schneider:
If the command with "
HEAD^" results inerror no matches found: HEAD^, see "git show HEAD^doesn't seem to be working. Is this normal?"
Update since 2011:
Using git push --force-with-lease (that I present here, introduced in 2013 with Git 1.8.5) is safer.
See Schwern's answer for illustration.
What if somebody has already pulled the repo? What would I do then?
Then I would suggest something that doesn't rewrite the history:
git revertlocally your last commit (creating a new commit that reverses what the previous commit did)- push the 'revert' generated by
git revert.
If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:
git reset --hard HEAD^
git push -f
(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)
Warning: git reset --hard discards local changes
As dremodaris correctly warns in the comments:
WARNING: the
--hardoption will also make you lose your changes in your local working directory!
Note, as commented by alien-technology in the comments below, on Windows (CMD session), you would need ^^:
git reset --hard HEAD^^
git push -f
And, as noted in the comments by Jon Schneider:
If the command with "
HEAD^" results inerror no matches found: HEAD^, see "git show HEAD^doesn't seem to be working. Is this normal?"
Update since 2011:
Using git push --force-with-lease (that I present here, introduced in 2013 with Git 1.8.5) is safer.
See Schwern's answer for illustration.
What if somebody has already pulled the repo? What would I do then?
Then I would suggest something that doesn't rewrite the history:
git revertlocally your last commit (creating a new commit that reverses what the previous commit did)- push the 'revert' generated by
git revert.
Set the local branch one revision back (HEAD^ means one revision back):
git reset --hard HEAD^
Push the changes to origin:
git push --force
You will have to force pushing because otherwise git would recognize that you're behind origin by one commit and nothing will change.
Doing it with --force tells git to overwrite HEAD in the remote repo without respecting any advances there.
Delete last Git commit from local and remote repositories.
Undoing last commit
Revert (merge) commits on remote repo
How can I completely delete commits from both local and remote?
How do I undo a merge commit?
What happens if I encounter conflicts while reverting a commit?
Videos
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID> to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
The above answer is not quite correct - git revert <ID> does not set your repository to that commit -- git revert <ID> creates a new commit that undoes the changes introduced by commit <ID>. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>, or git reset --hard <commit> The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>.
A note of warning that git reset will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!