Skip to content
Agentmart
Submit a tool

git-guardrails-claude-code

设置 Claude Code 钩子,在危险 Git 命令(push、reset --hard、clean、branch -D 等)执行前将其拦截。当用户想要防止破坏性 Git 操作、添加 Git 安全钩子或在 Claude Code 中阻止 git push/reset 时使用。

Skill by devcxl

IndexedMIT★ 507
Write a review

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/devcxl-git-guardrails-claude-code

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

设置 Claude Code 钩子,在危险 Git 命令(push、reset --hard、clean、branch -D 等)执行前将其拦截。当用户想要防止破坏性 Git 操作、添加 Git 安全钩子或在 Claude Code 中阻止 git push/reset 时使用。

Use cases: agent-building, version-control

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found
Runs as
Shell scripts

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/git-guardrails-claude-code

What's inside

Skill name
git-guardrails-claude-code
Description
设置 Claude Code 钩子,在危险 Git 命令(push、reset --hard、clean、branch -D 等)执行前将其拦截。当用户想要防止破坏性 Git 操作、添加 Git 安全钩子或在 Claude Code 中阻止 git push/reset 时使用。
License
MIT
Instructions
92 lines in SKILL.md · view on GitHub

Before installing

  • Ships executable scripts

Files (3)

  • SKILL.md
  • openai.yaml · agents/
  • block-dangerous-git.sh · scripts/

SKILL.md (first lines)

# 设置 Git 护栏

设置一个 PreToolUse 钩子,在 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. 复制钩子脚本

打包的脚本位于:[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. 将钩子添加到设置文件

添加到相应的设置文件中:

**项目级**(`.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"
          }
        ]
      }
    ]
  }
}
```

如果设置文件已存在,将钩子合并到现有的 `hooks.PreToolUse` 数组中:不要覆盖其他设置。

### 4. 询问自定义

询问用户是否需要从阻止列表中添加或移除任何模式。根据需求编辑已复制的脚本。

### 5. 验证

运行一个快速测试:

```bash
echo '{"tool_input":{"command":"git push origin main"}}' | <path-to-script>
```

应退出并返回代码 2,并向 stderr 打印一条 BLOCKED 消息。

From devcxl/mattpocock-skills-zh, MIT.

More from devcxl/mattpocock-skills-zh

Compare and alternatives

    Featured in

    Signals

    Stars
    507
    Forks
    44
    Open issues
    1
    Last commit (this folder)
    Oct 8, 2026 (18 days ago)
    Last release
    Oct 20, 2026 (7 days ago)

    From GitHub (fetched Oct 24, 2026).

    Details

    License
    MIT
    First seen
    Oct 24, 2026
    Last checked
    Oct 24, 2026

    More from devcxl

    Cite this page

    git-guardrails-claude-code skill. Agent Mart. https://agentmart.dev/skills/devcxl-git-guardrails-claude-code (last updated 2026-10-24)