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
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.
agent
proposes git push --force origin main
hook
reads the call as JSON on stdin
rule
deny-git-force-push matches
journal
the decision is recorded
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.
{
"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.
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.
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 mainRule
deny-git-force-pushno opinion
git push --force-with-lease origin feature/demoNo rule matched
allow
npm testRule
allow-read-only-shellask
npm publishRule
ask-publish
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.
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.
npx leashr initgit clone https://github.com/Leashr/leashr-skills
node leashr-skills/skills/lsh-init/scripts/init.js --target <your-project>claude plugin marketplace add Leashr/leashr-skills
claude plugin install leashr@leashrRestart 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.