Search code examples
gitgit-pushgit-commit

How can I undo a `git commit` locally and on a remote after `git push`


I have performed git commit followed by a git push. How can I revert that change on both local and remote repositories?

$ git log
commit 364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8
Author: Michael Silver <Michael Silver@gmail.com>
Date:   Tue Jun 11 12:24:23 2011 -0700

Solution

  • git reset --hard HEAD~1
    git push -f <remote> <branch>
    

    (Example push: git push -f origin bugfix/bug123)

    This will undo the last commit and push the updated history to the remote. You need to pass the -f because you're replacing upstream history in the remote.

    Edit:

    Please note that --hard will make your commit unreachable (i.e. it will appear to be deleted, but you can still git show <hash> or git log <hash> it if you remember its hash). If you want to keep your changes, run:

    git reset [--mixed] HEAD~1
    

    At this point you have unstaged changes because you used --mixed, which is the default.

    You may first want to update the remote tree first (i.e. remove the commit): git push -f <remote> <branch>

    Since you still have your changes locally you can create another branch and commit them there (and push as you see fit).