Skip to content

Instantly share code, notes, and snippets.

@Justintime50
Last active July 14, 2020 22:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justintime50/c847da5286955e5e35a2fb769965d566 to your computer and use it in GitHub Desktop.
Save Justintime50/c847da5286955e5e35a2fb769965d566 to your computer and use it in GitHub Desktop.
Reset your latest Git commit as if it never happened.

Remove Latest Git Commit

Have you ever pushed something you wish you hadn't? There is a simple solution to this problem. You can reset your latest commit and then push that to remote as if the commit never happened.

Warning: Doing so will remove that commit. This is detremental to repos that have collaborators on them. Only use these on branches only you have access to (feature branches) or on private repos. Using this method on the master/develop branch is highly discouraged.

Usage

# Roll us back to the last commit
git reset --hard HEAD^
# Push back to origin "as if it never happened."
git push origin -f

Let's say we really goofed and want to remove multiple commits? Simply add a number after the ~ symbol:

# Roll us back to the last 3 commits
git reset --hard HEAD~3
# Push back to origin "as if it never happened."
git push origin -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment