Skip to main content
hooksSource-backedReview first Safety Privacy

Git Branch Protection - Hooks

Prevents direct edits to protected branches like main or master, enforcing PR-based workflows.

by JSONbored·added 2025-09-19·
HarnessClaude Code
Trigger:PreToolUse
Review first review before installing

Open the source and read safety notes before installing.

Citation facts

Source-backed facts for citing this resource, derived directly from the registry — also available as plain text for AI assistants.

Source URLs
https://git-scm.com/docs/githooks, https://github.com/JSONbored/awesome-claude/blob/main/content/hooks/git-branch-protection.mdx
Safety notes
Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.
Privacy notes
Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.
Author
JSONbored
Claim status
unclaimed
Last verified
2025-09-19

Decision playbook

Review trust signals before you adopt

Signals are present but mixed. Use the checklist below to confirm the source and operational safety for your environment.

Compare context
Selected

0

Current score

78

Baseline

Delta

No baseline selected

No major trust-signal divergence detected in the current selection.

Source and provenance checks

Complete

Confirm ownership and provenance before trusting install instructions.

  • Source link availableRequired

    Open the canonical repository and verify ownership.

    Done
  • Source provenance statusRequired

    Marked as source-backed.

    Done
  • Metadata reviewed

    Registry metadata indicates a reviewed listing.

    Done

Safety and privacy checks

Complete

Validate risk disclosures before installation or API wiring.

  • Safety notes presentRequired

    Review the listed safety guidance before running commands.

    Done
  • Privacy notes presentRequired

    Review data handling notes before connecting accounts or secrets.

    Done
  • Trust level risk gateRequired

    Trust level does not block evaluation.

    Done

Package and install checks

Needs review

Check package metadata and artifact integrity signals.

  • Install payload available

    Install or copy payload is available for review.

    Done
  • Package verification flag

    No package verification flag provided.

    Pending
  • Checksum metadata

    No checksum provided for downloaded artifact.

    Pending

Compare-driven decision checks

Needs review

Use compare context to validate trade-offs before adoption.

  • Compare tray has multiple entries

    Add at least one more entry to compare trust differences.

    Pending
  • Baseline comparison available

    No baseline peer selected yet.

    Pending
  • Diverging trust signals identified

    No major trust-signal divergence found.

    Pending

Setup at a glance

CLI install

Copy-ready — paste the snippet to get started.

Install command

Provided

Config snippet

Provided

Copy snippet

Provided

Prerequisites

None

Platforms

1 listed

Difficulty

0/100

Adoption plan

Balanced adoption plan

Current risk score 16/100. Use staged verification before broader rollout.

Risk 16

Pre-adoption checks

Validate source and review signals before any execution.

  • Confirm source provenanceRequired

    Source URL/provenance metadata is present.

    Done
  • Confirm metadata review state

    Listing has review metadata.

    Done
  • Verify install payload

    Install/config payload exists and can be inspected.

    Done

Security checks

Confirm safety, privacy, and package integrity signals.

  • Review safety notesRequired

    Safety notes are present.

    Done
  • Review privacy notesRequired

    Privacy notes are present.

    Done
  • Verify package integrity metadata

    No package verification/checksum metadata.

    Pending

Rollout

Adopt in controlled steps based on the selected plan.

  • Run in isolated sandbox firstRequired

    Use a constrained sandbox and observe behavior across multiple tasks.

    Pending
  • Roll out graduallyRequired

    Roll out to a small cohort before wider usage.

    Pending
  • Set monitoring and fallback

    Define rollback path and monitor errors after adoption.

    Pending

Evidence readiness

Evidence readiness matrix · balanced

Required evidence gates are covered (5/6 signals complete).

Risk 15

Source provenance

Present

Source repository/provenance is listed.

Required in this preset

Metadata review

Present

Review metadata is present.

Required in this preset

Safety notes

Present

Safety notes are present.

Required in this preset

Privacy notes

Present

Privacy notes are present.

Optional in this preset

Package integrity

Missing

Package integrity metadata is missing.

Optional in this preset

Install payload

Present

Install payload is available.

Required in this preset

Required evidence gates are covered for this preset.

Decision timeline

Decision timeline · balanced

5/6 steps complete with no blocking gaps for this preset.

Risk 14

triage

Confirm source provenanceRequired

Source/provenance metadata is available.

Done

triage

Check metadata review statusRequired

Review metadata is available.

Done

verify

Review safety notesRequired

Safety notes are available.

Done

verify

Review privacy notes

Privacy notes are available.

Done

verify

Validate package integrity metadata

Package integrity metadata is missing.

Pending

rollout

Verify install payload and commandsRequired

Install payload is available.

Done

No required blockers for this timeline preset.

Safety & privacy surface

Safety & privacy surface

1 safety and 1 privacy notes across 2 risk areas. Review closely: credentials & tokens, permissions & scopes.

2 areas
  • SafetyPermissions & scopesRuns automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.
  • PrivacyCredentials & tokensReceives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.

Safety notes

  • Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.

Privacy notes

  • Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.

Schema details

Install type
cli
Reading time
1 min
Difficulty score
0
Troubleshooting
Yes
Breaking changes
No
Skill and platform metadata
Retrieval sources
https://git-scm.com/docs/githookshttps://code.claude.com/docs/en/hooks
Runtime and command metadata
Trigger
PreToolUse
Script language
bash
Script body
#!/usr/bin/env bash

# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
  # Not in a git repo, allow the operation
  exit 0
fi

# Get current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

if [ -z "$CURRENT_BRANCH" ]; then
  # Can't determine branch, allow operation with warning
  echo "⚠️ Warning: Unable to determine current Git branch" >&2
  exit 0
fi

echo "🔍 Checking branch protection for: $CURRENT_BRANCH" >&2

# Define protected branches (can be customized via environment variables)
PROTECTED_BRANCHES=()

# Default protected branches
DEFAULT_PROTECTED=("main" "master" "production" "prod" "release" "staging" "develop")

# Add custom protected branches from environment variable
if [ -n "$PROTECTED_BRANCHES_LIST" ]; then
  IFS=',' read -ra CUSTOM_PROTECTED <<< "$PROTECTED_BRANCHES_LIST"
  PROTECTED_BRANCHES+=("${CUSTOM_PROTECTED[@]}")
else
  PROTECTED_BRANCHES+=("${DEFAULT_PROTECTED[@]}")
fi

# Check if current branch is protected
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done

# Check for pattern-based protection (e.g., release/* branches)
PROTECTED_PATTERNS=("release/*" "hotfix/*" "support/*")
if [ -n "$PROTECTED_PATTERNS_LIST" ]; then
  IFS=',' read -ra CUSTOM_PATTERNS <<< "$PROTECTED_PATTERNS_LIST"
  PROTECTED_PATTERNS+=("${CUSTOM_PATTERNS[@]}")
fi

for pattern in "${PROTECTED_PATTERNS[@]}"; do
  if [[ "$CURRENT_BRANCH" == $pattern ]]; then
    IS_PROTECTED=true
    break
  fi
done

# Allow override in emergency situations
if [ "$FORCE_ALLOW_PROTECTED_EDIT" = "true" ]; then
  echo "⚠️ EMERGENCY OVERRIDE: Allowing edit on protected branch $CURRENT_BRANCH" >&2
  echo "💡 Remove FORCE_ALLOW_PROTECTED_EDIT=true when done" >&2
  exit 0
fi

# If branch is protected, prevent the operation
if [ "$IS_PROTECTED" = true ]; then
  echo "" >&2
  echo "🚫 BRANCH PROTECTION VIOLATION" >&2
  echo "=====================================" >&2
  echo "❌ Direct edits to '$CURRENT_BRANCH' branch are not allowed" >&2
  echo "" >&2
  echo "🔒 Protected branches: ${PROTECTED_BRANCHES[*]}" >&2
  echo "🔒 Protected patterns: ${PROTECTED_PATTERNS[*]}" >&2
  echo "" >&2
  echo "✅ Recommended workflow:" >&2
  echo "   1. Create a feature branch:" >&2
  echo "      git checkout -b feature/your-feature-name" >&2
  echo "" >&2
  echo "   2. Make your changes on the feature branch" >&2
  echo "" >&2
  echo "   3. Push and create a Pull Request:" >&2
  echo "      git push -u origin feature/your-feature-name" >&2
  echo "" >&2
  
  # Suggest specific branch names based on the file being edited
  if [ -n "$FILE_PATH" ]; then
    BASE_NAME=$(basename "$FILE_PATH" | cut -d. -f1)
    SUGGESTED_BRANCH="feature/update-${BASE_NAME}"
    echo "💡 Suggested branch name: $SUGGESTED_BRANCH" >&2
    echo "   Quick command: git checkout -b $SUGGESTED_BRANCH" >&2
    echo "" >&2
  fi
  
  # Show current branch status
  echo "📊 Current repository status:" >&2
  echo "   Current branch: $CURRENT_BRANCH" >&2
  
  # Show available branches
  AVAILABLE_BRANCHES=$(git branch | grep -v "\*" | head -5 | xargs)
  if [ -n "$AVAILABLE_BRANCHES" ]; then
    echo "   Available branches: $AVAILABLE_BRANCHES" >&2
  fi
  
  # Check if there are uncommitted changes
  if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
    echo "   ⚠️ You have uncommitted changes" >&2
    echo "   💡 Consider: git stash (to save changes temporarily)" >&2
  fi
  
  echo "" >&2
  echo "🆘 Emergency override (use with caution):" >&2
  echo "   FORCE_ALLOW_PROTECTED_EDIT=true [your command]" >&2
  echo "" >&2
  echo "📚 Branch protection helps maintain:" >&2
  echo "   • Code quality through peer review" >&2
  echo "   • Stable main/master branches" >&2
  echo "   • Proper CI/CD pipeline execution" >&2
  echo "   • Team collaboration standards" >&2
  echo "" >&2
  
  # Exit with error to prevent the tool from running
  exit 1
fi

# Branch is not protected, show informational message
echo "✅ Branch '$CURRENT_BRANCH' is not protected - operation allowed" >&2

# Show branch protection tips for unprotected branches
if [[ "$CURRENT_BRANCH" == feature/* ]] || [[ "$CURRENT_BRANCH" == bugfix/* ]] || [[ "$CURRENT_BRANCH" == hotfix/* ]]; then
  echo "💡 Working on feature branch - remember to create a PR when ready" >&2
fi

# Check if branch is ahead/behind remote
if git rev-parse --verify "origin/$CURRENT_BRANCH" > /dev/null 2>&1; then
  AHEAD=$(git rev-list --count "origin/$CURRENT_BRANCH"..HEAD 2>/dev/null || echo "0")
  BEHIND=$(git rev-list --count HEAD.."origin/$CURRENT_BRANCH" 2>/dev/null || echo "0")
  
  if [ "$AHEAD" -gt 0 ]; then
    echo "📤 Branch is $AHEAD commits ahead of origin" >&2
  fi
  
  if [ "$BEHIND" -gt 0 ]; then
    echo "📥 Branch is $BEHIND commits behind origin - consider pulling" >&2
  fi
fi

# All checks passed, allow the operation
exit 0
Full copyable content
{
  "hooks": {
    "preToolUse": {
      "script": "./.claude/hooks/git-branch-protection.sh",
      "matchers": [
        "edit",
        "write",
        "multiEdit"
      ]
    }
  }
}

About this resource

Features

  • Protection of critical branches (main, master, production, release, staging, develop) with default protected branch list and environment variable customization via PROTECTED_BRANCHES_LIST for project-specific protection
  • Configurable protected branch patterns including wildcard patterns (release/, hotfix/, support/*) with pattern matching support and environment variable customization via PROTECTED_PATTERNS_LIST for flexible branch protection rules
  • Clear error messages with actionable guidance including suggested branch names based on file names, feature branch creation commands (git checkout -b), pull request workflow instructions, and repository status information
  • Feature branch creation suggestions based on file names being edited with sanitized branch name generation removing special characters and providing quick command suggestions for branch creation
  • Integration with CI/CD workflow enforcement preventing direct commits that bypass CI/CD pipelines and ensuring all changes go through proper review and testing processes
  • Override capability for emergency changes with FORCE_ALLOW_PROTECTED_EDIT environment variable and safety warnings including reminders to remove override after emergency changes are complete
  • Branch naming convention validation with pattern matching for feature/, bugfix/, hotfix/* branches with informational messages and pull request reminders for proper workflow adherence
  • Repository safety and collaboration enforcement with detailed protection violation messages, workflow guidance, branch status information (ahead/behind remote), and uncommitted changes detection with stash suggestions

Use Cases

  • Enforcing pull request workflows in team environments automatically preventing direct commits to protected branches and ensuring all changes go through code review and approval processes
  • Preventing accidental direct commits to main branches protecting critical branches from accidental modifications and maintaining stable production code with proper change management
  • CI/CD pipeline protection and quality gates ensuring all changes go through CI/CD pipelines with proper testing and validation before merging to protected branches
  • Code review process enforcement requiring pull requests for all changes to protected branches and ensuring peer review and collaboration before code integration
  • Repository governance and compliance maintaining repository standards and compliance requirements with enforced branch protection and documented change workflows
  • Development workflow integration seamlessly integrating branch protection into development workflows without manual branch checking or separate protection tools

Installation

  1. Create hooks directory: mkdir -p .claude/hooks
  2. Create hook file: touch .claude/hooks/git-branch-protection.sh
  3. Make executable: chmod +x .claude/hooks/git-branch-protection.sh
  4. Add configuration from Hook Configuration section above to .claude/settings.json or ~/.claude/settings.json
  5. Alternative: Use the interactive /hooks command in Claude Code

Config paths

  • Local (not committed): .claude/settings.local.json
  • User settings (global): ~/.claude/settings.json
  • Project-wide (committed): .claude/settings.json

Requirements

  • Claude Code CLI installed
  • Project directory initialized
  • Git repository initialized
  • Bash shell available
  • Git command-line tool (git 2.0+ recommended for branch protection features)
  • Write access to .git/config or repository configuration for branch protection rules

Hook Configuration

{
  "hooks": {
    "preToolUse": {
      "script": "./.claude/hooks/git-branch-protection.sh",
      "matchers": ["edit", "write", "multiEdit"]
    }
  }
}

Hook Script

#!/usr/bin/env bash

# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
  # Not in a git repo, allow the operation
  exit 0
fi

# Get current branch
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

if [ -z "$CURRENT_BRANCH" ]; then
  # Can't determine branch, allow operation with warning
  echo "⚠️ Warning: Unable to determine current Git branch" >&2
  exit 0
fi

echo "🔍 Checking branch protection for: $CURRENT_BRANCH" >&2

# Define protected branches (can be customized via environment variables)
PROTECTED_BRANCHES=()

# Default protected branches
DEFAULT_PROTECTED=("main" "master" "production" "prod" "release" "staging" "develop")

# Add custom protected branches from environment variable
if [ -n "$PROTECTED_BRANCHES_LIST" ]; then
  IFS=',' read -ra CUSTOM_PROTECTED <<< "$PROTECTED_BRANCHES_LIST"
  PROTECTED_BRANCHES+=("${CUSTOM_PROTECTED[@]}")
else
  PROTECTED_BRANCHES+=("${DEFAULT_PROTECTED[@]}")
fi

# Check if current branch is protected
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done

# Check for pattern-based protection (e.g., release/* branches)
PROTECTED_PATTERNS=("release/*" "hotfix/*" "support/*")
if [ -n "$PROTECTED_PATTERNS_LIST" ]; then
  IFS=',' read -ra CUSTOM_PATTERNS <<< "$PROTECTED_PATTERNS_LIST"
  PROTECTED_PATTERNS+=("${CUSTOM_PATTERNS[@]}")
fi

for pattern in "${PROTECTED_PATTERNS[@]}"; do
  if [[ "$CURRENT_BRANCH" == $pattern ]]; then
    IS_PROTECTED=true
    break
  fi
done

# Allow override in emergency situations
if [ "$FORCE_ALLOW_PROTECTED_EDIT" = "true" ]; then
  echo "⚠️ EMERGENCY OVERRIDE: Allowing edit on protected branch $CURRENT_BRANCH" >&2
  echo "💡 Remove FORCE_ALLOW_PROTECTED_EDIT=true when done" >&2
  exit 0
fi

# If branch is protected, prevent the operation
if [ "$IS_PROTECTED" = true ]; then
  echo "" >&2
  echo "🚫 BRANCH PROTECTION VIOLATION" >&2
  echo "=====================================" >&2
  echo "❌ Direct edits to '$CURRENT_BRANCH' branch are not allowed" >&2
  echo "" >&2
  echo "🔒 Protected branches: ${PROTECTED_BRANCHES[*]}" >&2
  echo "🔒 Protected patterns: ${PROTECTED_PATTERNS[*]}" >&2
  echo "" >&2
  echo "✅ Recommended workflow:" >&2
  echo "   1. Create a feature branch:" >&2
  echo "      git checkout -b feature/your-feature-name" >&2
  echo "" >&2
  echo "   2. Make your changes on the feature branch" >&2
  echo "" >&2
  echo "   3. Push and create a Pull Request:" >&2
  echo "      git push -u origin feature/your-feature-name" >&2
  echo "" >&2

  # Suggest specific branch names based on the file being edited
  if [ -n "$FILE_PATH" ]; then
    BASE_NAME=$(basename "$FILE_PATH" | cut -d. -f1)
    SUGGESTED_BRANCH="feature/update-${BASE_NAME}"
    echo "💡 Suggested branch name: $SUGGESTED_BRANCH" >&2
    echo "   Quick command: git checkout -b $SUGGESTED_BRANCH" >&2
    echo "" >&2
  fi

  # Show current branch status
  echo "📊 Current repository status:" >&2
  echo "   Current branch: $CURRENT_BRANCH" >&2

  # Show available branches
  AVAILABLE_BRANCHES=$(git branch | grep -v "\*" | head -5 | xargs)
  if [ -n "$AVAILABLE_BRANCHES" ]; then
    echo "   Available branches: $AVAILABLE_BRANCHES" >&2
  fi

  # Check if there are uncommitted changes
  if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
    echo "   ⚠️ You have uncommitted changes" >&2
    echo "   💡 Consider: git stash (to save changes temporarily)" >&2
  fi

  echo "" >&2
  echo "🆘 Emergency override (use with caution):" >&2
  echo "   FORCE_ALLOW_PROTECTED_EDIT=true [your command]" >&2
  echo "" >&2
  echo "📚 Branch protection helps maintain:" >&2
  echo "   • Code quality through peer review" >&2
  echo "   • Stable main/master branches" >&2
  echo "   • Proper CI/CD pipeline execution" >&2
  echo "   • Team collaboration standards" >&2
  echo "" >&2

  # Exit with error to prevent the tool from running
  exit 1
fi

# Branch is not protected, show informational message
echo "✅ Branch '$CURRENT_BRANCH' is not protected - operation allowed" >&2

# Show branch protection tips for unprotected branches
if [[ "$CURRENT_BRANCH" == feature/* ]] || [[ "$CURRENT_BRANCH" == bugfix/* ]] || [[ "$CURRENT_BRANCH" == hotfix/* ]]; then
  echo "💡 Working on feature branch - remember to create a PR when ready" >&2
fi

# Check if branch is ahead/behind remote
if git rev-parse --verify "origin/$CURRENT_BRANCH" > /dev/null 2>&1; then
  AHEAD=$(git rev-list --count "origin/$CURRENT_BRANCH"..HEAD 2>/dev/null || echo "0")
  BEHIND=$(git rev-list --count HEAD.."origin/$CURRENT_BRANCH" 2>/dev/null || echo "0")

  if [ "$AHEAD" -gt 0 ]; then
    echo "📤 Branch is $AHEAD commits ahead of origin" >&2
  fi

  if [ "$BEHIND" -gt 0 ]; then
    echo "📥 Branch is $BEHIND commits behind origin - consider pulling" >&2
  fi
fi

# All checks passed, allow the operation
exit 0

Examples

Git Branch Protection Hook Script

Complete hook script that prevents direct edits to protected branches

#!/usr/bin/env bash
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if ! git rev-parse --git-dir > /dev/null 2>&1; then
  exit 0
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -z "$CURRENT_BRANCH" ]; then
  exit 0
fi
PROTECTED_BRANCHES=("main" "master" "production" "prod" "release" "staging" "develop")
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done
if [ "$FORCE_ALLOW_PROTECTED_EDIT" = "true" ]; then
  echo "⚠️ EMERGENCY OVERRIDE: Allowing edit on protected branch $CURRENT_BRANCH" >&2
  exit 0
fi
if [ "$IS_PROTECTED" = true ]; then
  echo "🚫 BRANCH PROTECTION VIOLATION" >&2
  echo "❌ Direct edits to '$CURRENT_BRANCH' branch are not allowed" >&2
  echo "✅ Recommended workflow:" >&2
  echo "   1. Create a feature branch: git checkout -b feature/your-feature-name" >&2
  echo "   2. Make your changes on the feature branch" >&2
  echo "   3. Push and create a Pull Request" >&2
  exit 1
fi
exit 0

Hook Configuration

Complete hook configuration for .claude/settings.json to enable branch protection

{
  "hooks": {
    "preToolUse": {
      "script": "./.claude/hooks/git-branch-protection.sh",
      "matchers": ["edit", "write", "multiEdit"]
    }
  }
}

Configurable Protected Branches and Patterns

Enhanced hook script for configurable protected branches and pattern matching

#!/usr/bin/env bash
INPUT=$(cat)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
PROTECTED_BRANCHES=()
DEFAULT_PROTECTED=("main" "master" "production" "prod" "release" "staging" "develop")
if [ -n "$PROTECTED_BRANCHES_LIST" ]; then
  IFS=',' read -ra CUSTOM_PROTECTED <<< "$PROTECTED_BRANCHES_LIST"
  PROTECTED_BRANCHES+=("${CUSTOM_PROTECTED[@]}")
else
  PROTECTED_BRANCHES+=("${DEFAULT_PROTECTED[@]}")
fi
PROTECTED_PATTERNS=("release/*" "hotfix/*" "support/*")
if [ -n "$PROTECTED_PATTERNS_LIST" ]; then
  IFS=',' read -ra CUSTOM_PATTERNS <<< "$PROTECTED_PATTERNS_LIST"
  PROTECTED_PATTERNS+=("${CUSTOM_PATTERNS[@]}")
fi
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done
for pattern in "${PROTECTED_PATTERNS[@]}"; do
  if [[ "$CURRENT_BRANCH" == $pattern ]]; then
    IS_PROTECTED=true
    break
  fi
done
if [ "$IS_PROTECTED" = true ] && [ "$FORCE_ALLOW_PROTECTED_EDIT" != "true" ]; then
  echo "🚫 BRANCH PROTECTION: Direct edits to '$CURRENT_BRANCH' are not allowed" >&2
  exit 1
fi
exit 0

Feature Branch Name Suggestions

Enhanced hook script for feature branch name suggestions based on file names

#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
PROTECTED_BRANCHES=("main" "master" "production")
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done
if [ "$IS_PROTECTED" = true ] && [ "$FORCE_ALLOW_PROTECTED_EDIT" != "true" ]; then
  if [ -n "$FILE_PATH" ]; then
    BASE_NAME=$(basename "$FILE_PATH" | cut -d. -f1)
    SUGGESTED_BRANCH=$(echo "$BASE_NAME" | tr ' /' '-' | tr -cd 'a-zA-Z0-9-')
    echo "💡 Suggested branch name: feature/update-$SUGGESTED_BRANCH" >&2
    echo "   Quick command: git checkout -b feature/update-$SUGGESTED_BRANCH" >&2
  fi
  exit 1
fi
exit 0

Branch Status and Detached HEAD Handling

Enhanced hook script for branch status information and detached HEAD state handling

#!/usr/bin/env bash
INPUT=$(cat)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$CURRENT_BRANCH" = "HEAD" ]; then
  echo "⚠️ Detached HEAD state detected - allowing operation" >&2
  exit 0
fi
PROTECTED_BRANCHES=("main" "master" "production")
IS_PROTECTED=false
for protected_branch in "${PROTECTED_BRANCHES[@]}"; do
  if [[ "$CURRENT_BRANCH" == "$protected_branch" ]]; then
    IS_PROTECTED=true
    break
  fi
done
if [ "$IS_PROTECTED" = true ] && [ "$FORCE_ALLOW_PROTECTED_EDIT" != "true" ]; then
  if [ -n "$(git status --porcelain 2>/dev/null)" ]; then
    echo "⚠️ You have uncommitted changes" >&2
    echo "💡 Consider: git stash (to save changes temporarily)" >&2
  fi
  AHEAD=$(git rev-list --count "origin/$CURRENT_BRANCH"..HEAD 2>/dev/null || echo "0")
  BEHIND=$(git rev-list --count HEAD.."origin/$CURRENT_BRANCH" 2>/dev/null || echo "0")
  if [ "$AHEAD" -gt 0 ]; then
    echo "📤 Branch is $AHEAD commits ahead of origin" >&2
  fi
  if [ "$BEHIND" -gt 0 ]; then
    echo "📥 Branch is $BEHIND commits behind origin - consider pulling" >&2
  fi
  exit 1
fi
exit 0

Troubleshooting

Hook blocks edits even when FORCE_ALLOW_PROTECTED_EDIT set to true

Environment variable not exported to hook subprocess. Use: 'export FORCE_ALLOW_PROTECTED_EDIT=true' before command. Or add to .bashrc/.zshrc for session-wide availability. Verify: 'env | grep FORCE_ALLOW_PROTECTED_EDIT'. Check environment variable is set in correct shell context.

Protected branch pattern matching fails for release/* branches

Bash pattern requires proper glob: use 'case "$CURRENT_BRANCH" in release/*) IS_PROTECTED=true;;' instead of [[ match. Or use regex: '[[ "$CURRENT_BRANCH" =~ ^release/ ]]'. Verify pattern syntax matches bash glob patterns. Test pattern matching with sample branch names.

Custom protected branches from PROTECTED_BRANCHES_LIST ignored

CSV parsing expects exact format. Ensure no spaces: 'main,staging,prod' not 'main, staging, prod'. Or handle: 'IFS=',' read -ra CUSTOM | tr -d ' '' stripping whitespace. Verify environment variable is exported. Check CSV format matches expected pattern.

Branch detection fails in detached HEAD state showing empty branch

git rev-parse --abbrev-ref returns 'HEAD' when detached. Add check: 'if [ "$CURRENT_BRANCH" = "HEAD" ]; then echo "Detached HEAD allowed"; exit 0; fi' before protection logic. Verify detached HEAD state handling. Consider allowing operations in detached HEAD state or requiring branch checkout.

Suggested branch names contain special characters breaking git commands

Filename with spaces/slashes creates invalid branch names. Sanitize: 'SUGGESTED=$(echo "$BASE_NAME" | tr ' /' '-' | tr -cd 'a-zA-Z0-9-')' removing unsafe characters before suggestion. Verify branch name sanitization. Test with various file names containing special characters.

Hook doesn't protect branches in subdirectories or nested repositories

Hook checks current directory Git repository. Verify you're in the correct repository root. Check for nested Git repositories: 'find . -name .git -type d'. Use git rev-parse --show-toplevel to get repository root. Consider adding repository root detection.

Pattern matching doesn't work for complex branch naming conventions

Bash glob patterns have limitations. Use case statements for complex patterns: 'case "$CURRENT_BRANCH" in release/v*) IS_PROTECTED=true;; esac'. Or use regex matching: '[[ "$CURRENT_BRANCH" =~ ^release/v[0-9]+ ]]'. Test pattern matching with actual branch names. Consider using extended glob patterns: 'shopt -s extglob'.

Emergency override doesn't work in CI/CD environments

CI/CD environments may not have environment variables available. Use Git config for override: 'git config hooks.allow-protected-edit true'. Or use file-based override: 'touch .claude/allow-protected-edit'. Verify CI/CD environment variable availability. Check hook execution context in CI/CD pipelines.

Source citations

Add this badge to your README

Show that Git Branch Protection - Hooks is listed on HeyClaude. Paste this Markdown into your README — it renders the badge and links back to this page.

Listed on HeyClaude
[![Listed on HeyClaude](https://heyclau.de/badge/hooks/git-branch-protection.svg)](https://heyclau.de/entry/hooks/git-branch-protection)

How it compares

Git Branch Protection - Hooks side by side with 3 alternatives on trust, install, platform support, and disclosed safety notes — all from reviewed registry metadata.

Field

Prevents direct edits to protected branches like main or master, enforcing PR-based workflows.

Open dossier

Automatically creates timestamped backups of files before modification to prevent data loss. This hook runs before file editing operations (Edit, Write, Multiedit) and creates versioned backups in a centralized .backups directory with ISO 8601-compliant timestamps including nanoseconds for collision prevention.

Open dossier

A Stop hook that syncs your changed files to cloud storage when a Claude Code session ends, using rclone to push to S3, Google Cloud Storage, or any of its 70+ supported backends.

Open dossier

Automated accessibility testing and compliance checking for web applications following WCAG 2.1 and WCAG 2.2 guidelines. This hook automatically runs accessibility scans on HTML files after they are written or edited, using axe-core for comprehensive WCAG compliance testing.

Open dossier
Next steps
Trust
Review statusReviewedMaintainer reviewedReviewedMaintainer reviewedReviewedMaintainer reviewedReviewedMaintainer reviewed
Package trustPackage not verifiedPackage not verifiedPackage not verifiedPackage not verified
Source provenanceSource-backedSource-backedSource-backedSource-backed
Submitter
Install riskReview firstReview firstReview firstReview first
Notes Safety Privacy Safety Privacy Safety Privacy Safety Privacy
BrandAWS logoAWS
Categoryhookshookshookshooks
Sourcesource-backedsource-backedsource-backedsource-backed
AuthorJSONboredJSONboredJSONboredJSONbored
Added2025-09-192025-09-192025-09-192025-09-19
Platforms
Claude Code
Claude Code
Claude Code
Claude Code
Source repo
Safety notesRuns automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.Runs automatically at session end and can create compressed archives of modified git files. Uploads backups through AWS CLI, Google Cloud SDK, or rclone when those tools and bucket variables are configured. Writes a temporary archive under /tmp when using the rclone fallback and removes it after the copy attempt.Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.
Privacy notesReceives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.Sends modified file contents to the configured cloud storage destination. Uses locally configured cloud credentials and bucket environment variables but does not define or manage them. Backup archives may include source code, docs, generated files, and any unignored local changes listed by git diff.Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.
Prerequisites— none listed— none listed— none listed— none listed
Install
mkdir -p .claude/hooks && touch .claude/hooks/git-branch-protection.sh && chmod +x .claude/hooks/git-branch-protection.sh
mkdir -p .claude/hooks && touch .claude/hooks/auto-save-backup.sh && chmod +x .claude/hooks/auto-save-backup.sh
mkdir -p .claude/hooks && touch .claude/hooks/cloud-backup-on-session-stop.sh && chmod +x .claude/hooks/cloud-backup-on-session-stop.sh
mkdir -p .claude/hooks && touch .claude/hooks/accessibility-checker.sh && chmod +x .claude/hooks/accessibility-checker.sh
Config
{
  "hooks": {
    "preToolUse": {
      "script": "./.claude/hooks/git-branch-protection.sh",
      "matchers": [
        "edit",
        "write",
        "multiEdit"
      ]
    }
  }
}
{
  "hooks": {
    "preToolUse": {
      "script": "./.claude/hooks/auto-save-backup.sh",
      "matchers": [
        "edit",
        "write",
        "multiedit"
      ]
    }
  }
}
{
  "hooks": {
    "stop": {
      "script": "./.claude/hooks/cloud-backup-on-session-stop.sh"
    }
  }
}
{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/accessibility-checker.sh",
      "matchers": [
        "write",
        "edit"
      ]
    }
  }
}
Citations
ClaimUnclaimedUnclaimedUnclaimedUnclaimed
Open 4 picks in the interactive comparison tool

Related guides

Signals

Loading live community signals…

More like this, weekly

A short, calm digest of reviewed Claude resources. Unsubscribe any time.