ai newsFr., 23. Okt.

PreToolUse-Hook blockiert riskante git-Befehle in Claude Code

AI-Agenten mit Shell-Zugriff versuchen irgendwann Befehle wie git push --force oder git reset --hard. Ein kleines PreToolUse-Hook-Skript für Claude Code lässt sich so einrichten, dass gefährliche git-Kommandos automatisch gestoppt werden, bevor sie ausgeführt werden.

dev.toFreitag 03:13 · 23.10.DE1 Min. Lesezeit
Bild: dev.to

An agent with shell access will eventually try git push --force or git reset --hard when it gets confused. You don't need to watch every command. A small PreToolUse hook can stop the dangerous ones before they run.

Save this as.claude/hooks/block-risky-git.sh and make it executable ( chmod +x ):

#!/usr/bin/env bash # Reads the tool call as JSON on stdin; exit code 2 blocks it. cmd = $( jq -r '.tool_input.command // ""' ) if echo " $cmd " | grep -Eq 'git (push.*(--force|-f)|reset --hard|clean -fd|branch -D|checkout -- \.)'; then echo "Blocked: ' $cmd ' can destroy work. Ask the user to run it themselves." > &2 exit 2 fi exit 0 Enter fullscreen mode Exit fullscreen mode Then register it in.claude/settings.json:

{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [{ "type": "command", "command": ".claude/hooks/block-risky-git.sh" }] } ] } } Enter fullscreen mode Exit fullscreen mode What happens When Claude tries one of those commands, the hook exits with code 2. The command never runs, and the message on stderr goes back to Claude, so it explains the situation to you instead of retrying blindly.

I ship this hook alongside a Bash allow-list,.env deny rules, a /review command, and a short CLAUDE.md in Vildanden Agent Config. The free sample is enough to try the pattern today.

Originaltitel: Block risky git commands in Claude Code with one PreToolUse hookQuelle: dev.to (dev.to). Original öffnen