Nightlatch / Guides

Stop AI coding agents from running git push --force

Updated 2026-11-29 · Claude Code · Cursor · Codex

Short version: one hook script covers Claude Code (PreToolUse) and Cursor (beforeShellExecution), a two-line Codex prefix_rule covers Codex, and server-side branch protection catches anything that slips through.

What counts as a force push

git push --force and -f are the obvious ones. The flag can also sit in a cluster (-uf), at the end (git push origin main --force), or after git’s global options (git -C app push -f). A refspec with a leading plus (git push origin +main) also forces. The script below blocks all of these. It allows --force-with-lease, which refuses to overwrite commits you haven’t seen. If you want that blocked too, delete the --force-with-lease exception in your copy.

The hook script (Claude Code and Cursor)

Save as .claude/hooks/block-force-push.sh and/or .cursor/hooks/block-force-push.sh, then chmod +x it. Needs jq.

block-force-push.sh
#!/usr/bin/env bash
# block-force-push.sh: deny git push --force / -f / +refspec, allow --force-with-lease.
# Works as a Claude Code PreToolUse hook and as a Cursor beforeShellExecution hook.
# Needs jq.
input=$(cat)
event=$(jq -r '.hook_event_name // empty' <<<"$input")
cmd=$(jq -r '.tool_input.command // .command // empty' <<<"$input")

is_force_push() {
  printf '%s\n' "$1" | tr ';&|()`"'"'" '\n' | while IFS= read -r seg; do
    # a git invocation (any global options like -C dir or -c k=v) whose subcommand is push
    if grep -Eq '(^|[[:space:]/])git([[:space:]]+(-C|-c)[[:space:]]+[^[:space:]]+|[[:space:]]+--?[a-zA-Z-]+(=[^[:space:]]+)?)*[[:space:]]+push([[:space:]]|$)' <<<"$seg"; then
      # --force (but not --force-with-lease / --force-if-includes), -f inside a short-flag cluster, or a +refspec
      if grep -Eq '(^|[[:space:]])(--force([[:space:]]|$)|-[a-zA-Z]*f[a-zA-Z]*([[:space:]]|$)|\+[^[:space:]]+)' <<<"$seg"; then
        echo hit
        break
      fi
    fi
  done | grep -q hit
}

reason="Blocked: force push rewrites shared history. Push a new branch, or ask the user to run git push --force-with-lease themselves."

if is_force_push "$cmd"; then
  if [ "$event" = "beforeShellExecution" ]; then
    jq -n --arg r "$reason" '{permission: "deny", user_message: $r, agent_message: $r}'
  else
    jq -n --arg r "$reason" '{hookSpecificOutput: {hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: $r}}'
  fi
  exit 0
fi

[ "$event" = "beforeShellExecution" ] && echo '{"permission": "allow"}'
exit 0

Claude Code

.claude/settings.json
{
  "permissions": {
    "deny": [
      "Bash(git push --force *)",
      "Bash(git push -f *)"
    ]
  },
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-force-push.sh", "timeout": 10 }
        ]
      }
    ]
  }
}

The deny rules catch the common spellings at no cost. The hook catches the forms they miss. The permissions docs use git -C . push as their own example of a form a Bash(git push *) rule doesn’t match.

Cursor

.cursor/hooks.json
{
  "version": 1,
  "hooks": {
    "beforeShellExecution": [
      { "command": ".cursor/hooks/block-force-push.sh", "timeout": 10, "matcher": "push" }
    ]
  }
}

For beforeShellExecution, the matcher is a regex tested against the command, so the script only starts for commands containing “push”. The script answers {"permission": "deny", "user_message": ..., "agent_message": ...}, and the agent sees the reason.

Codex

~/.codex/rules/default.rules
prefix_rule(
    pattern = ["git", "push", ["-f", "--force"]],
    decision = "forbidden",
    justification = "Force push rewrites shared history. Push a new branch instead.",
    match = ["git push -f", "git push --force origin main"],
    not_match = ["git push origin feat", "git push --force-with-lease"],
)
prefix_rule(
    pattern = ["git", "push", "origin", ["-f", "--force"]],
    decision = "forbidden",
    justification = "Force push rewrites shared history.",
    match = ["git push origin --force main"],
)

Both rules return "decision":"forbidden" in codex execpolicy check. Prefix rules can’t see a flag at the end of a command, so git push origin feat --force isn’t matched. See the Codex rules guide for details.

The backstop: branch protection

Local guards protect against the agent on your machine. On the server, turn off force pushes for main and release branches in your Git host’s branch protection or rulesets. On GitHub that’s a branch protection rule or ruleset with force pushes blocked. Then a force push fails even if every local check is bypassed.

Test it

terminal
echo '{"hook_event_name":"beforeShellExecution","command":"git push origin main --force","cwd":"/repo"}' \
  | .cursor/hooks/block-force-push.sh
output
{
  "permission": "deny",
  "user_message": "Blocked: force push rewrites shared history. Push a new branch, or ask the user to run git push --force-with-lease themselves.",
  "agent_message": "Blocked: force push rewrites shared history. Push a new branch, or ask the user to run git push --force-with-lease themselves."
}

Limits