Skip to content

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.

active
IDE:
vscode
Version:
1.0.0
Owner:epic-platform-sre
git
worktrees
hygiene
cleanup
gh-cli
cross-tool

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:

  1. The user has authenticated gh (run gh auth status).
  2. The repo has an origin remote pointing at GitHub.
  3. The user has confirmed the default branch for the repo (main for most, develop for gitflow repos like otc-awesome-llm).

Decision algorithm

For each entry returned by git worktree list --porcelain:

  1. Skip and KEEP if any of:
    • Path equals the main/bare clone root.
    • Worktree is detached HEAD.
    • git status --porcelain is non-empty (uncommitted changes).
    • git rev-list --count origin/<branch>..HEAD is non-zero (unpushed commits).
  2. 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 1 returns a non-empty array.
    • git merge-base --is-ancestor <branch-tip> origin/<default> succeeds.
  3. 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 remove must 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 origin remote on GitHub.
  • Surface the per-entry decisions to the user verbatim; do not summarize away the audit trail.

Related Assets

Stale Item Cleanup

active

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.

claude
codex
vscode
agile
sprint-hygiene
technical-debt
github
gh-cli
+1

Owner: thudak_uhg

Daily Standup Preparation

active

Comprehensive standup preparation assistant that aggregates blockers, PR status, issue progress, and team updates into a concise, actionable format for daily standup meetings.

claude
codex
vscode
agile
sprint-ceremonies
standup
scrum
github
+1

Owner: thudak_uhg

Sprint PR Health Check

active

Analyze pull requests across repositories for age, review status, blockers, and patterns. Generates sprint hygiene report with actionable recommendations for standup discussions.

claude
codex
vscode
agile
sprint-hygiene
pull-requests
github
gh-cli
+1

Owner: thudak_uhg

Git Worktree Enforcement

active

Mandatory git worktree policy preventing branch conflicts and lost work by requiring all feature work in worktrees/ subdirectories.

claude
codex
vscode
git
worktree
branching
enforcement
version-control
+1

Owner: platform-devops

git-expert

active

Git version control, branching strategies, advanced operations, troubleshooting, and collaboration workflows

codex
git
version-control
branching
merging
rebase
+1

Owner: platform-devops

git-worktree-enforcement

active

Mandatory git worktree policy ensuring feature work happens in worktrees/ subdirectories, not the main clone

codex
git
worktree
branching
enforcement
version-control
+1

Owner: platform-devops