Rolling back local and remote git repository by 1 commit
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.
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)
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 revert
locally your last commit (creating a new commit that reverses what the previous commit did)- push the 'revert' generated by
git revert
.
If you want revert last commit listen:
Step 1:
Check your local commits with messages
$ git log
Step 2:
Remove last commit without resetting the changes from local branch (or master)
$ git reset HEAD^
OR if you don't want last commit files and updates listens
$ git reset HEAD^ --hard
Step 3:
We can update the files and codes and again need to push with force it will delete previous commit. It will keep new commit.
$ git push origin branch -f
That's it!