护栏克劳德代码
- 作者仓库星标 0
- 作者更新于 2026年8月24日 14:19
- 作者仓库 skills
- 领域
- 人工智能
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @mattpocock · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# 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 --hard`
- `git clean -f` / `git clean -fd`
- `git branch -D`
- `git 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](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`):
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
```
**Global** (`~/.claude/settings.json`):
```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 the 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:
```bash
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
```
Should exit with code 2 and print a BLOCKED message to stderr.
章节与文件索引
按说明文件中的顺序排列。
什么被阻止
1.询问范围
2.复制钩子脚本
3.设置中添加钩子
4.询问定制
文件/命令 -> git push · --force · git reset --hard · git clean -f · git clean -fd · git branch -D · git checkout . · git restore .
内容校验值 -> f8086a818b12
设计思路
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,按范围拷到:.claude/hooks/block-dangerous-git.sh~/.claude/hooks/block-dangerous-git.shchmod +x加可执行位。Step 3:注册 hook 项目级 settings 里:
全局 settings 把 command 换成
~/.claude/hooks/block-dangerous-git.sh。settings 已存在时合并进hooks.PreToolUse数组——别覆盖其它配置。Step 4:定制清单 问用户要不要加 / 减拦截模式,按需改 hook 脚本里的 patterns。
Step 5:验证
应该退出码 2、stderr 打 BLOCKED 信息。
适合谁
不适合
配套
careful(更广义的 bash 危险命令拦截)、freeze(编辑路径围栏)、unfreeze(解除围栏);三者叠加做多层 agent 安全网。