worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Feature request: Batch removal of integrated branches with `wt prune`

Open prassanna-ravishankar opened this issue 9 months ago • 12 comments

Problem

After working with multiple feature branches that get merged into the default branch, I end up with many worktrees for branches that are already integrated. Currently, I need to manually run wt remove for each merged branch to clean them up, which is tedious and time-consuming.

Proposed Solution

Add a wt prune command that automatically identifies and removes all integrated branches and their worktrees in a single operation.

Example Usage

# See what would be removed
wt prune --dry-run

# Remove all integrated branches
wt prune

# Skip confirmation
wt prune --yes

# Only remove specific patterns
wt prune --pattern='feature/*'

Expected Behavior

The command should:

  • Identify branches that are fully integrated into the default branch (or a specified target)
  • Include prunable worktrees (missing directories)
  • Apply the same safety protections as wt remove:
    • Never remove the current branch
    • Never remove the default branch
    • Never remove locked worktrees
  • Run post-remove hooks for each removal
  • Show a summary of what will be removed and ask for confirmation (unless --yes is used)
  • Support pattern filtering to prune only specific branches

Benefits

  • Reduces manual cleanup work when working with many feature branches
  • Maintains the same safety-first principles as wt remove
  • Provides batch operations while still running hooks individually
  • Helps keep the worktree list clean and manageable

Alternative Considered

Continue using wt remove manually for each branch, but this doesn't scale well for workflows with many short-lived feature branches.

prassanna-ravishankar avatar Feb 08 '26 11:02 prassanna-ravishankar

I've thought about this

one practical problem: how does it discriminate between a worktree that finished vs just started?

when do you find that wt remove isn't sufficient for the clean-up?

max-sixty avatar Feb 08 '26 14:02 max-sixty

My thought process:-

It doesn't. Both are safe to prune because neither has unique work:

  • Just started: same_commit(branch, main) returns true — branch points to same commit as main, no work done yet
  • Finished (merged): same_commit() or is_ancestor() returns true — branch's work is already in main's history

The real check is whether the branch has commits that aren't in main? If no → safe to prune.

Work-in-progress branches fail these checks and won't be pruned (unless --force).

prassanna-ravishankar avatar Feb 08 '26 18:02 prassanna-ravishankar

but what if someone has just a started a session on a new worktree?

max-sixty avatar Feb 08 '26 18:02 max-sixty

if its not dirty , it'll be pruned, which is okay right? there's a good chance you want to do prune to clean up? unless you have something else in mind?

I feel like more than necessary application logic here will result in a lot of corner cases?

prassanna-ravishankar avatar Feb 08 '26 22:02 prassanna-ravishankar

I appreciate the contribution; at the moment I don't think it's robust enough to merge; there will be too many false-positives

and wt remove should clean up each worktree

I'll leave this open if anyone has well-thought through ideas

max-sixty avatar Feb 09 '26 01:02 max-sixty

would git reflog be a potential solution?

$ git reflog show --date=iso example                                                                                                                                                         
  b0a6072e example@{2026-02-15 14:38:24 -0700}: branch: Created from HEAD 

Check is non dirty and 60 min old or something like that. Could be conservative, so if not present in reflog cache than it also gets skipped.

davidbeesley avatar Feb 16 '26 01:02 davidbeesley

Check is non dirty and 60 min old or something like that.

I think this might work, but seems a bit arbitrary...

if someone can run a script in their own workflow that does something like this, and confidently announce "this actually ended up being really useful for the past few weeks", I would update on that...

max-sixty avatar Feb 16 '26 06:02 max-sixty

In my custom tool (which I'm trying to migrate from to worktrunk) I have wtree prune which removes all worktrees on merged branches, and it works great. I basically run it once a day or so.

what if someone has just a started a session on a new worktree

In that situation I just wouldn't run prune. Maybe it could refuse to remove the cwd worktree

jameshfisher avatar Feb 25 '26 21:02 jameshfisher

I was looking for something like this too. I currently have some script to remove merged branches that does something like this: git branch -vv | grep -E ": gone\]". It seems that merged branches (ie. once pushed, but then removed from origin, have a gone suffix in git)

ngotchac avatar Feb 25 '26 21:02 ngotchac

I have this working fine:

git-clean-wt: aliased to git branch -vv \\n  | grep -E ": gone\]" \\n  | grep -E '^\+' \\n  | awk '{print $2}' \\n  | fzf -m --sync --bind start:last+select-all --bind q:abort \\n  | xargs -I% wt remove -f -D -y %\n

ngotchac avatar Feb 25 '26 21:02 ngotchac

fyi this is coming! https://github.com/max-sixty/worktrunk/pull/1191

max-sixty avatar Feb 26 '26 02:02 max-sixty

Resolved by wt step prune in #1191 (landed in v0.28.0 as commit 35c2172b).

The implementation covers the core request — batch removal of integrated worktrees/branches — with the min-age guard (--min-age, default 1h) addressing the "just started a session" concern from the discussion. --dry-run, --yes, locked/current/default-branch skipping, and per-removal hooks are all included.

The --pattern filter mentioned in the original proposal wasn't included; that could be a follow-up if there's demand.

worktrunk-bot avatar Feb 27 '26 06:02 worktrunk-bot