Skip to content

Instantly share code, notes, and snippets.

@camwest
Created July 1, 2025 14:49
Show Gist options
  • Save camwest/cd0c52b2dda4fc0e5d5702a01d0616c9 to your computer and use it in GitHub Desktop.
Save camwest/cd0c52b2dda4fc0e5d5702a01d0616c9 to your computer and use it in GitHub Desktop.
Claude Code Hook: Branch Protection - Prevents direct commits to main branch
#!/bin/bash
# Claude Code Hook: Branch Protection
# Prevents direct commits/pushes to main branch
# Get the git command from Claude tool input
COMMAND=$(echo "$CLAUDE_TOOL_INPUT" | jq -r '.command')
# Check for dangerous git operations on main branch
if echo "$COMMAND" | grep -q 'git.*commit\|git.*push.*main'; then
# Block the tool call with structured JSON output
cat <<EOF
{
"decision": "block",
"reason": "🚫 Direct commits to main prohibited. Please use feature branches:\n git checkout -b feat/your-feature-name\n # make changes\n git add . && git commit -m 'feat: description'\n git push -u origin feat/your-feature-name\n gh pr create"
}
EOF
exit 0
fi
# Approve the tool call
echo '{"decision": "approve"}'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment