首页/AI 代码生成与质效/git-guardrails-claude-code
G

git-guardrails-claude-code

by @mattpocockv
4.8(20)

负责配置Claude Code钩子,以阻止潜在危险的Git命令(如强制推送),确保代码库的安全性和完整性。

git-workflowscode-reviewci/cdversion-controldeveloper-best-practicesGitHub
安装方式
npx skills add https://github.com/mattpocock/skills --skill git-guardrails-claude-code
compare_arrows

Before / After 效果对比

1
使用前

团队协作中,误操作Git命令(如强制推送、硬重置)可能导致代码丢失或项目混乱。缺乏有效防护机制,代码安全面临风险,影响开发效率。

使用后

运用此技能,能够设置Claude Code钩子,有效阻止危险的Git命令执行。确保代码库安全稳定,避免人为失误造成的损失,提升团队协作效率。

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

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

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量106.6K
评分4.8 / 5.0
版本
更新日期2026年7月9日
对比案例1 组

用户评分

4.8(20)
5
15%
4
45%
3
35%
2
5%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code
🔧OpenClaw
🔧OpenCode
🔧Codex
🔧Gemini CLI
🔧GitHub Copilot
🔧Amp
🔧Kimi CLI

时间线

创建2026年3月16日
最后更新2026年7月9日
🎁 Agent 知识卡片
调研问卷