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

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).

Leave a Comment