# Git 危险命令拦截 (git-guardrails-claude-code)

> 为 Claude Code 配置 PreToolUse hook，在执行前拦截 git push、reset --hard 等危险命令。

- 分类: [安全加固](https://www.myskills.tools/zh/categories/hardening/)
- 来源: 社区 · Matt Pocock
- 代码仓库: https://github.com/mattpocock/skills/tree/main/skills/misc/git-guardrails-claude-code (commit 3216582)
- SKILL.md: https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md
- 安装命令: `npx skills add mattpocock/skills --skill git-guardrails-claude-code`
- 许可证: MIT
- 兼容工具: Claude Code, Codex, Copilot, Gemini CLI
- 权限: read-repo ✓, write-files ✓, shell ✓, network ✗
- 状态: 扫描通过
- 规则扫描无法覆盖所有风险，安装前请阅读此技能的 SKILL.md，确认权限后再使用。
- ⚠ 会修改 Claude Code 的 hooks 设置（项目级或全局），之后 Claude 将无法执行 git push 等被拦截的命令。

## 核心规则

已有 settings 文件时，把 hook 合并进 hooks.PreToolUse 数组，不覆盖其他设置。

## 执行阶段

1. **确认安装范围** — 询问用户只为当前项目（.claude/settings.json）安装，还是为所有项目（~/.claude/settings.json）安装。
2. **复制 hook 脚本** — 将自带的 block-dangerous-git.sh 复制到 .claude/hooks/ 或 ~/.claude/hooks/，并用 chmod +x 设为可执行。
3. **写入 settings** — 在对应的 settings.json 中注册 matcher 为 Bash 的 PreToolUse hook，与已有配置合并。
4. **自定义规则** — 询问用户是否要增删拦截模式，并相应修改复制后的脚本。
5. **验证** — 向脚本输入一条模拟的 git push 请求，应以退出码 2 结束并在 stderr 输出 BLOCKED。

## 权限说明（据 SKILL.md 整理）

| 能力 | 是否需要 | 说明 |
|---|---|---|
| 文件写入 | 部分需要 | 只写入 hook 脚本（.claude/hooks/ 下）和对应的 settings.json。 |
| 命令执行 | 部分需要 | 只运行 chmod +x，以及用测试输入调用 hook 脚本进行验证。 |
| 网络外发 | 不需要 | 不需要网络访问。 |

## SKILL.md 头部信息（YAML）

```yaml
---
name: git-guardrails-claude-code
description: Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
---
```

首页: https://www.myskills.tools/zh/ · URL: https://www.myskills.tools/zh/skills/git-guardrails-claude-code/
