Skip to content

Instantly share code, notes, and snippets.

@audreyfeldroy
Last active August 1, 2020 13:53
Show Gist options
  • Save audreyfeldroy/5e37281b38bc27aa7e26 to your computer and use it in GitHub Desktop.
Save audreyfeldroy/5e37281b38bc27aa7e26 to your computer and use it in GitHub Desktop.
How to remove embarrassing commits that you just made to a public Git repo

Warning: This can be really bad to do. It changes your git history, which is bad and could affect other remotes. Don't do this to a project that other people are using too. Now that you've been warned, here's how to do some bad things to your repo. Do it quickly before anyone notices.

Roll back 1 commit locally:

git reset --hard HEAD~1

Roll back 6 commits locally:

git reset --hard HEAD~6

Push the rollbacks to the remote master branch:

git push origin master --force

All traces of the commit are now gone!

@nashley
Copy link

nashley commented Jun 5, 2019

I know this gist is 5 years old, but in case anyone else comes across it, this is false:

All traces of the commit are now gone!

This doesn't remove the commit from the GitHub reflog (e.g., https://github.com/$USER/$PROJECT/$COMMIT_ID) or from your local cache.

See this article for recovering commits from GitHub's reflog.

If you have sensitive data you want to expunge from your repo, you'll want to reference these steps instead. However, you should really rotate your credentials and think about reassessing how files make it into your git repos (e.g., consider adding a .gitignore file and use git add -i && git commit instead of git commit -am "Message").

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