Stop putting secrets in .env files committed to git
Mar 15, 2026·4 min read
This is the most common security mistake in modern web development. Developers create a .env file, put secrets in it, commit it, then add it to .gitignore. The .gitignore entry removes it from future commits but doesn't remove it from history.
Check your own repo
git log --all --full-history -- .env
git show <commit-hash>:.envIf that returns output, your secrets were committed. Anyone who clones the repo — now or in the future — can retrieve them.
The tools attackers use
truffleHog and git-secrets are purpose-built for this. They scan git history for entropy patterns that look like API keys. They're free, fast, and run on CI.
What to do right now
If you've committed secrets:
- Rotate every key that was exposed — assume it's compromised
- Remove the history with
git filter-repoor BFG Repo Cleaner - Force push to all remotes
- Check if any forks exist — they may still have the old history
Going forward: use a secrets manager (Vercel env vars, Doppler, AWS Secrets Manager) instead of .env files. Never commit a .env file. Add .env* to your global gitignore.