git-guardrails-claude-code
- 信任分
- 88/100
- 兼容 Agent
- 1
- 领域
- AI 智能
- 兼容 Agent
- Claude Code
- 信任分
- 88 / 100 · 社区维护
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 安装命令数
- 1 条
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
想读作者英文原文? ↓ 滚到正文区切换 · 在 GitHub 查看 ↗
设计思路
git-guardrails-claude-code 是给 Claude Code 装一套「Pretool 拦截 hook」——危险 git 命令在执行前就被拦下来,agent 收到「你没有权限跑这个」的信号,避免 git push --force 这种破坏性动作被无意触发。和 gstack 的 careful 思路一致,但专注 git 命令、且通过 .claude/settings.json 的 PreToolUse hook 实现。
默认拦截清单
git push(包括--force各变种)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
被拦时 Claude 会看到一条「你没有权限访问这些命令」的消息。
安装步骤
Step 1:问范围
project 范围(.claude/settings.json)还是全局(~/.claude/settings.json)。
Step 2:复制 hook 脚本
bundled 脚本在 scripts/block-dangerous-git.sh,按范围拷到:
- Project:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
chmod +x 加可执行位。
Step 3:注册 hook 项目级 settings 里:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
全局 settings 把 command 换成 ~/.claude/hooks/block-dangerous-git.sh。settings 已存在时合并进 hooks.PreToolUse 数组——别覆盖其它配置。
Step 4:定制清单 问用户要不要加 / 减拦截模式,按需改 hook 脚本里的 patterns。
Step 5:验证
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
应该退出码 2、stderr 打 BLOCKED 信息。
适合谁
- 用 Claude Code 自动跑 git 操作的人
- 给团队 / 客户机器装 agent 工具时想加 git 安全网
- 历史上被 force-push 烧过、不想再来一次的工程师
何时不该用
- 你专门要做 force-push 类操作的工作流(rebase + push)——会一直被拦
- 不是 Claude Code 环境(hook 配置不通用)
配套
careful(更广义的 bash 危险命令拦截)、freeze(编辑路径围栏)、unfreeze(解除围栏);三者叠加做多层 agent 安全网。
Setup Git Guardrails
Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before Claude executes them.
What Gets Blocked
git push(all variants including--force)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
When blocked, Claude sees a message telling it that it does not have authority to access these commands.
Steps
1. Ask scope
Ask the user: install for this project only (.claude/settings.json) or all projects (~/.claude/settings.json)?
2. Copy the hook script
The bundled script is at: scripts/block-dangerous-git.sh
Copy it to the target location based on scope:
- Project:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
Make it executable with chmod +x.
3. Add hook to settings
Add to the appropriate settings file:
Project (.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
Global (~/.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
If the settings file already exists, merge the hook into existing hooks.PreToolUse array — don't overwrite other settings.
4. Ask about customization
Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly.
5. Verify
Run a quick test:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
Should exit with code 2 and print a BLOCKED message to stderr.