Skip to content

Clean up merged worktrees and branches with one command - #57

Merged
taspinar merged 1 commit into
mainfrom
feature/56-cleanup-worktree
Oct 2, 2026
Merged

taspinar merged 1 commit into
mainfrom
feature/56-cleanup-worktree

Conversation

@taspinar

@taspinar taspinar commented Oct 2, 2026

Copy link
Copy Markdown
Owner

What changed

  • ./scripts/cleanup-worktree.sh <issue-number> finds the worktree of feature/<issue>-*; planning/<name> finds a planning worktree; a path keeps working, for any branch.
  • It runs only from the primary checkout and asks GitHub whether the branch's pull request is merged, so it also works after a squash merge.
  • It refuses an unmerged worktree, a branch with commits after its merged pull request head, and a worktree with uncommitted changes. Ignored review and triage files do not block it.
  • After a merge it removes the worktree and the local branch, prunes worktree metadata, and fast-forwards main in a clean primary checkout of main (otherwise it fetches and says so).
  • --merged removes every merged worktree and lists the kept ones with the reason; --discard removes an unmerged worktree after confirmation.
  • A failed removal or branch deletion is reported as an error with a non-zero status.
  • finish-feature.sh and finish-planning.sh mention the cleanup step; README, docs/development.md, docs/workflow.md, docs/example.md, and docs/project-map.md use the new form.

Issue / acceptance criteria

Closes #56

Risk

  • Low
  • Medium
  • High

Verification evidence

  • ./scripts/verify.sh passed, also without global or system Git configuration
  • Tests added/updated where appropriate (tests/cleanup-worktree-test.sh with real worktrees and squash merges on a local remote, also passing under macOS system bash 3.2)
  • Independent review completed when required
  • Architecture/docs/ADR updated when required

Independent review: Codex (gpt-6-astra), read-only. CHANGES REQUIRED:

  • M1 (a failed removal reported success, because the cleanup runs in a condition that disables errexit): fixed; every destructive step checks its status and failures exit non-zero. Tested with a locked worktree; a failing branch deletion is handled the same way but not separately tested.
  • MIN1 (the primary-checkout rule was not enforced): fixed, with a test from a linked worktree.
  • MIN2 (paths only worked for feature and planning branches): fixed, with a fix/ branch test.

Not re-reviewed after the fixes.

Agent involvement

Planner: —
Implementer: Claude (Opus 5.5)
Reviewer: Codex (gpt-6-astra)

Production impact

None.

🤖 Generated with Claude Code

cleanup-worktree.sh <issue>, planning/<name>, or a path removes a
worktree and its local branch once GitHub reports its pull request as
merged, which also works after a squash merge, and fast-forwards main.
It runs only from the primary checkout and refuses unmerged, dirty, and
ahead-of-PR worktrees; ignored review files do not count. --merged
cleans every merged worktree, and --discard removes an unmerged one
after confirmation. Failed removals are reported as errors.

Closes #56

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@taspinar
taspinar merged commit e2b4788 into main Oct 2, 2026
1 check passed
@taspinar
taspinar deleted the feature/56-cleanup-worktree branch October 2, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean up merged worktrees and branches with one command

1 participant