I had two Claude Code sessions open in the same repo. One was fixing a header, the other was halfway through a refactor. The first one finished, ran git add -A && git commit, and committed half of the refactor with it. Nothing was broken yet, but the history was, and untangling it took longer than the header fix.
My CLAUDE.md already said "add files by name, never git add -A". Claude followed it most of the time. Most of the time is the problem: a rule in a prompt competes with everything else in the context, and on a long session it loses sometimes.
A hook runs every time
Claude Code can run a script before each tool call. If the script exits with code 2, the call is cancelled and whatever the script printed on stderr goes back to Claude as the reason. Claude reads it and changes approach.
Here is the whole hook:
#!/usr/bin/env python3
import json, re, sys
event = json.load(sys.stdin)
cmd = (event.get("tool_input") or {}).get("command", "")
if re.search(r"\bgit\b[^;&|\n]*\badd\b[^;&|\n]*(\s-A\b|\s--all\b|\s\.(\s|$))", cmd):
print("Seatbelt blocked this command: git add -A / git add . sweeps in .env files "
"and other sessions' work. "
"Add files by name.", file=sys.stderr)
sys.exit(2)
Save it as ~/.claude/hooks/no_add_all.py and register it in ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": "python3 ~/.claude/hooks/no_add_all.py" }]
}
]
}
}
Restart Claude Code. Next time it tries, you see this:
● Bash(git add -A && git commit -m "fix header spacing")
⎿ Seatbelt blocked this command: git add -A / git add . sweeps in
.env files and other sessions' work. Add files by name.
● Bash(git add src/Header.tsx && git commit -m "fix header spacing")
⎿ [main 4e1c9a2] fix header spacing
Three details that matter
- The message is for Claude, not for you. "Blocked" alone makes it retry the same thing or give up. "Add files by name" tells it what to do instead.
-
Test the pass cases too.
git add .gitignoremust go through. The\s\.(\s|$)part only matches a lone dot. - Exit code 2, not 1. Exit 1 shows an error to you and lets the command run.
The free version
I packaged this one with rm -rf ~ and force push as a plugin: seatbelt-lite, MIT, two commands to install.
Top comments (2)
The pattern catches the command in your opening story, but the same accident has a second route it lets through:
git commit -am "fix header"stages every modified tracked file without ever callinggit add.I ran the regex from the post against a few variants. Caught:
git add -A,git add .,git add -- .,git -C . add -A. Not caught:git commit -am,git commit -a -m,git add -u, andgit add -Av(bundled short flags). The-aand-upair is the dangerous one for two sessions in one tree, because they pick up the other session's half-finished edits to existing files, and that is most of what a refactor in progress looks like.So in the spirit of "Test the pass cases too.", the fail cases need the same list: commit with
-ain any flag cluster,add -u, andaddwithAinside a cluster.Did you look at a worktree per session as well? Two agents sharing one index is the root of it, and a hook can only block the commands someone thought to write down.
You were right on all four. Since v1.1.6 the plugin's guard blocks git commit -am, git commit -a -m, git add -u and bundled flags like git add -Av, and your variants are in the tests now. A commit message that mentions -a or -u still goes through, because the message is stripped before matching.
I haven't moved to one worktree per session yet. You're right that it removes the cause, and the hook only catches the commands someone wrote down. Thanks for running the variants.