Worktree Cleanup banner
davila7 davila7

Worktree Cleanup

Development community intermediate

Description

Remove worktrees and branches that have been merged: $ARGUMENTS

Installation

This entry records only its repository, not the path inside it, so there is no exact command to give. Open the source below and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

Repository README

This is the README for davila7/claude-code-templates, shared by 7 entries in this directory. It describes the repository, not this entry specifically.


allowed-tools: Bash(git:*), Bash(rm:*), Bash(ls:*), Bash(pwd:*), Bash(grep:*) argument-hint: --all | --branch wt/name | --dry-run description: Clean up merged worktrees and their branches

Worktree Cleanup

Remove worktrees and branches that have been merged: $ARGUMENTS

Instructions

You are in the **main repository** (not a worktree). Clean up finished worktrees.

Step 1: Validate Environment

  1. Verify this is the main working tree (first entry in git worktree list)
  2. If inside a worktree, warn: "Run /worktree-cleanup from the main repo, not from a worktree."
  3. Fetch latest from origin: git fetch origin --prune
  4. Get the main branch name (main or master)

Step 2: Parse Arguments

Parse `$ARGUMENTS` for options:

  • --all — clean up ALL merged wt/* worktrees and branches
  • --branch wt/ — clean up a specific worktree/branch
  • --dry-run — show what would be cleaned up without doing anything
  • No arguments — list worktrees and ask which to clean up

Step 3: Identify Worktrees

  1. List all worktrees: git worktree list
  2. List all wt/* branches: git branch --list 'wt/*'
  3. For each wt/* branch, check if it's been merged into main:
    git branch --merged origin/ | grep 'wt/'
  4. Also check remote branches:
    git branch -r --merged origin/ | grep 'origin/wt/'

Step 4: Display Status

Show a table of all `wt/*` worktrees/branches:

| Branch | Worktree Path | Merged? | Action |
|--------|--------------|---------|--------|
| wt/login-page | ../worktrees/repo/wt-login-page | Yes | Will remove |
| wt/auth-bug | ../worktrees/repo/wt-auth-bug | No | Skipped (not merged) |

Step 5: Confirm and Execute

If `--dry-run` was specified, show the table and stop.

Otherwise, use AskUserQuestion to confirm cleanup (unless `--all` was specified with only merged branches).

For each **merged** worktree/branch to clean up:

  1. Remove the worktree:

    git worktree remove 

    If that fails (dirty worktree), warn and skip — **never force-remove**.

  2. Delete the local branch:

    git branch -d wt/

    Use `-d` (not `-D`) — this is safe because it only deletes merged branches.

  3. Delete the remote branch:

    git push origin --delete wt/

    If the remote branch doesn't exist, ignore the error silently.

Step 6: Prune

After all removals:

git worktree prune

Step 7: Summary

Show what was cleaned up:

Cleanup Complete
──────────────────────────────────
Removed:   worktree(s)
Deleted:   local branch(es)
Deleted:   remote branch(es)
Skipped:   unmerged branch(es)
──────────────────────────────────

If any unmerged branches were skipped, list them and suggest:

  • Merge the PR first, then run cleanup again
  • Or use git worktree remove and git branch -D wt/ manually if the work is truly abandoned