Keys Committed to GitHub: Deleting the File Does Not Delete the Secret
If a secret was ever pushed to a git remote, treat it as leaked. Deleting the file in a new commit leaves the value in history, where git log finds it in seconds. Rewriting history does not reach forks, clones or caches. The only fix that works is to revoke the key at the provider and issue a new one. Do that first, then clean up.
On this page
The typical story with an AI-built app: the project started without a .gitignore, or the agent created .env.production or config.js with real values, and git add . took everything. Weeks later someone notices, deletes the file, commits "remove env", and considers it handled.
Why deleting is not enough
Git stores every version of every file. The value is one command away:
git log -p --all -S 'sk_live_' | head -40
git log --all --full-history -- .env
git show <commit>:.env
On a public repository, automated scanners watch the GitHub event feed and pick up new keys within minutes of a push. On a private repository the audience is smaller but real: every collaborator, every CI system, every laptop with a clone, every integration with repo access, and anyone the repo is shared with or sold to later.
Step 1: rotate
For each exposed secret, go to the provider, create a new key, deploy it, and revoke the old one. Order matters when the app is live: new key into your hosting environment first, redeploy, confirm it works, then revoke.
Things people forget to rotate:
- Database passwords (the connection string is a secret).
- JWT or session signing secrets. Rotating logs everyone out. Do it anyway.
- Webhook signing secrets (Stripe
whsec_...). - OAuth client secrets.
- Supabase secret /
service_rolekey and the database password. - SMTP and SMS credentials.
Public values such as a Supabase publishable key or Stripe publishable key do not need rotation. Our post on secret keys in the frontend lists which is which.
Step 2: check what happened while it was exposed
Look at each provider's logs and usage for the exposure window. Unknown IP addresses, usage spikes, new API keys or team members you did not create, unexpected database rows, emails sent that you did not send. If the secret gave access to personal data and you see signs of use, you may have notification duties under GDPR or similar laws. That is a question for a lawyer, and it is better asked early.
Step 3: find everything else in history
One leaked key usually has company. Scan the full history, not just the current files:
# gitleaks
gitleaks detect --source . --verbose --redact
# or search by hand for common shapes
git log -p --all | grep -nE "sk_live_|sk-[A-Za-z0-9_-]{20,}|AKIA[0-9A-Z]{16}|whsec_|-----BEGIN [A-Z ]*PRIVATE KEY-----|postgres(ql)?://[^ ]+:[^ ]+@" | head
--redact keeps the secrets out of your terminal scrollback and CI logs. Rotate everything the scan finds that is still valid.
Step 4: decide whether to rewrite history
After rotation, the old values in history are dead strings. Rewriting is optional cleanup. It makes sense when the history contains things you cannot rotate (customer data in a seed file, personal information) or when the repo will be made public.
pip install git-filter-repo
git filter-repo --invert-paths --path .env --path .env.production
git push --force --all
Costs: every collaborator has to re-clone, open pull requests break, and commit hashes change. Copies that already exist are not affected. On GitHub, old commits can stay reachable by hash through cached views and forks until you ask GitHub Support to remove them.
Step 5: stop it happening again
.gitignore before the first commit:
.env
.env.*
!.env.example
*.pem
Commit a .env.example with variable names and fake values so the next developer (or agent) knows what to set.
If a file is already tracked, ignoring it does nothing until you untrack it:
git rm --cached .env
Push protection. GitHub secret scanning with push protection blocks pushes containing known key formats. It is free for public repositories and can be enabled in the repository's security settings.
A pre-commit hook:
# .git/hooks/pre-commit
gitleaks protect --staged --redact || exit 1
A CI job that runs the same scan on every pull request, so a hook skipped with --no-verify still gets caught.
Instructions for your coding agent. Add to CLAUDE.md, AGENTS.md or your rules file: "Never write real secrets to any file. Use environment variables. Never commit .env* files except .env.example." Agents do read these, and they also create helper scripts, test fixtures and docker-compose.yml files where keys end up inline. Check those in review.
Quick reference
| Situation | Do |
|---|---|
| Key pushed, any repo | Rotate now |
| Key only in a local commit, never pushed | Amend or reset the commit; rotation optional |
| Key in a public repo for more than a few minutes | Rotate, then audit provider logs |
| Unrotatable data in history | Rewrite history, contact the host about cached copies |
Secrets in git are check 5 in our security checklist for vibe-coded apps. If you want a full history scan, rotation plan and guards set up for you, that is part of our app rescue and security audit.
