Block destructive commands with a Claude Code hook
Write a PreToolUse hook that blocks a destructive command with exit code 2 and a reason, and place it in the settings scope the requirement calls for.
Scenario
Your team's CLAUDE.md says: "Never force-push." On Tuesday, deep into a long rebase session, Claude Code ran git push --force on a shared branch and overwrote a colleague's commits.
CLAUDE.md is guidance Claude reads. To make a rule hold every time, you need something that runs whether or not Claude remembers it.
Explanation
A hook is a command (or HTTP endpoint, MCP tool, prompt or agent) that Claude Code runs at a fixed point in its lifecycle. It runs every time, so it can enforce what an instruction can only ask for.
Three levels of configuration, in a settings file:
- The event:
PreToolUsefires before a tool runs and can block it.PostToolUsefires after: the tool has already run, so it can report but not prevent. - The matcher: for tool events it filters on the tool name.
Bashmatches only the Bash tool;Edit|Writematches either of those exactly. Other characters make it a regular expression. - The handler: for a command hook,
type: "command"and thecommandto run (withargs, it runs as exec form, without a shell).
What the script sees and returns. Claude Code sends the event as JSON on stdin; for Bash, the command is in tool_input.command. Then the exit code decides:
| Exit code | Effect on PreToolUse |
|---|---|
0 |
No objection; the normal permission flow applies |
2 |
Blocks the tool call; stderr is the reason, shown to Claude |
| anything else | By default a non-blocking error: a notice appears, and the action goes ahead |
Exit 1, the usual Unix failure code, does not block. For a policy hook, use exit 2. (A hook can instead exit 0 and print JSON with permissionDecision: "deny".) A hook that can't start, such as a mistyped path, is also a non-blocking error, so watch its first run, or set "onFailure": "block" on the handler so a failed start blocks the call.
Where it lives decides who it binds:
| Location | Reaches |
|---|---|
~/.claude/settings.json |
All your projects, your machine only |
.claude/settings.json |
Everyone on this project (committed) |
.claude/settings.local.json |
This project, you only |
| Managed policy settings | The whole organization, admin-controlled |
Hooks merge across levels: user and project settings add hooks but can't remove managed ones, and disableAllHooks outside managed settings can't turn managed hooks off. Admins can go further with allowManagedHooksOnly, which blocks user, project and local hooks.
Worked example
Requirement: block force pushes for everyone who works in this repository.
The configuration, in .claude/settings.json (committed, so the whole team gets it):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "bash",
"args": ["${CLAUDE_PROJECT_DIR}/hooks/guard.sh"]
}]
}
]
}
}
The script, hooks/guard.sh:
#!/bin/bash
input=$(cat)
cmd=$(jq -r '.tool_input.command' <<<"$input")
force='git +push.*( -f| --force| \+)'
if [[ "$cmd" =~ $force ]]; then
echo "Blocked: no force pushes. Push a new branch." >&2
exit 2
fi
exit 0
When Claude tries git push --force, git push -f or a +branch refspec, the call never runs. Claude receives the stderr text as the tool's error and can take the route the message suggests. Every other command exits 0 and goes through the usual permission checks.
Matching on command text is best-effort: an alias, a script or sh -c can spell the same push another way. Back the hook with a deny rule, and for a guarantee, protect the branch on the Git server too, which refuses a force push whatever the client sends.
${CLAUDE_PROJECT_DIR} points at the project root, so the hook finds the script from any working directory. The docs suggest exec form (args) when a command uses such a placeholder.