An AI coding agent needs a key to test something, finds one in your environment (an OpenAI, Claude or GitHub token), writes it into a config file to make the test pass, commits, and pushes. It did exactly what you asked. The key is now in your git history.
Once a secret reaches a remote, deleting it is not enough: forks, clones, mirrors and caches may already have it. The only real fix is rotating the key, and the only cheap fix is never letting it leave your machine.
This guide sets that up so it holds even when the agent is the one pushing.
Why the usual safeguards don't hold
.gitignore keeps .env out of git, but the agent didn't put the key in .env. It put it in config.go, a test fixture, or a README example.
A pre-commit hook scans for keys, but it runs on the same machine as the agent, as the same user. One git commit --no-verify and it's skipped. Hooks also aren't versioned by git, so a fresh clone or a new worktree has none.
GitHub push protection does block many key types before they land, and on public repositories it's on by default. But anyone with write access can bypass it by picking a reason like "used in tests", on private repositories it's part of the paid GitHub Secret Protection add-on, and it only exists on GitHub - not on Gitea, a self-hosted GitLab or anything else.
The common problem: every one of these either sits where the agent can switch it off, can be waved through, or doesn't cover the repo you care about.
Already covered? If your repo is public on GitHub and nobody would bypass a warning, push protection already does much of this. The rest of this guide is for private repos without the add-on, other git hosts, and anyone who wants a check with no bypass option.
A newer leak: the agent's own settings file
Agents keep their own config inside the repo, and that config can hold secrets too. When you approve a command in Claude Code with "Yes, and don't ask again", it saves that exact command to .claude/settings.local.json so it can match it next time. If the command had a password or token in it, the secret is now in that file. Claude Code sets git to ignore the file when it creates it, but that doesn't help once the file is already tracked. Apiiro scanned 104,185 public copies of the file on GitHub and found 1,122 with at least one real secret. Two widely used secret scanners, Gitleaks and TruffleHog, each missed hundreds of those files, and 215 slipped past both. Most of the misses were passwords typed into commands (PGPASSWORD=, sshpass -p, mysql -p), which carry no prefix for a scanner to recognise.
Check yours:
git ls-files .claude/settings.local.json # prints the path if git tracks it If it prints the path, untrack it and keep it ignored:
git rm --cached .claude/settings.local.json echo '.claude/settings.local.json' >> .gitignore Any secret in it that was ever pushed is leaked: rotate it (see "Already leaked?" below). To keep new ones out, pass secrets through environment variables rather than typing them into commands you approve.
The fix: a check the agent can't route around
Put the check on a separate machine between the agent and GitHub, and take away the agent's direct route to GitHub.
YOUR COMPUTER THE GATEWAY GITHUB (you + the agent) (checks every push) (your real repo) git push ──────────────► checks against rules ────► forwarded if clean rejected if not Two rules make this a real boundary:
- Your computer only pushes to the gateway. Your
originpoints at the gateway, not at GitHub. - Only the gateway can write to GitHub. It holds the GitHub token. Your computer, and so the agent, doesn't have one.
Now --no-verify changes nothing: the check runs on the gateway, on the server side of the push, where the agent can't reach it.
This is what nimblegate does. It's self-hosted, runs as one Docker container, and nothing in it phones home. The steps below use it; the ten-minute version is the quick start.
Prefer to watch? The whole setup, start to finish, in 6 minutes:
Step 1: Run the gateway
On any small server, VM or home-lab box with Docker (not your laptop):
curl -O https://raw.githubusercontent.com/nimblegate/nimblegate/main/compose.yaml docker compose up -d The admin dashboard is on port 7900 (bound to localhost) and git pushes go to port 2222. Claim your admin login with the setup token from the logs, add your SSH public key, and register the repo with its GitHub URL and a GitHub token. The token is stored on the gateway only.
Step 2: Point your repo at the gateway
On your computer, add a shortcut to ~/.ssh/config:
Host nimblegate Hostname 192.0.2.10 # your gateway's address Port 2222 User git IdentityFile ~/.ssh/nimblegate IdentitiesOnly yes Then switch the repo's remote:
git remote set-url origin nimblegate:myapp.git From here on, git push goes to the gateway, and clean pushes are forwarded to GitHub byte for byte: same commits, same SHAs, same signatures.
Step 3: Close the side door
The gateway only protects you if the agent can't push to GitHub directly. On the machine where the agent runs:
# remove stored HTTPS credentials for GitHub rm -f ~/.git-credentials git config --global --unset credential.helper # check this machine's SSH key isn't accepted by GitHub directly ssh -o BatchMode=yes -T git@github.com 2>&1 | grep -q "Permission denied" \ && echo "OK: github refused" || echo "BYPASS: this key works on github directly" If you see BYPASS, remove that key from your GitHub account (or use a different key for the gateway). Also check for a logged-in gh CLI (gh auth status) - it's another way around the gate.
Step 4: Test it with a fake key
Generate a random fake AWS key (it matches the real format but was never issued), commit it and push:
echo "AWS_KEY=AKIA$(LC_ALL=C tr -dc 'A-Z0-9' < /dev/urandom | head -c 16)" > leak-test.env git add leak-test.env && git commit -m "test: leak a fake key" git push The push is refused before anything reaches GitHub:
remote: error: push rejected by repository policy: remote: refs/heads/main: BLOCK [security/no-hardcoded-credentials] credentials detected (raw bytes redacted): leak-test.env:1 - AWS access key ! [remote rejected] main -> main (pre-receive hook declined) The key itself is redacted from the message. Undo the test commit with git reset --hard HEAD~1.
The same message is what an agent sees when it pushes a key. It names the file and line, so the agent can remove the key and push again, and the clean push goes through.
(Don't test with AWS's documented example key, AKIAIOSFODNN7EXAMPLE. It's exempt on purpose, because it appears in documentation everywhere.)
Already leaked? Rotate first, then clean the history
If a key has already reached GitHub, the order matters:
- Revoke or rotate the key at the provider, now. Deleting it from git does not un-leak it: automated scanners watch public GitHub and find new keys within minutes. Assume it was copied.
- Then remove it from the history, if the repo should be clean. With git filter-repo, either drop the file from every commit:
git filter-repo --invert-paths --path config/secrets.env or replace the key text wherever it appears (a file with one old-text==>***REMOVED*** line per secret):
git filter-repo --replace-text replacements.txt This rewrites history, so it needs a force-push and everyone re-clones.
- Forks and cached views may keep the old commits. GitHub's guide "Removing sensitive data from a repository" covers asking them to purge those.
If your repo sits behind a gate that blocks force-pushes to the main branch, as it should, the rewrite is a deliberate operator action rather than something an agent can do. That is the point.
What it catches, and what it doesn't
Blocked out of the box: keys with a recognisable issuer prefix. That covers AWS, GitHub tokens (ghp_, github_pat_ and the rest), Stripe secret keys, Anthropic, OpenAI, Hugging Face, Groq, xAI, Slack and Google API keys, and private key files. These patterns almost never match anything innocent, so they don't slow you down with false alarms. The full list is on the hardcoded credentials check page.
Also blocked: passwords where the surrounding text gives them away. That means a password in a connection string (postgres://app:...@db), one passed to a command (mysql -p..., sshpass -p, curl -u user:...), or one in a credential file such as .pgpass, .netrc or .npmrc. That covers the settings-file leak above too. See the plaintext passwords check.
Not blocked: a random-looking string with no prefix and nothing around it that marks it as a secret. Detecting those means guessing from randomness, which flags hashes and IDs all day, so the Security strict kit only warns about them (possible secret). Keep secrets in environment variables or a secret manager, and don't hand the agent more than it needs.
Not ready to block yet?
Turn on observe mode for the repo. Every push is checked and recorded on the dashboard, but nothing is rejected. Run it for a week, see what it would have caught, then switch to enforce. Before you switch, the repo's Policy page shows what the next push would block under the current rules, so the first enforced push holds no surprises.
nimblegate is free for non-commercial use; commercial use needs a license. Questions: contact@nimblegate.com.
Originally published at nimblegate.com.

