branch-guard — never write on protected branches
Direct writes to main / master / trunk are the most common cause of "oh no I pushed straight to main". A hook (installed by this project's install.sh) enforces the rule at tool-call time; this skill teaches the recovery flow.
What the hook does
PreToolUse hook ~/.claude/hooks/branch-guard.sh:
- Runs before
Write/Edit/MultiEdit/NotebookEdit. - Runs
git rev-parse --abbrev-ref HEADin the tool's target directory. - If the current branch is
main/master/trunk(or configured as protected), exits 2 with a message. - If not in a git repo, exits 0 (no-op).
Bypass:
WW_ALLOW_MAIN_WRITE=1env var (temporary, for CI / setup scripts).- Writing to non-repo paths (
~/.claude/,/tmp/, etc.).
When this skill triggers
- The hook blocked a write. The tool result will include
exit code 2and a message like "branch-guard: refuse write on protected branch 'main'". - The user asks "am I on main?" / "let's branch" / "create a new branch".
- You're about to start a task that will write to a git repo and haven't verified the branch.
Recovery recipe
# 1. Confirm current state
git status
git rev-parse --abbrev-ref HEAD
# 2. Branch off. Name the branch by scope:
# feat/<short-scope> — a feature
# fix/<issue-or-symptom> — a bug fix
# chore/<what> — non-code (docs, deps, config)
git checkout -b feat/<scope>
# 3. Re-attempt the write. The hook now allows it.
Do NOT
- Set
WW_ALLOW_MAIN_WRITE=1in your shell rc permanently. That defeats the guard for interactive sessions. - Force-push after branching. The commit stays on the feature branch;
mainis untouched. - Use
git commit --amendonmainto "fix" the guard. If you got here, your prior commit is still onmain— reset it:git reset --soft HEAD~1(moves the change back to staged), branch off, commit again on the branch,git pushon the branch, PR normally.
Configuration
The hook reads WW_PROTECTED_BRANCHES env var (comma-separated, default main,master,trunk). Set it in your shell rc if your repo uses a different name.
# Example: also protect a release branch
export WW_PROTECTED_BRANCHES="main,master,trunk,release"