Done
Wraps up a worktree-based git workflow: removes branches/worktrees whose PR has landed (merged or closed), leaves anything still open strictly alone, and returns you to the default branch — fetching it fresh only once nothing is left in flight.
0. Survey, before touching anything
git fetch --prune origin # updates remote-tracking refs, drops ones GitHub already deleted on merge
git worktree list --porcelain # every worktree + the branch it's on
git branch -vv # local branches, upstream, and `[gone]` markers
gh pr list --state all --json number,headRefName,state,url --limit 200
DEFAULT_BRANCH="$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null \
|| git remote show origin | sed -n 's/.*HEAD branch: //p')"
For every local branch that isn't $DEFAULT_BRANCH, match it to a PR by headRefName and note the PR's state.
Classify, don't assume:
MERGEDorCLOSED→ legacy, safe to remove.OPEN→ leave completely alone — no delete, no prune.- No PR found for the branch → do not delete. Flag it and ask; it could be unpushed WIP nobody filed a PR for yet.
1. Remote: delete legacy branches
git fetch --prune already dropped the remote-tracking ref for any branch GitHub auto-deleted on merge (the common case — nothing left to do there). For a legacy branch whose ref is still on the remote:
git ls-remote --exit-code --heads origin <branch> && git push origin --delete <branch>
2. Local: remove legacy worktrees and branches
For each legacy branch that has a worktree:
git -C <worktree-path> status --porcelain # must be empty
If it's dirty: stop and tell the user what's uncommitted there instead of removing anything — never discard work silently.
If clean:
git worktree remove <worktree-path>
git branch -D <branch>
Use -D, not -d. The PR's state (from gh, checked in step 0) is already the source of truth that the branch's work landed — git branch -d's own merge-ancestry check will falsely refuse a squash-merged branch, since its commits' SHAs never actually appear on $DEFAULT_BRANCH even though the PR merged cleanly. A legacy branch with no worktree just skips the worktree remove line.
3. Switch back to the default branch
Move the session to the primary worktree (the one already checked out to $DEFAULT_BRANCH — look for it in the step 0 git worktree list output).
- If any branch is still classified
OPEN(including ones skipped in step 0 for having no PR at all): print each one — branch name + PR URL — as a clear warning, and stop there. Do not fetch or fast-forward past it silently; the user should know work is still in flight. - If nothing is left open (every branch found was either just cleaned up as legacy in steps 1–2, or there were none to begin with): bring
$DEFAULT_BRANCHup to date —
Ifgit switch "$DEFAULT_BRANCH" git fetch origin "$DEFAULT_BRANCH" git merge --ff-only "origin/$DEFAULT_BRANCH"--ff-onlyrefuses (local branch diverged from remote), stop and report it rather than force-resetting.
Report
Summarize: what was deleted (remote branches, local worktrees, local branches), what was left alone and why (open PR, or no PR found), and whether $DEFAULT_BRANCH was fetched and fast-forwarded.
Claude Code note: if a worktree being cleaned up was created by the EnterWorktree tool (its path is under .claude/worktrees/), prefer ExitWorktree with action: "remove" for that one instead of a raw git worktree remove — it keeps the harness's own bookkeeping in sync. Everything else here is plain git/gh and behaves the same in Codex.
