AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified Apache-2.0 Self-run

Cleanup Worktrees

skill-space-dinosaurs-dinostack-cleanup-worktrees · by Space-Dinosaurs

Clean up stale git worktrees and local branches in the current repository. Covers both worktree removal and local branch prune - see `content/references/worktree-lifecycle.md` §Branch prune for the ca

No reviews yet
0 installs
36 views
0.0% view→install

Install

$ agentstack add skill-space-dinosaurs-dinostack-cleanup-worktrees

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-space-dinosaurs-dinostack-cleanup-worktrees)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
2mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Cleanup Worktrees? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

/cleanup-worktrees

> Run the Activation preflight from METHODOLOGY.md before proceeding. If inactive, no-op and exit.

Clean up stale git worktrees and local branches in the current repository. Covers both worktree removal and local branch prune - see content/references/worktree-lifecycle.md §Branch prune for the canonical branch-prune command block.

Use proactively after finishing a task, when a PR is merged, when worktrees are accumulating, or any time you want to confirm the repo is in a clean state. Also invoke when the user says "prune worktrees", "clean up branches", "tidy the repo", or "remove stale worktrees". Works in any git repo.

Execution model

Run all steps directly in the conductor session via Bash - do NOT spawn background agents. Worktree cleanup is sequential and fast.


Step 1: Fetch and prune metadata

git fetch origin 2>/dev/null || true
git worktree prune

git fetch origin is non-fatal - repos without a remote (local test repos, offline environments) will fail here and that is fine. Always continue. git worktree prune removes entries pointing to directories that no longer exist on disk.


Step 2: List active worktrees

git worktree list

The first entry is always the main worktree - the repo root directory. Skip it unconditionally regardless of what branch it is on.

Categorize each remaining entry by its branch name:

  • Isolation worktrees - branch matches worktree-agent-*. Temporary agent sandboxes. Go to Step 3.
  • Feature worktrees - branch matches feature/*, fix/*, or chore/*. Long-lived task branches. Go to Step 4.
  • Anything else - report it to the user and skip removal.

Step 3: Remove isolation worktrees

For each isolation worktree, check its status before touching it:

git -C  status --porcelain

There are three cases:

Directory does not exist (command errors with "not a git repository" or similar): The directory was already removed before this command ran. Run git worktree prune to clean the stale metadata, then delete the branch.

Directory exists, clean (no output): Remove the worktree and delete the branch:

git worktree remove 
git branch -D 

Directory exists, dirty (output present): List the dirty files and skip removal. Report to the user - do not remove without explicit confirmation. Uncommitted work in an agent worktree may be important.


Step 4: Remove feature worktrees with merged PRs

For each feature worktree, check whether its PR has been merged:

gh pr list --state all --head  --json number,state,title

If state is MERGED: remove the worktree and delete the branch:

git worktree remove 
git branch -D 

If state is OPEN or CLOSED (not merged): skip removal. Report the branch name, PR number, and state to the user so they can decide.

If no PR exists: skip removal. Report the branch as having no PR and needing manual review.

If gh is not available: skip the PR check for all feature worktrees. Report each feature worktree as "needs manual review - gh CLI not available". Do not block or error.


Step 5: Prune stale local branches

Run the canonical branch prune from content/references/worktree-lifecycle.md §Branch prune (stale local branches). It targets three classes of stale local branch with safe signals only - branches with no upstream and not merged into origin/main are left alone and reported to the user for manual review.


Step 6: Final state report

git worktree prune
git worktree list

Report a summary:

  • What was removed (worktree path + branch name for each)
  • What was skipped (branch name + reason: dirty, PR open, no PR, unknown type)
  • Final worktree count

Notes

  • Safety first: never remove a worktree with uncommitted changes without explicit user confirmation. The status check in Step 3 is not optional.
  • Never remove a feature worktree whose PR is still OPEN. Only MERGED PRs are safe to clean up automatically.
  • The main worktree (first entry in git worktree list) is always skipped.
  • Works on the repository in the current working directory - not project-specific.
  • If gh is not available, flag feature worktrees for manual review and continue.

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.