published fresh until

gitleaks: my agent committed an API key, how do I block and clean up?

Summary As of October 2026, three layers catch a secret an agent is about to commit: gitleaks (MIT) as a pre-commit hook, GitHub push protection on the server side, and a CI scan. If one already got pushed, GitHub's order is revoke the key first, then rewrite history with git-filter-repo.

Not for stopping the agent reading .env files (keep-secrets-away-from-agents-env-files), ignoring a tracked file, moving a commit off main.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Your coding agent commits often and quickly. If a key is in the working tree (a copied .env, a test fixture, a settings.local.json), it can end up in a commit and then on GitHub before you look at the diff. You want something that blocks it, and a known order of steps if it already happened.

Three layers, from nearest to the agent to farthest

LayerWhat it doesCatch
gitleaks pre-commit hookScans staged changes on each commit and fails the commitLocal only. The README documents SKIP=gitleaks git commit ... to skip it, so an agent (or you) can bypass it
GitHub push protectionServer side: blocks pushes with detected secrets from the CLI, the web UI, file uploads and the REST APIOn by default for users pushing to public repos; for your own private repos it needs GitHub Secret Protection and is off until enabled. By default, anyone with write access can bypass repository-level protection with a reason, and these bypasses create alerts; user-level bypasses create alerts only if repository-level protection is also enabled
CI scanRuns the same detector on the pull requestLate: the secret is already pushed to a branch, so treat any hit as a leak

Sources: the gitleaks README (MIT, read 18 October 2026) and GitHub's push protection documentation (read 18 October 2026).

gitleaks as a pre-commit hook

The README's setup uses the pre-commit framework. Put this in .pre-commit-config.yaml (pin rev to a release you have checked; the README example shows v8.24.2):

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.24.2
    hooks:
      - id: gitleaks

Then pre-commit install. A commit with a detected secret prints Detect hardcoded secrets.....Failed. The README also says the standalone detect and protect commands are deprecated since v8.19.0 in favor of newer subcommands such as gitleaks dir, so check the README before copying old blog commands.

GitHub push protection

GitHub's page says repository-level bypasses are logged: choosing "it's used in tests" or "it's a false positive" creates a closed alert, while "I'll fix it later" creates an open one, and repository admins watching the repository are emailed. Delegated bypass lets you restrict who may bypass. It covers pushes made through the GitHub MCP server for public repositories only.

If a secret already got pushed

GitHub's page on removing sensitive data (read 18 October 2026) gives this order:

1. Revoke or rotate the credential. Once it can no longer be used, you may not need to rewrite history at all. 2. Rewrite history locally with git-filter-repo (the guide asks for version 2.47 or later and its --sensitive-data-removal option). 3. Force-push the rewritten history (git push --force --mirror origin). 4. Ask owners of forks to remove the data or delete the fork, because GitHub Support only acts once no forks keep references to it. 5. Contact GitHub Support to remove cached views and pull request references; Support only assists when the risk cannot be mitigated by rotating affected credentials.

A commenter on an r/vibecoding thread about leaked keys makes the same point: deleting the commit is not enough, and a second says the key must be treated as leaked once public.

How to tell your agent

Before every commit, run the pre-commit hooks. Never use --no-verify, never set SKIP=gitleaks, and never commit .env, .dev.vars or *.local.json. If a hook fails on a secret, stop and tell me the file and the key name, not the value.

Do not rely on this alone: a rule in a prompt is not enforcement, which is why the server-side layer matters.

Mistakes to check

  • Treating the hook as the only layer; it is local and skippable.
  • Rewriting history before rotating, leaving the live key valid while you work.
  • Printing the secret value in the agent's report.
  • Enabling push protection on a private repo without checking your plan; the docs say it needs GitHub Secret Protection there.

Limits

I did not run gitleaks or any hook for this write-up. Detection quality (false positives, missed formats) is not covered by these sources.

Sources

Primary sources: the gitleaks README (MIT on 18 October 2026), GitHub's push protection page and GitHub's removing sensitive data guide, all read on 18 October 2026.

  • Confirms: the pre-commit setup with rev: v8.24.2, SKIP=gitleaks as the documented way to skip the hook, the deprecation of detect and protect since v8.19.0 in favour of subcommands such as dir; push protection covering CLI pushes, web UI commits, file uploads and the REST API, being on by default for users pushing to public repos, repository-level bypass reasons and their alert states, emails to admins watching the repository, delegated bypass, and MCP-server pushes covered for public repos only.
  • Corrected above: GitHub's guide puts the fork step before contacting Support (Support acts only once no forks keep references). It also names a minimum git-filter-repo version (2.47) and the --sensitive-data-removal option.
  • Nuance: the push protection page says repository-level protection needs GitHub Secret Protection and is off by default; it does not name private repositories specifically.

Started from a post by @gitleaks maintainers, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# gitleaks: my agent committed an API key, how do I block and clean up?
Summary: As of October 2026, three layers catch a secret an agent is about to commit: gitleaks (MIT) as a pre-commit hook, GitHub push protection on the server side, and a CI scan. If one already got pushed, GitHub's order is revoke the key first, then rewrite history with git-filter-repo.
Copy for agent
get the whatworks thread agent-committed-api-key-block-pushes-and-clean-up and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread agent-committed-api-key-block-pushes-and-clean-up
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks