用途:为 Claude Code 设置 Git 安全护栏,通过 PreToolUse 钩子在命令执行前拦截并阻止危险的 git 操作。
可阻止的命令包括:git push(含 --force 等所有变体)、git reset --hard、git clean -f / git clean -fd、git branch -D、git checkout . / git restore .。被阻止时,Claude 会收到提示,告知其无权访问这些命令。
安装步骤:
- 询问范围:仅当前项目(.claude/settings.json)还是所有项目(~/.claude/settings.json)。
- 复制钩子脚本:将 scripts/block-dangerous-git.sh 复制到 .claude/hooks/block-dangerous-git.sh(项目)或 ~/.claude/hooks/block-dangerous-git.sh(全局),并用 chmod +x 赋予可执行权限。
- 在设置文件中添加钩子:在 hooks.PreToolUse 数组中加入 matcher 为 Bash、type 为 command 的配置,command 分别使用 "$CLAUDE_PROJECT_DIR"/.claude/hooks/block-dangerous-git.sh(项目)或 ~/.claude/hooks/block-dangerous-git.sh(全局)。若设置文件已存在,应合并到现有 hooks.PreToolUse 数组中,不要覆盖其他设置。
- 询问自定义:可增删阻止列表中的模式,并相应编辑脚本。
- 验证:执行 echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>,应以退出码 2 结束并向 stderr 打印 BLOCKED 信息。
限制:资料未提供脚本内部匹配逻辑、绕过方式、支持的命令变体完整清单,以及除上述五项之外的更多危险命令覆盖情况。