Skip to content
Skillpedia
Deploy & cloud (DevOps)310k · #120

git-guardrails-claude-code

What it does

It adds a safety check to Claude Code that stops certain git commands from running. It is for people who want to prevent accidental damage to their code repository.

When to use it

  • ✓When you want to stop the agent from pushing code by accident.
  • ✓When you want to block destructive commands such as git reset --hard.
  • ✓When you want these safety rules applied to one project or to all of your projects.

Skill link

https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/mattpocock/skills/blob/main/skills/misc/git-guardrails-claude-code/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Set up safety hooks so Claude Code cannot push to or reset my git repo by accident.”

5 new skills a week. 1 minute to read.

How it works

The agent copies a hook script into your project or home settings and links it in the settings file. When a blocked command is tried, the hook stops it and tells the agent it lacks authority to run it.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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

FAQ

Which git commands are blocked?

Pushes in all forms, git reset --hard, git clean -f, git branch -D, and checkout or restore applied to all files.

Can I choose where the rules apply?

Yes. The skill asks whether to install them for this project only or for all projects.

What happens when a command is blocked?

Claude sees a message saying it does not have authority to access that command.

Details

Original description by the author

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.

repo: mattpocock/skills

Popularity

GitHub stars
310k
Ranking position
#120
License
MIT
Updated
11/1/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills