Created
March 27, 2026 04:06
-
-
Save pragmatist-nz/8cff6cee27cc5ff514b024c311a2f92d to your computer and use it in GitHub Desktop.
User global git hook to limit Claude Code git interaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| # | |
| # Global pre-commit hook: block Claude Code from committing on main worktrees. | |
| # Only gate Claude Code sessions. Human commits always pass through. | |
| [ -z "${CLAUDECODE:-}" ] && exit 0 | |
| # Paths where direct commits on main worktree are ALLOWED. | |
| # Everything else requires a worktree. | |
| ALLOWED_PREFIXES=( | |
| "/Users/tobias/code/mine/claude-code-skills/" | |
| ) | |
| MAIN_WORKTREE=$(git worktree list | head -1 | awk '{print $1}') | |
| CURRENT_DIR=$(git rev-parse --show-toplevel) | |
| # If already in a worktree (not main), always allow | |
| if [ "$CURRENT_DIR" != "$MAIN_WORKTREE" ]; then | |
| : # allow — we're in a worktree | |
| else | |
| # On main worktree — check if path is whitelisted | |
| ALLOWED=false | |
| for prefix in "${ALLOWED_PREFIXES[@]}"; do | |
| if [[ "$CURRENT_DIR" == "$prefix"* ]]; then | |
| ALLOWED=true | |
| break | |
| fi | |
| done | |
| if ! $ALLOWED; then | |
| echo "ERROR: Commits blocked on main worktree." | |
| echo " Repo: $CURRENT_DIR" | |
| echo " Either use a git worktree or allow this path" | |
| exit 1 | |
| fi | |
| fi | |
| # Chain to repo-local hook if it exists | |
| LOCAL_HOOK="$(git rev-parse --git-dir)/../.git/hooks/pre-commit" | |
| if [ -x "$LOCAL_HOOK" ]; then | |
| exec "$LOCAL_HOOK" "$@" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment