
1. Why agent commits need a local gate
A coding agent reads your .env to debug a failing test, writes the token into a fixture, and commits. Server-side push protection acts when the commit is pushed. By then the secret already sits in a local commit, possibly several, and an agent that commits in small steps can build on top of it before anyone looks. A local pre-commit hook stops the commit itself, before it creates history to rewrite. A blocked commit does not prove the credential stayed private: if a real token reached an agent service, log, or other unauthorized destination, revoke and rotate it even when the hook worked.
This guide sets up four things: a pinned Gitleaks hook on staged changes, a written policy for false positives, a CI check that catches commits that skipped the hook, and a drill that proves both layers block a planted fake key. Sanitizing what you send to a model is a separate problem, covered in Sanitizing Code and Data Before Sending to AI; this guide is about what the agent writes into Git.
One maintenance note before you adopt it. The current Gitleaks README opens with a warning that the project is feature complete, that new features are not being merged, that future releases will be security patches only, and that the maintainer is shifting focus to a successor project, Betterleaks. The commands below work today; put a review date on the tool choice.
2. Install via pre-commit with a pinned rev
The pre-commit framework is a Python tool (pip install pre-commit). Add this .pre-commit-config.yaml at the repository root:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1
hooks:
- id: gitleaks
Then run pre-commit install, which writes .git/hooks/pre-commit. Two version details matter. The Gitleaks README example still pins rev: v8.24.2, so copying it verbatim pins an older release. And v8.30.1, released on 21 March 2026, was the newest release on the releases page when this guide was checked on 29 October 2026. A tag is readable but movable; pre-commit autoupdate --freeze moves to the latest tag and stores its commit hash with a # frozen: v… comment, which is the stricter pin. Treat updates to that line as reviewed changes, not something an agent bumps on its own.
The gitleaks hook id uses language: golang. Since pre-commit 3.0.0 the framework bootstraps Go if it is missing and builds the hook in an isolated GOPATH, so the first run is slow. If you would rather use a binary you installed and verified yourself, the hook file also defines gitleaks-system; its binary version is your responsibility. The gitleaks-docker definition uses an untagged image, so pinning the hook repository does not pin that image. Use a reviewed image digest if you choose that variant.
3. Scan staged changes, and full history once
The Go and system hooks run this command; the Docker definition passes the same arguments to its image:
gitleaks git --pre-commit --redact --staged --verbose
In the v8.30.1 source, --staged makes Gitleaks read git diff -U0 --no-ext-diff --staged, which is the index. That is what gets committed, so it is the right input, but it has a consequence worth knowing: in our test, the same fake key in a modified but unstaged file produced scanned ~0 bytes and exit 0. --redact keeps the secret out of the hook output, which matters when an agent reads that output back into its context. Older snippets use gitleaks protect --staged; detect and protect have been deprecated since v8.19.0 and are hidden from --help.
The hook only sees new changes. Scan the existing history once, without --staged, which by default walks git log -p across all refs:
gitleaks git --redact --report-path gitleaks-report.json
Rotate anything real it finds: deleting the line in a later commit leaves the secret in every clone of the earlier one. If the remaining findings are reviewed test data, keep the report as a protected baseline outside the repository, or explicitly exclude the local report from Git. Redaction does not make a report public: paths, commit metadata, and context may still be sensitive. Do not let an agent add new findings to the baseline to make a check pass. Run with --baseline-path /path/to/reviewed-baseline.json and later reports contain only findings not represented by that baseline; in our run the second scan wrote an empty []. With the default flags, exit code 1 means "leaks or error", so a broken config also fails the commit, which is the safer failure.
4. False positives: .gitleaksignore, gitleaks:allow and allowlists
Some suppression is inevitable, and an agent that hits a blocked commit will look for the fastest way past it. Decide in advance which mechanism is allowed where. Gitleaks offers three:
| Mechanism | Scope | Fails when | Suggested policy |
|---|---|---|---|
gitleaks:allow comment on the line | One line | Anyone can add it, including the agent | Test fixtures only, reviewed; consider --ignore-gitleaks-allow in CI |
.gitleaksignore fingerprint | One finding | The line moves, or the fingerprint form does not match the scan | Known historical findings, one entry per reviewed item |
[[allowlists]] in .gitleaks.toml | Paths, regexes, stopwords, commits | A broad pattern hides real secrets | Narrow paths with targetRules, changed only through review |
The fingerprint row has a trap we confirmed by running it. A finding in a commit has the fingerprint commit:file:rule:line; a staged finding has no commit, so its fingerprint is file:rule:line. Gitleaks checks the short form first, so leak-test.env:github-pat:1 suppressed the staged finding (exit 0), while the same finding with a commit prefix did not (exit 1). After one line was inserted above the secret, the short entry stopped matching and the hook blocked again. The README also labels .gitleaksignore experimental. Both points argue for config allowlists over fingerprints for anything that changes.
A config that keeps the built-in rules and allows one fixture directory for one rule:
[extend]
useDefault = true
[[allowlists]]
description = "Reviewed synthetic tokens in auth tests"
targetRules = ["github-pat"]
paths = ['''^tests/fixtures/auth/''']
Keep it as .gitleaks.toml at the repository root, where the local hook finds it. The CI example below explicitly uses the reviewed base branch version. The path is an example; the point is that the exception names a rule and a directory instead of switching off detection for the repository. A rule-specific exception does not guarantee that the fixture passes: in this review, suppressing github-pat exposed a generic-api-key finding for the same synthetic value. Inspect all reported rule IDs and review each necessary exception; do not broaden the path allowlist to silence the whole scanner. If you use CODEOWNERS or branch rules, require review for .gitleaks.toml, .gitleaksignore, baselines, and .pre-commit-config.yaml alongside CI workflows. CODEOWNERS names reviewers; branch rules must require their approval to enforce that policy.
5. CI check for bypassed hooks
A pre-commit hook is advisory. git commit --no-verify skips the pre-commit hook entirely, SKIP=gitleaks skips this one, and a fresh agent sandbox may never have run pre-commit install. In our test, --no-verify committed the fake key without complaint. CI is where you enforce it; the AI-assisted CI/CD guide covers the wider pipeline.
Do not just run pre-commit run --all-files in CI for this hook. Its command scans the staged diff, and a fresh CI checkout has nothing staged. We reproduced that: after the bypassed commit, the staged scan reported no leaks found and exit 0, while a scan of the commit range exited 1. Scan the commits in the pull request instead:
name: gitleaks
on:
pull_request:
permissions:
contents: read
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.pull_request.base.sha }}
fetch-depth: 0
- name: Install gitleaks 8.30.1 with a pinned checksum
shell: bash
working-directory: ${{ runner.temp }}
run: |
curl -sSfL -o gitleaks.tar.gz https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz
echo "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb gitleaks.tar.gz" | sha256sum -c -
tar -xzf gitleaks.tar.gz gitleaks
- name: Scan PR history using base-branch policy
shell: bash
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
[[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ && "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]
[[ "$PR_NUMBER" =~ ^[0-9]+$ ]]
git fetch --no-tags origin "refs/pull/${PR_NUMBER}/head"
test "$(git rev-parse FETCH_HEAD)" = "$HEAD_SHA"
"$RUNNER_TEMP/gitleaks" git --redact --verbose \
--config .gitleaks.toml --ignore-gitleaks-allow \
--log-opts="${BASE_SHA}..${HEAD_SHA}"
The checksum is the linux_x64 line from the release's checksums file. The job checks out the base SHA, then fetches GitHub's PR head ref and verifies it still matches the event. It scans the resulting commit range without checking out or executing PR code. This keeps .gitleaks.toml and the automatically loaded .gitleaksignore on reviewed base-branch versions; --ignore-gitleaks-allow prevents an added inline comment from bypassing CI. Merge the initial policy file into the base branch before enabling the job. A moved PR head fails the identity check and requires a new run.
The job needs only read access to repository contents. Checkout retains that read-only credential for the subsequent fetch and removes it during cleanup; use GitHub-hosted ephemeral runners for this example. Protect the workflow itself with required review or an organization-controlled required workflow: a required check name alone cannot prevent a PR from redefining its producer. Pin or lock workflow dependencies and review updates. The range scan reports the commit for each finding; the check must be required before merging to block a bypassed local hook.
6. Prove it with a fake key; what push protection adds
Run the drill in a disposable local repository. Test hosted CI only in a designated test repository with a dummy credential, never with a real token. Generate the key at run time so no token-shaped string ever lives in your docs or scripts:
printf 'GITHUB_TOKEN=ghp_%s\n' "$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c 36)" > leak-test.env
git add leak-test.env
git commit -m "test: planted fake key"
The default github-pat rule matches ghp_ plus 36 letters and digits. Expected result: the commit is refused, the output shows RuleID: github-pat, Secret: REDACTED and at least one finding (other rules may also report), and git log has no new commit. Then bypass the local hook with --no-verify in that disposable repository and confirm that a range scan fails on the new commit. For a separate hosted integration drill, open a PR in the designated test repository and confirm the required CI job fails. Push protection may block the synthetic pattern before a PR exists; do not weaken production protection to complete the drill. Pass criterion: blocked locally, red in CI, and the key never appears unredacted in any log.
The original 10 October 2026 drill used a checksum-verified Windows Gitleaks 8.30.1 binary and a native Git hook. On 29 October, the published commands, allowlist, and PR-range scanning logic were checked again in isolated local repositories with Gitleaks 8.30.1 and Git 2.53.0.windows.2. The pre-commit 4.7.0 framework also installed the default Go hook from the pinned v8.30.1 source: it blocked the synthetic commit, while SKIP=gitleaks bypassed it locally and the subsequent range scan still failed. The hosted GitHub Actions workflow and GitHub push protection were not run; checkout, event, permission, and availability claims are documentation checks. Local scan tests do not prove that every credential format is detectable.
Push protection is the next layer, not a replacement. GitHub's 7 October 2026 changelog describes a fine-tuned model that reads surrounding code to find likely credentials, "including passwords without a recognizable token format", and AI push protection in private preview. The post describes eligibility for GitHub Secret Protection or Advanced Security customers on GitHub Enterprise Cloud or GitHub Team, with administrator enablement. It announces AI Credit consumption for the opt-in checks and says billing begins on public-preview opt-in; do not infer that every private-preview check was already billed on the announcement date. Verify current access and billing before enabling it. The post gives no detection rates. Gitleaks rules are patterns with entropy thresholds, so an unformatted password is exactly the case where a context-aware check can add something. If anything gets past both layers, revoke and rotate the credential; removing the commit does not undo the exposure. For limiting what an agent can reach in the first place, see Sandbox an AI Coding Agent in a Dev Container.
Sources checked 2026-10-29
- Gitleaks repository — README, hook definitions, staged-scan source, fingerprints, allowlists and exit codes.
- Gitleaks releases — v8.30.1 date, assets and checksums.
- pre-commit — installation,
autoupdate --freeze, SKIP and golang hooks. - GitHub changelog, 7 October 2026 — AI secret detection in push protection, availability and billing.
- Gitleaks v8.30.1 CLI source — configuration precedence and automatic source .gitleaksignore loading.
- Checkout v7.0.1 and GitHub Actions security guidance — action pin, untrusted code, and workflow trust.
- Remediating a leaked secret — revocation and cleanup are distinct.
Gitleaks commands and configuration are product-specific. The broader method is local detection plus an independently protected server check, reviewed exceptions, and credential revocation after exposure. Recheck by 2026-11-28 or after a relevant Gitleaks, pre-commit, or GitHub change.