Skip to content
Git Guardrails for Claude Code

Git Guardrails for Claude Code Skill

Matt Pocock's skill that installs a Claude Code PreToolUse hook blocking destructive git commands such as push, reset --hard, clean -f and branch -D before the agent can run them.

GitHub Docs

Install

Install with the skills CLI

One command for Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf and dozens of other agents. Needs Node.js; the skill is fetched straight from mattpocock/skills.

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

Run it in a terminal. The CLI finds the agents installed on your machine, asks which ones to add the skill to, and whether to install it into the folder you ran it from or for all your projects.

Other ways to install

Copy the whole skill folder — SKILL.md and any scripts or references beside it — into ~/.claude/skills/git-guardrails-claude-code/, where Claude Code picks it up in every project. Only git is needed.

tmp=$(mktemp -d) && \
  git clone --depth 1 --branch main https://github.com/mattpocock/skills.git "$tmp" && \
  mkdir -p ~/.claude/skills && \
  cp -R "$tmp"/skills/misc/git-guardrails-claude-code ~/.claude/skills/git-guardrails-claude-code

No scripts or references next to it? Saving the SKILL.md alone as ~/.claude/skills/git-guardrails-claude-code/SKILL.md works too.

Using another agent? Its personal skills folder
Codex
~/.agents/skills/
Cursor
~/.cursor/skills/
GitHub Copilot
~/.copilot/skills/
Gemini CLI
~/.gemini/skills/
Windsurf
~/.codeium/windsurf/skills/
OpenCode
~/.config/opencode/skills/

About

  • Blocks git push (incl. --force), reset --hard, clean -f, branch -D, checkout . and restore .
  • Implemented as a Claude Code PreToolUse hook on the Bash tool
  • Project-level or global install, merged into existing settings
  • Block list customisable after install
  • Built-in verification: blocked commands exit with code 2

Git Guardrails sets up a safety net for Claude Code: a bundled shell script registered as a PreToolUse hook on the Bash tool, which intercepts git commands and refuses the dangerous ones before they execute. Out of the box it blocks every variant of git push (including --force), git reset --hard, git clean -f and -fd, git branch -D, and git checkout . / git restore .. When a command is blocked, Claude is told it does not have authority to run it.

The agent walks you through the setup. It asks whether to install for the current project (.claude/settings.json, script in .claude/hooks/) or for all projects (~/.claude/settings.json, script in ~/.claude/hooks/), copies the script and makes it executable, and merges the hook into any existing hooks.PreToolUse array rather than overwriting your settings. It then offers to add or remove patterns from the block list and verifies the install by piping a sample git push origin main call into the script, which should exit with code 2 and print a BLOCKED message.

This one is specific to Claude Code's hook system; other agents need their own equivalent.

SKILL.md

The instructions your agent loads, as published by Matt Pocock.

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):

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

Install

Skill: Caveman Commit

by Julius Brussee

The Caveman family's commit-message skill: terse Conventional Commits that state intent, an imperative subject of 50 to 72 characters, a body only when the why is not obvious, and no AI attribution.

Skill: Wayfinder

by Matt Pocock

New

Matt Pocock's planning skill for work too big for one agent session: it charts a shared map of decision tickets on your issue tracker and resolves them one per session until the route to the goal is clear.

Skill: To Tickets

by Matt Pocock

New

Matt Pocock's skill for breaking a plan, spec or conversation into tracer-bullet tickets, each a thin end-to-end slice with explicit blocking edges, published to your issue tracker or as local files.

Skill: To Spec

by Matt Pocock

New

Matt Pocock's skill that turns what you have already discussed with your agent into a written spec and files it on the project's issue tracker, without a second round of interview questions.

Browse all skills →