Skip to content

Instantly share code, notes, and snippets.

@camwest
Last active July 1, 2025 15:12
Show Gist options
  • Save camwest/7fb0f7bbedcdb205a62e0805bb2f7dce to your computer and use it in GitHub Desktop.
Save camwest/7fb0f7bbedcdb205a62e0805bb2f7dce 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 -E 'git\s+(commit|push.*\smain|push.*origin\s+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