SuperAPI AI 工具集

git-guardrails-claude-code

智能体与模型

查看项目

更新于 10-16 00:20

技能介绍

用途:为 Claude Code 设置 Git 安全护栏,通过 PreToolUse 钩子在命令执行前拦截并阻止危险 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 赋予可执行权限。随后在对应 settings 文件中添加钩子配置:项目使用 "$CLAUDE_PROJECT_DIR"/.claude/hooks/block-dangerous-git.sh,全局使用 ~/.claude/hooks/block-dangerous-git.sh,matcher 为 Bash。若 settings 已存在,需合并到现有 hooks.PreToolUse 数组,不要覆盖其他设置。之后可询问用户是否增删拦截模式并编辑脚本。

验证:运行 echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>,应以退出码 2 结束并向 stderr 输出 BLOCKED 消息。

适用场景:希望在使用 Claude Code 时防止其执行破坏性 git 命令的开发者,可按项目或全局启用。

限制:资料未提供脚本内部实现细节、支持的命令完整清单、与其他钩子的兼容性说明及版本要求。