agentleFS
LaansDoleLaansDole/my-hermes-skills/skills/productivity/sweeping-merged-worktrees/SKILL.md

sweeping-merged-worktrees

Skill2 starsChanged 4 months agoMITView on GitHub

Use when a repo has accumulated worktrees under .worktrees/ from finished PRs, when asked to clean up or prune worktrees whose branches were merged or deleted on the remote, or when `git worktree list` shows stale/prunable entries

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Remove worktrees before deleting branches.

    line 43

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Sweeping Merged Worktrees

Overview

Survey, salvage, and remove worktrees whose branches are merged or gone from the remote.

Core principle: git worktree remove does not touch the branch ref, so committed work always survives. The only unrecoverable loss is uncommitted work. Salvage it, then remove — do not leave a merged worktree standing just because it holds a stray untracked file.

Usage

~/.agents/skills/sweeping-merged-worktrees/sweep.py <repo>...          # dry run, prints verdicts
~/.agents/skills/sweeping-merged-worktrees/sweep.py <repo>... --apply  # salvage + prune + remove

Dry run first. Read the table, then --apply. Multi-repo in one shot: sweep.py storybook nextportal e2e.

Baseline is origin/<branch the main checkout sits on> — no config needed.

Verdicts

VerdictMeaningAction taken by --apply
mergedAncestor of the baselineSalvage untracked, remove worktree
goneUpstream deleted (squash-merged PR, or abandoned)Salvage untracked, remove worktree
prunableRegistration points at a missing directorygit worktree prune
blockedMerged/gone but has uncommitted edits to tracked filesNothing — reported for you to review
keepNeither merged nor goneNothing

Non-ignored untracked files are copied into the main checkout at the same relative path before removal (.salvaged suffix on content conflict). Ignored files — build output, node_modules — are regenerable and left to die.

Order Matters

Remove worktrees before deleting branches. git branch -d/-D refuses a branch that is checked out in any worktree, so a branch-first cleanup silently skips exactly the branches you meant to remove.

Branch deletion is a separate step with its own precondition. A git cleanup-style alias (fetch -p, branch -D every [gone], branch -d every merged) runs unconditionally, so clear both footguns below before invoking one. sweep.py prints an [AT RISK] line for every [gone] branch whose patches are absent from the baseline — if any appear, that alias will orphan those commits.

Footguns

Stale local baseline. git branch --merged compares against the current local HEAD. If local dev is behind origin/dev, merged branches report as unmerged and cleanup quietly under-deletes. Always compare against origin/<base> — the tool does. Fast-forward the baseline before running a --merged-based alias.

Aliases that delete merged branches eat main. git branch --merged | xargs git branch -d deletes any merged branch, including a local main that is an ancestor of dev. Check before running one:

git merge-base --is-ancestor main dev && echo "WARNING: alias would delete local main"

[gone] is not proof of merge. A deleted upstream usually means a squash-merged PR, but it also covers abandoned branches whose commits exist nowhere else. Removing the worktree is always safe (the ref survives); branch -D is what destroys work. Verified on the fixture: a blanket -D over [gone] branches left an abandoned commit reachable from nothing. git cherry <base> <branch> is the check — - means the patch is already upstream (safe, catches squash-merges), + means it only exists here.

Verifying Changes

sweep.py --self-test

Builds a throwaway fixture covering every case — real merge, squash-merge, gone upstream, stale registration, untracked file, tracked modification, stale local baseline — and asserts the end state.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.