git-guardrails-claude-code
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese设置 Git 护栏
Set Up Git Guardrails
设置一个 PreToolUse 钩子,在 Claude 执行危险 Git 命令之前拦截并阻止它们。
Set up a PreToolUse hook to intercept and block dangerous Git commands before Claude executes them.
会被阻止的命令
Blocked Commands
- (包括
git push在内的所有变体)--force git reset --hard- /
git clean -fgit clean -fd git branch -D- /
git checkout .git restore .
当命令被阻止时,Claude 会看到一条消息,告知它没有权限访问这些命令。
- (all variants including
git push)--force git reset --hard- /
git clean -fgit clean -fd git branch -D- /
git checkout .git restore .
When a command is blocked, Claude will see a message informing it that it doesn't have permission to access these commands.
步骤
Steps
1. 询问安装范围
1. Ask About Installation Scope
询问用户:是仅针对当前项目安装(),还是所有项目安装()?
.claude/settings.json~/.claude/settings.jsonAsk the user: Should this be installed for the current project only () or for all projects ()?
.claude/settings.json~/.claude/settings.json2. 复制钩子脚本
2. Copy the Hook Script
打包的脚本位于:scripts/block-dangerous-git.sh
根据安装范围将其复制到目标位置:
- 项目级:
.claude/hooks/block-dangerous-git.sh - 全局:
~/.claude/hooks/block-dangerous-git.sh
使用 使其可执行。
chmod +xThe packaged script is located at: scripts/block-dangerous-git.sh
Copy it to the target location based on the installation scope:
- Project-level:
.claude/hooks/block-dangerous-git.sh - Global:
~/.claude/hooks/block-dangerous-git.sh
Use to make it executable.
chmod +x3. 将钩子添加到设置文件
3. Add the Hook to the Settings File
添加到相应的设置文件中:
项目级():
.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}全局():
~/.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}如果设置文件已存在,将钩子合并到现有的 数组中——不要覆盖其他设置。
hooks.PreToolUseAdd to the corresponding settings file:
Project-level ():
.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}Global ():
~/.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}If the settings file already exists, merge the hook into the existing array — do not overwrite other settings.
hooks.PreToolUse4. 询问自定义
4. Ask for Customization
询问用户是否需要从阻止列表中添加或移除任何模式。根据需求编辑已复制的脚本。
Ask the user if they need to add or remove any patterns from the block list. Edit the copied script according to their needs.
5. 验证
5. Verification
运行一个快速测试:
bash
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>应退出并返回代码 2,并向 stderr 打印一条 BLOCKED 消息。
Run a quick test:
bash
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>It should exit with code 2 and print a BLOCKED message to stderr.