force-push-blocker — Claude Code hook: what it does, how to install
Intercepts and rejects git force push commands before Claude Code executes them in your shell.
npx claude-code-templates@latest --hook security/force-push-blockerWhat force-push-blocker does
The hook hooks into the PreToolUse event for the Bash tool. It relies on conditional matchers targeting patterns like git push *--force* and git push *-f*. If Claude Code attempts to run those commands, execution stops immediately.
Whenever a match triggers, the hook outputs a JSON payload setting permissionDecision to deny. That returns an explicit refusal message directly to the agent instead of running the shell call.
The matching patterns catch --force, --force-with-lease, and the -f flag shorthand. Pushing forced updates intentionally requires running the command yourself in an external terminal window.
What it helps with
- prevent accidental git push --force invocations by the agent
- guard remote git history against destructive git push -f calls
- intercept --force-with-lease commands using PreToolUse event rules
Example prompts for the agent
- Run git push --force to update origin main
- Force push the branch using git push -f
- Try to push current changes with --force-with-lease
How to install force-push-blocker
1claude-code-templates
npx claude-code-templates@latest --hook security/force-push-blocker2Manually: put the files in place
.claude/settings.jsonThe hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json
Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command
Questions about force-push-blocker
How to install force-push-blocker in Claude Code?
Run npx claude-code-templates@latest --hook security/force-push-blocker in your project or paste the hook setup into .claude/settings.json.
What does force-push-blocker do?
It intercepts Bash commands right before execution and denies any git push command containing force flags.
Which force push flags are covered?
The conditions catch --force, --force-with-lease, alongside the shorter -f variant.
Source text
force-push-blocker.json · MIT
{
"description": "Block git force push commands using the if condition for efficient filtering. Prevents accidental force pushes that can overwrite remote history. Covers --force, --force-with-lease, and the -f shorthand.",
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"if": "Bash(git push *--force*)",
"command": "echo '{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Force push is blocked by hook\"}}'"
},
{
"type": "command",
"if": "Bash(git push *-f*)",
"command": "echo '{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"Force push (-f) is blocked by hook\"}}'"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates