Skip to main content
hook

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.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook security/force-push-blocker
Open on GitHub

What 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-blocker

2Manually: put the files in place

.claude/settings.json

The 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

Similar hooks

All hooks for Security →

From the same set804

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor