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

[PATCH v6 7/8] checkout: add advice for ambiguous "checkout <branch>"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jun 2, 2018, 11:50 UTC
Message-ID
<20180602115042.18167-8-avarab@gmail.com>
In-Reply-To
<20180602115042.18167-1-avarab@gmail.com>
As the "checkout" documentation describes:
    If <branch> is not found but there does exist a tracking branch in
    exactly one remote (call it <remote>) with a matching name, treat
    as equivalent to [...] <remote>/<branch.

This is a really useful feature. The problem is that when you add another remote (e.g. a fork), git won't find a unique branch name anymore, and will instead print this unhelpful message:

    $ git checkout master
    error: pathspec 'master' did not match any file(s) known to git
Now it will, on my git.git checkout, print:
    $ ./git --exec-path=$PWD checkout master
    error: pathspec 'master' did not match any file(s) known to git.
    hint: 'master' matched more than one remote tracking branch.
    hint: We found 26 remotes with a reference that matched. So we fell back
    hint: on trying to resolve the argument as a path, but failed there too!
    hint:
    hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
    hint: you can do so by fully qualifying the name with the --track option:
    hint:
    hint:     git checkout --track origin/<name>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 Documentation/config.txt |  7 +++++++
 advice.c                 |  2 ++
 advice.h                 |  1 +
 builtin/checkout.c       | 13 +++++++++++++
 t/t2024-checkout-dwim.sh | 14 ++++++++++++++
 5 files changed, 37 insertions(+)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index ab641bf5a9..dfc0413a84 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -344,6 +344,13 @@ advice.*::
 		Advice shown when you used linkgit:git-checkout[1] to
 		move to the detach HEAD state, to instruct how to create
 		a local branch after the fact.
+	checkoutAmbiguousRemoteBranchName::
+		Advice shown when the argument to
+		linkgit:git-checkout[1] ambiguously resolves to a
+		remote tracking branch on more than one remote in
+		situations where an unambiguous argument would have
+		otherwise caused a remote-tracking branch to be
+		checked out.
 	amWorkDir::
 		Advice that shows the location of the patch file when
 		linkgit:git-am[1] fails to apply it.
diff --git a/advice.c b/advice.c
index 370a56d054..75e7dede90 100644
--- a/advice.c
+++ b/advice.c
@@ -21,6 +21,7 @@ int advice_add_embedded_repo = 1;
 int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
+int advice_checkout_ambiguous_remote_branch_name = 1;
 
 static int advice_use_color = -1;
 static char advice_colors[][COLOR_MAXLEN] = {
@@ -72,6 +73,7 @@ static struct {
 	{ "ignoredhook", &advice_ignored_hook },
 	{ "waitingforeditor", &advice_waiting_for_editor },
 	{ "graftfiledeprecated", &advice_graft_file_deprecated },
+	{ "checkoutambiguousremotebranchname", &advice_checkout_ambiguous_remote_branch_name },
 
 	/* make this an alias for backward compatibility */
 	{ "pushnonfastforward", &advice_push_update_rejected }
diff --git a/advice.h b/advice.h
index 9f5064e82a..4d11d51d43 100644
--- a/advice.h
+++ b/advice.h
@@ -22,6 +22,7 @@ extern int advice_add_embedded_repo;
 extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
+extern int advice_checkout_ambiguous_remote_branch_name;
 
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 8c93c55cbc..baa027455a 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -22,6 +22,7 @@
 #include "resolve-undo.h"
 #include "submodule-config.h"
 #include "submodule.h"
+#include "advice.h"
 
 static const char * const checkout_usage[] = {
 	N_("git checkout [<options>] <branch>"),
@@ -1267,6 +1268,18 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	UNLEAK(opts);
 	if (opts.patch_mode || opts.pathspec.nr) {
 		int ret = checkout_paths(&opts, new_branch_info.name);
+		if (ret && dwim_remotes_matched > 1 &&
+		    advice_checkout_ambiguous_remote_branch_name)
+			advise(_("'%s' matched more than one remote tracking branch.\n"
+				 "We found %d remotes with a reference that matched. So we fell back\n"
+				 "on trying to resolve the argument as a path, but failed there too!\n"
+				 "\n"
+				 "If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
+				 "you can do so by fully qualifying the name with the --track option:\n"
+				 "\n"
+				 "    git checkout --track origin/<name>"),
+			       argv[0],
+			       dwim_remotes_matched);
 		return ret;
 	} else {
 		return checkout_branch(&opts, &new_branch_info);
diff --git a/t/t2024-checkout-dwim.sh b/t/t2024-checkout-dwim.sh
index ed32828105..fef263a858 100755
--- a/t/t2024-checkout-dwim.sh
+++ b/t/t2024-checkout-dwim.sh
@@ -76,6 +76,20 @@ test_expect_success 'checkout of branch from multiple remotes fails #1' '
 	test_branch master
 '
 
+test_expect_success 'checkout of branch from multiple remotes fails with advice' '
+	git checkout -B master &&
+	test_might_fail git branch -D foo &&
+	test_must_fail git checkout foo 2>stderr &&
+	test_branch master &&
+	status_uno_is_clean &&
+	test_i18ngrep "^hint: " stderr &&
+	test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
+		checkout foo 2>stderr &&
+	test_branch master &&
+	status_uno_is_clean &&
+	test_i18ngrep ! "^hint: " stderr
+'
+
 test_expect_success 'checkout of branch from a single remote succeeds #1' '
 	git checkout -B master &&
 	test_might_fail git branch -D bar &&
-- 
2.17.0.290.gded63e768a
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 65 of 95 in “checkout & worktree: introduce a core.DWIMRemote setting”
  1. checkout & worktree: introduce a core.DWIMRemote settingÆvar Arnfjörð Bjarmason, May 2, 2018
  2. Duy NguyenMay 2, 2018
  3. Eric SunshineMay 2, 2018
  4. Duy NguyenMay 2, 2018
  5. Ævar Arnfjörð BjarmasonMay 2, 2018
  6. checkout & worktree: introduce checkout.implicitRemoteÆvar Arnfjörð Bjarmason, May 3, 2018
  7. Duy NguyenMay 3, 2018
  8. Ævar Arnfjörð BjarmasonMay 4, 2018
  9. Duy NguyenMay 4, 2018
  10. Ævar Arnfjörð BjarmasonMay 4, 2018
  11. Eric SunshineMay 4, 2018
  12. checkout & worktree: introduce checkout.implicitRemoteÆvar Arnfjörð Bjarmason, May 24, 2018
  13. Junio C HamanoMay 25, 2018
  14. Duy NguyenMay 25, 2018
  15. Ævar Arnfjörð BjarmasonMay 25, 2018
  16. Duy NguyenMay 26, 2018
  17. Ævar Arnfjörð BjarmasonMay 31, 2018
  18. 0/9 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, May 31, 2018
  19. 1/9 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, May 31, 2018
  20. Junio C HamanoJun 1, 2018
  21. Ævar Arnfjörð BjarmasonJun 1, 2018
  22. 2/9 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, May 31, 2018
  23. 3/9 checkout.[ch]: move struct declaration into *.hÆvar Arnfjörð Bjarmason, May 31, 2018
  24. Thomas GummererMay 31, 2018
  25. Junio C HamanoJun 1, 2018
  26. Ævar Arnfjörð BjarmasonJun 1, 2018
  27. 4/9 checkout.[ch]: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, May 31, 2018
  28. Junio C HamanoJun 1, 2018
  29. 5/9 checkout.[ch]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, May 31, 2018
  30. 7/9 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, May 31, 2018
  31. 6/9 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, May 31, 2018
  32. 9/9 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, May 31, 2018
  33. Stefan BellerMay 31, 2018
  34. Eric SunshineJun 1, 2018
  35. Ævar Arnfjörð BjarmasonJun 1, 2018
  36. Thomas GummererMay 31, 2018
  37. Junio C HamanoJun 1, 2018
  38. 8/9 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, May 31, 2018
  39. Junio C HamanoJun 1, 2018
  40. Junio C HamanoJun 1, 2018
  41. Ævar Arnfjörð BjarmasonJun 1, 2018
  42. Junio C HamanoJun 4, 2018
  43. Ævar Arnfjörð BjarmasonJun 1, 2018
  44. Eric SunshineJun 1, 2018
  45. Ævar Arnfjörð BjarmasonJun 1, 2018
  46. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 1, 2018
  47. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 1, 2018
  48. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 1, 2018
  49. 3/8 checkout.[ch]: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 1, 2018
  50. Eric SunshineJun 1, 2018
  51. 4/8 checkout.[ch]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 1, 2018
  52. Eric SunshineJun 1, 2018
  53. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 1, 2018
  54. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 1, 2018
  55. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 1, 2018
  56. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 1, 2018
  57. Eric SunshineJun 1, 2018
  58. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 2, 2018
  59. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 2, 2018
  60. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 2, 2018
  61. 3/8 checkout.c: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 2, 2018
  62. 4/8 checkout.c]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 2, 2018
  63. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 2, 2018
  64. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 2, 2018
  65. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 2, 2018
  66. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 2, 2018
  67. Eric SunshineJun 3, 2018
  68. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 5, 2018
  69. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 5, 2018
  70. SZEDER GáborJun 5, 2018
  71. tests: make use of the test_must_be_empty functionÆvar Arnfjörð Bjarmason, Jul 27, 2018
  72. Junio C HamanoJul 27, 2018
  73. SZEDER GáborJul 31, 2018
  74. t6018-rev-list-glob: fix 'empty stdin' testSZEDER Gábor, Aug 22, 2018
  75. Eric SunshineAug 22, 2018
  76. SZEDER GáborAug 22, 2018
  77. Eric SunshineAug 22, 2018
  78. Junio C HamanoAug 22, 2018
  79. Junio C HamanoAug 22, 2018
  80. Jeff KingAug 22, 2018
  81. rev-list: make empty --stdin not an errorJeff King, Aug 22, 2018
  82. Junio C HamanoAug 22, 2018
  83. Jeff KingAug 22, 2018
  84. Junio C HamanoAug 22, 2018
  85. Jeff KingAug 22, 2018
  86. Junio C HamanoAug 22, 2018
  87. 3/8 checkout.c: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 5, 2018
  88. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 5, 2018
  89. 4/8 checkout.c: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 5, 2018
  90. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 5, 2018
  91. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 5, 2018
  92. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 5, 2018
  93. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 5, 2018
  94. Eric SunshineMay 4, 2018
  95. Eric SunshineMay 4, 2018

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.