DEV Community

Vildanden
Vildanden

Posted on

Stop Claude Code from wiping your working tree with a PreToolUse hook

A shell allow-list can already keep Claude Code off rm -rf and force-push. It often still leaves git reset --hard, git checkout -- ., and git clean -fdx. Those commands throw away uncommitted work in a single shot. After a messy diff or a failed merge, the model will reach for them as a "clean start." A small PreToolUse hook can refuse those wipes so the agent has to ask, or leave the recovery to you.

The hook should name the blocked command and exit 2.

The hook

Save this as .claude/hooks/block_git_wipe.py and make it executable:

#!/usr/bin/env python3
import json
import re
import sys

try:
    event = json.load(sys.stdin)
except json.JSONDecodeError:
    sys.exit(0)

tool = event.get("tool_name") or event.get("tool") or ""
inp = event.get("tool_input") or event.get("input") or {}
if tool != "Bash":
    sys.exit(0)

command = str(inp.get("command") or "")

# git reset --hard / --keep / --merge (discards or rewrites the worktree)
if re.search(r"\bgit(?:\s+-C\s+\S+)*\s+reset\s+(-[^\s]*\s+)*--(hard|keep|merge)\b", command):
    print("PreToolUse: blocked git reset that wipes the working tree. Commit, stash, or ask before discarding work.")
    sys.exit(2)

# git checkout -- <paths> and git checkout .  (restores files, drops local edits)
if re.search(r"\bgit(?:\s+-C\s+\S+)*\s+checkout\s+(?:-f\s+)?--\s", command) or re.search(r"\bgit(?:\s+-C\s+\S+)*\s+checkout\s+(?:-f\s+)?\.(?:\s|$)", command):
    print("PreToolUse: blocked git checkout that discards local changes. Leave the restore to a human.")
    sys.exit(2)

# git restore --source / git restore . / git restore --worktree / --staged --worktree
if re.search(r"\bgit(?:\s+-C\s+\S+)*\s+restore\b[^\n;]*?(?:--worktree|\s\.(?:\s|$)|--source\b)", command):
    print("PreToolUse: blocked git restore that can wipe the working tree. Leave the restore to a human.")
    sys.exit(2)

# git clean -f / -fd / -fdx / -ffdx (deletes untracked files)
if re.search(r"\bgit(?:\s+-C\s+\S+)*\s+clean\s+(-[^\s]*\s+)*-[a-zA-Z]*f", command):
    print("PreToolUse: blocked git clean. Untracked files stay until you delete them yourself.")
    sys.exit(2)
Enter fullscreen mode Exit fullscreen mode

Wire it in .claude/settings.json under PreToolUse with command set to python3 .claude/hooks/block_git_wipe.py. If your settings support a matcher, limit it to Bash.

Why this beats a pasted prompt

"Don't reset --hard" in CLAUDE.md only works when the model remembers it mid-turn. After a conflict or a bad edit, the next move is often a wipe "just to get back to a known state." The PreToolUse hook runs before the shell, so that escape hatch is closed. If the tree actually needs to be restored, run git status yourself and choose the restore — do not let the agent throw the work away.

Tips

  • Keep the hook local, fast, and free of network calls.
  • Pair it with the force-push, --no-verify, and rm -rf PreToolUse guards so destructive git and filesystem wipes are all blocked.
  • If a docs or test fixture command mentions these flags inside a quoted string, tighten the regex rather than deleting the hook. The patterns above look for git as a command, not the words in a commit message.
  • git reset without --hard/--keep/--merge (for example git reset HEAD~1 mixed, or git reset --soft) is left alone so the agent can still unstage or move HEAD safely.

Want the rest of the setup?

I keep copy-ready Claude Code configs (CLAUDE.md templates, slash commands, hooks, permission rules) in Vildanden Agent Config. There's a free sample: https://justly-mia-429.rehost.page/

Top comments (1)

Collapse
 
sandscale profile image
Sandscale •

The hook naming the blocked command and exiting 2 is the right shape. Your regex already allows git -C, which is the usual miss. One gap that still gets through is a wipe inside a script the hook only sees as python3 reset.py or bash ./clean.sh. I'd keep the hook for the direct git forms, and leave mixed or --soft resets alone, as you did. If a fixture command mentions --hard inside quotes, tightening the pattern is better than deleting the hook.