Skip to content

How to delete accidentally pushed sensitive files in Git

Posted on:July 29, 2024

We’ve all been there. You’re working on a project, pushing commits left and right, when suddenly you realize you’ve accidentally pushed a sensitive file to your repository. Maybe you forgot about it, and now you want to fix it. Git can be frustrating when you want to change history, because it’s something that shouldn’t be done lightly.

  1. Download BFG Repo-Cleaner from https://rtyley.github.io/bfg-repo-cleaner/ If you’re on Mac, you can install it with: brew install bfg

  2. To delete the sensitive file, use:

bfg --delete-files private.key
  1. After BFG has done its job, you need to force push the changes:
git push --force

It’s a good idea to do a clean checkout in a separate directory as a backup, just in case something goes wrong.