/agent-workflows:git-worktree-tidyfrom agent-workflows
Use when fetching and pruning a worktree-based repository, removing stale local branches, worktrees, or stashes, or bringing its important branches up to date.
Ships in agent-workflows. 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.
Routine hygiene for bare-repo + worktree layouts. Fetches origin, prunes gone branches and orphaned worktrees, reviews stashes, restores a missing default-branch checkout, and fast-forwards important branches.
User asks to "fetch prune", "clean up stale branches/worktrees", or "update main/dev to latest" in a worktree-based repo. Stash review applies to any repository.
wip/<topic> branch, not by leaving it stashed.--ff-only when updating branches. If ff-only fails, stop and ask..bare directory (or repo root) for branch/worktree
management commands..bare
named after that branch (../<default> from .bare). In any other layout,
report the missing checkout and do not invent a path.Determine the bare repo directory:
.bare/, use itgit rev-parse --git-common-dirAll branch and worktree management commands run from this directory.
git fetch --prune origin
Report what was pruned (deleted remote-tracking branches, updated refs).
git branch -vv | grep ': gone]'
Collect branch names whose upstream is gone. Also collect every other local
branch except the default: a merged PR whose remote branch was never deleted
leaves no gone marker, including on branches checked out in a worktree.
Step 3a classifies them all.
A deleted upstream does not prove shipping. Squash- and rebase-merges change SHAs, so ancestry cannot prove it either. Verify against the forge.
For each collected branch (GitHub example; substitute your forge CLI):
# Was there a merged PR from this head?
gh pr list --state all --head <branch> \
--json number,state,mergedAt,mergeCommit \
--jq '.[] | "#\(.number) \(.state) merged=\(.mergedAt // "no")"'
# If merged, confirm nothing was added to the branch AFTER the merge:
# the local tip should equal the PR head at merge.
gh pr view <pr> --json commits --jq '.commits[-1].oid' # vs: git rev-parse <branch>
Classify each collected branch:
git show -s --format=%ci <tip>) → genuine unshipped work. Flag it; do not
delete without explicit approval.If gh/the forge CLI is unavailable, say so and treat unverifiable branches as
at risk rather than assuming merged.
git worktree list
git worktree prune --dry-run
Cross-reference branches and check each candidate for dirty state:
cd <worktree-path> && git status --short
From the management directory, run the mandatory owner guard, including paths whose directories are already gone:
bash <skill-dir>/scripts/worktree.sh check <worktree-path> ...
It emits schema-1 JSONL: path, status, session/PID owners and discovery errors.
Exit 0 means owner-free, 3 means owned, 4 means unknown (2 is usage).
It requires Python 3.9+, lsof and zmx; SOX_BIN selects a sox CLI outside
PATH. It checks process cwds, sox's current cwd and zmx's initial cwd,
including descendants, deleted cwds and the sox backing namespace. Set
SOX_HOME/ZMX_DIR for the host's actual namespaces; repeat for other homes.
Run on the worktree's host. OS-inaccessible processes remain unverified.
Each discovery query has a 15-second timeout. On a loaded host, set
WORKTREE_QUERY_TIMEOUT_SECONDS to a positive number of seconds, at most 300;
timeouts still refuse removal and prune. This limit applies to each query,
so a caller's overall deadline must also allow the remaining discovery queries.
Categorize:
git stash list --format='%gd %ci %gs'
git stash show --stat --include-untracked stash@{N}
git log -1 --format='%h %s' stash@{N}^1 # the commit it was taken on
For each stash, record its age, base commit, and files, then read enough of
the diff to say whether that content now exists on the default branch
(git stash show -p stash@{N} | git apply --reverse --check - passing is
strong evidence; failing proves nothing, because later edits to the same lines
defeat it). Label each likely landed, superseded (the files or feature
were rewritten or removed since), or unshipped, with the evidence.
Present a summary table:
Stale worktrees to remove:
<path> (<branch>) [clean]
<path> (<branch>) [AT RISK — dirty / session tool:id / PID command / unknown: error]
Stale branches:
<branch> [merged — PR #N, safe to delete]
<branch> [AT RISK — no merged PR / commits after merge; review first]
Prunable worktree metadata:
<entry> [owner-free / AT RISK — owners or errors]
Stashes:
stash@{N} <age> on <base> <files> [likely landed | superseded | unshipped — evidence]
Proceed with verified-merged branches and clean, owner-free worktrees. Batch all at-risk items and stashes into one confirmation with ship status and the exact owners and teardown proposed. An owned worktree is never interior.
For an approved owned worktree, stop owners through their own tools first (the project's
stack-down command, sox kill <backing-id>, or zmx kill <name> in the reported
namespace). Signal only individually attributed PIDs; never a process-table
selector. Approval does not turn a discovery error into an empty inventory:
resolve it or skip. Recheck until owner-free, then remove through the guard:
bash <skill-dir>/scripts/worktree.sh remove <worktree-path>
The guard rechecks immediately before Git. A hit or error refuses removal;
report and skip. Add --force only for approved dirty state, or for a clean
worktree with initialized submodules, which Git refuses without it; first
confirm each submodule is clean and its commits are pushed. Never run
git submodule deinit in a linked worktree: submodule registration lives in
the shared .git/config, so it de-initializes every worktree's submodules,
the primary's included.
git branch -D <branch1> <branch2> ...
A merged branch still checked out in the primary working tree is deleted after step 9 switches it off.
Drop the stashes the user approved, highest index first so the remaining
indexes stay valid (git stash drop stash@{N}). For each stash the user keeps,
commit it to a wip/<topic> branch in a clean worktree, then drop the stash.
bash <skill-dir>/scripts/worktree.sh prune
This guards every prunable entry, including deleted directories. If any is owned or unknown, no prune runs: report it, obtain approval for teardown, and retry after owners are gone. Never substitute a destructive Git remove/prune.
Discover the default branch from the remote, not by guessing main:
git rev-parse --abbrev-ref origin/HEAD # origin/dev
# if missing:
git remote set-head origin -a
The branch name is the part after origin/. If origin/HEAD is still
unknown, report and skip restore rather than guessing.
Restore a missing checkout only for the default branch. Fast-forward every
worktree already on the default, main, dev, or a branch the user named.
Inspect git worktree list:
cd <path> && git pull --ff-only origin <branch>.
If that path is not the conventional .bare sibling, say so; do not try
to add a second checkout (git will refuse).git switch <default>, ff-only pull,
then delete the merged branch..bare, and
../<default> (relative to .bare) does not exist:
git worktree add ../<default> <default>git worktree add ../<default> -b <default> origin/<default>git switch such a
checkout onto the branch.If ff-only fails, report the divergence and ask for guidance.
Show a summary: what was removed, which stashes were dropped or kept as branches, what checkouts were restored, what was updated, any items skipped.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub alleneubank/agent-profile --plugin agent-workflows22 skills in this plugin
/chief-of-staffUse when taking, holding, or onboarding a workspace's chief of staff (the human's intake and router above that workspace's repository coordinators), or when deciding whether a finding goes to the human, a coordinator, or an issue./contentUse when authoring or changing a work whose reader is outside the current conversation — product text (labels, messages, errors, notifications, emails, command output), documentation, announcements, a change description, or a brief, prompt, or handoff for another agent. Not for replies in the conversation or incidental one-line messages./coordinatorUse when coordinating agent lanes across hosts or workspaces, starting or resuming a campaign from a tee-up agreement, preparing unattended work, rotating coordinators, or planning a workspace's backlog by theme. Bounded attended implementation needs no coordinator.