/empire-git:worktree-cleanup [--dry-run] [--days N]from empire-git
Batch cleanup of stale git worktrees and orphaned branches. Detects stale/missing/active worktrees, prunes remote refs, identifies stale local branches. Supports dry-run and custom days threshold.
Ships in empire-git. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Scan for stale worktrees and orphaned branches, then let the user decide what to clean up. This is the batch housekeeping counterpart to /empire-git:worktree-close (which handles one worktree at a time).
User input: $ARGUMENTS
If --dry-run is in the arguments, report what would be cleaned up without making any changes.
If --days N is in the arguments (e.g. --days 14), use N as the staleness threshold. Otherwise default to 7 days. Note: worktree-list uses a softer 3-day threshold (informational warning); cleanup uses 7 days because removal is the action and the longer window reduces false positives.
git worktree list --porcelain
For each worktree (excluding the main working tree), gather:
git -C "<path>" log -1 --format="%ct" 2>/dev/null
git -C "<path>" status --porcelain 2>/dev/null | wc -l | tr -d ' '
Classify each worktree:
| Status | Criteria |
|---|---|
| Stale | Last commit more than --days (default 7) ago and clean (no uncommitted work) |
| Missing | Directory no longer exists on disk (stale git metadata) |
| Active | Recent commits or has uncommitted changes |
First, prune remote-tracking references so git knows which remote branches are gone:
git fetch --prune
Determine which branches must never be flagged as orphans:
# The repo's default branch (main, master, develop, trunk, …) — detected, not assumed
DEFAULT_BRANCH=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's|^origin/||')
# Fallback 1: check well-known branch names
[ -z "$DEFAULT_BRANCH" ] && DEFAULT_BRANCH=$(git for-each-ref --format='%(refname:short)' refs/heads/main refs/heads/master refs/heads/trunk refs/heads/develop | head -n1)
# Fallback 2: use current HEAD (guaranteed as long as we are in a git repo)
[ -z "$DEFAULT_BRANCH" ] && DEFAULT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)
# Fallback 3: pick the most-recently-committed local branch
[ -z "$DEFAULT_BRANCH" ] && DEFAULT_BRANCH=$(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads | head -n1)
# Guard: if DEFAULT_BRANCH is still empty we cannot safely run merged checks
if [ -z "$DEFAULT_BRANCH" ]; then
echo "Warning: could not determine default branch — merged-check classification will be skipped."
fi
# The branch currently checked out in the main working tree
CURRENT_BRANCH=$(git -C "$(git rev-parse --show-toplevel)" branch --show-current)
# Branches checked out in any worktree (these are "in use" — git won't let you delete them anyway)
git worktree list --porcelain | awk '/^branch / { sub("refs/heads/", "", $2); print $2 }'
Then list candidate local branches, excluding the default branch, the current branch, and any branch that has a worktree:
git branch --format='%(refname:short)'
Filter that list in your head (or with grep -v) against $DEFAULT_BRANCH, $CURRENT_BRANCH, and the worktree branches above. Do not hardcode main|master — many repos use develop, trunk, or custom default branches, and the user may currently be checked out on a branch that isn't the default.
For each remaining local branch, check:
git branch --merged "$DEFAULT_BRANCH" | grep -w "<branch>"
git branch -vv --format='%(refname:short) %(upstream:track)' | grep '\[gone\]'
The [gone] marker means the branch once tracked a remote that no longer exists — a strong signal the PR was merged and the remote branch deleted.gh is available)
gh pr list --head "<branch>" --state open --json number 2>/dev/null
Classify:
| Status | Criteria |
|---|---|
| Remote deleted | Tracked a remote that's gone (PR likely merged and branch deleted on GitHub) |
| Merged orphan | Merged into $DEFAULT_BRANCH, no worktree, no remote — safe to delete |
| Unmerged orphan | Not merged into $DEFAULT_BRANCH, no worktree, no remote — may be abandoned work |
| Has open PR | Skip — still in use |
Show a summary grouped by action:
Worktree Cleanup Report
═══════════════════════
Stale worktrees (no commits in 7+ days, clean):
feat/old-experiment .claude/worktrees/feat-old-experiment-1a2b3c4d 12d ago
fix/typo-header .claude/worktrees/fix-typo-header-5e6f7a8b 8d ago
Missing worktrees (directory gone, metadata remains):
feat/deleted-thing (path no longer exists)
Branches with deleted remote (PR likely merged on GitHub):
feat/auth-refactor remote gone — safe to delete locally
fix/login-bug remote gone — safe to delete locally
Orphaned branches (no worktree, no remote):
feat/abandoned-idea merged into $DEFAULT_BRANCH — safe to delete
fix/half-done NOT merged — review before deleting
Active worktrees (no action needed):
feat/current-work .claude/worktrees/feat-current-work-9c0d1e2f 2h ago
If --dry-run, stop here.
Present cleanup options as a checklist — the user picks what to clean up:
git worktree prune to clear stale metadatagit branch -d for each (their PRs were merged)git branch -d for eachgit branch -D if they want to force)Ask which of these the user wants to proceed with. Don't auto-execute anything — the user confirms first.
For each action the user approved:
# Prune stale metadata
git worktree prune
# Close stale worktrees
git worktree remove "<path>"
# Delete merged orphan branches
git branch -d "<branch>"
Print a summary of what was done:
Cleanup complete:
2 stale worktrees removed
1 missing worktree pruned
3 merged orphan branches deleted
1 unmerged orphan branch kept (fix/half-done)
After the cleanup actions complete, prune the session registry so it forgets any worktree directories that were removed:
bash "${CLAUDE_PLUGIN_ROOT}/scripts/worktree-registry.sh" prune
If the call fails, print a warning and continue — registry hygiene is best-effort.
This is a housekeeping tool, not a destructive one. The default posture is conservative — show what could be cleaned up, let the user decide. The --dry-run flag makes it completely safe to explore.
Merged branches are safe to delete. Their commits live in the target branch. Unmerged branches might contain work the user forgot about — flag them but don't delete without explicit confirmation.
Use git worktree remove, not rm -rf. And use git branch -d (safe delete), not -D. If something refuses to delete, that's useful information — surface it.
What SKILL.md sets; muted values are Claude Code's defaults
haikuThree short reads, in order
Other plugins' skills for the same job
npx claudepluginhub marcoskichel/empire --plugin empire-git12 skills in this plugin
/activity-reportGenerate a concise activity report of the user's GitHub work over a period, grouped by area or feature instead of PR by PR. Use when user says "activity report", "weekly report", "what did I do this week", "what did I ship", "work summary", "summarize my week", "status update for my manager", "what did <user> ship", or `/empire-git:activity-report [start-date] [end-date] [author]`.
/pr-comment-replyRender the canonical reply text for a PR review comment. Use when drafting, writing, or posting a "PR comment reply", "review comment reply", "reply to this comment", "respond to this review comment", "answer the reviewer", "reply on the PR thread", or any text bound for `gh api` review-thread replies, `gh pr comment`, or a GitHub review comment box. Triggers on "reply to the comment", "answer this review comment", "respond to the reviewer", "draft a reply", "post a reply on the PR". Outputs plain text for the caller to post unchanged.
/pr-descriptionRender the canonical PR description body. Use when drafting, writing, updating, or regenerating a "PR description", "PR body", "pull request description", "pull request body", "PR summary", "PR template", "GitHub PR body", or any text bound for `gh pr create --body`, `gh pr create --body-file`, `gh pr edit --body`, `gh pr edit --body-file`, or a GitHub PR template. Triggers on "draft a PR", "write the PR", "summarize this branch for review", "regenerate PR body". MUST be invoked before any `gh pr create --body*` or `gh pr edit --body*` per empire-git rules. Outputs markdown for stdin.
/pr-mergeGate then merge a single pull request safely. Use when the user says "merge this PR", "merge the PR", "land this", "ship this PR", "merge when green". Verifies CI is green (investigates and attempts a fix when red), rebases and resolves conflicts when the branch is behind, triages unresolved review threads intelligently (resolve, fix, or ask — never blindly block), then merges and deletes the branch. Fast-forwards the local base checkout (e.g. updates `master`) when it is checked out.
/pr-merge-stackMerge an entire stack of dependent PRs bottom-up: merge the base PR, pull the base branch, rebase the next PR onto it, wait for CI, merge, repeat until the stack is empty. Use when the user says "merge the stack", "merge pr stack", "land the stack", "merge all these PRs", "merge the chain", "ship the whole stack", or names several stacked PRs to merge in order. Infers the stack from conversation; asks when it can't. Offers admin merge (skip review requirements) up front — CI must still pass either way.
/pr-review-postPost a GitHub PR review (verdict + inline comments) in one API call. Use when posting, submitting, or publishing a "PR review", "code review", "review with inline comments", "approve the PR", "request changes", or any payload bound for `gh api repos/*/pulls/*/reviews`. Triggers on "post the review", "submit the review", "publish the review comments", "approve this PR", "request changes on the PR", "leave a review". MUST be used for any review POST so comments and verdict land atomically. Never posts without the caller's confirmed verdict.