resources/skills/x-cleanup-git-branches/SKILL.md
Cleans local git: prune, remove non-main worktrees, delete branches except main/develop.
npx skillsauth add edercnj/claude-environment x-cleanup-git-branchesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Centralizes the "reset local git state" workflow for {{PROJECT_NAME}}: after a batch of merged PRs, stale worktrees and orphan local branches accumulate. This skill does the combined pass in one invocation — fetch with prune, remove every non-main worktree, delete every local branch outside the protected set (main, master, develop, plus epic/* and docs/* with open PR).
Destructive by design (the user explicitly asked for a sweep). Safety comes from the confirmation gate (y/N) before any deletion and from --dry-run preview mode.
git worktree list or git branch -l gets noisy./x-manage-worktrees remove --id <id> or git branch -D <name> directly./x-cleanup-git-branches — execute with interactive confirmation/x-cleanup-git-branches --dry-run — preview candidates, no changes/x-cleanup-git-branches --yes — execute, skip confirmation (CI / scripted use)| Flag | Type | Required | Default | Description |
|------|------|----------|---------|-------------|
| --dry-run | Boolean | No | false | Preview candidate worktrees and branches without deleting. Mutually exclusive with --yes. |
| --yes / -y | Boolean | No | false | Skip the y/N confirmation gate. For CI / scripted use. Mutually exclusive with --dry-run. |
| Name | Why protected |
|------|---------------|
| main | Production branch (Rule 09) |
| master | Legacy production alias |
| develop | Integration branch (Rule 09) |
| epic/* | Always protected until the manual epic-to-develop PR gate merges (Rule 21) |
| docs/* with open PR | Preserved until the PR is merged or closed (EPIC-0065 D-R6); fail-safe when gh is absent |
The currently checked-out branch (HEAD) is NOT in the protected set. If HEAD points at a candidate branch, the skill checks out develop (fallback: main) before deletion.
Exit codes:
| Exit | Condition |
|------|-----------|
| 0 | Success (cleanup completed, dry-run complete, nothing to clean, or user declined confirmation) |
| 1 | Operational failure (NOT_A_REPO, IN_WORKTREE_UNSAFE, NO_SAFE_FALLBACK_BRANCH) |
| 2 | Usage error (unknown flag, --dry-run and --yes both set) |
Stdout: human-readable cleanup plan + summary line "Worktrees removed: N / Branches deleted: M". Stderr: WARNING lines for individual failures (which do NOT abort the run).
1. PARSE_FLAGS -> validate --dry-run / --yes mutual exclusion
2. DETECT_CONTEXT -> abort with IN_WORKTREE_UNSAFE if running inside any linked worktree
3. RESOLVE_HEAD -> capture current branch (empty if detached)
4. FETCH -> git fetch --prune origin (skip if no origin)
5. ENUM_WORKTREES -> list non-main worktrees via git worktree list --porcelain
6. ENUM_BRANCHES -> local branches minus protected (main/master/develop/epic/*/docs-with-PR)
7. PRINT_PLAN -> human-readable candidate table; exit 0 if --dry-run or empty
8. CONFIRM_GATE -> y/N prompt unless --yes / --dry-run
9. SWITCH_IF_NEEDED -> checkout develop/main if HEAD is a candidate
10. REMOVE_WORKTREES -> git worktree remove --force + git worktree prune
11. DELETE_BRANCHES -> git branch -D per candidate
12. REPORT_SUMMARY -> counts to stdout; exit 0
Full bash blocks for all 12 steps, the 3-classifier detect_worktree_context() function (Rule 14 non-nesting invariant + git-dir suffix check + toplevel-vs-main comparison), heredoc-based candidate iteration (handles paths with spaces), epic/* and docs/* filtering with gh pr list fallback, and --force worktree-removal semantics live in references/full-protocol.md:
case loop parsing; mutual-exclusion check; usage banner.detect_worktree_context() function with JSON output; abort on inWorktree=true.origin-presence check; fail-open fetch.sed -n 's/^worktree //p' to preserve paths-with-spaces; tail -n +2 to skip the main worktree.for-each-ref enumeration; epic/* unconditional skip; docs/* with-PR check via gh pr list --head ... --state open (fail-safe when gh is absent).--dry-run early exit; interactive y/N gate.develop then main fallback; NO_SAFE_FALLBACK_BRANCH abort.while IFS= read -r over heredoc; per-failure WARNING without aborting; git worktree prune after the loop.| Scenario | Action |
|----------|--------|
| --dry-run and --yes both set | Abort with exit 2 (usage error) |
| Unknown flag passed | Abort with exit 2 (usage error) |
| Running inside any linked worktree | Abort with IN_WORKTREE_UNSAFE, exit 1 |
| Not a git repo | Abort with NOT_A_REPO, exit 1 |
| origin remote missing | Warn, skip fetch, continue |
| HEAD is a candidate and neither develop nor main exists | Abort with NO_SAFE_FALLBACK_BRANCH, exit 1 |
| Individual worktree remove fails | Warn, continue, count excludes it |
| Individual branch delete fails | Warn, continue, count excludes it |
| No candidates (worktrees + branches empty) | Print "Nothing to clean", exit 0 |
| Skill | Relationship |
|-------|-------------|
| x-manage-worktrees | cleanup operation is scoped to .claude/worktrees/* with MERGED/STALE/ORPHAN criteria; this skill is broader and unconditional. |
| x-push-branch | Typical upstream action after cleanup (push a fresh branch). |
| x-commit-changes | Used to author commits — unrelated to cleanup, referenced here only for context. |
detect_worktree_context() snippetMinimum viable contract above. Detailed bash for all 12 steps (3-classifier worktree detection, heredoc-based candidate iteration handling paths with spaces, epic/docs filtering, --force worktree removal semantics, HEAD-switch logic), error tables, and security notes live in references/full-protocol.md per ADR-0012 (skill body slim-by-default).
development
Documentation freshness gate: validates 6 dimensions (readme, api, adr, etc.) per PR.
testing
Conditional dep-policy gate: CVEs, licenses, versions, freshness; SARIF + report.
documentation
Incrementally updates the service or system architecture document; never regenerative.
development
Scans code and git history for leaked credentials, API keys, and tokens; SARIF output.