---
name: "codemaman-git-cleanup"
description: "Safely find and remove stale local/remote branches and git worktrees (merged, abandoned, or orphaned). Use for 'clean up my branches', 'prune worktrees', 'what branches can I delete'."
---

# Git Branch & Worktree Cleanup

Audit first, delete second. Nothing is removed without an explicit, itemised yes from the user, and unmerged or unpushed work is never deleted silently.

## 1. Orient (read-only)
- Confirm you're in a git repo; find the default branch (`git symbolic-ref refs/remotes/origin/HEAD`). Note the current branch.
- `git fetch --prune origin` (this only updates remote-tracking refs).
- Collect:
  - Local branches with upstream, ahead/behind, last commit date and author: `git for-each-ref refs/heads --format='%(refname:short)|%(upstream:short)|%(upstream:track)|%(committerdate:relative)|%(authorname)|%(subject)'`
  - Worktrees: `git worktree list --porcelain` (flag `prunable`, missing directories, detached HEADs, locked)
  - Stashes: `git stash list`
  - Merged state: `git branch --merged <default>` and, for squash/rebase merges, PR state via `gh pr list --state all --head <branch> --json number,state,mergedAt,url` (a branch whose PR is MERGED is safe even if git doesn't see it as merged)
  - Remote branches you authored that are merged or closed: `gh api` / `git branch -r --merged origin/<default>`
  - Jira keys in branch names: if a Jira tool is connected, check whether the ticket is Done/Closed (informational only).

## 2. Classify each branch and worktree
- **Safe to delete**: PR merged (or fully merged into default) AND no unpushed commits AND no uncommitted changes in its worktree.
- **Probably stale, needs a look**: no PR, no commits for 60+ days, upstream gone. Show the unique commits (`git log <default>..<branch> --oneline`).
- **Has unpushed work**: ahead of upstream or no upstream with unique commits. NEVER auto-delete; offer to push or create a PR.
- **Closed PR, unmerged**: show the PR and ask.
- **Active / protected**: current branch, default branch, `develop`, `release/*`, `main`, branches checked out in other worktrees, anything with an open PR, branches by other authors. Do not touch.
- **Worktrees**: for each, check `git -C <path> status --porcelain`. Dirty worktrees are never removed. Missing directories are pruned with `git worktree prune`.

## 3. Present the report
A table per category with branch, last activity, PR status, ahead/behind, and recommended action. Lead with the counts and a short list of safe deletions. Include the exact commands you would run.

## 4. Delete only what was approved
- Local branches: `git branch -d <name>` (safe delete). Use `-D` only for branches the user has explicitly confirmed are squash-merged or abandoned, after showing the tip SHA.
- Record each deleted branch's tip SHA in your final summary so it can be restored: `git branch <name> <sha>` (reflog also keeps it ~90 days).
- Worktrees: `git worktree remove <path>` (no `--force` unless the user approves a specific dirty one after seeing `git status`).
- Remote branches: only the user's own, merged ones, and only when they say so: `git push origin --delete <name>`. Never delete remote branches belonging to others or protected ones.
- Stashes: list them and let the user decide; don't drop any unprompted.
- Never run `git gc --prune=now`, `git clean -fdx`, or reset anything.

## 5. Report
What was removed (with SHAs for restoration), what was kept and why, and anything needing the user's attention (unpushed work, dirty worktrees). Offer to repeat on a schedule only if they ask.

## Rules
- Read-only until the user approves a specific list.
- If a check fails (e.g. `gh` not authenticated), say what was skipped and treat those branches as "needs a look", not "safe".