Skip to main content
hook

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.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook git/prevent-direct-push
Open on GitHub

What 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 main and develop
  • 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 main branch
  • Push the current commits to origin develop
  • Push these bug fixes straight into the main branch

How to install prevent-direct-push

1claude-code-templates

npx claude-code-templates@latest --hook git/prevent-direct-push

2Manually: put the files in place

.claude/settings.json

The 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

Similar hooks

All hooks for Git and GitHub →

From the same set804

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor