feat(rm): wt rm --claude and wt merged --rm to clean up worktrees + sessions - #4
Open
stilliard wants to merge 1 commit into
Open
feat(rm): wt rm --claude and wt merged --rm to clean up worktrees + sessions#4stilliard wants to merge 1 commit into
stilliard wants to merge 1 commit into
Conversation
…essions - `wt rm <name> --claude` removes the worktree and then deletes its Claude Code sessions via the official `claude rm <id>` (sessions are matched with the same job-state-enriched attribution as the tables, and only deleted after the worktree removal actually succeeded). - `wt merged --rm [base]` bulk-removes everything `wt merged` lists, showing the list and asking for confirmation first (-y/--yes to skip); with --claude it deletes each worktree's sessions too. - The main working tree is never removed, even when it happens to be checked out on a merged branch - it's skipped with a warning. - Each removal goes through _wt_rm, so pre-rm/post-rm hooks fire per worktree as usual; _wt_rm now also propagates git worktree remove's exit code instead of swallowing it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds safe cleanup workflows for merged Git worktrees, including optional deletion of related Claude Code sessions.
Changes:
- Add
wt merged --rm(with confirmation /-y) to remove merged worktrees while never deleting the main worktree - Add
wt rm --claudeandwt merged --rm --claudeto delete Claude Code sessions tied to removed worktrees - Expand Bats test coverage and update README/usage text for the new flags
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| wt.sh | Implements --rm for merged worktrees and adds session deletion via --claude during removal |
| test/merged.bats | Adds tests for wt merged --rm behavior (confirmation, selective removal, main worktree skip) |
| test/claude/rm.bats | Adds tests for wt rm --claude session deletion behavior and failure handling |
| test/claude/merged.bats | Adds integration test for wt merged --rm --claude -y |
| README.md | Documents new removal flags and Claude session cleanup behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+241
to
244
| local rc=0 | ||
| git worktree remove "$target" || rc=$? | ||
| _WT_HOOK_ROOT="$root" _wt_run_hook post-rm "$1" "$target" | ||
| _wt_run_adhoc_hook "$post_hook" "$1" "$target" |
Comment on lines
+245
to
+248
| if [ "$claude" -eq 1 ] && [ "$rc" -eq 0 ]; then | ||
| _wt_claude_rm_sessions "$target" | ||
| fi | ||
| return "$rc" |
Comment on lines
+334
to
+340
| if [ "$show_claude" -eq 1 ]; then | ||
| _wt_rm --claude "$branch" | ||
| else | ||
| _wt_rm "$branch" | ||
| fi | ||
| done <<< "$list" | ||
| return 0 |
Comment on lines
+132
to
+134
| printf '%s' "$_WT_CLAUDE_JSON" | "$_WT_JQ_BIN" -r --arg wt "$path" \ | ||
| '.[] | select(.cwd == $wt) | .id' | while IFS= read -r id; do | ||
| [ -z "$id" ] && continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #3 (targets that branch; retarget to
mainonce it merges).wt rm <name> --claude— removes the worktree, then deletes its Claude Code sessions via the officialclaude rm <id>. Sessions are matched using the same job-state-enriched attribution as the--claudetables, and only deleted after the worktree removal actually succeeded (a dirty worktree thatgit worktree removerefuses leaves the sessions untouched).wt merged --rm [base]— bulk-removes everythingwt mergedlists, showing the list/table and asking for confirmation first;-y/--yesskips the prompt. Add--claudeto delete each worktree's sessions too._wt_rm, sopre-rm/post-rmhooks fire per worktree as usual._wt_rmnow also propagatesgit worktree remove's exit code instead of swallowing it.Test plan
bats -r test— 64/64 passing (8 new tests: confirmation accept/abort, unmerged kept, main-worktree skip, session deletion via stubbedclaude rm, no-session case, failed-removal-keeps-sessions)n; nothing removed)🤖 Generated with Claude Code