Prune Git Worktrees
Safely prune git worktrees whose branches have been merged into the remote default. Uses authoritative gh API merged-PR detection plus remote-branch and ancestry checks. Skips worktrees with uncommitted or unpushed work.
Prune Git Worktrees
When the user asks Copilot Chat to prune, clean up, or remove stale git worktrees, follow this protocol. The same logic ships as a Claude skill and a Codex skill — VS Code Copilot Chat must produce equivalent behavior.
Required preconditions
Before suggesting any worktree removal:
- The user has authenticated
gh(rungh auth status). - The repo has an
originremote pointing at GitHub. - The user has confirmed the default branch for the repo (
mainfor most,developfor gitflow repos like otc-awesome-llm).
Decision algorithm
For each entry returned by git worktree list --porcelain:
- Skip and KEEP if any of:
- Path equals the main/bare clone root.
- Worktree is detached HEAD.
git status --porcelainis non-empty (uncommitted changes).git rev-list --count origin/<branch>..HEADis non-zero (unpushed commits).
- Mark PRUNE if any of:
git rev-parse --verify --quiet origin/<branch>fails (remote branch deleted).gh pr list --repo <owner>/<repo> --state merged --head <branch> --limit 1returns a non-empty array.git merge-base --is-ancestor <branch-tip> origin/<default>succeeds.
- Otherwise KEEP and report "not merged".
Mutating actions (only after explicit user approval)
git worktree remove <path>
git branch -D <branch> # only after the worktree remove succeeds
Never use git worktree remove --force to bypass a dirty-state guard.
Recommended invocation
The canonical Python implementation lives in this repo at
shared/skills/prune-worktrees/scripts/prune_worktrees.py. Prefer running it
over hand-rolling the logic:
# Always dry-run first
python3 shared/skills/prune-worktrees/scripts/prune_worktrees.py \
/path/to/repo --dry-run
# Apply when the user approves the dry-run output
python3 shared/skills/prune-worktrees/scripts/prune_worktrees.py \
/path/to/repo --default main
Hard rules
- Do NOT delete a branch whose worktree still exists —
git worktree removemust succeed first. - Do NOT run across multiple repos in a single command — one repo per call.
- Do NOT suggest this skill in a repo without an
originremote on GitHub. - Surface the per-entry decisions to the user verbatim; do not summarize away the audit trail.
Related Assets
Stale Item Cleanup
Deep analysis and bulk cleanup of stale pull requests and issues across repositories. Identifies candidates for closing, archiving, or escalating based on age, activity, and business value.
Owner: thudak_uhg
Daily Standup Preparation
Comprehensive standup preparation assistant that aggregates blockers, PR status, issue progress, and team updates into a concise, actionable format for daily standup meetings.
Owner: thudak_uhg
Sprint PR Health Check
Analyze pull requests across repositories for age, review status, blockers, and patterns. Generates sprint hygiene report with actionable recommendations for standup discussions.
Owner: thudak_uhg
Git Worktree Enforcement
Mandatory git worktree policy preventing branch conflicts and lost work by requiring all feature work in worktrees/ subdirectories.
Owner: platform-devops
git-expert
Git version control, branching strategies, advanced operations, troubleshooting, and collaboration workflows
Owner: platform-devops
git-worktree-enforcement
Mandatory git worktree policy ensuring feature work happens in worktrees/ subdirectories, not the main clone
Owner: platform-devops

