Skip to main content
skill · Claude Code

git-guardrails-claude-code — Claude Code skill guide

This skill adds a Claude Code hook that stops risky Git commands before they run. Use it when an agent must not push or discard working-tree changes.

  • Author Matt Pocock
  • License MIT
  • GitHub ★ 302,251
  • Installs 499,069
  • Updated 2026-10-22
  • Source shown below
Open on GitHub ↗

What git-guardrails-claude-code does

git-guardrails-claude-code adds a PreToolUse hook for Bash. It intercepts git push, including variants with --force, before Claude Code runs it. The hook also blocks git reset --hard, git clean -f and git clean -fd, git branch -D, git checkout ., and git restore ..

The skill asks whether to install for the current project in .claude/settings.json or for all projects in ~/.claude/settings.json. It copies scripts/block-dangerous-git.sh into .claude/hooks or ~/.claude/hooks, makes it executable with chmod +x, then adds it to hooks.PreToolUse. Existing settings are kept and the hook is merged into the current array.

You can edit the copied script to change blocked patterns, then test it with a tool_input.command payload. Install this for teams that want human approval over destructive Git actions; skip it when the agent is intentionally allowed to push or clean the worktree.

What it helps with

  • block git push before an agent runs it
  • prevent git reset --hard
  • stop cleanup through git clean -f
  • deny branch deletion with git branch -D
  • attach Git command checks to PreToolUse

How to install git-guardrails-claude-code

Command from the source

/plugin marketplace add mattpocock/skills

Command from the source

/plugin install mattpocock-skills@mattpocock

Command from the source

npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-code

With the Skills CLI (Vercel)

npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-code

Manually

~/.claude/skills/git-guardrails-claude-code/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/git-guardrails-claude-code/

How to use it: example prompts

  • “Install git-guardrails-claude-code for this project only.”
  • “Set up a global hook that blocks `git push`.”
  • “Check that the hook rejects `git push origin main`.”

Source text

SKILL.md · MIT
---
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 · MIT · mattpocock/skills

Questions about git-guardrails-claude-code

How do I install git-guardrails-claude-code in Claude Code?

Add the skill with npx skills add mattpocock/skills --skill git-guardrails-claude-code -a claude-code. Then choose .claude/settings.json for one project or ~/.claude/settings.json for all projects.

What does git-guardrails-claude-code do?

It configures a PreToolUse hook for Bash that stops risky Git commands before Claude Code executes them. When a command is blocked, Claude receives a message that it lacks authority to use it.

Which Git commands does the hook block?

The hook blocks git push, git reset --hard, git clean -f variants, git branch -D, git checkout ., and git restore .. You can change the blocked patterns in the copied script.

Similar

See also

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor