Skip to content

Instantly share code, notes, and snippets.

@davehague
Last active November 8, 2024 16:16
Show Gist options
  • Save davehague/7cb88364d60b845004f78a40f9405e00 to your computer and use it in GitHub Desktop.
Save davehague/7cb88364d60b845004f78a40f9405e00 to your computer and use it in GitHub Desktop.
Undoing a commit that includes sensitive data

If you accidentally perform a commit with sensitive data, I found this recovery approach to be fairly straightforward.

  1. (Optional) If you want to keep the file but not the changes, save off the file that has the secret. The command below will completely remove the file.
  2. Install Python if not already installed
  3. From command line pip install git-filter-repo
  4. Clone a fresh copy of the repository
  5. Run the command to remove the file
git filter-repo --path path/to/your_file_with_secret --invert-paths
  1. Add the remote origin if not set (git remote-v to check), and then check it was set
git remote add origin https://github.com/davehague/timeline.git
git remote -v
  1. Force push
git push --set-upstream origin main --force
  1. Re-clone your repository. Don't forget to move your non-committed files over before deleting the old one (.env)
  2. (Optional) Add the file back in, make sure not to include the secret!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment