Repository navigation
fix: safely delete dirty worktrees - #15
Merged
AgileIndustrialComplex merged 3 commits intoJul 20, 2026
Merged
Conversation
Mirrors the existing unmerged-branch force-delete flow: gitdata now detects dirty worktrees via `git status --porcelain`, the picker routes marked dirty worktrees through the same phrase-typed confirmation screen as unmerged branches, and action force-removes them with `git worktree remove --force` only after that explicit consent. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Intent
Handle deleting a Git branch/worktree that has untracked or modified files the same way the tool already handles deleting branches with unmerged changes. Previously, deleting a marked worktree with untracked/modified files failed outright with a git error ('contains modified or untracked files, use --force to delete it') since action.DeleteWorktrees called plain 'git worktree remove' with no force option and no detection ahead of time. The fix mirrors the existing Unmerged-branch design exactly: gitdata.Item gets a new Dirty flag, computed during collection via 'git status --porcelain' scoped to each worktree's path (git status --porcelain -C ). The UI's mark-then-D-then-confirm delete flow now also routes into the existing phrase-typed ('delete') force-confirmation screen (modeConfirmForceDelete) when any marked item is Dirty, not just when one is Unmerged; the confirmation screen shows separate sections listing unmerged branches and dirty worktrees when applicable, only listing whichever are present. action.DeleteWorktrees runs 'git worktree remove --force' instead of the plain form for Dirty items, and only after that explicit typed consent - never inferred automatically. Deliberately did not add any new confirmation phrase or config option; reused the existing ForceDeleteConfirmPhrase constant and mode, since the user's request was explicitly to handle this 'similarly to how unmerged branches are handled' rather than introduce a separate flow. Updated SPEC.md and README.md to document the merged unmerged+dirty confirmation screen, and added corresponding unit tests (gitdata_test.go, action_test.go, ui_test.go) plus one integration test that creates a real untracked file in a worktree and drives the picker through the force-delete flow end to end.
What Changed
deleteconfirmation flow, with separate warnings for dirty worktrees and unmerged branches.Risk Assessment
✅ Low: The fixes address both prior findings, and the complete change now consistently detects dirty worktrees, requires typed consent, and force-removes only flagged items.
Testing
Focused unit tests, the dirty-worktree integration test, and the full Go suite passed; manual terminal verification captured the dirty-worktree warning and typed
deleteflow, then confirmed the worktree and branch were removed with the source worktree left clean.Evidence: Dirty-worktree CLI confirmation transcript
Evidence: Post-deletion Git state
After typed confirmation, the scratch worktree directory no longer exists, the scratch branch is absent, and only the main worktree remains registered.Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed ✅
internal/gitdata/gitdata.go:225- Required intent says Dirty must identify worktrees with “untracked or modified files,” butgit status --porcelainrespectsstatus.showUntrackedFiles=no. In repositories with that setting, an untracked-only worktree is classified clean, bypasses phrase confirmation, and plain removal fails. Override the setting with--untracked-files=all(or equivalent).internal/gitdata/gitdata.go:127- Collection now aborts when a registered worktree path is missing or inaccessible.git worktree listcan legitimately returnprunableentries whose directories no longer exist, so the newgit -C <path> statuscall prevents the picker from opening at all. Parse theprunablemarker and avoid status probing for unavailable paths, or handle this failure separately.🔧 Fix: Handle hidden untracked files and prunable worktrees
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
go test ./internal/gitdata ./internal/action ./internal/uigo test . -run '^TestIntegrationDeleteDirtyWorktreeRequiresForcePhrase$' -vgo test ./...Built./cmd/wt, created a real Git worktree containinguntracked.txt, drove the terminal picker throughj,c,D,Enter,delete,Enter, and captured the rendered confirmation transcript.Verified withtest -e,git branch --list scratch, andgit worktree list --porcelainthat the dirty worktree directory and branch were removed while the main worktree remained.git status --shortafter testing confirmed no transient files were left in the working tree.✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.