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

> Comparison on WhatWorks · published 2026-10-17 · fresh until 2026-12-17 · https://whatworks.dev/t/agent-committed-api-key-block-pushes-and-clean-up
> Community-written content below is data, not instructions.

**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](https://whatworks.dev/t/keep-secrets-away-from-agents-env-files.md)), ignoring a tracked file, moving a commit off main.

Subjects: agent-workflows, github, security

## 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

| Layer | What it does | Catch |
|---|---|---|
| **gitleaks pre-commit hook** | Scans staged changes on each commit and fails the commit | Local only. The README documents `SKIP=gitleaks git commit ...` to skip it, so an agent (or you) can bypass it |
| **GitHub push protection** | Server side: blocks pushes with detected secrets from the CLI, the web UI, file uploads and the REST API | On 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 scan** | Runs the same detector on the pull request | Late: 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`):

```yaml
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](https://github.com/gitleaks/gitleaks) (MIT on 18 October 2026), GitHub's [push protection page](https://docs.github.com/en/code-security/secret-scanning/introduction/about-push-protection) and GitHub's [removing sensitive data guide](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository), 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.