Skill Learning

git-guardrails-claude-code

1. Bilingual SKILL.md

左右滑动可直接切换英文版与中文版;两种语言按段落自动对齐。英文中的蓝色虚线词语可点击查看解释。共标记 11 处。

入口 skill:git-guardrails-claude-code左右滑动切换语言 · 段落位置自动对齐
# Setup Git Guardrails
Sets up a PreToolUse hook that dangerous git commands before Claude executes them.
## What Gets Blocked
- `git push` ( `--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 to access these commands.
## Steps
### 1. Ask scope
Ask the user: install for **** (`.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 :
- **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, the existing `hooks.PreToolUse` array. .
### 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
:
```bash echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script> ```
Should and print a BLOCKED message to stderr.
# 设置 Git 护栏
设置一个 PreToolUse hook,在 Claude 执行危险 git 命令之前将其拦截并阻止。
## 会阻止哪些命令
- `git push`(所有变体,包括 `--force`) - `git reset --hard` - `git clean -f` / `git clean -fd` - `git branch -D` - `git checkout .` / `git restore .`
命令被阻止时,Claude 会看到一条消息,说明它无权使用这些命令。
## 步骤
### 1. 询问作用域
询问用户:仅为**当前项目**(`.claude/settings.json`)安装,还是为**所有项目**(`~/.claude/settings.json`)安装?
### 2. 复制 hook 脚本
随附脚本位于:[scripts/block-dangerous-git.sh](scripts/block-dangerous-git.sh)
根据作用域将它复制到目标位置:
- **项目级**:`.claude/hooks/block-dangerous-git.sh` - **全局级**:`~/.claude/hooks/block-dangerous-git.sh`
使用 `chmod +x` 赋予其可执行权限。
### 3. 将 hook 添加到设置
将以下内容添加到对应的设置文件:
**项目级**(`.claude/settings.json`):
```json { "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh" } ] } ] } } ```
**全局级**(`~/.claude/settings.json`):
```json { "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "~/.claude/hooks/block-dangerous-git.sh" } ] } ] } } ```
如果设置文件已经存在,请把这个 hook 合并进现有的 `hooks.PreToolUse` 数组。不要覆盖其他设置。
### 4. 询问是否需要自定义
询问用户是否希望在阻止列表中添加或移除任何模式。相应地编辑复制后的脚本。
### 5. 验证
运行一个快速测试:
```bash echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script> ```
预期以退出码 2 结束,并向 stderr 输出一条 BLOCKED 消息。

Entry SHA-256: 69b291d655626a01602b998a1836fcb93c058ede68dfafddfd71e87f17074764

2. Why This Skill Is Clever

一句话核心机制

把危险 Git 操作放进 Claude Code 的 PreToolUse 强制门:先让用户选择项目级或全局级作用域,再增量安装拦截脚本与 hook,并用一个预期失败的调用验证拒绝路径。

触发与调用边界

直接来源。 frontmatter 明确聚焦 Claude Code 与 Git 安全:Use when user wants to prevent destructive git operations。它不是通用 Git 教程,也不会替用户决定哪些命令应永远禁止。

直接来源。 此 skill 自己拥有完整步骤、决策、配置模板和验收结果,所以不是纯 wrapper。它链接的 scripts/block-dangerous-git.sh 是其中一个实施部件,不承担整个工作流;本报告只把 SKILL.md 作为所选原文。

解释。 这份报告学习的是外部来源所表达的设计,不执行来源中的复制、改权限或测试命令。外部仓库只作为不可信证据。

信息架构 / 执行顺序

  1. 先定义风险面。 列出 push、reset --hard、clean -f、branch -D 与工作树整体恢复等会被阻止的操作。
  2. 先问作用域。 用户选择当前项目还是所有项目,决定文件位置与影响半径。
  3. 复制执行部件。 将随附脚本放到对应 hooks 目录,并赋予执行权限。
  4. 增量接线。 把 command hook 合并到 hooks.PreToolUse,保留所有既有设置。
  5. 开放策略定制。 让用户决定是否增删阻止模式,而不是把默认列表伪装成永恒政策。
  6. 验证拒绝路径。 注入一条 git push origin main 工具输入,要求退出码 2 与 stderr 中的 BLOCKED 消息同时出现。

最巧妙的设计点

1. 把安全控制放在执行之前

before Claude executes them

直接来源。 这里使用的是 PreToolUse,不是事后日志、失败恢复或“请谨慎”提示。危险命令在到达 shell 前就被策略层截住。

解释。 这是能力安全里很强的结构:与其依赖模型每次都记得规则,不如让执行边界本身拒绝未授权能力。

2. 作用域成为第一项用户决策

this project only ... or all projects

直接来源。 项目级写入 .claude/settings.json,全局级写入 ~/.claude/settings.json。skill 不擅自选择影响范围。

解释。 把作用域放在复制文件之前,避免“技术步骤都做完后才发现装错位置”。这是良好的 prerequisite gate。

3. 默认政策可见,但不冒充用户政策

Ask if user wants to add or remove any patterns

直接来源。 默认列表足够具体,可立即提供安全价值;同时第四步明确把增删规则留给用户。

解释。 这在安全性和自治之间取得平衡:skill 提供保守起点,但承认不同仓库可能允许 push、却严格禁止 clean。

4. 配置修改采用合并不变量

Don't overwrite other settings.

直接来源。 若 settings 已存在,必须把 hook 合并进 hooks.PreToolUse 数组,而不是用示例 JSON 替换整个文件。

解释。 示例配置最常见的失败模式就是“复制即覆盖”。这句短护栏保护了未知的既有 hooks、permissions 与项目设置。

5. 验收检查机器结果与人类诊断

exit with code 2 and print a BLOCKED message to stderr

直接来源。 验证同时要求结构化退出码和可读错误消息,而不是只看终端中似乎出现了某段文字。

解释。 双重标准分别服务调用方与操作者:退出码让 harness 知道工具被拒绝,stderr 告诉 Claude 拒绝原因。

防失败机制与显式不变量

Trade-off、可移植性与假设

可迁移原则

  1. 把高风险能力做成执行前的强制门,而不是只在 prompt 里写禁止事项。
  2. 任何会扩大影响半径的安装,都先显式确认 project / user / system scope。
  3. 给用户一套安全默认值,但把组织策略的最终裁量留给用户。
  4. 修改共享配置时写下“保留未知字段”的合并不变量。
  5. 验收拒绝路径时,同时检查机器可读状态与人类可读诊断。

今日实践题

如果你的自动化只能在“阻止所有 git push”和“允许所有 git push”之间二选一,你会如何增加一个既保持 fail-closed、又允许受控发布的授权分支?请写出它的作用域、触发条件和可检查的验收结果。

Evidence note

Source & provenance
Repository path
skills/misc/git-guardrails-claude-code/SKILL.md
Generated
2026-08-28 12:03 Asia/Shanghai
Upstream commit
6654f6b60cd9d5be8b54c6fafe44346dabeb3b76
Source
View on GitHub