agentleFS
BK202503BK202503/open-claude-all/skills/branch-guard/SKILL.md

branch-guard

Skill7 starsChanged 4 months agoMITView on GitHub

Enforce "never write on main/master/trunk". Backing PreToolUse hook blocks Write/Edit/MultiEdit/NotebookEdit when HEAD is on a protected branch, aborting with exit 2. This skill documents the rule and provides the "branch off and continue" recipe Claude should reach for whenever the guard fires or whenever a task begins on a protected branch. Auto-trigger when the guard blocks a write, when the user asks "am I on main?" / "create a new branch", or at the start of any task that will write to a git repo.

  • Commits and pushes

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If the current branch is main / master / trunk (or configured as protected), exits 2 with a message.

    line 16
  2. Distinctive:

    If not in a git repo, exits 0 (no-op).

    line 17
  3. Very specific:

    Use git commit --amend on main to "fix" the guard.

    line 49

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 HEAD in 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=1 env 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 2 and 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=1 in your shell rc permanently. That defeats the guard for interactive sessions.
  • Force-push after branching. The commit stays on the feature branch; main is untouched.
  • Use git commit --amend on main to "fix" the guard. If you got here, your prior commit is still on main — reset it: git reset --soft HEAD~1 (moves the change back to staged), branch off, commit again on the branch, git push on 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"

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.