git-guardrails-claude-code
- 作者仓库星标 0
- 作者更新于 2026年8月24日 22:19
- 作者仓库 skills
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 --hardgit clean -f/git clean -fdgit branch -Dgit 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
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):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-git.sh"
}
]
}
]
}
}
Global (~/.claude/settings.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:
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
Should exit with code 2 and print a BLOCKED message to stderr.
- 流狐分类
- AI 智能
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 需简单配置
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- 未声明
- 底层运行要求
- 未声明
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- Shell 执行
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
作者没有在当前 SKILL.md 中定义固定输出样例。 git push (all variants including --force) git reset --hard git clean -f / git clean -fd
Steps
Ask the user: install for this project only (.claude/settings.json) or all projects (~/.claude/settings.json)?
The bundled script is at: scripts/block-dangerous-git.sh Copy it to the target location based on scope: Project: .claude/hooks/block-dangerous-git.sh
Add to the appropriate settings file: Project (.claude/settings.json): Global (~/.claude/settings.json):
Ask if user wants to add or remove any patterns from the blocked list. Edit the copied script accordingly.
# 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
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> What Gets Blocked → Steps → 1. Ask scope → 2. Copy the hook script → 3. Add hook to settings → 4. Ask about customization
要点 -> this project only · all projects · Project · Global
文件/命令 -> git push · --force · git reset --hard · git clean -f · git clean -fd · git branch -D · git checkout . · git restore .
内容 SHA-256 -> 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 安全网。