git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/2] worktree: reap ephemeral branches created by "worktree add"

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Oct 21, 2026, 06:52 UTC
Message-ID
<CAPig+cRsSN9gY405MGsCQzLuxzBKcfHo2Oc8Z4WB-U3BGd6m_Q@mail.gmail.com>
In-Reply-To
<6e2acdc5e2578da4e915fd2ac541a776349fb23e.1792512249.git.gitgitgadget@gmail.com>

On Tue, Oct 20, 2026 at 12:09 PM Jason Newton via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 28 quoted lines
> When "git worktree add <path>" 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/<id>/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 <nevion@gmail.com>

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 <path>`. 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.

Previous: Jason Newton via GitGitGadgetNext: Jason Newton
Message 12 of 23 in “worktree: copy-on-write creation and shared-branch worktrees”
  1. 0/2 worktree: copy-on-write creation and shared-branch worktreesJason Newton via GitGitGadget, Jun 5, 2026
  2. 1/2 worktree: add --reflink for copy-on-write worktree creationJason Newton via GitGitGadget, Jun 5, 2026
  3. 2/2 worktree: allow sharing a checked-out branch across worktreesJason Newton via GitGitGadget, Jun 5, 2026
  4. brian m. carlsonJun 5, 2026
  5. Junio C HamanoJun 8, 2026
  6. 0/2 worktree: copy-on-write creation and ephemeral branchesJason Newton via GitGitGadget, Oct 20, 2026
  7. 1/2 worktree: add --reflink for copy-on-write worktree creationJason Newton via GitGitGadget, Oct 20, 2026
  8. brian m. carlsonOct 20, 2026
  9. Jason NewtonOct 21, 2026
  10. brian m. carlsonOct 22, 2026
  11. 2/2 worktree: reap ephemeral branches created by "worktree add"Jason Newton via GitGitGadget, Oct 20, 2026
  12. Eric SunshineOct 21, 2026
  13. Jason NewtonOct 21, 2026
  14. Eric SunshineOct 21, 2026
  15. Jason NewtonOct 21, 2026
  16. Phillip WoodOct 21, 2026
  17. Jason NewtonOct 21, 2026
  18. Eric SunshineOct 21, 2026
  19. 0/4 worktree: copy-on-write creation and ephemeral branchesJason Newton via GitGitGadget, Oct 21, 2026
  20. 1/4 worktree: add --reflink for copy-on-write worktree creationJason Newton via GitGitGadget, Oct 21, 2026
  21. 2/4 worktree: reap ephemeral branches created by "worktree add"Jason Newton via GitGitGadget, Oct 21, 2026
  22. 3/4 worktree: disable reflink on WindowsJason Newton via GitGitGadget, Oct 21, 2026
  23. 4/4 worktree: keep the reflink probe referenced on WindowsJason Newton via GitGitGadget, Oct 21, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.