Commit

05cf4ceb5bc28580ef30cb45c8bbd8b447431cf4

checkout: improve message for ambiguous remote branch name

When the user runs 'git checkout 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 cannot determine
a single remote.

To make it easier to resolve the ambiguity, provide the names of the
matching remotes for the specified branch name.

To achieve that, add an optional feature to the
`unique_tracking_name()` function that allows the matching remote
names to be exposed to the caller.

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
4 files changed, +37 −13
modified builtin/checkout.c
@@ -1340,9 +1340,12 @@ enum checkout_command {
1340 1340 CHECKOUT_RESTORE = 3,
1341 1341 };
1342 1342
1343 - static void advise_disambiguating_remotes(enum checkout_command which_command)
1343 + static void advise_disambiguating_remotes(enum checkout_command which_command,
1344 + const char *branch,
1345 + const struct string_list *matched_remote_names)
1344 1346 {
1345 1347 const char *cmdname;
1348 + struct string_list_item *item;
1346 1349
1347 1350 switch (which_command) {
1348 1351 case CHECKOUT_CHECKOUT:
@@ -1357,15 +1360,19 @@ static void advise_disambiguating_remotes(enum checkout_command which_command)
1357 1360 break;
1358 1361 }
1359 1362
1360 - advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1363 + advise(_("Branch name '%s' appears in multiple remotes:"), branch);
1364 + for_each_string_list_item(item, matched_remote_names) {
1365 + advise(_(" %s"), item->string);
1366 + }
1367 + advise(_("If you meant to check out a remote tracking branch on <remote>,\n"
1361 1368 "you can do so by fully qualifying the name with the --track option:\n"
1362 1369 "\n"
1363 - " git %s --track origin/<name>\n"
1370 + " git %s --track <remote>/%s\n"
1364 1371 "\n"
1365 - "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1372 + "If you'd like to always have checkouts of an ambiguous name prefer\n"
1366 1373 "one remote, e.g. the 'origin' remote, consider setting\n"
1367 1374 "checkout.defaultRemote=origin in your config."),
1368 - cmdname);
1375 + cmdname, branch);
1369 1376 }
1370 1377
1371 1378 static char *parse_remote_branch(const char *arg,
@@ -1374,7 +1381,10 @@ static char *parse_remote_branch(const char *arg,
1374 1381 enum checkout_command which_command)
1375 1382 {
1376 1383 int num_matches = 0;
1377 - char *remote = unique_tracking_name(arg, rev, &num_matches);
1384 + struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
1385 +
1386 + char *remote = unique_tracking_name(arg, rev, &num_matches,
1387 + &matched_remote_names);
1378 1388
1379 1389 if (remote && could_be_checkout_paths) {
1380 1390 die(_("'%s' could be both a local file and a tracking branch.\n"
@@ -1384,12 +1394,14 @@ static char *parse_remote_branch(const char *arg,
1384 1394
1385 1395 if (!remote && num_matches > 1) {
1386 1396 if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
1387 - advise_disambiguating_remotes(which_command);
1388 -
1397 + advise_disambiguating_remotes(which_command, arg,
1398 + &matched_remote_names);
1389 1399 die(_("'%s' matched multiple (%d) remote tracking branches"),
1390 1400 arg, num_matches);
1391 1401 }
1392 1402
1403 + string_list_clear(&matched_remote_names, 0);
1404 +
1393 1405 return remote;
1394 1406 }
1395 1407
modified builtin/worktree.c
@@ -781,7 +781,7 @@ static char *dwim_branch(const char *path, char **new_branch)
781 781 *new_branch = branchname;
782 782 if (guess_remote) {
783 783 struct object_id oid;
784 - char *remote = unique_tracking_name(*new_branch, &oid, NULL);
784 + char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL);
785 785 return remote;
786 786 }
787 787 return NULL;
@@ -903,7 +903,7 @@ static int add(int ac, const char **av, const char *prefix,
903 903
904 904 commit = lookup_commit_reference_by_name(branch);
905 905 if (!commit) {
906 - remote = unique_tracking_name(branch, &oid, NULL);
906 + remote = unique_tracking_name(branch, &oid, NULL, NULL);
907 907 if (remote) {
908 908 new_branch = branch;
909 909 branch = new_branch_to_free = remote;
modified checkout.c
@@ -8,6 +8,7 @@
8 8 #include "checkout.h"
9 9 #include "config.h"
10 10 #include "strbuf.h"
11 + #include "string-list.h"
11 12
12 13 struct tracking_name_data {
13 14 /* const */ char *src_ref;
@@ -17,6 +18,7 @@ struct tracking_name_data {
17 18 const char *default_remote;
18 19 char *default_dst_ref;
19 20 struct object_id *default_dst_oid;
21 + struct string_list *remote_names;
20 22 };
21 23
22 24 #define TRACKING_NAME_DATA_INIT { 0 }
@@ -39,6 +41,8 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
39 41 oidcpy(dst, cb->dst_oid);
40 42 cb->default_dst_oid = dst;
41 43 }
44 + if (cb->remote_names)
45 + string_list_append(cb->remote_names, remote->name);
42 46 if (cb->dst_ref) {
43 47 free(query.dst);
44 48 return 0;
@@ -48,14 +52,19 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
48 52 }
49 53
50 54 char *unique_tracking_name(const char *name, struct object_id *oid,
51 - int *dwim_remotes_matched)
55 + int *dwim_remotes_matched,
56 + struct string_list *dwim_remote_names)
52 57 {
53 58 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
54 59 const char *default_remote = NULL;
55 - if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote))
60 +
61 + if (!repo_config_get_string_tmp(the_repository,
62 + "checkout.defaultremote",
63 + &default_remote))
56 64 cb_data.default_remote = default_remote;
57 65 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
58 66 cb_data.dst_oid = oid;
67 + cb_data.remote_names = dwim_remote_names;
59 68 for_each_remote(check_tracking_name, &cb_data);
60 69 if (dwim_remotes_matched)
61 70 *dwim_remotes_matched = cb_data.num_matches;
modified checkout.h
@@ -3,6 +3,8 @@
3 3
4 4 #include "hash.h"
5 5
6 + struct string_list;
7 +
6 8 /*
7 9 * Check if the branch name uniquely matches a branch name on a remote
8 10 * tracking branch. Return the name of the remote if such a branch
@@ -10,6 +12,7 @@
10 12 */
11 13 char *unique_tracking_name(const char *name,
12 14 struct object_id *oid,
13 - int *dwim_remotes_matched);
15 + int *dwim_remotes_matched,
16 + struct string_list *dwim_remote_names);
14 17
15 18 #endif /* CHECKOUT_H */