Use after your PR has been merged to main, before starting new work
Remove the worktree and feature branch after a PR has been merged. Keeps your workspace clean.
Announce at start: "I'm using envoy:cleanup to remove the worktree and branch."
Before cleanup, verify:
# 1. PR is actually merged
gh pr view --json state,mergedAt | jq -r '.state, .mergedAt'
# Should show: MERGED and a timestamp
# 2. You're in the worktree (not main repo)
pwd
# Should be in .worktrees/<topic>
If PR is not merged, do NOT clean up.
Discipline rule: Every step in this skill MUST be executed as written, not narrated.
git branch --show-current and capture outputThis is a cleanup operation. Mistakes here mean lost work. Execute exactly.
BRANCH=$(git branch --show-current)
WORKTREE_PATH=$(pwd)
MAIN_REPO=$(git worktree list | grep -v ".worktrees" | awk '{print $1}')
cd "$MAIN_REPO"
git checkout main
git pull origin main
Remove ephemeral session files from the worktree before it is removed:
# Clear session state before removing worktree
rm -f "$WORKTREE_PATH/.envoy-session.json"
rm -f "$WORKTREE_PATH/.envoy-scratchpad.json"
If docs/wiki/ was changed on the branch, sync to the GitHub wiki:
# Check if docs/wiki/ was changed on this branch
WIKI_CHANGED=$(git diff main...HEAD --name-only | grep "^docs/wiki/" | wc -l | tr -d ' ')
if [ "$WIKI_CHANGED" -gt 0 ]; then
# Run wiki sync from within the worktree before removing it
/envoy:wiki-sync
fi
If wiki-sync fails: STOP. Do NOT proceed to Step 6 (worktree removal). Removing the worktree before a successful wiki sync would permanently lose the documentation changes. Diagnose and fix the wiki-sync failure first, then re-run cleanup.
git worktree remove "$WORKTREE_PATH"
If there are uncommitted changes (shouldn't happen if PR is merged):
git worktree remove --force "$WORKTREE_PATH"
Local branch:
git branch -d "$BRANCH"
Remote branch (if not auto-deleted by GitHub):
git push origin --delete "$BRANCH"
Remove the workflow label from the linked issue:
# Get the PR number for this branch
PR_NUMBER=$(gh pr list --head "$BRANCH" --state merged --json number --jq '.[0].number')
# Get the linked issue number from the PR body
ISSUE_NUMBER=$(gh pr view "$PR_NUMBER" --json body --jq '.body' | grep -oE 'Closes #[0-9]+|Fixes #[0-9]+|Resolves #[0-9]+' | grep -oE '[0-9]+' | head -1)
if [ -n "$ISSUE_NUMBER" ]; then
gh issue edit "$ISSUE_NUMBER" --remove-label "in progress" 2>/dev/null || true
fi
Close the GitHub issue linked to this PR:
if [ -n "$ISSUE_NUMBER" ]; then
# Close the issue if not already closed
ISSUE_STATE=$(gh issue view "$ISSUE_NUMBER" --json state --jq '.state')
if [ "$ISSUE_STATE" != "CLOSED" ]; then
gh issue close "$ISSUE_NUMBER" --comment "Completed via PR #$PR_NUMBER"
echo "✓ Issue #$ISSUE_NUMBER closed"
else
echo "✓ Issue #$ISSUE_NUMBER already closed"
fi
else
echo "⚠ No linked issue found in PR"
fi
# Worktree removed
git worktree list
# Branch deleted
git branch -a | grep "$BRANCH"
# Should return nothing
# Session state cleared
[ ! -f "$WORKTREE_PATH/.envoy-session.json" ] && echo "✓ session.json gone" || echo "✗ session.json still exists"
[ ! -f "$WORKTREE_PATH/.envoy-scratchpad.json" ] && echo "✓ scratchpad.json gone" || echo "✗ scratchpad.json still exists"
# Issue closed
gh issue view "$ISSUE_NUMBER" --json state --jq '.state'
# Should show: CLOSED
**Cleanup complete**
| Item | Status |
|------|--------|
| Session state | ✓ Cleared |
| Wiki | ✓ Synced / ⊘ No changes |
| Worktree | ✓ Removed |
| Local branch | ✓ Deleted |
| Remote branch | ✓ Deleted |
| "in progress" label | ✓ Removed |
| Issue #<number> | ✓ Closed |
You're now on `main` with a clean workspace.
Ready for next task.
**Cannot cleanup: PR not merged**
PR state: <state>
Wait for PR to be merged before cleaning up.
To check PR status: gh pr view
**Worktree has uncommitted changes**
Changes:
<git status output>
Options:
1. Commit and push (then re-run cleanup)
2. Discard changes (git checkout .)
3. Force remove (loses changes)
Choice?
**Could not delete branch: <branch>**
This might happen if:
- Branch has unmerged commits
- Branch is protected
To force delete: git branch -D <branch>
| Flag | Effect |
|---|---|
| (none) | Clean up current worktree/branch |
<branch-name> | Clean up specific branch |
--force | Force remove even with uncommitted changes |
--all | Clean up ALL merged worktrees |
With --all flag, find and clean all worktrees whose PRs are merged:
# List all worktrees
git worktree list
# For each worktree (except main):
# Check if branch's PR is merged
# If merged, remove worktree and branch