How to stop Claude Code from force pushing to main

Last updated ยท OsmoSync LLC

Short answer

Use two layers. Turn on branch protection in GitHub so the server refuses force pushes to main, and add a Claude Code PreToolUse hook that blocks git push --force before it leaves your Mac. Passkey for AI's git preset is that hook, built to read commands the way the shell does and to ask for Touch ID on a plain push to main.

Why it happens

Claude Code runs git through its Bash tool. If a push is rejected, a model under pressure to finish the task can reach for git push --force origin main. One command rewrites remote history, and teammates, CI and deploys all pull from it.

A permission prompt in the terminal helps, but the session itself can answer it and "allow always" is one click away. You want a refusal that does not depend on anyone remembering to read a prompt.

Layer 1: branch protection on the server

This is the strongest control because GitHub enforces it no matter which tool pushes.

  1. Open the repository, then Settings, then Branches (or Rules for rulesets).
  2. Add a rule for main.
  3. Leave Allow force pushes off on a classic branch protection rule, or enable Block force pushes in a ruleset. Add Restrict deletions too.

Do this on every repository an agent can push to. It protects the branch from everyone, including you on a bad day.

Layer 2: a Claude Code PreToolUse hook

A hook runs before the tool call. It receives the call as JSON on stdin, and exiting with code 2 blocks it and sends your message back to Claude. Register it in ~/.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "~/.claude/hooks/no-force-push.sh" }]
      }
    ]
  }
}

A minimal script that catches the plain forms (needs jq):

#!/bin/bash
cmd=$(jq -r '.tool_input.command // ""')
if echo "$cmd" | grep -Eq 'git[[:space:]]+push' \
   && echo "$cmd" | grep -Eq -- '(--force|-f|[[:space:]]\+)' \
   && echo "$cmd" | grep -Eq '(main|master)'; then
  echo "Blocked: no force pushes to main." >&2
  exit 2
fi
exit 0

Know its limits. A regex over shell text misses quoted arguments, variables, git -c options, aliases, wrappers like sudo or xargs, and a script written first and run later. It also fires on a commit message that merely contains those words if you are not careful. That is the gap a purpose-built hook closes.

Layer 3: the Passkey for AI git preset

Passkey reads shell commands the way the shell does: quotes, escapes, variables, cd, globs, redirects, heredocs, pipes and wrappers such as sudo, env, xargs and find -exec. The git preset (Pro) then applies these rules:

ActionWhat happens
Force push to main or master (--force, -f, +ref)Blocked, no prompt
Deleting main, or any --mirror pushBlocked, no prompt
Push to main or masterTouch ID
Push of HEAD, @, a glob or --allTouch ID
git push or git push origin with no branchTouch ID
Git aliases that pushTouch ID
Push to a named feature branchPasses
A commit message that says "force push"Passes

Turn it on at install time or later in your config:

./install.sh --presets stripe,git --hardened

Every decision lands in ~/.claude/passkey/audit.log.

What each layer catches

Branch protectionDIY hookPasskey git preset
Enforced byGitHubYour MacYour Mac, with Touch ID
Plain push --forceYesYesYes
Quoted, aliased or wrapped formsYesOften missedParsed
Plain push to mainAllowed unless PRs requiredNot coveredAsks for your finger
Works on repos you forgot to protectNoYesYes

Frequently asked questions

Does Claude Code force push on its own?

It can run any git command you let it run. A force push to main usually happens when the agent is trying to fix a rejected push or a messy rebase and picks --force as the way out. A hook or branch protection turns that mistake into a refusal.

Does the Passkey git preset catch git push -f and +ref?

Yes. It blocks --force, -f and the +ref form when the target is main or master, blocks deleting main, and blocks any --mirror push. A push to a named feature branch passes.

What about a normal push to main?

It asks for Touch ID. So do a push of HEAD, a push with a glob or --all, a push that names no branch, and git aliases that push.

Is branch protection enough?

It is the best single backstop because the server enforces it. It does not cover every repository you touch, and it does not stop a deploy or a delete. Use it with a local hook.