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

Re: [PATCH v6 3/3] worktree add: improve message for ambiguous remote branch name

From
Yoichi Nakayama <yoichi.nakayama@gmail.com>
Date
Aug 24, 2026, 22:25 UTC
Message-ID
<CAF5D8-uDROnfs9gZXm1gwQdsOr-xP5Ai3OG-w7eOSHos3_pa8Q@mail.gmail.com>
In-Reply-To
<xmqqld9yvznl.fsf@gitster.g>
On Sun, Aug 23, 2026 at 2:22 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 82 quoted lines
>
> Yoichi Nakayama <yoichi.nakayama@gmail.com> writes:
>
> > No. The exit codes of the command 'git worktree add ../topic-branch'
> > are the same (== 0). but the results are different.
> >
> > If there is a unique match found in dwim_branch(), it creates a local
> > branch named topic-branch which tracks <remote>/topic-branch.
> > In case of no match or multiple matches, it creates a local branch
> > named topic-branch from HEAD.
> >
> > Since Git treats both cases as successful, either can be considered
> > the intended behavior.
> > (Although, if there are multiple matches, there is a fair chance the
> > result might not be what was intended.)
> >
> > I am confident that it is appropriate to provide a hint when a command
> > fails, but it is difficult to decide what to do when a command succeeds.
>
> I actually think it falls into the same class of bug you are fixing
> in this topic, which was caused by not considering the possibility
> that there can be any case other than 0-match and 1-match, and not
> thinking through the ramifications of treating 2-match and 0-match
> the same way.
>
> It is of course OK to fix one bug and leave the other one
> unaddressed, to be fixed in a later follow-up effort.
>
> The rest of this message is only for those who will tackle the
> "later follow-up effort" part after the dust settles once the
> current topic lands (aka #leftoverbits).
>
> In the beginning, before Thomas Gummerer started his topic in
> November 2017 [*1*], 'git worktree add <path> [<branch>]' created a
> new branch from the checked-out HEAD, without looking at any
> remote.
>
>  - 'git worktree add <path> <branch>' before Thomas's effort errored
>    out if <branch> did not exist.  It was safe to add DWIM from
>    remote-tracking branches without requiring any option.
>
>  - 'git worktree add <path>' used to create a new branch whose name
>    is derived from basename(path) that points at the current HEAD,
>    without erroring out.  Enabling DWIM from remote-tracking
>    branches unconditionally would have meant a silent behavior
>    change.  So DWIM was added to this case to require the
>    '--guess-remote' option to enable [*2*].
>
> Back then, unique_tracking_name() did not let the callers
> distinguish between 0-match and multiple-match cases, so when you
> had multiple matches, 'git worktree add <path> [<branch>]' triggered
> the same code path as 0-matches.  When the DWIM feature was
> designed, handling the multiple-match case correctly was on nobody's
> radar.
>
> Even when Ævar Arnfjörð Bjarmason updated unique_tracking_name() in
> 3c87aa946a (checkout: pass the "num_matches" up to callers,
> 2018-06-05), in a topic that ends at 8d7b558bae (checkout &
> worktree: introduce checkout.defaultRemote, 2018-06-05), to allow
> callers to distinguish between 0-match and ambiguous multi-match
> cases, this work unfortunately concentrated on improving "git
> checkout", and callers of unique_tracking_name() in "git worktree"
> were updated to pass NULL, i.e., teaching them to count how many
> matches they got was postponed.
>
> We know that the update to unique_tracking_name() in this work back
> then was not complete on the "git worktree" side.  After all, that
> is how this topic arose to fix one of the two code paths that call
> the function so that we react differently between 0-match and
> multiple-match cases.
>
> Now that we are aware of the issue, I think the code should error
> out, instead of creating the new branch out of HEAD, when there are
> multiple remotes with the name of the branch.  In other words, the
> existing code that behaves the same way in 0-match and 2-match cases
> is buggy, and we should eventually fix it.
>
>
> [Footnotes]
>
>  *1* https://lore.kernel.org/git/20171112134305.3949-1-t.gummerer@gmail.com/
>  *2* https://lore.kernel.org/git/20171126194356.16187-1-t.gummerer@gmail.com/

Thank you for the analysis. I believe the behavior of treating multiple matches as an error is appropriate.

I feel that now is the time to implement the fix that had been postponed. I'll make another commit for it.

Thanks,
-- 
Yoichi NAKAYAMA
Previous: Junio C HamanoNext: Yoichi NAKAYAMA via GitGitGadget
Message 38 of 62 in “worktree add: improve message for ambiguous remote branch name”
  1. worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 8, 2026
  2. Junio C HamanoAug 8, 2026
  3. Junio C HamanoAug 8, 2026
  4. Harald NordgrenAug 9, 2026
  5. Junio C HamanoAug 9, 2026
  6. Harald NordgrenAug 10, 2026
  7. Junio C HamanoAug 9, 2026
  8. Yoichi NakayamaAug 10, 2026
  9. Yoichi NakayamaAug 10, 2026
  10. D. Ben KnobleAug 10, 2026
  11. Yoichi NakayamaAug 10, 2026
  12. Junio C HamanoAug 10, 2026
  13. Yoichi NakayamaAug 10, 2026
  14. Ben KnobleAug 11, 2026
  15. Yoichi NakayamaAug 12, 2026
  16. worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 10, 2026
  17. worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 10, 2026
  18. Junio C HamanoAug 11, 2026
  19. Yoichi NakayamaAug 11, 2026
  20. Junio C HamanoAug 12, 2026
  21. Yoichi NakayamaAug 15, 2026
  22. worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 11, 2026
  23. 0/2 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 19, 2026
  24. 1/2 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 19, 2026
  25. D. Ben KnobleAug 19, 2026
  26. Junio C HamanoAug 20, 2026
  27. Yoichi NakayamaAug 20, 2026
  28. 2/2 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 19, 2026
  29. 0/3 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 20, 2026
  30. 1/3 checkout: extract function to display advice for ambiguous remotesYoichi NAKAYAMA via GitGitGadget, Aug 20, 2026
  31. 2/3 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 20, 2026
  32. 3/3 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 20, 2026
  33. Junio C HamanoAug 21, 2026
  34. Yoichi NakayamaAug 21, 2026
  35. Junio C HamanoAug 21, 2026
  36. Yoichi NakayamaAug 22, 2026
  37. Junio C HamanoAug 22, 2026
  38. Yoichi NakayamaAug 24, 2026
  39. 0/3 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 22, 2026
  40. 1/3 checkout: extract function to display advice for ambiguous remotesYoichi NAKAYAMA via GitGitGadget, Aug 22, 2026
  41. 2/3 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 22, 2026
  42. 3/3 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 22, 2026
  43. Junio C HamanoAug 22, 2026
  44. 0/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 25, 2026
  45. 1/4 checkout: extract function to display advice for ambiguous remotesYoichi NAKAYAMA via GitGitGadget, Aug 25, 2026
  46. 2/4 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 25, 2026
  47. 3/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 25, 2026
  48. 4/4 worktree add: treat multiple matches with --guess-remote as an errorYoichi NAKAYAMA via GitGitGadget, Aug 25, 2026
  49. Junio C HamanoAug 25, 2026
  50. 0/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 26, 2026
  51. 1/4 checkout: extract function to display advice for ambiguous remotesYoichi NAKAYAMA via GitGitGadget, Aug 26, 2026
  52. Junio C HamanoAug 26, 2026
  53. 2/4 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 26, 2026
  54. Junio C HamanoAug 26, 2026
  55. 3/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 26, 2026
  56. 4/4 worktree add: treat multiple matches with --guess-remote as an errorYoichi NAKAYAMA via GitGitGadget, Aug 26, 2026
  57. 0/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 27, 2026
  58. 1/4 checkout: extract function to display advice for ambiguous remotesYoichi NAKAYAMA via GitGitGadget, Aug 27, 2026
  59. 2/4 checkout: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 27, 2026
  60. 3/4 worktree add: improve message for ambiguous remote branch nameYoichi NAKAYAMA via GitGitGadget, Aug 27, 2026
  61. 4/4 worktree add: treat multiple matches with --guess-remote as an errorYoichi NAKAYAMA via GitGitGadget, Aug 27, 2026
  62. Junio C HamanoAug 27, 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.