|
| 1 | +--- |
| 2 | +name: worktree-status |
| 3 | +description: Audit all git worktrees in the current project. Use when the user asks about worktree status, which branches are merged, which have uncommitted changes, or which worktrees can be safely cleaned up. |
| 4 | +--- |
| 5 | + |
| 6 | +# worktree-status |
| 7 | + |
| 8 | +Report the status of every git worktree for the current project, covering |
| 9 | +dirty state and merge status. |
| 10 | + |
| 11 | +## When to use |
| 12 | + |
| 13 | +- User asks "which worktrees can I clean up?" |
| 14 | +- User asks "what's the status of my worktrees / branches?" |
| 15 | +- Before batch-cleaning worktrees, to avoid losing uncommitted work |
| 16 | + |
| 17 | +## Procedure |
| 18 | + |
| 19 | +### 1. Pull latest main (MANDATORY) |
| 20 | + |
| 21 | +You MUST pull latest main before any status checks. Without this, merge |
| 22 | +detection (both ancestry and content diff) will produce stale results and |
| 23 | +you may mistakenly conclude a branch is not merged. |
| 24 | + |
| 25 | +```bash |
| 26 | +cd "$(git rev-parse --show-toplevel)" && git pull origin main |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Collect worktree info |
| 30 | + |
| 31 | +```bash |
| 32 | +PROJECT_DIR="$(git rev-parse --show-toplevel)" |
| 33 | + |
| 34 | +for wt in $(git worktree list --porcelain | grep "^worktree " | sed 's/^worktree //' | grep -v "$PROJECT_DIR$"); do |
| 35 | + branch=$(git -C "$wt" branch --show-current 2>/dev/null) |
| 36 | + [ -z "$branch" ] && branch="(detached)" |
| 37 | + name=$(basename "$wt") |
| 38 | + |
| 39 | + # dirty? |
| 40 | + if [ -z "$(git -C "$wt" status --short 2>/dev/null)" ]; then |
| 41 | + dirty="clean" |
| 42 | + else |
| 43 | + dirty="DIRTY" |
| 44 | + fi |
| 45 | + |
| 46 | + # merged into origin/main? |
| 47 | + # NOTE: `git merge-base --is-ancestor` does NOT detect squash-merged |
| 48 | + # branches. Always follow up with a content diff (step 3) for branches |
| 49 | + # that appear "not merged". |
| 50 | + if [ "$branch" != "(detached)" ]; then |
| 51 | + if git merge-base --is-ancestor "$branch" origin/main 2>/dev/null; then |
| 52 | + merged="merged" |
| 53 | + else |
| 54 | + merged="not merged (verify with content diff)" |
| 55 | + fi |
| 56 | + else |
| 57 | + merged="n/a" |
| 58 | + fi |
| 59 | + |
| 60 | + echo "" |
| 61 | + echo "[$name] branch=$branch $dirty $merged" |
| 62 | + if [ "$dirty" = "DIRTY" ]; then |
| 63 | + git -C "$wt" status --short 2>/dev/null | sed 's/^/ /' |
| 64 | + fi |
| 65 | +done |
| 66 | +``` |
| 67 | + |
| 68 | +### 3. Detect squash-merged branches (content diff) |
| 69 | + |
| 70 | +For any branch that shows "not merged", check whether the branch's |
| 71 | +changes are already in main. The correct method is: |
| 72 | + |
| 73 | +1. Find the files the branch actually changed (relative to merge-base). |
| 74 | +2. For each changed file, compare the branch version with main. |
| 75 | + If all files are identical, the branch was squash-merged. |
| 76 | + |
| 77 | +**⚠️ Do NOT use `git diff origin/main <branch>`** — that compares the |
| 78 | +two tips directly, so commits added to main *after* the branch diverged |
| 79 | +will show up as false differences. |
| 80 | + |
| 81 | +```bash |
| 82 | +BRANCH="<branch>" |
| 83 | +BASE=$(git merge-base origin/main "$BRANCH") |
| 84 | + |
| 85 | +# List files the branch touched |
| 86 | +FILES=$(git diff --name-only "$BASE" "$BRANCH") |
| 87 | + |
| 88 | +# Compare each file between branch and current main |
| 89 | +for f in $FILES; do |
| 90 | + d=$(git diff "$BRANCH" origin/main -- "$f" | wc -l) |
| 91 | + if [ "$d" != "0" ]; then |
| 92 | + echo "❌ $f — differs" |
| 93 | + else |
| 94 | + echo "✅ $f — identical in main" |
| 95 | + fi |
| 96 | +done |
| 97 | +# All ✅ = squash-merged |
| 98 | +``` |
| 99 | + |
| 100 | +### 4. Present results |
| 101 | + |
| 102 | +**Always present results as a Markdown table.** Every worktree must appear |
| 103 | +as a row. Never use abbreviated or prose-only summaries. |
| 104 | + |
| 105 | +| Worktree | Branch | Dirty | Merged | Can clean? | |
| 106 | +|---|---|---|---|---| |
| 107 | +| `example-wt` | `feat-foo` | ✅ clean | ✅ squash-merged | ✅ | |
| 108 | +| `another-wt` | `fix-bar` | ⚠️ 3 files | ❌ not merged | ❌ dirty + not merged | |
| 109 | +| `detached-wt` | (detached) | ⚠️ 14 files | n/a | ❌ has uncommitted changes | |
| 110 | + |
| 111 | +Column definitions: |
| 112 | + |
| 113 | +- **Dirty**: `✅ clean` or `⚠️ N files` |
| 114 | +- **Merged**: `✅ merged` / `✅ squash-merged` (confirmed via content diff) / `❌ not merged` / `n/a` |
| 115 | +- **Can clean?**: `✅` only when merged (or squash-merged) AND clean |
| 116 | + |
| 117 | +Add extra columns (e.g. notes) only when relevant. |
| 118 | + |
| 119 | +### 5. Cleanup (only when asked) |
| 120 | + |
| 121 | +Only clean worktrees the user explicitly approves. For each: |
| 122 | + |
| 123 | +```bash |
| 124 | +NAME="<worktree-name>" |
| 125 | +git worktree remove "/path/to/$NAME" |
| 126 | +git branch -D "<branch>" # only if the branch is no longer needed |
| 127 | +``` |
0 commit comments