Skip to content

Instantly share code, notes, and snippets.

@clayball
Last active January 1, 2018 04:15
Show Gist options
  • Select an option

  • Save clayball/91e0cc7ac4f951a992305b2fa67d31ec to your computer and use it in GitHub Desktop.

Select an option

Save clayball/91e0cc7ac4f951a992305b2fa67d31ec to your computer and use it in GitHub Desktop.
Undo the last commit that was pushed to origin
Undo the last commit that was pushed to origin
I tend to use this in conjunction with creating a new branch, GOTO 30
WARNING: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES.
Be sure to stash any local changes you want to keep before running these commands
$ git reset --hard HEAD~1
HEAD~1 means the commit before head.
If you want to go back more than one commit..
- look at the output of git log
- find the commit id of the commit you want to revert back to
- specify the sha1 commit id when doing the git reset, for example..
$ git reset --hard <sha1-commit-id>
Because the commit was pushed to origin, we need to do a force push to get rid of the commit
$ git push origin HEAD --force
Create a branch based on the last commit, i.e. HEAD, then revert to previous commit and push
$ git checkout -b featureX
$ git checkout master
$ git reset --hard HEAD~1
$ git push origin HEAD --force
Work can now continue on featureX and the last commit on the master branch was removed.
@clayball
Copy link
Author

clayball commented Jan 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment