From: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Harald Nordgren <haraldnordgren@gmail.com>,
Yoichi Nakayama <yoichi.nakayama@gmail.com>,
"D. Ben Knoble" <ben.knoble@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: [PATCH v8 4/4] worktree add: treat multiple matches with --guess-remote as an error
Date: Tue, 25 Aug 2026 21:04:35 +0000 [thread overview]
Message-ID: <927856e0a07cf6d9e7e1093a4fbf979584141224.1787691875.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2197.v8.git.1787691875.gitgitgadget@gmail.com>
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
When 'git worktree add <path>' is invoked without <commit-ish> and
with the --guess-remote option (or when worktree.guessRemote is set to
true), it tries to find a remote-tracking branch matching the basename
of <path>.
Currently, the behavior when multiple matches are found is the same as
when no match is found: it falls back to creating a branch from
HEAD. This has been the behavior since 71d6682d8c (worktree: add
--guess-remote option to add subcommand, 2017-11-29), when the option
was first introduced.
However, if the specified <path> matches any remote-tracking branch,
we infer that the user intended to use one of the remote-tracking
branches as the start-point rather than HEAD. So we abort the creation
of the branch and worktree when there are multiple matches, and
instruct the user to choose the start-point.
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
Documentation/config/worktree.adoc | 5 +--
Documentation/git-worktree.adoc | 4 ++-
builtin/worktree.c | 58 ++++++++++++++++++------------
t/t2400-worktree-add.sh | 13 +++++++
4 files changed, 55 insertions(+), 25 deletions(-)
diff --git a/Documentation/config/worktree.adoc b/Documentation/config/worktree.adoc
index a248076ea5..0930183b91 100644
--- a/Documentation/config/worktree.adoc
+++ b/Documentation/config/worktree.adoc
@@ -5,8 +5,9 @@
set to true, `worktree add` tries to find a remote-tracking
branch whose name uniquely matches the new branch name. If
such a branch exists, it is checked out and set as "upstream"
- for the new branch. If no such match can be found, it falls
- back to creating a new branch from the current `HEAD`.
+ for the new branch. If multiple matches are found, the command
+ fails. If no such match can be found, it falls back to
+ creating a new branch from the current `HEAD`.
`worktree.useRelativePaths`::
Link worktrees using relative paths (when "`true`") or absolute
diff --git a/Documentation/git-worktree.adoc b/Documentation/git-worktree.adoc
index fbf8426cd9..32787eacc3 100644
--- a/Documentation/git-worktree.adoc
+++ b/Documentation/git-worktree.adoc
@@ -219,7 +219,9 @@ To remove a locked worktree, specify `--force` twice.
of creating a new branch from `HEAD`, if there exists a tracking
branch in exactly one remote matching the basename of _<path>_,
base the new branch on the remote-tracking branch, and mark
- the remote-tracking branch as "upstream" from the new branch.
+ the remote-tracking branch as "upstream" from the new branch. If
+ there are multiple matches, the command fails. If there is no
+ match, the command falls back to creating a new branch from `HEAD`.
+
This can also be set up as the default behaviour by using the
`worktree.guessRemote` config option.
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 320b18873d..07163bf9b7 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -764,7 +764,26 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote)
return 1;
}
-static char *dwim_branch(const char *path, char **new_branch)
+static void advise_disambiguating_remotes(const char *path, const char *branch,
+ const struct string_list *matched_remote_names)
+{
+ struct string_list_item *item;
+
+ advise(_("Branch name '%s' appears in multiple remotes:"), branch);
+ for_each_string_list_item(item, matched_remote_names) {
+ advise(_(" %s"), item->string);
+ }
+ advise(_("If you meant to create a worktree from a remote tracking branch on\n"
+ "<remote>, you can do so by:\n"
+ "\n"
+ " git worktree add -b %s %s <remote>/%s\n"
+ "\n"
+ "If you'd like to always prefer some remote, e.g. 'origin',\n"
+ "consider setting checkout.defaultRemote=origin in your config."),
+ branch, path, branch);
+}
+
+static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch)
{
int n;
int branch_exists;
@@ -782,31 +801,26 @@ static char *dwim_branch(const char *path, char **new_branch)
*new_branch = branchname;
if (guess_remote) {
struct object_id oid;
- char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL);
+ char *remote;
+ int num_matches = 0;
+ struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
+
+ remote = unique_tracking_name(*new_branch, &oid, &num_matches,
+ &matched_remote_names);
+ if (!remote && num_matches > 1) {
+ if (!opts->quiet &&
+ advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
+ advise_disambiguating_remotes(path, *new_branch,
+ &matched_remote_names);
+ die(_("'%s' matched multiple (%d) remote tracking branches"),
+ *new_branch, num_matches);
+ }
+ string_list_clear(&matched_remote_names, 0);
return remote;
}
return NULL;
}
-static void advise_disambiguating_remotes(const char *path, const char *branch,
- const struct string_list *matched_remote_names)
-{
- struct string_list_item *item;
-
- advise(_("Branch name '%s' appears in multiple remotes:"), branch);
- for_each_string_list_item(item, matched_remote_names) {
- advise(_(" %s"), item->string);
- }
- advise(_("If you meant to create a worktree from a remote tracking branch on\n"
- "<remote>, you can do so by:\n"
- "\n"
- " git worktree add -b %s %s <remote>/%s\n"
- "\n"
- "If you'd like to always prefer some remote, e.g. 'origin',\n"
- "consider setting checkout.defaultRemote=origin in your config."),
- branch, path, branch);
-}
-
static int add(int ac, const char **av, const char *prefix,
struct repository *repo UNUSED)
{
@@ -909,7 +923,7 @@ static int add(int ac, const char **av, const char *prefix,
opts.orphan = dwim_orphan(&opts, !!opt_track, 0);
} else if (ac < 2) {
/* DWIM: Guess branch name from path. */
- char *s = dwim_branch(path, &new_branch_to_free);
+ char *s = dwim_branch(&opts, path, &new_branch_to_free);
if (s)
branch = branch_to_free = s;
new_branch = new_branch_to_free;
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index 5c105cf252..a37137042d 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -669,6 +669,19 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' '
test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
)
'
+
+test_expect_success 'git worktree add --guess-remote fails if there are multiple matches' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git remote add repo_a2 ../repo_a &&
+ git fetch repo_a2 &&
+ test_must_fail git worktree add --guess-remote ../foo 2>actual &&
+ test_grep "matched multiple (2) remote tracking branches" actual
+ )
+'
+
test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '
test_when_finished rm -rf repo_a repo_b foo &&
setup_remote_repo repo_a repo_b &&
--
gitgitgadget
next prev parent reply other threads:[~2026-08-25 21:04 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-08 8:21 [PATCH] worktree add: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-08 17:00 ` Junio C Hamano
2026-08-08 21:57 ` Junio C Hamano
2026-08-09 7:45 ` Harald Nordgren
2026-08-09 18:19 ` Junio C Hamano
2026-08-10 10:12 ` Harald Nordgren
2026-08-09 18:17 ` Junio C Hamano
2026-08-10 13:04 ` Yoichi Nakayama
2026-08-10 13:00 ` Yoichi Nakayama
2026-08-10 13:07 ` D. Ben Knoble
2026-08-10 13:35 ` Yoichi Nakayama
2026-08-10 15:06 ` Junio C Hamano
2026-08-10 21:36 ` Yoichi Nakayama
2026-08-11 16:38 ` Ben Knoble
2026-08-12 13:14 ` Yoichi Nakayama
2026-08-10 15:07 ` [PATCH v2] " Yoichi NAKAYAMA via GitGitGadget
2026-08-10 20:55 ` [PATCH v3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-11 0:03 ` Junio C Hamano
2026-08-11 6:31 ` Yoichi Nakayama
2026-08-12 19:22 ` Junio C Hamano
2026-08-15 4:36 ` Yoichi Nakayama
2026-08-11 6:35 ` [PATCH v4] " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 12:50 ` [PATCH v5 0/2] " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 12:50 ` [PATCH v5 1/2] checkout: " Yoichi NAKAYAMA via GitGitGadget
2026-08-19 22:54 ` D. Ben Knoble
2026-08-20 2:18 ` Junio C Hamano
2026-08-20 15:41 ` Yoichi Nakayama
2026-08-19 12:50 ` [PATCH v5 2/2] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03 ` [PATCH v6 0/3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03 ` [PATCH v6 1/3] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03 ` [PATCH v6 2/3] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-20 21:03 ` [PATCH v6 3/3] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-21 3:54 ` Junio C Hamano
2026-08-21 23:15 ` Yoichi Nakayama
2026-08-21 23:49 ` Junio C Hamano
2026-08-22 0:50 ` Yoichi Nakayama
2026-08-22 17:22 ` Junio C Hamano
2026-08-24 22:25 ` Yoichi Nakayama
2026-08-22 3:22 ` [PATCH v7 0/3] " Yoichi NAKAYAMA via GitGitGadget
2026-08-22 3:22 ` [PATCH v7 1/3] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-22 3:22 ` [PATCH v7 2/3] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-22 3:22 ` [PATCH v7 3/3] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-22 18:08 ` [PATCH v7 0/3] " Junio C Hamano
2026-08-25 21:04 ` [PATCH v8 0/4] " Yoichi NAKAYAMA via GitGitGadget
2026-08-25 21:04 ` [PATCH v8 1/4] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-25 21:04 ` [PATCH v8 2/4] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-25 21:04 ` [PATCH v8 3/4] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-25 21:04 ` Yoichi NAKAYAMA via GitGitGadget [this message]
2026-08-25 21:31 ` [PATCH v8 4/4] worktree add: treat multiple matches with --guess-remote as an error Junio C Hamano
2026-08-26 10:45 ` [PATCH v9 0/4] worktree add: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-26 10:45 ` [PATCH v9 1/4] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-26 15:24 ` Junio C Hamano
2026-08-26 10:45 ` [PATCH v9 2/4] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-26 16:46 ` Junio C Hamano
2026-08-26 10:45 ` [PATCH v9 3/4] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-26 10:45 ` [PATCH v9 4/4] worktree add: treat multiple matches with --guess-remote as an error Yoichi NAKAYAMA via GitGitGadget
2026-08-27 14:41 ` [PATCH v10 0/4] worktree add: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-27 14:41 ` [PATCH v10 1/4] checkout: extract function to display advice for ambiguous remotes Yoichi NAKAYAMA via GitGitGadget
2026-08-27 14:41 ` [PATCH v10 2/4] checkout: improve message for ambiguous remote branch name Yoichi NAKAYAMA via GitGitGadget
2026-08-27 14:41 ` [PATCH v10 3/4] worktree add: " Yoichi NAKAYAMA via GitGitGadget
2026-08-27 14:41 ` [PATCH v10 4/4] worktree add: treat multiple matches with --guess-remote as an error Yoichi NAKAYAMA via GitGitGadget
2026-08-27 17:32 ` [PATCH v10 0/4] worktree add: improve message for ambiguous remote branch name Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=927856e0a07cf6d9e7e1093a4fbf979584141224.1787691875.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=ben.knoble@gmail.com \
--cc=git@vger.kernel.org \
--cc=haraldnordgren@gmail.com \
--cc=yoichi.nakayama@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox