clean-branches
Clean Git Branches
Safely remove merged and stale git branches with confirmation.
Process
0. Parse arguments
If $ARGUMENTS provided, treat it as a glob pattern to filter branch candidates (e.g., feature/* shows only feature branches). Pass it to the candidate script in Step 2.
1. Fetch latest state
git fetch --all --prune
If fetch fails (no remotes configured), note remote data is unavailable and continue with local analysis only.
2. Identify candidates
Run the candidate detection script, passing the optional pattern filter:
bash ${CLAUDE_PLUGIN_ROOT}/skills/clean-branches/scripts/find-candidates.sh "$PATTERN"
The script outputs two labeled sections (=== MERGED === and === STALE ===), one branch per line. Merged branches checked out in a worktree are annotated: branch-name [worktree:/path/to/wt]. Parse each section into its own list, preserving the worktree annotation for use in Step 5.
3. Present results
Display branches in three groups: merged (safe to delete), stale (no recent commits), and protected (never touch). If a merged branch carries a worktree annotation, note it with "(active worktree — will be removed first)" so the user understands what will happen. If both candidate lists are empty, report "No branches to clean" and stop — do not proceed to Step 4.
4. Confirm before deletion
Use AskUserQuestion with concrete options derived from the candidates found. Structure:
- Header: "Branch cleanup"
- For merged branches: ask "Delete these merged branches?" with options like "Delete all N merged branches" (label) / "Removes: branch-a, branch-b, ..." (description), and "Keep all merged branches"
- For stale branches with multiple candidates: use multiSelect:true so the user can pick individual branches. Each option: label = branch name, description = age (e.g., "3 months ago")
- Always include a "Skip — keep all" option
Never proceed to deletion without explicit user confirmation through AskUserQuestion.
5. Execute deletion
Delete only what the user confirmed. For each branch:
-
If the branch has a
[worktree:/path]annotation, remove the worktree first:git worktree remove /path/to/wtIf the worktree has uncommitted changes,
git worktree removewill refuse — report the error and skip that branch rather than force-removing. -
Then delete the branch:
git branch -d <branch-name>Use
-d(not-D) — git refuses to delete branches with unmerged commits. -
If the user explicitly requests remote cleanup:
git push origin --delete <branch-name>Remote deletion requires explicit user request — never delete remotes unless the user says so directly.
Safety Rules
- Never delete: main, master, develop, release/*
- Use
-dnot-Dto preserve git's unmerged-commit safety check - Remote deletion only on explicit user request
Output
Summary of actions taken:
- Branches deleted (local)
- Branches deleted (remote, if requested)
- Branches kept
- Any errors encountered
More from gupsammy/claudest
create-skill
>
13generate-image
>
11improve-skill
>
9repair-skill
This skill should be used when the user asks to "repair my skill", "audit this skill", "fix my skill", "review skill quality", "check if my skill is well-written", "diagnose skill problems", "what's wrong with this skill", or wants structural correctness fixes. Not for adding features or improving effectiveness — use improve-skill for that. Not for agents — use repair-agent.
7brainstorm
>
6run-research
>
3