Skip to content

All posts

AI

How to stop AI coding agents from committing secrets to git

Ritesh Patel

AI: How to stop AI coding agents from committing secrets to git

To stop an AI coding agent from pushing secrets, deny git push in Claude Code settings, add a PreToolUse hook that reads the whole command, check staged changes in a git pre-commit hook, and turn on GitHub push protection. Each layer sees something different, so no single one is enough.

Which layer sees what

The commit path has five places where a check can run. They differ in what they can read and in whether the agent can skip them. The table below compares them using the docs cited in each section. Checked against the Claude Code hooks and permissions docs, the GitHub push protection docs and the git documentation on its master branch, as of 2026-11-17. The Claude Code docs do not state a CLI version for these fields, so confirm them against your installed release.

Layer Runs when What it can read Can the agent skip it?
Claude Code deny rule Before a Bash call The command text Claude wrote Yes, with another spelling, such as git -C . push
Claude Code PreToolUse hook Before a Bash call The full command text in tool_input.command Yes, if the git call sits inside a script the agent runs
git pre-commit hook When git commit runs The staged changes Yes, with --no-verify
git pre-push hook When git push runs The refs and objects being pushed Yes, with --no-verify
GitHub push protection When the push reaches GitHub The pushed content, against secret patterns Writers can bypass it with a reason, and the bypass is logged

The first two layers run on the agent’s side and can be bypassed by how the command is written. The git hooks run on the developer’s machine. GitHub is the only layer that does not depend on the machine the agent runs on.

Step 1: Deny git push in Claude Code settings

Start with a deny rule in the shared project settings. The permissions docs say deny rules are evaluated before allow rules, so a broad entry in a committed file applies to everyone who opens the repository.

  1. Open or create .claude/settings.json at the project root.
  2. Add Bash(git push *) to the deny list under permissions.
  3. Commit the file, so the rule applies to every clone.
{
  "permissions": {
    "deny": [
      "Bash(git push *)"
    ]
  }
}

The docs are direct about the limit. A Bash rule “isn’t a security boundary around the program,” and its table lists Bash(git push *) as stopping git push origin main but not git -C . push origin main or git -c push.default=current push origin main. A deny rule is a guard against the usual form, not a complete block. The next step covers the other forms.

Step 2: Add a PreToolUse hook that reads the whole command

A PreToolUse hook receives the command text before it runs, so it can check forms that a rule misses. The hooks reference shows the tool input sent as JSON on stdin to the hook, with the command under tool_input.command, and a decision printed to stdout. First create the script the settings entry points to, at .claude/hooks/check-git-command.sh, and make it executable with chmod +x .claude/hooks/check-git-command.sh. It needs jq. This version denies a git commit or git push that skips git hooks with --no-verify, which is the bypass step 3 explains. Extend the pattern for other forms you want to refuse.

#!/usr/bin/env bash
# PreToolUse hook for the Bash tool. Reads the hook JSON on stdin.
cmd=$(jq -r '.tool_input.command // empty')

if printf '%s' "$cmd" | grep -Eq 'git[[:space:]].*(commit|push)' && printf '%s' "$cmd" | grep -q -e '--no-verify'; then
  jq -n '{hookSpecificOutput: {hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: "git hooks must not be skipped (--no-verify)"}}'
fi
exit 0

We ran the script by hand with sample hook input on 2026-11-17. git commit -m fix, git push origin main and ls -la printed nothing, so they are allowed. git commit --no-verify -m fix and git -C . push --no-verify origin main printed the deny decision. Then register the hook for the Bash matcher in the same settings file.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/check-git-command.sh"
          }
        ]
      }
    ]
  }
}

When the script finds a command it does not allow, it prints a decision with permissionDecision set to "deny". The hooks reference shows this output shape:

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "Destructive command blocked by hook"
  }
}

The hooks docs also list exit code 2 as a way to block: “On events that can block, Claude Code stops the action.” The stderr text becomes the reason Claude sees. A hook can add a block. The permissions docs state that a PreToolUse decision does not bypass a deny rule: “a matching deny rule blocks the call” whatever the hook returns, so the rule from step 1 still applies.

Step 3: Check the staged changes in a git pre-commit hook

Claude Code hooks see the command, not the content. A git pre-commit hook sees what is about to be committed. Git looks for hooks in $GIT_DIR/hooks by default, and the githooks docs say the location can be changed with core.hooksPath. Set it to a directory your team commits, so every clone uses the same checks.

  1. Create the hook directory and a pre-commit script inside it.
  2. Point git at the directory with core.hooksPath.
  3. In the script, inspect the output of git diff --cached. The git-diff page says this form “is to view the changes you staged for the next commit.”
  4. Exit with a non-zero status to abort the commit.
  5. Make the script executable with chmod +x .githooks/pre-commit, and commit it with the mode set (git add --chmod=+x .githooks/pre-commit). The githooks page says hooks that do not have the executable bit set are ignored, so a script without it silently does nothing, in your clone and in every other one.
git config core.hooksPath .githooks
git diff --cached

The githooks page is also the reason this layer is not enough alone. It says the pre-commit hook “can be bypassed with the --no-verify option.” The git-push page says that for the pre-push hook, “With --no-verify, the hook is bypassed completely.” An agent that runs git commit --no-verify skips the commit hook entirely. Add a Claude Code hook that denies any command containing --no-verify, so the bypass is refused before git sees it.

Step 4: Turn on GitHub push protection

Push protection is the only check that runs after the push leaves the machine. The GitHub docs describe push protection as blocking pushes that contain secrets before they reach the repository. Turn it on at the repository, organization or enterprise level. Per the docs, the repository form requires GitHub Secret Protection and is disabled by default, so a repository admin has to enable it.

The supported push surfaces table includes pushes from the command line, so a push from an agent’s terminal is covered when the feature is on. Bypass is the detail to plan for. By default, anyone with write access can bypass a block by giving a reason, and each reason leaves a different alert:

Bypass reason Alert created
It’s used in tests Closed, resolved as “used in tests”
It’s a false positive Closed, resolved as “false positive”
I’ll fix it later Open

If you need stricter control, the docs describe delegated bypass, which limits who can bypass and who can approve a bypass request. Custom patterns are regular expressions you define for secrets specific to your repository, organization or enterprise, and you can enable push protection for them.

What a hook cannot block

The layers above share some gaps, and the docs are specific about several of them.

  • Other tools. The hooks reference says “Bash matches the Bash tool, and on PreToolUse and PermissionRequest also Monitor commands.” So the Monitor case applies only to PreToolUse and PermissionRequest hooks. A commit made by another tool, or by a person in an IDE, is not seen by a Claude Code hook. The git hooks and GitHub still apply.
  • Wrapped commands. The permissions docs say sh -c 'curl ...' and bash -c 'rm ...' escape a Bash rule. A hook has to read the full text, including what sits inside a wrapper.
  • The bypass flag. --no-verify skips git’s own hooks, as the githooks page states, so the Claude Code hook is what catches it.
  • Secrets already in history. Push protection checks what is being pushed. A secret that was committed locally and stayed in history before the feature was on is not removed by it.
  • Unsupported secret shapes. Push protection covers the pattern types in its docs. A credential that matches none of them, and that AI detection does not flag, passes.

Where to go next

Start with the control that matches your gap. For reads of .env files and other secret paths, read how to stop Claude Code from reading secrets and .env files. For the Bash side of the same problem, read how to block destructive commands in Claude Code, and to limit where an agent can run commands at all, read how to run Claude Code in a sandbox. For the full set of controls across coding agents, see governance for AI coding agents, and for the product side, see AIControls.

Frequently asked questions

Does a Claude Code deny rule stop an agent from pushing secrets to git?

Only for the command form the rule matches. The permissions docs say a Bash rule matches the command text Claude writes, not the program it runs. Bash(git push *) stops git push origin main, but not git -C . push origin main. Pair the rule with a PreToolUse hook and with GitHub push protection on the server.

Can a git pre-commit hook stop an agent from committing a secret?

Yes, unless the commit skips it. Git runs pre-commit during git commit, and a non-zero exit aborts the commit. The githooks documentation says the hook can be bypassed with --no-verify. A Claude Code hook that refuses that flag covers the common bypass, and the git hook can then inspect the staged changes.

Does GitHub push protection catch every kind of secret?

No. The docs list the types it blocks: provider-specific credentials, structured secrets such as private keys and connection strings, custom patterns you define, and unstructured secrets found by AI, which is in public preview. Push protection for repositories requires GitHub Secret Protection and is disabled by default.

Is a push protection bypass recorded anywhere?

Yes. According to the docs, a bypass creates an alert in the Security and quality tab, adds the event to the audit log and emails owners and watchers. The alert closes if the reason is used in tests or is a false positive, and stays open if the reason is I'll fix it later.

Ritesh Patel, Founder & VP Product, Nirmata

Ritesh Patel is a founder of Nirmata and leads its products. He writes about governing AI coding agents, MCP security and Kubernetes policy as code.

LinkedIn

Govern AI agents with AIControlsControl what AI agents do, and prove it.
Share

Stay up-to-date with the latest in AI, governance, and infrastructure

Subscribe to our free monthly newsletter to get industry news, DevOps tips, and articles right to your inbox on Kyverno, containerization, Kubernetes deployment & management and more.