SuperAPI AI 工具集

git-guardrails

智能体与模型

查看项目

更新于 10-15 20:35

技能介绍

用途:Git Guardrails 用于防止 AI 代理执行破坏性 git 操作。它通过 Claude Code 的 PreToolUse 钩子,在命令执行前拦截并阻止危险命令;被阻止时 Claude 会收到无权限运行的提示,用户需自行手动执行。钩子只拦截 AI 代理运行的命令,不影响用户在终端手动执行任何 git 命令,也不修改 git 配置。

主要能力:拦截 git push 全部变体、git push --force 与 --force-with-lease、git reset --hard、git clean -f 与 -fd、git branch -D、git checkout .、git restore .,以及在 main/master 上的 rebase。匹配使用正则,因此 git push 也能捕获带参数的推送。

使用方式:1)询问范围:仅当前项目(.claude/settings.json)还是全部项目(~/.claude/settings.json);2)将捆绑脚本 reference/block-dangerous-git.sh 复制到 .claude/hooks/block-dangerous-git.sh 或 ~/.claude/hooks/block-dangerous-git.sh,并执行 chmod +x <path-to-script>;3)在对应 settings.json 的 hooks.PreToolUse 数组中加入 matcher 为 Bash 的 command 钩子,命令分别为 "$CLAUDE_PROJECT_DIR"/.claude/hooks/block-dangerous-git.sh 或 ~/.claude/hooks/block-dangerous-git.sh;已有设置需合并,不要覆盖其他配置;4)可按需增删拦截模式,例如增加 git stash drop、git tag -d,或仅拦截 force 推送;5)验证:用 echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script> 测试,应退出码 2 并输出 BLOCKED;用 git status 测试应退出码 0。

限制与注意:需预先安装 jq(brew install jq 或 apt-get install jq);若缺少 jq,钩子会 fail open,放行所有命令,安装后务必确认 jq 可用。钩子接收 stdin 的 JSON,用 jq 提取 command 字段,匹配则退出码 2,否则退出码 0。其他功能细节暂未提供。