This skill guides cleanup of stale Git worktrees, protecting designated branches and checking disk usage while distinguishing merged from unmerged branches. It provides dry-run, automatic, and interactive modes, with optional remote-branch deletion and reminders to clean up corresponding database branches. It recommends previewing potentially destructive cleanup and running the process weekly.
66%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Low
Low-risk findings worth noting
Fix and improve this skill with Tessl
tessl review fix ./examples/skills/git-worktree-clean/SKILL.mdBatch cleanup of stale git worktrees. Safely removes merged branches, reports disk usage, and handles unmerged branches interactively.
Core principle: Auto-clean merged worktrees, interactive review for unmerged, always report what was reclaimed.
Part of: Worktree Lifecycle Suite: /git-worktree | /git-worktree-status | /git-worktree-remove
git worktree list| Flag | Effect |
|---|---|
--dry-run | Preview what would be cleaned, no changes |
--all | Include unmerged worktrees (interactive confirmation each) |
--remote | Also delete the remote branch of each removed worktree (off by default) |
--force | Remove all worktrees without confirmation (dangerous) |
# Get main branch name
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
MAIN_BRANCH=${MAIN_BRANCH:-main}
# Protected branches (never auto-clean)
PROTECTED="main master develop staging production"
# List all worktrees (skip main working tree)
git worktree list --porcelain | while read line; do
# Parse worktree path and branch
# Skip the main worktree (first entry)
donefor WORKTREE in $WORKTREES; do
BRANCH=$(git -C "$WORKTREE" rev-parse --abbrev-ref HEAD)
# Skip protected
if echo "$PROTECTED" | grep -qw "$BRANCH"; then
echo "PROTECTED: $BRANCH (skipped)"
continue
fi
# Check merge status
if git merge-base --is-ancestor "$BRANCH" "$MAIN_BRANCH" 2>/dev/null; then
echo "MERGED: $BRANCH → safe to remove"
MERGED_LIST="$MERGED_LIST $WORKTREE"
else
echo "UNMERGED: $BRANCH → requires review"
UNMERGED_LIST="$UNMERGED_LIST $WORKTREE"
fi
donefor WORKTREE in $ALL_WORKTREES; do
# Size excluding node_modules (GNU du: --exclude, BSD/macOS du: -I)
if du -sh -I 'node_modules' "$WORKTREE" >/dev/null 2>&1; then
SIZE=$(du -sh -I 'node_modules' "$WORKTREE" | cut -f1)
else
SIZE=$(du -sh --exclude='node_modules' "$WORKTREE" | cut -f1)
fi
echo " $WORKTREE: $SIZE"
done# --dry-run: show what would happen without making changes
echo "=== Dry Run ==="
echo ""
echo "Would remove (merged):"
for WT in $MERGED_LIST; do
echo " $WT ($BRANCH) - $SIZE"
done
echo ""
echo "Would ask about (unmerged):"
for WT in $UNMERGED_LIST; do
echo " $WT ($BRANCH) - $SIZE - last commit: $(git log -1 --format='%s' $BRANCH)"
done
echo ""
echo "Total space to reclaim: $TOTAL_SIZE"
echo ""
echo "Run without --dry-run to execute."Only removes merged worktrees and their local branches. Safe by default. Remote branches are left alone unless --remote is passed. A branch merged by squash or rebase is not an ancestor of the main branch, so it is classified as unmerged: confirm those on the forge before removing.
echo "Cleaning merged worktrees..."
for WORKTREE in $MERGED_LIST; do
BRANCH=$(git -C "$WORKTREE" rev-parse --abbrev-ref HEAD)
# Remove worktree
git worktree remove "$WORKTREE"
# Delete local branch
git branch -d "$BRANCH" 2>/dev/null
# Delete the remote branch only with --remote (outward-facing, not reversible locally)
if [ "$REMOTE_FLAG" = true ]; then
git push origin --delete "$BRANCH" 2>/dev/null
fi
echo " Removed: $WORKTREE ($BRANCH)"
done
# Report unmerged (not touched)
if [ -n "$UNMERGED_LIST" ]; then
echo ""
echo "Unmerged worktrees (kept):"
for WT in $UNMERGED_LIST; do
echo " $WT - use /git-worktree-remove or --all to review"
done
fiReviews unmerged worktrees one by one:
for WORKTREE in $UNMERGED_LIST; do
BRANCH=$(git -C "$WORKTREE" rev-parse --abbrev-ref HEAD)
LAST_COMMIT=$(git log -1 --format='%h %s (%cr)' "$BRANCH")
AHEAD=$(git rev-list --count "$MAIN_BRANCH".."$BRANCH")
echo ""
echo "Unmerged: $WORKTREE"
echo " Branch: $BRANCH ($AHEAD commits ahead of $MAIN_BRANCH)"
echo " Last commit: $LAST_COMMIT"
echo " Size: $SIZE"
echo ""
echo " [r]emove [k]eep [s]kip remaining"
# Wait for user decision per worktree
doneAfter cleanup:
=== Worktree Cleanup Report ===
Removed (merged):
.worktrees/feat/auth (feat/auth) - 2.3 MB
.worktrees/fix/login-bug (fix/login-bug) - 1.1 MB
.worktrees/chore/deps-update (chore/deps-update) - 0.8 MB
Kept (unmerged):
.worktrees/feat/experimental (feat/experimental) - 4.2 MB
Last commit: a1b2c3d "WIP: new auth flow" (3 days ago)
Kept (protected):
.worktrees/develop (develop)
Space reclaimed: 4.2 MB
Worktrees remaining: 2
References pruned: yes
DB branches to clean:
neonctl branches delete feat-auth
neonctl branches delete fix-login-bug
neonctl branches delete chore-deps-updateDry run report:
=== Dry Run - No Changes Made ===
Would remove (3 merged):
.worktrees/feat/auth - 2.3 MB
.worktrees/fix/login-bug - 1.1 MB
.worktrees/chore/deps-update - 0.8 MB
Would keep (1 unmerged):
.worktrees/feat/experimental - 4.2 MB
Would keep (1 protected):
.worktrees/develop
Potential space savings: 4.2 MB| Situation | Action |
|---|---|
| Default (no flags) | Remove merged worktrees only |
--dry-run | Preview without changes |
--all | Merged (auto) + unmerged (interactive) |
--force | Remove everything except protected |
| Protected branch | Always kept |
| Merged branch | Auto-removed |
| Unmerged branch | Kept (default) or interactive (--all) |
| DB branches detected | Reminder with exact commands |
Running --force without --dry-run first
--dry-run before force-cleaningForgetting DB branch cleanup
Not running cleanup regularly
/git-worktree-clean --dry-run weekly./git-worktree-clean
/git-worktree-clean --dry-run
/git-worktree-clean --allFlags: $ARGUMENTS
f97e4f7
gitgit worktreegit worktree listgit symbolic-refsedgit rev-parsegrepgit merge-baseechoducutgit loggit worktree removegit branchgit pushgit rev-listreadneonctlneonctl branchesneonctl branches deletegitneonctlIf you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.