| name | worktrees:finish |
| description | Use when completing work in a worktree. Handles PR creation or direct merge, worktree removal, and branch cleanup. Invoke with "/worktrees:finish" or when user mentions "finish worktree", "clean up worktree", "merge worktree", "done with worktree", or "complete worktree work". |
| argument-hint | [--pr | --merge] [--force] |
| version | 0.1.0 |
| arguments | [{"name":"--pr","description":"Create a pull request (default for peer workflow)","required":false},{"name":"--merge","description":"Direct merge to target branch (for orchestrator integration)","required":false},{"name":"--force","description":"Force cleanup even with uncommitted changes","required":false}] |
Finish Worktree
Complete work in a worktree and clean up.
Arguments
--pr - Create a pull request (default)
--merge - Direct merge to target branch
--force - Force cleanup with uncommitted changes
Pre-Completion Checklist
Before finishing, verify:
git status
npm test
git push -u origin $(git branch --show-current)
Option 1: Create Pull Request (--pr)
For peer workflows and standard code review.
Step 1: Ensure Up to Date
git fetch origin main
git rebase origin/main
git push --force-with-lease
Step 2: Create PR
gh pr create \
--base main \
--title "feat: <description>" \
--body "## Summary
<What this PR accomplishes>
## Changes
- <Bullet list of changes>
## Testing
<How to test>
"
Step 3: After PR Merge
cd ../..
git checkout main
git pull origin main
git worktree remove .worktrees/<name>
git branch -d feature/<name> 2>/dev/null
git worktree prune
Option 2: Direct Merge (--merge)
For orchestrator integration where work merges directly.
Step 1: Switch to Target Branch
cd /path/to/main/project
git checkout <target-branch>
Step 2: Merge Worktree Branch
git merge feature/<name> --no-ff -m "Merge <description>"
Step 3: Run Tests
npm test
Step 4: Cleanup
git worktree remove .worktrees/<name>
git branch -d feature/<name>
git push origin --delete feature/<name>
git worktree prune
Force Cleanup (--force)
When worktree has uncommitted changes and you want to discard:
git worktree remove --force .worktrees/<name>
git branch -D feature/<name>
git worktree prune
Warning: --force discards uncommitted work permanently.
Complete Cleanup Script
For cleaning up multiple worktrees:
#!/bin/bash
NAME=$1
BRANCH="feature/${NAME}"
WORKTREE=".worktrees/${NAME}"
if git worktree list | grep -q "$WORKTREE"; then
git worktree remove "$WORKTREE" || git worktree remove --force "$WORKTREE"
fi
git branch -d "$BRANCH" 2>/dev/null || git branch -D "$BRANCH" 2>/dev/null
git push origin --delete "$BRANCH" 2>/dev/null
git worktree prune
echo "Cleanup complete for $NAME"
Verification
After cleanup:
git worktree list
git branch -a | grep <name>
Troubleshooting
Cannot Remove: Uncommitted Changes
Error: fatal: cannot remove worktree with uncommitted changes
Solutions:
- Commit your changes:
git add . && git commit -m "WIP"
- Stash changes:
git stash
- Force remove (loses changes):
git worktree remove --force .worktrees/<name>
Cannot Remove: Worktree Locked
Error: fatal: worktree is locked
Solution:
git worktree unlock .worktrees/<name>
git worktree remove .worktrees/<name>
Branch Cannot Be Deleted
Error: error: branch is not fully merged
Solutions:
- If work is merged via PR, force delete:
git branch -D feature/<name>
- If work needs preserving, merge first
Stale Worktree Entry
If worktree directory was manually deleted:
git worktree prune
Quick Reference
| Task | Command |
|---|
| Remove worktree | git worktree remove .worktrees/<name> |
| Force remove | git worktree remove --force .worktrees/<name> |
| Delete local branch | git branch -d feature/<name> |
| Force delete branch | git branch -D feature/<name> |
| Delete remote branch | git push origin --delete feature/<name> |
| Prune stale entries | git worktree prune |
| Create PR | gh pr create --base main |
| Merge PR | gh pr merge --squash --delete-branch |