护栏克劳德代码

人工智能 社区

技能解读

设计思路

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 --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git 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 安全网。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
人工智能
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@mattpocock · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。