Guides / Leaked keys
Claude Code pushed my keys to GitHub. What now?
It happens more than anyone admits: an AI coding session copies a key into a file, and the commit goes out with everything else. Here is the order that limits the damage in the first hour, and the setup that stops it next time.
Do this first: revoke the key with the company that issued it, right now. Deleting the file, the commit or even the repository does not make a leaked key safe. Copies already exist in clones, forks and caches.
How it happens
An AI coding session is trying to make something work. It reads your .env file, copies a key into a config file or a test so the request succeeds, and the change gets committed and pushed with everything else. Nobody typed the key into the code on purpose, which is exactly why nobody noticed.
Claude Code reads the files it is allowed to read. Its documentation recommends denying reads of .env files explicitly if you never want them read, so on a default setup nothing stops a session from opening one. If you also approved git commit or git push with “Yes, and don't ask again”, the last safety check was gone too.
The first hour, minute by minute
- Minutes 0 to 10: revoke and replace the key. Go to the provider, not to GitHub. Revoke or roll the exposed key, create a new one, and put the new one in your hosting platform's secret settings, never in code. Typical places:
- Anthropic: Claude Console, API keys: delete the key.
- OpenAI: API keys page: revoke.
- Stripe: Developers, API keys: roll the secret key.
- Supabase: project API settings: rotate the service role key. The anon key is meant to be public; the service role key never is.
- AWS: IAM, the user's access keys: deactivate, then delete.
- GitHub tokens: Settings, Developer settings: revoke.
- Minutes 10 to 20: check whether it was used. Open the provider's usage, billing and log pages for the time since the push: unexpected AI spend, new Stripe refunds or payouts, Supabase queries you do not recognise, AWS activity in CloudTrail. Set a spend limit if the provider offers one.
- Minutes 20 to 40: find every other secret. One leaked key usually means more. Check the repository's Security tab for secret scanning alerts, search the history for key prefixes (for example
git log -p -S "sk-"), and run a scanner such as Gitleaks or TruffleHog over the full history. Look in.envfiles, test fixtures, config files and anything an AI session created. - Minutes 40 to 60: clean the history, only after rotating. GitHub's guidance is to revoke or rotate first, then remove the data. Use
git filter-repo(version 2.47 or later has a--sensitive-data-removaloption) to replace the secrets across all commits, force-push, and ask everyone with a clone to re-clone. Rewriting changes every later commit hash, and you cannot clean other people's clones or forks, which is why rotation comes first.
Public repository or private?
Public: assume the key was copied within minutes. GitHub forwards many leaked keys to their issuers through its secret scanning partner program. Anthropic has been a partner since August 2024, so a Claude API key pushed to a public repository is reported to Anthropic, which can revoke it and email the key's owner. Check your inbox, but rotate anyway: not every provider is a partner, and revocation does not undo use that already happened.
Private: still rotate. Everyone with access, every integration and every future change of visibility can expose the history. A private repository is not a secret store.
GitHub push protection blocks pushes that contain known key formats, and is on by default for public repositories for personal accounts. Since October 2026 it blocks Anthropic keys by default. It can be bypassed with one click, and it does not scan private repositories unless your organization pays for it. Treat it as a seatbelt, not a lock.
Stop it happening again: a 15-minute setup
These are the settings I put in every project that uses Claude Code. The file .claude/settings.json is meant to be committed, so the whole team gets the same rules.
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)"
],
"ask": [
"Bash(git push *)",
"Bash(git commit *)"
]
}
}
- Deny reads of secret files. Deny rules win over everything else, and Claude Code's documentation notes they still apply in the mode that skips prompts. A
Readdeny also coverscat .env. - Always ask before commit and push. These rules match the command text, so they are a reminder, not a wall: the documentation is explicit that Bash rules are not a security boundary.
- Turn on the sandbox with
/sandboxfor real enforcement of shell commands on macOS and Linux, and add your~/.sshand cloud credential folders to its blocked reads. - Scan before every push. A pre-commit hook running Gitleaks, plus push protection on the repository, catches keys a person or an AI wrote into code.
- Keep secrets out of reach. Server-side only, in a vault (Infisical, AWS Secrets Manager, Azure Key Vault), separate keys for development and production, and spend limits on every AI key.
- Review what was approved. Run
/permissionsand read.claude/settings.local.json: every “don't ask again” lives there. See which Claude Code permissions are safe for a small team.
The full pre-launch list is in the launch checklist for vibe-coded apps.
Sources
Checked on 2 December 2026. Product menus change; the documentation below is the reference.
How I help
If a key has leaked and you want a second pair of hands, call me the same day. I rotate, check the logs, clean the history and set up the guardrails, in your accounts.
- Same day: rotation of every exposed key, a check of the provider logs, and the history clean-up.
- Launch review: one week, $2,500 fixed. The code and full git history scanned for secrets, access checked on the server, and a ranked fix list.
- Team setup: a shared Claude Code settings file, secret scanning before every push, and keys moved into a vault.
Done by a Claude Certified Architect and Certified Lovable Expert. Everything stays in your accounts.
Questions
Is deleting the commit or the repository enough?
No. The key is already in clones, forks, caches and possibly in someone else's tooling. Revoke and replace the key first; cleaning the history comes after and only limits further exposure.
Did Anthropic already revoke my Claude API key?
If the repository was public, GitHub reports Anthropic keys to Anthropic through its secret scanning partner program, and Anthropic can revoke the key and email its owner. Check your email and the Claude Console, and rotate the key yourself either way.
My repository is private. Do I still need to rotate?
Yes. Everyone with access, every connected app and any future change to public exposes the history. Rotation takes minutes; finding out later that a key was used takes much longer.
How do I know if someone used the key?
Check the provider's usage, billing and logs for the period since the push: AI token spend, Stripe events, Supabase logs, AWS CloudTrail. Unusual volume, new regions or unfamiliar requests are the signs.
How do I stop Claude Code from reading my .env file?
Add deny rules such as Read(./.env) and Read(./.env.*) to .claude/settings.json and commit the file. Deny rules take priority over allow rules and apply in every permission mode.
Start with a conversation
Thirty minutes, no charge. Tell me what you have built and where it stopped. If I am not the right person, I will say so and point you somewhere better.
Calendar not loading? Open the booking page or email me.