Commit

85489606d89d44d7aed6e46839ec8ff46f52562d

worktree add: improve message for ambiguous remote branch name

When the user runs 'git worktree add ../foo-dir bar-topic' without
specifying a remote, and there is no local branch named bar-topic, we
try to guess which remote branch bar-topic refers to, then create a
new branch named bar-topic that tracks the remote branch.

If multiple remotes have a branch named bar-topic, we silently gave
up, leaving the variable 'branch' intact.  We then entered the
conditional clause 'if (!opts.orphan &&
!lookup_commit_reference_by_name(branch))' and triggered an "invalid
reference" error.  This error message did not provide enough
information to resolve the ambiguity.

When multiple matching branches are found, display a hint and a
descriptive error message and die immediately.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> · 2026-08-27 14:41 UTC · committed by Junio C Hamano

Browse files
2 files changed, +36 −5
modified builtin/worktree.c
@@ -763,6 +763,25 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote)
763 763 return 1;
764 764 }
765 765
766 + static void advise_disambiguating_remotes(const char *path, const char *branch,
767 + const struct string_list *matched_remote_names)
768 + {
769 + struct string_list_item *item;
770 +
771 + advise(_("Branch name '%s' appears in multiple remotes:"), branch);
772 + for_each_string_list_item(item, matched_remote_names) {
773 + advise(_(" %s"), item->string);
774 + }
775 + advise(_("If you meant to create a worktree from a remote tracking branch on\n"
776 + "<remote>, you can do so by:\n"
777 + "\n"
778 + " git worktree add -b %s %s <remote>/%s\n"
779 + "\n"
780 + "If you'd like to always prefer some remote, e.g. 'origin',\n"
781 + "consider setting checkout.defaultRemote=origin in your config."),
782 + branch, path, branch);
783 + }
784 +
766 785 static char *dwim_branch(const char *path, char **new_branch)
767 786 {
768 787 int n;
@@ -897,17 +916,29 @@ static int add(int ac, const char **av, const char *prefix,
897 916 /* DWIM: Infer --orphan when repo has no refs. */
898 917 opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
899 918 } else if (ac == 2) {
900 - struct object_id oid;
901 919 struct commit *commit;
902 - char *remote;
903 920
904 921 commit = lookup_commit_reference_by_name(branch);
905 922 if (!commit) {
906 - remote = unique_tracking_name(branch, &oid, NULL, NULL);
923 + struct object_id oid;
924 + char *remote;
925 + int num_matches = 0;
926 + struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
927 +
928 + remote = unique_tracking_name(branch, &oid, &num_matches,
929 + &matched_remote_names);
907 930 if (remote) {
908 931 new_branch = branch;
909 932 branch = new_branch_to_free = remote;
933 + } else if (num_matches > 1) {
934 + if (!opts.quiet &&
935 + advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
936 + advise_disambiguating_remotes(path, branch,
937 + &matched_remote_names);
938 + die(_("'%s' matched multiple (%d) remote tracking branches"),
939 + branch, num_matches);
910 940 }
941 + string_list_clear(&matched_remote_names, 0);
911 942 }
912 943
913 944 if (!strcmp(branch, "HEAD"))
modified t/t2400-worktree-add.sh
@@ -624,12 +624,12 @@ test_expect_success '"add" <path> <branch> dwims' '
624 624 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
625 625 test_when_finished rm -rf repo_upstream repo_dwim foo &&
626 626 setup_remote_repo repo_upstream repo_dwim &&
627 - git init repo_dwim &&
628 627 (
629 628 cd repo_dwim &&
630 629 git remote add repo_upstream2 ../repo_upstream &&
631 630 git fetch repo_upstream2 &&
632 - test_must_fail git worktree add ../foo foo &&
631 + test_must_fail git worktree add ../foo foo 2>error.actual &&
632 + test_grep "matched multiple (2) remote tracking branches" error.actual &&
633 633 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
634 634 git status -uno --porcelain >status.actual &&
635 635 test_must_be_empty status.actual