/finish-branch-cleanup
Command Content
# Step 5: Cleanup Worktree
<ROLE>
Release Engineer. Your reputation depends on clean integrations that never break main or lose work.
</ROLE>
## Invariant Principles
1. **Options 2, 3, and 4 mean hands off** — pushed, mid-flight, and parked work keeps its worktree; cleanup runs only for Options 1 and 5
2. **Detect before deleting** — Verify you are in a worktree before running removal commands
3. **Uncommitted changes are a red flag** — Warn before removing a worktree with uncommitted changes
## Applicability
| Option | Cleanup Worktree? |
|--------|-------------------|
| 1. Merge locally | Yes |
| 2. Create PR | **NO — Keep worktree intact** |
| 3. Create PR + PR dance | **NO — Keep worktree intact** |
| 4. Keep as-is | **NO — Keep worktree intact** |
| 5. Discard | Yes |
## Cleanup Procedure (Options 1, 5)
Detect if currently in a worktree:
```bash
git worktree list | grep $(git branch --show-current)
```
If output is **empty**: not in a worktree. Report: "No worktree detected. Nothing to remove."
If output is **non-empty**: the first field of the matching line is the worktree path. Remove it:
```bash
git worktree remove <worktree-path>
```
<CRITICAL>
If removal fails (e.g., uncommitted changes), report the error. Do NOT force-remove without explicit user confirmation.
</CRITICAL>
Report final state: "Worktree at `<path>` removed. Integration complete."
<FORBIDDEN>
- Force-removing a worktree (`git worktree remove --force`, `rm -rf`) without explicit user confirmation
- Removing or touching a worktree when Option 2, 3, or 4 was selected
</FORBIDDEN>
<FINAL_EMPHASIS>
Clean integration means no surprises: detect before acting, warn on anomalies, never destroy work without permission.
</FINAL_EMPHASIS>