用途:设置一个 PreToolUse hook,在 Claude 执行前拦截并阻止危险的 git 命令。适用于希望阻止破坏性 git 操作、为 Claude Code 添加 git 安全 hook,或在其环境中阻止 git push/reset 的场景。
被阻止的命令:git push(含 --force 等所有变体)、git reset --hard、git clean -f / git clean -fd、git branch -D、git checkout . / git restore .。被阻止时,Claude 会收到一条消息,告知其没有权限访问这些命令。
使用方式(步骤):1. 询问范围:仅本项目(.claude/settings.json)还是所有项目(~/.claude/settings.json)。2. 复制 hook 脚本:捆绑脚本位于 scripts/block-dangerous-git.sh;按范围复制到 .claude/hooks/block-dangerous-git.sh(项目级)或 ~/.claude/hooks/block-dangerous-git.sh(全局),并用 chmod +x 使其可执行。3. 将 hook 添加到相应 settings 文件:项目级使用 "command": ""$CLAUDE_PROJECT_DIR"/.claude/hooks/block-dangerous-git.sh";全局使用 "command": "~/.claude/hooks/block-dangerous-git.sh";matcher 为 Bash。若 settings 已存在,将 hook 合并进现有 hooks.PreToolUse 数组,不要覆盖其他设置。4. 询问是否需要自定义:可从阻止列表增删模式并编辑复制的脚本。5. 验证:运行 echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>,应以代码 2 退出并向 stderr 输出 BLOCKED 消息。
限制:资料未提供更详细的兼容性或平台要求,相关内容暂未提供。