Claude Skills Collection Logo
Back to notes

NOTES / GUIDES

Blocking git push Inside Claude Code Takes One Hook and Nine Patterns

The git-guardrails skill installs a PreToolUse hook that greps Bash commands against nine patterns and exits 2. Short, and it catches what agents actually do.

6 min read

The git-guardrails-claude-code skill installs a PreToolUse hook that blocks nine destructive git patterns before Claude Code runs them, and the whole mechanism is a 302-word SKILL.md plus a 20-line bash script. It is not a permission system or a sandbox — it is a grep against the command string, and it stops the specific mistakes that agents make. On skills.pub it is listed as git-guardrails-claude-code from a repository with 328,182 stars.

What exactly gets blocked?

Nine patterns, matched with grep -qE against the shell command:

PatternWhy it is on the list
git pushCovers all variants, including --force
git reset --hardDiscards uncommitted work irrecoverably
git clean -fdDeletes untracked files and directories
git clean -fDeletes untracked files
git branch -DDeletes a branch without a merge check
git checkout \.Discards all working-tree changes
git restore \.Same, in the newer syntax
push --forceCatches the version without the git prefix
reset --hardSame reason — matches when the command is piped or wrapped

The last two are redundancies, and they are deliberate. push --force without a git prefix appears when a command is constructed through a shell pipeline or a variable, so matching only git push would miss it.

How does the hook actually stop anything?

By exiting with code 2 and writing to stderr. The script is short enough to read in full:

#!/bin/bash
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')

DANGEROUS_PATTERNS=(
  "git push" "git reset --hard" "git clean -fd" "git clean -f"
  "git branch -D" "git checkout \." "git restore \." "push --force" "reset --hard"
)

for pattern in "${DANGEROUS_PATTERNS[@]}"; do
  if echo "$COMMAND" | grep -qE "$pattern"; then
    echo "BLOCKED: '$COMMAND' matches dangerous pattern '$pattern'. The user has prevented you from doing this." >&2
    exit 2
  fi
done
exit 0

Exit code 2 is the mechanism: Claude Code treats a non-zero exit from a PreToolUse hook as a refusal, so the tool call never happens. The stderr message reaches the model, and the wording matters — "The user has prevented you from doing this" tells the agent it hit a policy boundary rather than a bug it should work around.

MechanismWhat it relies on
Interception pointPreToolUse with matcher Bash
SignalExit code 2 plus stderr
Dependencyjq, to read .tool_input.command from the JSON payload
ScopeProject (.claude/settings.json) or global (~/.claude/settings.json)

Is this a security boundary?

No, and it is worth being precise about what it is. The pattern list matches a substring of the command. git push inside a string literal, a path named git press, or a command that assembles the words at runtime will not be caught — and a command that contains the phrase for an unrelated reason will be blocked.

What it actually defends against is a specific behaviour: an agent deciding on its own to push, force-push, or hard-reset to make a plan work. Those are the operations where an autonomous loop can destroy work that was never committed, and they are exactly the commands where the agent has enough context to want to run them.

The SKILL.md's own framing is honest about this. The blocked message says the agent lacks authority, which is a policy statement, not a security claim.

Why a hook instead of an instruction?

Because instructions are advisory and hooks are not. A CLAUDE.md line saying "never force-push" is text the model may or may not weight correctly under pressure; a PreToolUse hook runs outside the model's discretion. The skill picks the mechanism that matches the guarantee.

There is a cost: the hook runs on every Bash call, so it adds a shell invocation and a jq parse to each one. For a guard against data loss that is a reasonable tax, and the script exits 0 immediately when nothing matches.

How is it installed?

Five steps, and the skill asks two questions along the way rather than deciding for you:

  1. Ask scope — this project only, or all projects.
  2. Copy the script to .claude/hooks/block-dangerous-git.sh or ~/.claude/hooks/block-dangerous-git.sh, then chmod +x.
  3. Merge the hook into the settings file. The skill is explicit: "If the settings file already exists, merge the hook into the existing hooks.PreToolUse array. Don't overwrite other settings."
  4. Ask about customization — whether to add or remove patterns, editing the copied script.
  5. Verify with a real command.

Step 3 is the one that matters operationally. Overwriting settings.json would silently drop every other hook you have, and the skill calls that out.

How do you verify it works?

The skill gives a one-liner that tests the script in isolation, without needing Claude Code:

echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>

Expected: exit code 2 and a BLOCKED message on stderr. If you get exit 0, either the script is not executable, jq is missing, or the path is wrong.

TestExpectedIf it fails
git push origin mainexit 2, BLOCKEDjq missing, or wrong script path
git statusexit 0, silentA pattern is too broad
git reset --hard HEAD~1exit 2, BLOCKEDThe array lost an entry during editing

What are the gaps worth knowing?

  1. No git commit protection. An agent can still commit anything, including secrets, before being stopped at push. If that matters, the guard needs a pre-commit secret scan, which this skill does not provide.
  2. jq is an undeclared dependency. The SKILL.md does not mention it; the script uses it. Without jq, COMMAND is empty, and an empty string matches no pattern — the guard fails open, silently.
  3. Global scope is per-user, not per-repo. If you install globally, every project on the machine inherits the block, including projects where pushing is the agent's job.
  4. Patterns are substring matches. git branch -D is blocked, but git branch --delete --force is a different spelling of the same operation and is not in the list.

That second point is the one to fix first: add a jq presence check that exits non-zero, so a missing dependency blocks rather than silently permits.

Should you install it?

If you let an agent run Bash in a repository with uncommitted work, yes — the cost is one hook and a small latency increase, and the failure it prevents is unrecoverable. If you already run agents in a sandbox where the working tree is disposable, the guard is redundant with the sandbox.

The general pattern is worth stealing even if you do not use this skill: a PreToolUse hook that matches a command string and exits 2 is the cheapest way to turn "please don't" into "cannot", and it works in any harness that honours hook exit codes.

The pattern list, the script body, the install steps and the verification command were read from the repository on 2026-11-20: SKILL.md and block-dangerous-git.sh. The claim that Claude Code treats exit code 2 as a refusal is stated in the SKILL.md and NOT independently verified here.

Sources