Why deleting the file is not enough
A later commit that deletes .env removes it from the current files, not from history.
Anyone with the repository can run git log -p and read it. Public repositories are scanned by bots
continuously, so assume a secret pushed to a public repo was copied within minutes. Rewriting history helps
for the future, but it can't un-copy anything: forks and clones keep the old commits. That is why rotation
comes first.
Step 1: rotate every secret in the file, now
GitHub's own guide says the first step is to revoke or rotate the secret. Go provider by provider:
- Supabase: create a new secret API key and delete the leaked one. If the leak is a legacy JWT-based key (anon or service_role), Supabase no longer lets you rotate the legacy JWT secret: create new publishable and secret keys, move your apps to them, turn off the legacy keys, then revoke the legacy JWT secret (see Supabase's JWT signing keys guide). Reset the database password if the connection string leaked.
- Stripe: roll the secret key in the Dashboard; replace webhook signing secrets that were in the file.
- OpenAI, Anthropic and other AI providers: delete the key and create a new one, then check usage for spikes.
- Cloud credentials: deactivate the access key or service account key, then review the provider's audit log for activity from it.
Update the new values in your hosting provider's environment settings and redeploy. Then check each provider's logs for use of the old key between the push and the rotation.
Step 2: find every commit that contains it
git log --all --full-history --oneline -- .env
git log --all -p -S 'sk_live_' --oneline # search contents by a fragment of the secretStep 3: stop tracking the file
echo ".env*" >> .gitignore
echo "!.env.example" >> .gitignore
git rm --cached .env
git commit -m "Stop tracking .env"Step 4: rewrite history with git-filter-repo
Work on a fresh clone. GitHub's guide uses git-filter-repo (version 2.47 or later for the --sensitive-data-removal option):
git clone git@github.com:you/app.git app-clean && cd app-clean
# remove the file from every commit
git filter-repo --sensitive-data-removal --invert-paths --path .env
# or replace secret strings wherever they appear (one per line in a file outside the repo)
# ../secrets.txt contains lines like: sk_live_abc123==>REMOVED
git filter-repo --sensitive-data-removal --replace-text ../secrets.txt
git push --force --mirror originPushes to refs/pull/* are rejected because GitHub treats them as read-only, so old pull requests can still show the secret. GitHub's guide says to contact GitHub Support with the repository and the first changed commits so they can remove cached views and affected pull request refs. Everyone with a clone must re-clone; an old clone pushed back brings the secret back.
Step 5: block the next one
- Turn on secret scanning and push protection in the repository's security settings, so GitHub blocks pushes that contain recognised keys.
- Add a local check, for example
gitleaks gitin CI and as a pre-commit hook, to catch keys GitHub doesn't recognise. - Keep a committed
.env.examplewith names only, and real values in your host's secret store. - Keep server secrets out of variables with public prefixes (
NEXT_PUBLIC_,VITE_): those end up in the browser bundle anyway.
How to verify
The old keys fail with an authentication error at each provider. git log --all -p -S '<fragment>' on a fresh clone finds nothing. A test commit containing a fake key in a recognised format is blocked by push protection.
Related guides
- Supabase service_role or secret key exposed in frontend code – detect, rotate, fix
- AI API keys and system prompts in frontend code – find them and move the call server-side
- Per-user token quotas for an LLM endpoint – stop unbounded consumption (OWASP LLM10)
- Supabase “JWT expired” – why the token went stale and how to keep it fresh
More in Auth, JWT and secrets · all guides