I pushed my .env file to GitHub
What just happened
Your secrets (API keys, passwords, tokens) are in the repository history. Bots scan public repositories within seconds. Removing the file in a new commit does NOT remove it from history.
Potential risks
- Leaked keys can be abused immediately (cloud bills, data theft).
- Forks and clones keep a copy even after you clean history.
Before you run anything
- STEP ZERO: revoke and regenerate every secret in the file. Cleaning history is not enough.
- Check your providers' logs for suspicious use.
- History rewriting requires a backup clone and the team's agreement.
- Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix โ it costs nothing.
Recovery steps
- 1
Stop tracking the file
Removes .env from the index but keeps it on your disk, then prevents it from coming back.
Safe$ git rm --cached .env$ echo ".env" >> .gitignore$ git commit -m "chore: stop tracking .env" - 2
Make a backup clone
A mirror clone, just in case the next step goes wrong.
Safe$ git clone --mirror git@github.com:OWNER/REPO.git backup-repo.git - 3
Erase the file from all history
git filter-repo (to install separately) rewrites every commit and all hashes change. It also removes the โoriginโ remote: add it back before pushing (git remote add origin <url>).
Destructive$ git filter-repo --force --invert-paths --path .env - 4
Force push the cleaned history
Everyone must then re-clone. On GitHub, also contact support to purge cached views and pull request refs.
Destructive$ git push --force --all origin$ git push --force --tags origin
These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer