WO

worktree-cleanup

Corrects the order of operations for removing Git branches linked to active worktrees.

Install

mkdir -p .claude/skills/worktree-cleanup && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/16324" && unzip -o skill.zip -d .claude/skills/worktree-cleanup && rm skill.zip

Installs to .claude/skills/worktree-cleanup

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Enforce the correct order when removing a branch that's pinned by a git worktree: `git worktree remove --force <path>` THEN `git branch -D <branch>`. Trying to delete the branch first fails with `error: cannot delete branch X used by worktree at Y`. Trigger this skill when: - User says "remove branch", "delete branch", "cleanup worktree", "prune branches". - `gh pr merge --delete-branch` errors with `worktree` in the message. - `git branch -D` errors with `used by worktree`. - After merging a PR that has an associated worktree.
533 chars✓ has a “when” triggerlonger than Claude Code's old 250-char listing cap (fine on current versions)
Beginner

Key capabilities

  • Detect the branch to clean up from user messages or PRs
  • List worktrees associated with a specified branch
  • Remove a git worktree forcefully if it exists
  • Delete a git branch after its worktree is removed
  • Report on removed branches and pruned worktrees
  • Clean up all merged branches that have associated worktrees

How it works

The skill detects a branch for cleanup, lists associated worktrees, and then executes 'git worktree remove --force <path>' followed by 'git branch -D <branch>'. If no worktree is found, it directly deletes the branch.

Inputs & outputs

You give it
User message indicating a branch to remove or a PR merge event
You get back
Confirmation of branch deletion and worktree removal

When to use worktree-cleanup

  • Cleaning up stale git branches
  • Removing git worktrees safely
  • Batch deleting merged branches

About this skill

The rule

Sequence is ALWAYS:

git worktree remove --force <path>   # step 1
git branch -D <branch>               # step 2

If you skip step 1, step 2 fails. gh pr merge --delete-branch behaves like step 2 — it also fails when a worktree pins the branch.

Behavior

  1. Detect the branch to clean up (from user's message or the PR).
  2. List worktrees touching the branch:
    git worktree list --porcelain | grep -B2 "branch refs/heads/<branch>"
    
  3. If a worktree exists at path <path>:
    • Show it to the user.
    • Run git worktree remove --force <path> (the --force handles dirty state in the worktree; warn user if there are uncommitted changes).
    • THEN run git branch -D <branch>.
  4. If no worktree exists for the branch:
    • Just git branch -D <branch>.
  5. Report what was removed.

Multiple worktrees on one branch

Impossible under git (each worktree pins its own unique branch). If detection shows multiple, that's a stale entry — clean with git worktree prune first, then retry.

Batch cleanup

If user asks "cleanup all merged branches with worktrees":

  1. git branch --merged dev (or main) for merged branches.
  2. For each: check worktree via git worktree list --porcelain.
  3. Run sequence per branch.
  4. Report tally: N branches removed, M worktrees pruned.

Do not

  • Never git branch -D before worktree remove — always error, waste of round-trip.
  • Never git worktree remove <path> without --force unless the worktree is spotless — the flag is standard in Nimbus flow.
  • Never delete a branch with unmerged commits without warning the user + showing them the commits that would be lost.
  • Never touch main, dev, master — those are protected default branches.

Cross-repo cleanup

If user asks "cleanup old branches across all my repos", iterate:

for r in ~/Development/cidca/cidca-platform ~/Development/cidca/cidca-services \
         ~/Development/horus/horus-v2 ~/Development/zotea; do
  cd "$r" && echo "=== $(basename $r) ===" && git branch --merged | grep -v -E "(main|dev|master|\*)"
done

Then present the list and let user choose which to remove.

When not to use it

  • When the user is requesting to delete a branch without first removing its worktree
  • When the user wants to remove a worktree without using the --force flag
  • When attempting to delete a protected branch like main, dev, or master

Limitations

  • Cannot delete a branch with unmerged commits without user warning
  • Does not touch protected default branches like main, dev, or master
  • Cannot handle multiple worktrees on one branch, requiring pruning stale entries first

How it compares

This skill automates the two-step process of removing a git worktree and then deleting its branch, preventing errors that occur when the branch is deleted first.

Compared to similar skills

worktree-cleanup side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
worktree-cleanup (this skill)01moNo flagsBeginner
resolve-conflicts818moReviewIntermediate
openspec-onboard106moReviewBeginner
codex-cli-bridge99moReviewIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry