git-guardrails-claude-code
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.
Git Guardrails — Add Git Safety Hooks to Claude Code
Skill Overview
Git Guardrails uses Claude Code’s PreToolUse hooks to intercept destructive Git operations such as git push, git reset --hard, git clean -fd, git branch -D, and git checkout . before the commands actually execute, adding a layer of protection before AI can modify your code repository.
Use Cases
- Prevent AI from accidentally deleting uncommitted code: When Claude tries to use
git reset --hardorgit checkout .to “clean up” the working directory, the hook blocks it immediately. The changes you spent all afternoon writing but haven’t committed won’t be wiped out in one click. - Prevent force pushes from overwriting remote branches: In shared repositories,
git push --forceis one of the easiest ways to cause irreversible damage. Once the hook is installed, all variants ofpush(including--force) will be blocked, and Claude can only leave the changes locally for you to handle. - Add a unified layer of protection for Claude Code across your team: You can install it only in the current project or add it to the global configuration so Claude Code follows the same Git safety rules across all projects. This is especially suitable for teams with many newcomers or relatively permissive repository access.
Core Features
- Interception before execution, rather than after-the-fact recovery: The hook is attached to the
PreToolUseBashmatcher, so the command is blocked before it runs and exits with code 2. This is far more reliable than depending ongit reflogfor recovery afterward. - Five categories of high-risk commands covered by default:
git push(all variants, including--force),git reset --hard,git clean -f/git clean -fd,git branch -D, andgit checkout ./git restore .. - Installable per project or globally, with a customizable blocklist: Project-level configuration is written to
.claude/settings.json, while global configuration is written to~/.claude/settings.json. If configuration already exists, the hook is merged into the existinghooks.PreToolUsearray without overwriting your other settings. After installation, you can add or remove blocked patterns as needed.
Frequently Asked Questions
Which Git commands does this skill block?
By default, it blocks git push (all variants, including --force), git reset --hard, git clean -f and git clean -fd, git branch -D, and git checkout . / git restore .. This list is not exhaustive—commands such as git rebase and git stash drop are not included by default. If you want to block them, you can modify the patterns in the hook script.
What happens when a command is blocked?
The hook exits with code 2 and outputs a BLOCKED message to stderr. Claude will see a prompt telling it that it does not have permission to use these commands. As a result, the command will not execute, and Claude must use another approach to complete the task or hand the operation back to you for manual execution.
Will installing the hook affect Git commands I enter in my own terminal?
No. The hook only applies to Claude Code’s Bash tool calls, meaning it blocks only commands executed by Claude. Your own terminal sessions are completely unaffected, and you can still run push commands normally.
How can I verify that the hook is working?
You can verify it with a test command by piping {"tool_input":{"command":"git push origin main"}} to the hook script. Under normal circumstances, it will exit with code 2 and print a BLOCKED message to stderr. Seeing this result confirms that the script itself is working properly.