设置 Git Guardrails
设置一个 PreToolUse hook,在 Claude 执行之前拦截和阻止危险的 git 命令。
Sets up a PreToolUse hook that intercepts and blocks dangerous git commands before Claude executes them.
被阻止的命令 (What Gets Blocked)
git push(包括--force等所有变体)git push(all variants including--force)git reset --hardgit clean -f/git clean -fdgit branch -Dgit checkout ./git restore .
被阻止时,Claude 会看到一条消息,告知它没有权限访问这些命令。
When blocked, Claude sees a message telling it that it does not have authority to access these commands.
步骤 (Steps)
1. 询问范围
询问用户:仅安装到本项目(.claude/settings.json)还是所有项目(~/.claude/settings.json)?
Ask the user: install for this project only (
.claude/settings.json) or all projects (~/.claude/settings.json)?
2. 复制 hook 脚本
捆绑脚本位于:scripts/block-dangerous-git.sh
The bundled script is at: scripts/block-dangerous-git.sh
根据范围复制到目标位置:
Copy it to the target location based on scope:
- 项目级:
.claude/hooks/block-dangerous-git.shProject:
.claude/hooks/block-dangerous-git.sh - 全局:
~/.claude/hooks/block-dangerous-git.shGlobal:
~/.claude/hooks/block-dangerous-git.sh
使用 chmod +x 使其可执行。
Make it executable with
chmod +x.
3. 将 hook 添加到 settings
添加到相应的 settings 文件:
Add to the appropriate settings file:
项目级(.claude/settings.json):
Project (
.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
全局(~/.claude/settings.json):
Global (
~/.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
如果 settings 文件已存在,将 hook 合并到现有的 hooks.PreToolUse 数组中 —— 不要覆盖其他设置。
If the settings file already exists, merge the hook into existing
hooks.PreToolUsearray — don't overwrite other settings.
4. 询问是否需要自定义
询问用户是否想从阻止列表中添加或删除任何模式。相应地编辑复制的脚本。
Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly.
5. 验证
运行快速测试:
Run a quick test:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
应以代码 2 退出,并向 stderr 输出 BLOCKED 消息。
Should exit with code 2 and print a BLOCKED message to stderr.