prune-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. Use when the user asks to "prune worktrees", "clean up worktrees", "remove old worktrees", "cleanup git worktrees", or wants to know "which worktrees are safe to delete". Trigger phrases include "git hygiene", "worktree cleanup", "stale worktrees".
Prune Git Worktrees
Removes worktrees whose branches are merged on the remote default branch, while preserving any worktree with uncommitted changes, unpushed commits, detached HEAD, or unresolved state.
When to use
- Before starting fresh work in a repo with many open worktrees
- When
git worktree listshows entries you don't recognize - After a batch of PRs has merged and you want to reclaim disk + namespace
- As part of a periodic git-hygiene pass across multiple repos
What this skill does
git fetch origin --prune --quietto refresh remote state.- Walks
git worktree list --porcelainand classifies each entry. - Skips: main clone, bare clone, detached HEAD, missing path (admin-prunes those), uncommitted, unpushed.
- Decides PRUNE if any of the following hold:
origin/<branch>is gone (typical squash-merge with delete-on-merge),gh pr list --head <branch> --state mergedreturns a result, or- branch tip is an ancestor of
origin/<default>(catches merge-commits).
- Otherwise KEEPs and reports the reason.
How to invoke
# Always start with --dry-run for a new repo
python3 ~/.claude/plugins/otc-awesome-llm/skills/prune-worktrees/scripts/prune_worktrees.py \
/path/to/repo --dry-run
# Repo whose default branch is develop (gitflow)
python3 .../prune_worktrees.py /path/to/repo --default develop --dry-run
# Apply
python3 .../prune_worktrees.py /path/to/repo --default main
The script must be invoked once per repo. For multi-repo passes, loop in shell:
for repo in repo-a repo-b repo-c; do
python3 .../prune_worktrees.py "$HOME/scm/$repo" --default main
done
Safety guarantees
- Read-only when
--dry-run— every action is logged but nothing mutates. - Never touches uncommitted work —
git status --porcelainnon-empty -> KEEP. - Never touches unpushed commits —
origin/<branch>..HEADnon-zero -> KEEP. - Defers on gh API failure — falls through to ancestry check, never assumes merged.
- Only removes via
git worktree remove— refuses on dirty paths automatically; on failure the script keeps the entry and continues. - Branch deletion is
-Donly after a successfulworktree remove— never deletes a branch whose worktree is still active.
What this skill does NOT do
- Does not push, force-push, or rewrite history.
- Does not touch
main, the bare clone, or any clone outside the targeted repo path. - Does not run across repos in one call — one repo per invocation, by design.
- Does not delete unmerged branches, even if their worktree is empty.
Required tooling
- Python 3.11+ (stdlib only)
git2.20+ (worktree porcelain output)ghCLI authenticated (gh auth status) — without this, only remote-branch + ancestry detection runs
Cross-tool parity
This skill is canonical for Codex (via target_tool: codex) and the
script ships here. A separate VS Code instruction at
vscode/instructions/prune-worktrees.instruction.md references the
same prune_worktrees.py for the VS Code surface.
Related issues
optum-tech-compute/otc-awesome-llm#478— original feature request
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

