Stopping accidental credential pushes with Betterleaks

'A secret in the diff? Stop the push.' above a terminal panel showing git push, rule github-pat, finding token = "REDACTED" and result push refused, captioned as a synthetic Betterleaks 1.9.0 pre-push fixture.

Betterleaks v1.9.0 blocked a push containing a fake credential in a throwaway repository. I enabled the same pre-push hook in my blog checkout on November 20, 2026, adding a check before code leaves the machine.

The short version: the hook runs a wrapper script that scans the push range and prints matches with the value redacted. It finds the patterns the scanner knows, git push --no-verify skips it, and the CI job behind it runs after the push.

When I work with a coding agent, I need to be sure it won’t leak credentials, and the Betterleaks check is one harness I added around it. I wrote about the other half, keeping secret values out of the agent’s session, in Giving Claude Code and Codex their own 1Password service account.

I had been describing the Betterleaks check as a pre-commit check. Repository inspection on November 20, 2026 confirmed the hook runs at push time and needs enabling per clone. I asked for it to be enabled and verified in my blog checkout that day.

Why the check runs at push, not at commit

The original GitHub issue for this check proposed scanning staged changes before a local commit.1 The repository’s decision record chose a pre-push hook instead. Its reasoning: a pre-commit hook “catches a leak earliest, but runs on every commit, and slow commits get bypassed”, while a pre-push hook “still runs before anything leaves the machine and runs once per push.”2 The decision record gives reasoning, not timings.

The same record explains the scanner choice: Gitleaks was considered, but its maintainer directed feature work to Betterleaks, which reads the same config format.2

Betterleaks can scan staged changes itself, with betterleaks git . --pre-commit --staged,3 but the wrapper in my repository uses neither flag. So an agent can commit a fake token here, and the first sign of trouble is the push. The docs say what to do then: the commit hasn’t left the machine, so remove the value, amend or rebase the unpushed commits, and push again.4

Why cloning didn’t turn it on

The hook is a checked-in script, so cloning doesn’t enable it. The setup installs the scanner, enables the hook and checks the integration:4

  1. Install the pinned scanner and verify its checksum.
scripts/secret-scan.sh install
  1. Enable the checked-in hook for this clone.
git config core.hooksPath .githooks
  1. Run the integration tests, including the blocked-push test.
node --test tests/secret-scan.test.mjs

core.hooksPath replaces .git/hooks for that clone, and the hook is a one-line script that calls scripts/secret-scan.sh pre-push.4,5 Git ignores hooks without the executable bit, and a non-zero exit from a pre-push hook aborts the push.6 I approved enabling it, and the agent ran the configuration command in my blog checkout on November 20, 2026. The suite then reported 24 passed, 0 failed.

One of those tests is named “the pre-push hook blocks a real git push”. It pushes to a disposable local bare repository, then checks the remote didn’t receive the commit.7 Three of the 24 tests use a stand-in scanner binary (wrong version, forced scanner error, and a check of which flags get passed), so they test the wrapper, not Betterleaks itself.

What a blocked push looks like

The agent also ran the same wrapper and hook in throwaway repositories, using generated fake tokens and local bare repositories as remotes. None of these fixtures used a real credential.

In a throwaway repository with the hook enabled and a generated fake GitHub-style token committed to a text file, a normal git push to a local bare remote exited 1 and printed this (shortened):

secret-scan: refs/heads/main -> refs/heads/main (<old-sha>..<new-sha>)
┌─github-pat──○
│
│ 1 │ token = "REDACTED"
│   │          ^^^^^^^^
│
│ attributes:
│   confidence ............ high
│   git.message ........... synthetic fixture edit
│   path .................. second.txt
│   resource .............. git.patch_content
└○

secret-scan: credentials found in the commits above (values redacted).
error: failed to push some refs to '<local bare remote>'

What gets redacted and what doesn’t

The wrapper always runs Betterleaks with --redact, which hides secrets in logs and stdout (the output above shows REDACTED),8 and it never passes --validation. Validation sends found values to provider APIs; this wrapper leaves it off.4 The v1.9.0 flag defaults to false.9 The wrapper requests no report file.4

Redaction covers the matched value only. The rule name, file path and commit message print as they are, which is what an agent needs to fix the problem without seeing the secret again. The test run stopped at detection and push refusal; it did not exercise an agent reading the output and repairing its next attempt.

Exit codes are 0 for clean, 1 for findings and 2 for a setup or scan failure such as a missing binary, and the docs say a failure is never reported as clean.4 A match only means the text looks like a credential. With validation off, the scanner doesn’t say whether a token is live.

Where a push still gets through

This check catches recognizable credential patterns in the tested cases.

Gaps in what gets scanned

  • Betterleaks finds credential patterns the built-in rules know. Anything else gets through, and it says nothing about client names, internal links, private notes or images.4
  • The wrapper scans a commit range. With a known remote tip, that is the remote tip through the local tip. For a new branch or an unfetched tip, it excludes commits reachable from any local remote-tracking ref, including another remote’s refs. Uncommitted changes are outside this scan.4
  • Content that exists only in a merge commit, like a conflict resolution, isn’t in git log -p output, so it isn’t scanned.4 In the agent’s throwaway repository, the range scan exited 0 on a merge-only fake token and a directory scan of the same tree exited 2.

Gaps in who gets stopped

  • The hook is opt-in and git push --no-verify skips it.4 In a fixture repository, both git push --no-verify and git -c core.hooksPath=/dev/null push reached a local bare remote with the hook configured. Anyone who can run git in the repository can pass the same options.
  • The Secrets (Betterleaks) CI job scans after the push, so in a public repository it catches a leak after it is public.4 The docs say the check wasn’t required when written; current branch protection was outside this verification.

If you use coding agents:

  • Decide where the check runs. This repository’s decision record chose once-per-push scanning over scanning every commit.2
  • Pin the scanner version and its checksum, and use version-matched docs. This integration targets v1.9.0; the repository documentation warns that the upstream default branch describes v2.4
  • Try the hook on a fake token in a throwaway repository before you rely on it, and try the bypasses too.

Sources