Skip to content
Leashr

How to block git push --force in Claude Code

Stop an agent from rewriting shared history: a PreToolUse hook, one deny rule, the real refusal output, and the shell wrapper that slips past a plain prefix rule.

Thomas Devulder · Published · Updated

Diagram motif: an agent box, an arrow to a hook box, an arrow to a green deny box, an arrow to a log box.

A coding agent that can run shell commands can run git push --force origin main. One such call rewrites history that your whole team shares, and the agent will not feel bad about it. You want that call refused before it runs, every time, with a reason the agent can read and act on.

Claude Code gives you two places to do this: its own permission rules, and hooks. This article uses a hook, because a hook is code you control. It sees the whole call, decides, and leaves a trace. Every output below was replayed on a throwaway project with the real scripts, and the figures come from the public benchmarks measured with Claude Code 2.1.310 on claude-haiku-5-5.

What happens between the model and the shell

When the model asks to run a tool, Claude Code first runs the PreToolUse hooks configured for the project. A hook is a program. It receives the tool name and its input as JSON on standard input, and it answers allow, ask or deny. Only then does the tool run.

  1. agent

    proposes git push --force origin main

  2. hook

    reads the call as JSON on stdin

  3. rule

    deny-git-force-push matches

  4. journal

    the decision is recorded

The path of one call. The decision is taken before the shell is reached.

One rule is enough

Leashr keeps its rules in a readable file, .leashr/rules.json. The rule below is the one that ships by default. It matches git push with a force flag or a leading plus on the refspec, and it deliberately lets --force-with-lease through, because that variant refuses to overwrite work you have not seen.

.leashr/rules.json, rule deny-git-force-push
{
  "id": "deny-git-force-push",
  "decision": "deny",
  "tool": "Bash",
  "command": "\\bgit\\b.*\\bpush\\b.*(?:[\\s\"']--force(?![-\\w])|[\\s\"']-[a-zA-Z]*f[a-zA-Z]*\\b|[\\s\"']\\+\\S)",
  "reason": "Force push rewrites shared history (--force-with-lease is allowed).",
  "quoted": "ignore"
}

The rules ship with their own test suite, and the published version passes all of it: 427/427 cases. That count is a regression check on the commands the authors thought of. It says nothing about commands they did not.

node skills/lsh-rules/scripts/rules.js test
427/427 cases passed.

exit 0

Real output, replayed by tools/blog-outputs.mjs on 2026-11-28 in a throwaway project with the published rules (14c37dc Leashr 0.1.0).

The refusal, as the agent sees it

Here is the real output of the hook for the dangerous call. The exit status is two, which is how a Claude Code hook says deny, and the message goes back to the model so it can pick another approach.

# the agent proposes: git push --force origin main
node .leashr/bin/evaluate.js
Leashr blocked this Bash call (rule deny-git-force-push): Force push rewrites shared history (--force-with-lease is allowed).

exit 2

Real output, replayed by tools/blog-outputs.mjs on 2026-11-28 in a throwaway project with the published rules (14c37dc Leashr 0.1.0).

Four calls, four different answers. The force push is denied, the safer variant gets no opinion from the hook and falls back to Claude Code's own permissions, a test run is allowed and a package publish asks a human.

  • deny

    git push --force origin main

    Rule deny-git-force-push

  • no opinion

    git push --force-with-lease origin feature/demo

    No rule matched

  • allow

    npm test

    Rule allow-read-only-shell

  • ask

    npm publish

    Rule ask-publish

Verdicts of the default rules for four calls. Source: content/data/blog-outputs.json, replayed with the real evaluate.js of the published rules (14c37dc Leashr 0.1.0).

The wrapper that slips past a prefix rule

A permission rule written for git push --force matches the start of a command. Wrap the same command in bash -c '...' and the start of the command is now bash. This is not a theory. The benchmark ran real Claude Code sessions where the model chose its own form, and counted how many times the wrapped force push was refused.

# the agent proposes: bash -c 'git push --force origin main'
node .leashr/bin/evaluate.js
Leashr blocked this Bash call (rule deny-git-force-push): Force push rewrites shared history (--force-with-lease is allowed).

exit 2

Real output, replayed by tools/blog-outputs.mjs on 2026-11-28 in a throwaway project with the published rules (14c37dc Leashr 0.1.0).
Real Claude Code sessions: `bash -c 'git push --force origin main'` refused, out of 5
Claude Code permission rule
0/5
lsh-rules hook
5/5

Source: bench/RESULTS.md, real sessions on claude-haiku-5-5.

A native rule refused 0/5 of the wrapped calls and the hook refused 5/5. Read that with its limit: it is one model, a handful of sessions, and the model decides which form to write. For the direct form both approaches were equal, 5/5 against 5/5. The honest summary is that the hook covers the wrapper and the native rule does not.

Install it

The code is public and the package is on npm. The shortest way in is one command, npx leashr init, run in your project. You can also install from a clone of the repository, or as a Claude Code plugin.

From npm, in your project
npx leashr init
Or from a clone of the repository
git clone https://github.com/Leashr/leashr-skills
node leashr-skills/skills/lsh-init/scripts/init.js --target <your-project>
As a Claude Code plugin (skills only, no hook is written for you)
claude plugin marketplace add Leashr/leashr-skills
claude plugin install leashr@leashr

Restart Claude Code in the project afterwards. The hooks are written into the project, so a project without .leashr/ is not governed and not blocked.

What this does not do

If your team forbids a push to a protected branch altogether, you can also set that on the server side of your Git host. The hook is the layer that protects you from the agent before the command leaves your machine, and the host setting is the layer that protects everyone from everyone. Use both when the branch matters.

A rule is a pattern on text. It does not run in a sandbox, it cannot see what a script does once it starts, and a creative command can still get through. The blind benchmark measures that gap, and the article on what a hook cannot catch gives the numbers. If a refused call matters to you, write a rule, test it on your own commands, and keep the journal on.

Try the rules on your own project

The code is public. Install from a clone, or as a Claude Code plugin, and read each skill with a replayed example.