worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Stash on `wt remove -f`

Open yajo opened this issue 1 month ago • 3 comments

When running wt remove something, if the worktree contains dirty changes, it refuses to remove.

When running with -f, it removes everything.

Would it make sense that -f stores dirty stuff as a stash, just in case you force-removed but later change your mind?

yajo avatar Oct 21 '26 07:10 yajo

Worktrunk already does something close to this, just not for remove. Before wt merge squashes, it saves the index as a commit under refs/wt-backup/<branch> and records each save in that ref's reflog (create_safety_backup). A --force removal could save the dirty state the same way. Right now the force path only prints the files it's about to discard and then deletes the directory (stage_worktree_removal).

I checked the git side by hand: running git stash push -u inside a linked worktree, then git worktree remove and git branch -D, leaves the stash entry visible from the main worktree. The stash list is shared across a repo's worktrees, so the snapshot outlives both the worktree and the branch. That covers tracked edits and untracked files. Ignored files, such as build output, would still be lost.

A maintainer would need to settle a few design questions before this could be built:

  • Where the snapshot goes. If it goes in the stash list, plain git stash list / git stash apply can recover it, but it also shows up in every other worktree's stash list. A refs/wt-backup/-style ref keeps it out of the way, though recovering it takes a less familiar command. That ref is the approach wt merge already takes.
  • Always or opt-in. Saving every time costs one snapshot per force-remove and keeps -f from losing anything you can't get back. An opt-in flag would grow the CLI, which the project tends to avoid.
  • Cleanup. Saved snapshots pile up over time, and nothing prunes them today.

The project's data-safety guidelines already prefer recoverable outcomes, so I've left this open for a maintainer to decide rather than opening a PR.

worktrunk-bot avatar Oct 21 '26 07:10 worktrunk-bot

this could work quite well

@worktrunk-bot make a PR that does a similar approach to what we do before squashing

max-sixty avatar Oct 21 '26 08:10 max-sixty

Opened #4545. wt remove --force now saves the dirty state to refs/wt-backup/<branch>, the same ref and reflog the squash backup uses, and prints a wt switch --create --base <sha> command to recover it. Staged, unstaged, and untracked files are saved; ignored files are not. If the backup fails, the removal is refused. The PR body flags that choice, and the cost of large unignored files, for review.

worktrunk-bot avatar Oct 21 '26 08:10 worktrunk-bot