用途:在 Claude Code 中设置 Git 护栏,通过 PreToolUse hook 在 Claude 执行危险 git 命令前拦截并阻止,避免破坏性操作。
主要能力:阻止 git push(含 --force 等所有变体)、git reset --hard、git clean -f / -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 赋予执行权限。再把 hook 合并进对应 settings 文件的 hooks.PreToolUse 数组,matcher 为 Bash,command 指向脚本路径(项目级用 "$CLAUDE_PROJECT_DIR"/.claude/hooks/block-dangerous-git.sh,全局级用 ~/.claude/hooks/block-dangerous-git.sh);若文件已存在需合并,不要覆盖其他设置。随后可询问是否增删阻止模式并编辑脚本。验证:echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>,应以退出码 2 退出并在 stderr 输出 BLOCKED 消息。
适用场景:希望防止 Claude Code 误执行破坏性 git 操作、添加 git 安全 hook、阻止 git push/reset 的开发者。
限制:仅拦截所列命令模式,需手动安装配置;自定义模式需自行编辑脚本。其他细节暂未提供。