跳转到内容

git-guardrails-claude-code

设置一个 PreToolUse hook,在 Claude 执行危险的 git 命令前拦截并阻止它们。

  • git push(包括 --force 在内的所有形式)
  • git reset --hard
  • git clean -f / git clean -fd
  • git branch -D
  • git checkout . / git restore .

命令被阻止时,Claude 会看到一条消息,说明它无权执行这些命令。

询问用户要只在当前项目(.claude/settings.json)安装,还是在所有项目(~/.claude/settings.json)安装。

打包的脚本位于:scripts/block-dangerous-git.sh

根据安装范围,将脚本复制到对应位置:

  • 项目:.claude/hooks/block-dangerous-git.sh
  • 全局:~/.claude/hooks/block-dangerous-git.sh

使用 chmod +x 赋予脚本可执行权限。

将 hook 添加到对应的设置文件:

项目(.claude/settings.json):

{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}

全局(~/.claude/settings.json):

{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}

如果设置文件已经存在,请把 hook 合并到现有的 hooks.PreToolUse 数组里。不要覆盖其他设置。

询问用户是否想在阻止列表中添加或移除某些模式。根据需要编辑复制后的脚本。

运行一个快速测试:

终端窗口
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>

预期退出码为 2,并在 stderr 打印一条 BLOCKED 消息。

读者可以点名用它,模型也可以自己调用它。它不在 Claude Code 插件里。

配置 Claude Code 的 PreToolUse hook,在危险的 git 命令执行前将其拦截并阻止。

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.
英文 SKILL.md
---
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.
---
# 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.

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号