---
name: git-cleanup-merged
description: Clean up local branches and worktrees that have been merged into a target branch (default: main). Args: [target-branch]
---

# Git Cleanup Merged

Clean up local branches and worktrees whose changes have already been merged into a target branch.

## Args

`/git-cleanup-merged [target-branch]`

- `target-branch` — branch to check against. Default: `main`.

Refer to the target branch as `$TARGET` below.

## Workflow

### 1. Detect merged branches

Use the `git-detect-merged` skill with `$TARGET` to identify all merged branches, their merge type, active work status, and associated worktrees.

- Treat untracked files under `.tmp/` as ignorable temp output, not active work.

### 2. Build cleanup candidates

From the detection results:

- Include branches that are merged AND have no active work (status: `clean`).
- Include worktrees whose branch is merged AND clean.
- Exclude `$TARGET` and `main` — NEVER delete either.
- Exclude branches with active work (dirty or checked-out), even if merged.
- If `.tmp/` is the only untracked content, still treat worktree as clean.

### 3. Present summary

Show the user a single summary listing:

- **Worktrees to remove** (path + branch)
- **Branches to delete** (name + merge type: regular/squash). Mark squash-merged branches as requiring `-D` — `git branch -d` cannot see squash merges and always fails on them.
- **Skipped** — merged branches/worktrees excluded due to active work, with the reason (e.g. "has uncommitted changes", "currently checked out")

If nothing to clean, say so and stop.

### 4. Confirm

Ask the user for a single confirmation before proceeding. Do NOT proceed without approval. This confirmation covers `-D` for branches marked squash-merged in the summary — their merge was already verified by patch-id in detection.

### 5. Execute cleanup

- Remove worktrees first (`git worktree remove <path>`), then delete branches.
- Regular merges → `git branch -d <branch>`. Squash merges → `git branch -D <branch>` (covered by step 4 confirmation).
- If `-d` unexpectedly fails on a regular-merge branch, ask the user before using `-D`.
- Report results after each step.

## Rules

- NEVER delete `$TARGET` or `main`.
- NEVER touch remote branches or remote tracking refs.
- Use `-d` (safe delete) for regular merges. `-D` for squash-merged branches is pre-approved by the step 4 confirmation; any other `-D` requires explicit user approval.
- If a worktree removal fails, report the error and continue with the remaining items.
- If a branch delete fails, report the error and continue with the remaining items.
