From: Eric Sunshine Date: Wed, 21 Oct 2026 06:52:28 GMT Subject: Re: [PATCH v2 2/2] worktree: reap ephemeral branches created by "worktree add" Message-ID: In-Reply-To: <6e2acdc5e2578da4e915fd2ac541a776349fb23e.1792512249.git.gitgitgadget@gmail.com> On Tue, Oct 20, 2026 at 12:09 PM Jason Newton via GitGitGadget wrote: > When "git worktree add " is given no branch to check out, it > auto-creates one named after the path. Such a branch exists only to back > the new worktree, yet it outlives it: removing the worktree leaves the > branch behind, so a fleet of throwaway worktrees fills refs/heads/ with > dead branches that have to be cleaned up by hand. > > Treat an auto-created branch as owned by the worktree. Record it in the > worktree's administrative directory (worktrees//owned-branch) at > creation time -- the one moment we can know for certain the branch is > worktree-private, since we are the one that created it. "git worktree > remove" then deletes the owned branch once the worktree is gone and it > is no longer checked out. > > Ownership is recorded only when "worktree add" itself creates the branch > via DWIM auto-naming; a branch named explicitly with -b/-B, or one that > already existed, is never owned and never reaped. Deletion is > unconditional (even for unmerged commits) because an ephemeral branch is > disposable by contract: to keep the work, pass --keep-branch, or anchor > the commits with an ordinary branch beforehand -- a normal branch can > point at the same commits, after which reaping the ephemeral ref loses > nothing. "git branch -m" updates the marker so ownership follows a > rename, and the branch stays ephemeral. > > The marker is just another per-worktree administrative file, so older > versions of Git simply ignore it (they keep the branch on removal, as > today). > > Signed-off-by: Jason Newton Sorry, I may be missing something obvious, but this message never explains why we should add all this additional complexity for a use-case already directly supported by `git worktree add -d `. Specifically, worktree's --detach option is exactly what you want when creating throwaway worktrees, even "fleets" of them (using terminology from this apparently fully or partially LLM-generated message). If your tooling or agent is not using -d/--detach for throwaway worktrees, then the problem is fixable by modifying the tooling or by teaching the agent about --detach, without bulking up the git-worktree implementation unnecessarily. Moreover, a change like this which makes the new behavior unconditional is certainly going to break existing tooling and workflows, thus it will not go over well, even with the --keep-branch escape-hatch. At minimum, to be acceptable, assuming such new functionality is even desirable, to avoid breaking existing setups, it would have to be opt-in, not opt-out (via --keep-branch). But if it's going to be opt-in, then you may as well just use --detach to achieve the same effect.