prevent-direct-push — Claude Code hook: what it does, how to install
Blocks direct git pushes to protected main and develop branches in Claude Code. It enforces Git Flow by requiring feature branches and pull requests.
npx claude-code-templates@latest --hook git/prevent-direct-pushWhat prevent-direct-push does
The hook intercepts Bash tool executions in Claude Code before git pushes reach protected branches like main or develop. It halts attempts to push changes directly, routing workflow through feature branches and pull requests.
A PreToolUse event triggers before the terminal command executes. When matching the Bash tool, Claude Code runs prevent-direct-push.py from .claude/hooks/ using python3. That script checks the command parameters and stops execution if protected branches are targeted.
Without Python 3 installed in your path, the execution check fails.
What it helps with
- prevent direct git pushes to
mainanddevelop - enforce Git Flow workflow during Claude Code sessions
- require feature branches and pull requests for new commits
- stop unintended commits from landing directly on release branches
Example prompts for the agent
- Push my recent changes directly to the
mainbranch - Push the current commits to
origin develop - Push these bug fixes straight into the
mainbranch
How to install prevent-direct-push
1claude-code-templates
npx claude-code-templates@latest --hook git/prevent-direct-push2Manually: put the files in place
.claude/settings.jsonThe hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json
Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command
Questions about prevent-direct-push
How to install prevent-direct-push in Claude Code?
Run npx claude-code-templates@latest --hook git/prevent-direct-push and check the registered hook configuration in .claude/settings.json.
What does prevent-direct-push do?
It intercepts Bash commands within Claude Code and interrupts any git push targeting main or develop. This ensures code goes through pull requests.
What is required to run prevent-direct-push?
You need python3 installed in your environment to execute .claude/hooks/prevent-direct-push.py.
Source text
prevent-direct-push.json · MIT
{
"description": "Prevent direct pushes to protected branches (main, develop). Blocks git push commands targeting main or develop branches to enforce Git Flow workflow. Requires using feature/release/hotfix branches and pull requests instead of direct commits to protected branches.",
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/prevent-direct-push.py"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates