Skip to content

gc-worktrees.sh's --prune-branches uses git branch -d, which refuses genuinely squash-merged branches #2310

Closed

Description

@carlos-alm

Follow-up from #2309 (same root cause, third occurrence, found while fixing the other two).

`.claude/scripts/gc-worktrees.sh` correctly classifies a worktree as "stale" via GitHub PR state (`gh pr list --state all --json headRefName,state`, not git ancestry) — see the script's own `classify()` function and its documented rationale for why ancestry is unreliable under squash merges.

But the actual branch-deletion step (`--prune-branches`) still uses plain `git branch -d`:

```bash

line ~216

if git -C "$main_root" branch -d "$b" >/dev/null 2>&1; then echo " branch deleted $b"; fi
```

`-d`'s own internal check is ancestry-based, independent of the `gh`-based classification that selected the branch as safe to delete. Verified directly (see #2309): once stale remote-tracking refs are pruned, `git branch -d` refuses a branch that GitHub confirms is genuinely merged via squash, because the local branch tip is never an ancestor of the squash commit on the default branch. Since `classify()` already ran the authoritative check, this refusal is silent — the script just doesn't print "branch deleted" for that branch and moves on, so the branch quietly survives every future `--prune-branches` run indefinitely.

Suggested fix: use `git branch -D` for branches `classify()` already confirmed merged via `gh` PR state — exactly the same fix applied to `housekeep`'s Phase 4d and Phase 1e in #2309's PR, with the same justification: this isn't a blanket force-delete, it's using the authoritative GitHub signal instead of git's unreliable-under-squash-merge ancestry heuristic.

Activity

  1. carlos-alm commented on Aug 7, 2026

    @carlos-alm
    ContributorAuthor

    Related finding from Greptile's review of PR #2311 (the housekeep/fixer fix this issue is a follow-up to) — worth fixing together with the -d-vs--D issue above, since both live in the same classify() function / call site:

    `classify()` matches a worktree's branch to "settled" purely by name against `gh pr list`'s `headRefName` history — it never checks whether the branch's current commit is the one that actually got merged. If a branch name is reused after its earlier PR merged (a later run, or a human, creating a new branch of the same name for unrelated work), `classify()` would still return "settled" for the new, never-merged commits, and --prune-branches's git branch -d/-D would delete them.

    The fix applied to `housekeep`'s Phase 4a in #2311 pins the check to `headRefOid` (the exact SHA GitHub recorded as the merged PR's head), not just `headRefName` — comparing the local branch's current tip SHA against that recorded SHA before treating it as safe to delete. The same binding should apply to `gc-worktrees.sh`'s `classify()` when fixing the `-d`-vs-`-D` bug, so both bugs (wrong deletion command, and name-only matching) get closed in the same pass rather than one being reintroduced while fixing the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions