Clean up after a feature branch or experiment is complete. This skill deletes the git worktree and branch ONLY AFTER the associated PR has been merged. It also closes any related GitHub issues. Use this after your PR has been merged to keep the repository clean.
This skill handles the cleanup after a PR has been merged. It will:
IMPORTANT: This skill will NOT delete anything unless the PR is confirmed merged.
Invoke this skill when:
First, ask the user which branch to clean up:
Which branch would you like to finish?
Please provide:
1. Branch name (e.g., exp/cot-faithfulness or feature/add-dark-mode)
2. PR number (if known) or I can find it
CRITICAL: Before any deletion, verify the PR is merged:
# Find PR for the branch
gh pr list --head {branch-name} --state merged
# Or check specific PR
gh pr view {pr-number} --json state,mergedAt
# The PR MUST have state: "MERGED" before proceeding
If the PR is NOT merged:
## Cannot Proceed
The PR for branch '{branch-name}' is not yet merged.
Current state: {state}
Options:
1. Wait for the PR to be merged, then run /finish-branch again
2. Merge the PR now (if you have permissions)
3. Cancel cleanup
What would you like to do?
# Get the PR body and look for issue references
gh pr view {pr-number} --json body,closingIssuesReferences
# Common patterns to find:
# - "Closes #123"
# - "Fixes #123"
# - "Resolves #123"
# - Issue links in PR body
# List worktrees to find the one for this branch
git worktree list
# Remove the worktree
# The worktree path is usually ../worktree-{name} or ../experiment-{name}
git worktree remove ../worktree-{name}
# If the worktree has uncommitted changes, force removal (after user confirmation)
git worktree remove --force ../worktree-{name}
# Delete local branch
git branch -d {branch-name}
# If the branch wasn't fully merged (shouldn't happen if PR is merged)
# Ask user before force deleting
git branch -D {branch-name}
# Delete remote branch
git push origin --delete {branch-name}
# Close each related issue
gh issue close {issue-number} --comment "Closed via merged PR #{pr-number}"
## Branch Cleanup Complete
**Branch**: {branch-name}
**PR**: #{pr-number} (merged at {merged-at})
### Cleanup Actions
- [x] PR verified as merged
- [x] Worktree removed: ../worktree-{name}
- [x] Local branch deleted: {branch-name}
- [x] Remote branch deleted: origin/{branch-name}
- [x] Related issues closed: #{issue-1}, #{issue-2}
### Notes
{any-warnings-or-notes}
Could not find a PR for branch '{branch-name}'.
Options:
1. Provide the PR number manually
2. Check if the branch was pushed to remote
3. Cancel cleanup
No worktree found for branch '{branch-name}'.
This is fine if you were working in the main repository.
Proceeding with branch deletion...
WARNING: Branch '{branch-name}' has commits not in the merged PR.
Unmerged commits:
- {commit-hash}: {commit-message}
Options:
1. Force delete anyway (lose these commits)
2. Cherry-pick commits to a new branch first
3. Cancel cleanup
Could not close issue #{issue-number}: {error}
This might be because:
- Issue doesn't exist
- Issue is already closed
- Insufficient permissions
Proceeding with other cleanup tasks...
This skill includes several safety measures:
User: /finish-branch
Assistant: Which branch would you like to finish? Please provide the branch name or PR number.
User: exp/mi-estimation, PR #42
Assistant: Let me verify PR #42 is merged...
[Checks PR status]
PR #42 is merged. Proceeding with cleanup...
[Performs cleanup]
Branch cleanup complete!
User: /finish-branch feature/add-auth
Assistant: Found PR #55 for branch feature/add-auth.
PR is merged (merged 2 hours ago).
Found related issues: #50 (Closes), #51 (Fixes)
Proceeding with cleanup...
- Removed worktree: ../worktree-add-auth
- Deleted branch: feature/add-auth
- Closed issue #50
- Closed issue #51
All done!
The research-executor agent MUST use this skill for cleanup. The workflow is:
/finish-branch for cleanupThe research-executor will prompt:
Experiment complete and PR merged!
Would you like me to clean up the worktree and branch?
Upon confirmation, invoke /finish-branch with the experiment branch name.
Critical Rules:
This ensures experimental worktrees don't accumulate and the repository stays clean.