Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Agent Skill

Git guardrails for Claude Code

Matt Pocock's skill that sets up a Claude Code hook to block destructive Git commands such as push, reset --hard, clean and branch -D before they run.

name: git-guardrails-claude-codeBy Matt PocockChecked
View source on GitHub
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark

Install

Commands are pinned to commit abeeaf9, the version we read, wherever the install method supports it.

npx skills

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

Copies the skill into .claude/skills/ in this project, pinned to the reviewed commit.

Plugin marketplace

Claude Code prompt
/plugin install mattpocock-skills@claude-plugins-official

Run inside Claude Code. Installs the whole mattpocock-skills plugin from its default branch, not the pinned commit; the official marketplace is available by default.

What it does

A setup skill rather than a working-style skill. It asks whether to install at project or user scope, copies scripts/block-dangerous-git.sh into place, adds a PreToolUse hook to the matching Claude Code settings file, offers to customise the blocked list and verifies the hook works.

When to use it

Claude Code users who run long or unattended sessions and want a hard stop on git push --force, git reset --hard and similar commands. A hook is enforced by Claude Code itself, so it holds even when the model ignores an instruction.

It is also a good small example of a setup skill: the skill installs something (a script and a hook) and then gets out of the way, rather than shaping how the agent works.

When to skip it

Other agents: the hook format is Claude Code’s. And if you only need to block a couple of commands, Claude Code’s permission deny rules do it without a script.

Try it

Set up git guardrails for this project only. Block force pushes,
hard resets and branch deletion, then show me a blocked command to prove
the hook works.

What it can touch

Bundles scripts
Yes
Pre-approved tools
None declared
Needs
Claude Code; jq (used by the hook script)
Client-specific fields
None, spec fields only

claude.ai and the Claude API reject uploads that use fields outside the open spec or names containing "claude" or "anthropic", so this skill needs editing before you upload it there.

Its scripts run with your permissions, and it may have changed since the commit we read. How to check a skill.

Read the shell script; it is short. The hook only applies to Claude Code. The skill name contains “claude”, which claude.ai and the Skills API reserve, so it cannot be uploaded there.

At commit abeeaf9 the SKILL.md is 96 lines long. Alongside it:

  • agents/ (1 entry)
  • scripts/ (1 entry)

Alternatives

Claude Code’s own permission rules, described in our Agent Skills in Claude Code guide. Finishing a development branch handles merges and pushes deliberately at the end of a task.

SKILL.md

The file exactly as published at the reviewed commit, reproduced under its MIT licence.

skills/misc/git-guardrails-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.
  • Devcontainer setup (Trail of Bits)

    Writes a .devcontainer/ for your project with Claude Code, Python, Node, Rust or Go tooling and persistent volumes, so an agent can work inside a container.

    DevOps, cloud and observabilityBundles scriptsChecked at 149d7b2

  • Skill creator

    Anthropic's skill for writing new skills and improving old ones: interviews you, drafts SKILL.md, runs evals against a baseline and tunes the description.

    AnthropicBundles scriptsChecked at e4bb5d9

  • CLAUDE.md improver (Anthropic)

    Anthropic's Claude Code skill that finds every CLAUDE.md in a repo, grades each one A to F, shows a report, then edits only the files you approve.

    Claude Code onlyChecked at 30c0ac7