Git History and Secret Remediation
What You'll Learn
You'll understand why committed secrets remain dangerous even after deletion, and how to surgically remove them from git history using specialized tools.
The Problem: Secrets Never Truly Disappear
When you accidentally commit a secret (API key, password, token) to git and then delete it in a later commit, the secret still exists in your repository's history. Anyone who clones the repo can run git log -p or git checkout <old-commit> to retrieve it.
Think of git history like a permanent ledger—erasing an entry from the current page doesn't remove it from previous pages. Every commit is preserved, including the one containing your secret.
Remediation Techniques
BFG Repo-Cleaner
A fast, simple tool designed specifically for removing sensitive data:
# Remove any file named "secrets.txt" from all history
bfg --delete-files secrets.txt my-repo.git
# Replace any text matching a pattern across all commits
bfg --replace-text passwords.txt my-repo.git
BFG is faster and simpler than git's native tools, rewriting history to eliminate the secret entirely.
git filter-branch
Git's built-in (but slower) history rewriting tool:
# Remove a specific file from all commits
git filter-branch --tree-filter 'rm -f config/secrets.yml' HEAD
# Remove specific text patterns
git filter-branch --tree-filter 'sed -i "s/API_KEY=.*/API_KEY=REDACTED/" .env' HEAD
After rewriting history with either tool, you must force-push to remote repositories and inform all collaborators to re-clone, since history has been altered.
Critical Post-Remediation Steps
Even after removing secrets from git history:
- Rotate the exposed secret immediately—assume it was already compromised
- Force-push to all remote locations
- Notify team members to delete and re-clone
Key Takeaway: Secrets committed to git persist forever in history; use BFG Repo-Cleaner or git filter-branch to surgically remove them, but always rotate the secret immediately as the safest assumption is that it's already compromised.