{"thread":{"id":"65546","subject":"[PATCH] checkout: add --fetch to fetch remote before resolving start-point","startedAt":"2026-04-24T10:03:14Z","lastAt":"2026-07-23T23:48:23Z","messageCount":73,"participants":["Harald Nordgren via GitGitGadget","Ramsay Jones","D. Ben Knoble","Kristoffer Haugsbakk","Marc Branchaud","Junio C Hamano","Harald Nordgren","Ben Knoble","Phillip Wood","Kristofer Karlsson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"542241","messageId":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":null,"subject":"[PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-04-24T10:03:11Z","receivedAt":"2026-04-24T10:03:14Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a --fetch option to git checkout and git switch, plus a\ncheckout.autoFetch config to enable it by default. When set and the\nstart-point argument names a configured remote (either bare, like\n\"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\nresolving the ref. Aborts the checkout if the fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n    A workflow I run several times a day looks like:\n    \n    git fetch origin\n    git checkout -b new_branch origin/some-branch\n    \n    \n    The first command exists purely to make the second one see an up-to-date\n    view of the remote. If I forget it, origin/some-branch points at a stale\n    commit, and I end up creating a local branch from the wrong starting\n    point.\n    \n    This series teaches git checkout (and git switch) a new --fetch flag\n    that folds the two steps into one:\n    \n    git checkout --fetch -b new_branch origin/some-branch\n    \n    \n    When the start-point argument names a configured remote — either bare\n    (origin, which resolves to the remote's default branch) or in / form —\n    git fetch is run before the start-point is resolved. If the fetch fails,\n    the checkout aborts and no local branch is created.\n    \n    A new checkout.autoFetch config option enables the same behavior by\n    default, for users who always want it.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1\nPull-Request: https://github.com/git/git/pull/2281\n\n builtin/checkout.c    | 48 ++++++++++++++++++++++++++++++++++++++--\n t/t7201-co.sh         | 51 +++++++++++++++++++++++++++++++++++++++++++\n t/t9902-completion.sh |  1 +\n 3 files changed, 98 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..c8fbc4923b 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint auto_fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,34 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(var, \"checkout.autofetch\")) {\n+\t\topts->auto_fetch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n@@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->auto_fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\n@@ -2052,6 +2092,8 @@ int cmd_checkout(int argc,\n \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n \t\tOPT_END()\n \t};\n \n@@ -2102,6 +2144,8 @@ int cmd_switch(int argc,\n \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n \t\t\t N_(\"throw away local modifications\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n \t\tOPT_END()\n \t};\n \ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..60ddebd9c3 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n+'\n+\n+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_cfg &&\n+\ttest_commit -C fetch_upstream u_cfg &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n+\tgit -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n+'\n+\n+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 2f9a597ec7..dc1d63669f 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2602,6 +2602,7 @@ test_expect_success 'double dash \"git checkout\"' '\n \t--ignore-other-worktrees Z\n \t--recurse-submodules Z\n \t--auto-advance Z\n+\t--fetch Z\n \t--progress Z\n \t--guess Z\n \t--no-guess Z\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542248","messageId":"d7ffcab0-cec3-4cea-97d7-1b359b4ff55e@ramsayjones.plus.com","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsayjones.plus.com","sentAt":"2026-04-24T13:48:04Z","receivedAt":"2026-04-24T13:48:07Z","isPatch":true,"body":"\n\nOn 24/04/2026 11:03 am, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> Add a --fetch option to git checkout and git switch, plus a\n> checkout.autoFetch config to enable it by default. When set and the\n> start-point argument names a configured remote (either bare, like\n> \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n> resolving the ref. Aborts the checkout if the fetch fails.\n> \n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n\n[snip]\n\n>  \n> diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> index 9bcf7c0b40..60ddebd9c3 100755\n> --- a/t/t7201-co.sh\n> +++ b/t/t7201-co.sh\n> @@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n>  \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n>  '\n>  \n> +test_expect_success 'setup upstream for --fetch tests' '\n> +\tgit checkout main &&\n> +\tgit init fetch_upstream &&\n> +\ttest_commit -C fetch_upstream u_main &&\n> +\tgit remote add fetch_upstream fetch_upstream &&\n> +\tgit fetch fetch_upstream &&\n> +\tgit -C fetch_upstream checkout -b fetch_new &&\n> +\ttest_commit -C fetch_upstream u_new\n> +'\n> +\n> +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> +\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n> +'\n> +\n> +test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_new2 &&\n> +\ttest_commit -C fetch_upstream u_new2 &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n> +\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n> +\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n> +'\n> +\n> +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n> +\tgit checkout main &&\n> +\ttest_might_fail git branch -D bogus &&\n> +\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n> +'\n> +\n> +test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_cfg &&\n> +\ttest_commit -C fetch_upstream u_cfg &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n> +\tgit -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n> +'\n> +\n> +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_switch &&\n> +\ttest_commit -C fetch_upstream u_switch &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> +\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> +'\n> +\n\nI was just skimming the list (so if this is not appropriate, please just ignore) and,\nalthough I think '--no-fetch' will probably countermand the autoFetch config, I do not\nsee a test that confirms it.\n\nThanks.\n\nATB,\nRamsay Jones\n\n\n"},{"id":"542267","messageId":"CALnO6CCNoo8y2V5KmE0KQ6qDurZELipFowcr=ZpZ3ocVB-uLjA@mail.gmail.com","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-04-24T17:12:36Z","receivedAt":"2026-04-24T17:12:50Z","isPatch":true,"body":"On Fri, Apr 24, 2026 at 6:08 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Add a --fetch option to git checkout and git switch, plus a\n> checkout.autoFetch config to enable it by default. When set and the\n> start-point argument names a configured remote (either bare, like\n> \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n> resolving the ref. Aborts the checkout if the fetch fails.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>     checkout: add --fetch to fetch remote before resolving start-point\n>\n>     A workflow I run several times a day looks like:\n>\n>     git fetch origin\n>     git checkout -b new_branch origin/some-branch\n>\n>\n>     The first command exists purely to make the second one see an up-to-date\n>     view of the remote. If I forget it, origin/some-branch points at a stale\n>     commit, and I end up creating a local branch from the wrong starting\n>     point.\n\nWhen you realize this, \"git pull --rebase\" should help correct it.\n\n>\n>     This series teaches git checkout (and git switch) a new --fetch flag\n>     that folds the two steps into one:\n>\n>     git checkout --fetch -b new_branch origin/some-branch\n>\n>\n>     When the start-point argument names a configured remote — either bare\n>     (origin, which resolves to the remote's default branch) or in / form —\n>     git fetch is run before the start-point is resolved. If the fetch fails,\n>     the checkout aborts and no local branch is created.\n>\n>     A new checkout.autoFetch config option enables the same behavior by\n>     default, for users who always want it.\n\nI could certainly see this being convenient. (I don't have any comment\non the code at this time.)\n"},{"id":"542268","messageId":"89f923bf-e5fc-4557-a2f0-d240db07eaf9@app.fastmail.com","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-04-24T17:38:28Z","receivedAt":"2026-04-24T17:38:49Z","isPatch":true,"body":"On Fri, Apr 24, 2026, at 12:03, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Add a --fetch option to git checkout and git switch, plus a\n> checkout.autoFetch config to enable it by default. When set and the\n\nWhy is the config not `checkout.config`? So it’s named the same as the\noption (modulo snake case/camel case which is not relevant here).\n\n> start-point argument names a configured remote (either bare, like\n> \"origin\", or prefixed, like \"origin/foo\"),\n\nIt’s great that it only fetches when you have a remote-tracking branch\nor alias for `<remote>/HEAD`. Doing a fetch on every <start-point> would\nhave been bad.\n\n> fetch that remote before\n> resolving the ref. Aborts the checkout if the fetch fails.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>     checkout: add --fetch to fetch remote before resolving start-point\n>\n>     A workflow I run several times a day looks like:\n>\n>     git fetch origin\n>     git checkout -b new_branch origin/some-branch\n>\n>\n>     The first command exists purely to make the second one see an up-to-date\n>     view of the remote. If I forget it, origin/some-branch points at a stale\n>     commit, and I end up creating a local branch from the wrong starting\n>     point.\n>\n>     This series teaches git checkout (and git switch) a new --fetch flag\n>     that folds the two steps into one:\n>\n>     git checkout --fetch -b new_branch origin/some-branch\n\nThe motivation for why this is being proposed maybe might as well go in\nthe commit message. Maybe that’s just me.\n\nThe commit message just says that “this thing is added”. Not why.\n\n>\n>\n>     When the start-point argument names a configured remote — either bare\n>     (origin, which resolves to the remote's default branch) or in / form —\n>     git fetch is run before the start-point is resolved. If the fetch fails,\n>     the checkout aborts and no local branch is created.\n>\n>     A new checkout.autoFetch config option enables the same behavior by\n>     default, for users who always want it.\n>\n> Published-As:\n> https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git\n> pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1\n> Pull-Request: https://github.com/git/git/pull/2281\n>\n>  builtin/checkout.c    | 48 ++++++++++++++++++++++++++++++++++++++--\n>  t/t7201-co.sh         | 51 +++++++++++++++++++++++++++++++++++++++++++\n>  t/t9902-completion.sh |  1 +\n>  3 files changed, 98 insertions(+), 2 deletions(-)\n\nI guess a later version will have the changes to the documentation.\n\n>\n> diff --git a/builtin/checkout.c b/builtin/checkout.c\n>[snip]\n>  \t\targv += n;\n>  \t\targc -= n;\n>  \t} else if (!opts->accept_ref && opts->from_treeish) {\n> @@ -2052,6 +2092,8 @@ int cmd_checkout(int argc,\n>  \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode\n> (default)\")),\n>  \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n>  \t\t\t N_(\"auto advance to the next file when selecting hunks\n> interactively\")),\n> +\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n> +\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n\ns/remote-tracking ref/remote-tracking branch/ ?\n\ngit(1) doesn’t have a namespace for tracking refs in general.\n\n>  \t\tOPT_END()\n>  \t};\n>\n> @@ -2102,6 +2144,8 @@ int cmd_switch(int argc,\n>  \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n>  \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n>  \t\t\t N_(\"throw away local modifications\")),\n> +\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n> +\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n\nDitto.\n\n>  \t\tOPT_END()\n>  \t};\n>[snip]\n"},{"id":"542269","messageId":"42a620db-6110-4894-97fb-8e49ec577817@gmail.com","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Marc Branchaud","fromEmail":"marcnarc@gmail.com","sentAt":"2026-04-24T17:42:46Z","receivedAt":"2026-04-24T17:42:49Z","isPatch":true,"body":"\nOn 2026-04-24 04:03, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> Add a --fetch option to git checkout and git switch, plus a\n> checkout.autoFetch config to enable it by default. When set and the\n> start-point argument names a configured remote (either bare, like\n> \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n> resolving the ref. Aborts the checkout if the fetch fails.\n\nWhy tie the behaviour to the nature of the start-point?  That seems \nover-designed and prone to tripping people up.  Are you trying to cater \nto users who have multiple remotes?\n\nI can imagine people who just want to do a checkout of anything after \nfetching -- maybe they want to checkout a new tag, or some other \ndetached HEAD, or just an already-existing local branch.  They see that \ncheckout has this nifty --fetch option so they think they can combine\n\tgit fetch; git checkout\ninto a single command ... but no, only if they checkout something in a \nremote's namespace.\n\nI don't personally feel the need for this new option, but I think you'll \nhave a much easier time implementing and maintaining it if you just make \n--fetch do a plain fetch without caring about what the starting-point is.\n\n\t\tM.\n\n\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>      checkout: add --fetch to fetch remote before resolving start-point\n>      \n>      A workflow I run several times a day looks like:\n>      \n>      git fetch origin\n>      git checkout -b new_branch origin/some-branch\n>      \n>      \n>      The first command exists purely to make the second one see an up-to-date\n>      view of the remote. If I forget it, origin/some-branch points at a stale\n>      commit, and I end up creating a local branch from the wrong starting\n>      point.\n>      \n>      This series teaches git checkout (and git switch) a new --fetch flag\n>      that folds the two steps into one:\n>      \n>      git checkout --fetch -b new_branch origin/some-branch\n>      \n>      \n>      When the start-point argument names a configured remote — either bare\n>      (origin, which resolves to the remote's default branch) or in / form —\n>      git fetch is run before the start-point is resolved. If the fetch fails,\n>      the checkout aborts and no local branch is created.\n>      \n>      A new checkout.autoFetch config option enables the same behavior by\n>      default, for users who always want it.\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1\n> Pull-Request: https://github.com/git/git/pull/2281\n> \n>   builtin/checkout.c    | 48 ++++++++++++++++++++++++++++++++++++++--\n>   t/t7201-co.sh         | 51 +++++++++++++++++++++++++++++++++++++++++++\n>   t/t9902-completion.sh |  1 +\n>   3 files changed, 98 insertions(+), 2 deletions(-)\n> \n> diff --git a/builtin/checkout.c b/builtin/checkout.c\n> index e031e61886..c8fbc4923b 100644\n> --- a/builtin/checkout.c\n> +++ b/builtin/checkout.c\n> @@ -30,7 +30,9 @@\n>   #include \"repo-settings.h\"\n>   #include \"resolve-undo.h\"\n>   #include \"revision.h\"\n> +#include \"run-command.h\"\n>   #include \"setup.h\"\n> +#include \"strvec.h\"\n>   #include \"submodule.h\"\n>   #include \"symlinks.h\"\n>   #include \"trace2.h\"\n> @@ -61,6 +63,7 @@ struct checkout_opts {\n>   \tint count_checkout_paths;\n>   \tint overlay_mode;\n>   \tint dwim_new_local_branch;\n> +\tint auto_fetch;\n>   \tint discard_changes;\n>   \tint accept_ref;\n>   \tint accept_pathspec;\n> @@ -112,6 +115,34 @@ struct branch_info {\n>   \tchar *checkout;\n>   };\n>   \n> +static void fetch_remote_for_start_point(const char *arg)\n> +{\n> +\tconst char *slash;\n> +\tchar *remote_name;\n> +\tstruct remote *remote;\n> +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n> +\n> +\tif (!arg || !*arg)\n> +\t\treturn;\n> +\n> +\tslash = strchr(arg, '/');\n> +\tif (slash == arg)\n> +\t\treturn;\n> +\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n> +\n> +\tremote = remote_get(remote_name);\n> +\tif (!remote || !remote_is_configured(remote, 1)) {\n> +\t\tfree(remote_name);\n> +\t\treturn;\n> +\t}\n> +\n> +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n> +\tcmd.git_cmd = 1;\n> +\tfree(remote_name);\n> +\tif (run_command(&cmd))\n> +\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n> +}\n> +\n>   static void branch_info_release(struct branch_info *info)\n>   {\n>   \tfree(info->name);\n> @@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value,\n>   \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n>   \t\treturn 0;\n>   \t}\n> +\tif (!strcmp(var, \"checkout.autofetch\")) {\n> +\t\topts->auto_fetch = git_config_bool(var, value);\n> +\t\treturn 0;\n> +\t}\n>   \n>   \tif (starts_with(var, \"submodule.\"))\n>   \t\treturn git_default_submodule_config(var, value, NULL);\n> @@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>   \t\t\topts->dwim_new_local_branch &&\n>   \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n>   \t\t\t!opts->new_branch;\n> -\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n> -\t\t\t\t\t     &new_branch_info, opts, &rev);\n> +\t\tint n;\n> +\n> +\t\tif (opts->auto_fetch)\n> +\t\t\tfetch_remote_for_start_point(argv[0]);\n> +\n> +\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n> +\t\t\t\t\t &new_branch_info, opts, &rev);\n>   \t\targv += n;\n>   \t\targc -= n;\n>   \t} else if (!opts->accept_ref && opts->from_treeish) {\n> @@ -2052,6 +2092,8 @@ int cmd_checkout(int argc,\n>   \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n>   \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n>   \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n> +\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n> +\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n>   \t\tOPT_END()\n>   \t};\n>   \n> @@ -2102,6 +2144,8 @@ int cmd_switch(int argc,\n>   \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n>   \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n>   \t\t\t N_(\"throw away local modifications\")),\n> +\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n> +\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n>   \t\tOPT_END()\n>   \t};\n>   \n> diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> index 9bcf7c0b40..60ddebd9c3 100755\n> --- a/t/t7201-co.sh\n> +++ b/t/t7201-co.sh\n> @@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n>   \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n>   '\n>   \n> +test_expect_success 'setup upstream for --fetch tests' '\n> +\tgit checkout main &&\n> +\tgit init fetch_upstream &&\n> +\ttest_commit -C fetch_upstream u_main &&\n> +\tgit remote add fetch_upstream fetch_upstream &&\n> +\tgit fetch fetch_upstream &&\n> +\tgit -C fetch_upstream checkout -b fetch_new &&\n> +\ttest_commit -C fetch_upstream u_new\n> +'\n> +\n> +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> +\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n> +'\n> +\n> +test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_new2 &&\n> +\ttest_commit -C fetch_upstream u_new2 &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n> +\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n> +\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n> +'\n> +\n> +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n> +\tgit checkout main &&\n> +\ttest_might_fail git branch -D bogus &&\n> +\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n> +'\n> +\n> +test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_cfg &&\n> +\ttest_commit -C fetch_upstream u_cfg &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n> +\tgit -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n> +'\n> +\n> +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_switch &&\n> +\ttest_commit -C fetch_upstream u_switch &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> +\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> +'\n> +\n>   test_done\n> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\n> index 2f9a597ec7..dc1d63669f 100755\n> --- a/t/t9902-completion.sh\n> +++ b/t/t9902-completion.sh\n> @@ -2602,6 +2602,7 @@ test_expect_success 'double dash \"git checkout\"' '\n>   \t--ignore-other-worktrees Z\n>   \t--recurse-submodules Z\n>   \t--auto-advance Z\n> +\t--fetch Z\n>   \t--progress Z\n>   \t--guess Z\n>   \t--no-guess Z\n> \n> base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n\n"},{"id":"542282","messageId":"xmqqeck4xan3.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-04-24T22:21:20Z","receivedAt":"2026-04-24T22:21:22Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Add a --fetch option to git checkout and git switch, plus a\n> checkout.autoFetch config to enable it by default. When set and the\n> start-point argument names a configured remote (either bare, like\n> \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n> resolving the ref. Aborts the checkout if the fetch fails.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n\nIt is true that \"checkout\" does funny things to special case the\nremote-tracking branches, like setting up the branch.<name>.merge\nconfiguration or even inferring the name of the local branch to be\ncreated.  \n\nBut I have to say that this one, especially the configuration\nvariable, goes way too far.  The usual uses of remote-tracking\nbranch names, e.g.,\n\n    git log -1 origin/master\n    git grep frotz origin/master\n    git rev-list --count origin/maint..origin/master\n\nto name a specific object all assume and rely on the stability of\nthem.  Should the configuration cause a fetch to happen before any\nof these uses of remote-tracking branches for consistency?\n"},{"id":"542284","messageId":"xmqqfr4jwxzi.fsf@gitster.g","threadId":"65546","inReplyTo":"xmqqeck4xan3.fsf@gitster.g","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-04-25T02:54:41Z","receivedAt":"2026-04-25T02:54:43Z","isPatch":true,"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n>> From: Harald Nordgren <haraldnordgren@gmail.com>\n>>\n>> Add a --fetch option to git checkout and git switch, plus a\n>> checkout.autoFetch config to enable it by default. When set and the\n>> start-point argument names a configured remote (either bare, like\n>> \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n>> resolving the ref. Aborts the checkout if the fetch fails.\n>>\n>> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n>> ---\n>\n> It is true that \"checkout\" does funny things to special case the\n> remote-tracking branches, like setting up the branch.<name>.merge\n> configuration or even inferring the name of the local branch to be\n> created.  \n> ...\n> ...  Should the configuration cause a fetch to happen before any\n> of these uses of remote-tracking branches for consistency?\n\nThe last one was a rhetorical question.  I do not want to see such a\nconfiguration variable to implicitly trigger fetching at all.\n\nI am somewhat sympathetic to the desire \"I want to be sure that I\nstart the new branch in a state as fresh as possible\".  It is tied\nto the \"--track\" option of \"git checkout -b topic --track\norigin/main\".  If you are merely starting at a single arbitrary\ncommit, instead of anticipating to having to repeatedly sync with\nthe remote-tracking branch that will subsequently move, there is no\npoint jumping to a \"freshest\" commit that you haven't even seen let\nalone inspected (i.e., you do not even know if it is a good base to\nbuild on).\n\nSo instead of introducing a totally new option that can only be used\nonly when \"--track\" is given, it might make more sense to introduce\nthis as a variant of \"--track\", perhaps \"--track=fetch,[in]direct\"\nor something like that.  And extend branch.autosetup{Merge,Rebase}\nthat controls what happens when a branch is created with \"checkout\n-t -b\" or \"branch --track\" so that the remote-tracking branch gets\nupdated, perhaps.\n\nAs to \"git checkout origin/main\" (nothing else on the command line),\nit has \"magic\" compared to \"git checkout origin/main~0\" already by\ntreating the parameter not just as a SHA-1 expression that names a\ncommit object but as a remote-tracking branch (this is necessary for\n\"-t\").  So I am not fundamentally opposed to the idea to give an\noption to treat that form specifically.\n\nHaving said all that, quite honestly, I prefer not to see any of the\nabove changes, including the original patch.  It leaves too many\nusability questions unaddressed.  For a starter, if you interact\nwith a repository with two or more branches, should\n\n   $ git checkout --track=fetch -b topic origin/main\n\nupdate an unrelated remote-tracking branch origin/maint from the\nsame remote?  As I already said, most Git tools _depend_ on the\nstability of remote-tracking branches---the desire to update the\norigin/main when a new branch that builds on origin/main is created\nmay be a valid one, but it is unclear if that warrants updating\nother remote-tracking branches only because they come from the same\nremote repository.  There may be a dozen other UI/usability issues\nthat will be introduced if we start to \"fetch from remote\"\nautomatically, but I won't even try to be exhaustive while I am\nstill on a leave ;-)\n\n"},{"id":"542298","messageId":"20260425172451.21365-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"CALnO6CCNoo8y2V5KmE0KQ6qDurZELipFowcr=ZpZ3ocVB-uLjA@mail.gmail.com","subject":"Comments on Phillip's review","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:24:51Z","receivedAt":"2026-04-25T17:24:54Z","isPatch":false,"body":"> When you realize this, \"git pull --rebase\" should help correct it.\n\nSure. I always run with\n\n```\ngit config --global pull.rebase=true\n```\n\nI love rebasing and recomdend it to all colleagues that will listen, but\nit still sucks to expose yourself to a possible merge conflict when you\nrealize you worked hours on top of a stale main branch.\n\n> I could certainly see this being convenient.\n\n🙌🏻\n\n\nHarald\n"},{"id":"542299","messageId":"20260425174157.44868-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"89f923bf-e5fc-4557-a2f0-d240db07eaf9@app.fastmail.com","subject":"Comments on Phillip's review","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:41:57Z","receivedAt":"2026-04-25T17:42:00Z","isPatch":false,"body":"> > Add a --fetch option to git checkout and git switch, plus a\n> > checkout.autoFetch config to enable it by default. When set and the\n> \n> Why is the config not `checkout.config`? So it’s named the same as the\n> option (modulo snake case/camel case which is not relevant here).\n\nWill rename the config to 'checkout.fetch'.\n\n> The motivation for why this is being proposed maybe might as well go in\n> the commit message. Maybe that’s just me.\n> \n> The commit message just says that “this thing is added”. Not why.\n\nI will update it.\n\n> I guess a later version will have the changes to the documentation.\n\nI forgot that, will add it!\n\n> s/remote-tracking ref/remote-tracking branch/ ?\n> \n> git(1) doesn’t have a namespace for tracking refs in general.\n\n👍\n\n\nHarald\n"},{"id":"542300","messageId":"20260425174423.45523-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"20260425172451.21365-1-haraldnordgren@gmail.com","subject":"Wrong subject line","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:44:23Z","receivedAt":"2026-04-25T17:44:26Z","isPatch":false,"body":"I know you are not Ben. Forgot to change the subject line from a previous message.\n\n\nHarald\n"},{"id":"542301","messageId":"20260425174448.45681-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"20260425174157.44868-1-haraldnordgren@gmail.com","subject":"Wrong subject line","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:44:48Z","receivedAt":"2026-04-25T17:44:51Z","isPatch":false,"body":"I know you are not Ben. Forgot to change the subject line from a previous message.\n\n\nHarald\n"},{"id":"542302","messageId":"20260425174811.46941-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"42a620db-6110-4894-97fb-8e49ec577817@gmail.com","subject":"Wrong subject line","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:48:11Z","receivedAt":"2026-04-25T17:48:14Z","isPatch":false,"body":"I know you are not Ben. Forgot to change the subject line from a previous message.\n\n\nHarald\n"},{"id":"542303","messageId":"20260425175824.48380-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"xmqqfr4jwxzi.fsf@gitster.g","subject":"Multiple remotes","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T17:58:24Z","receivedAt":"2026-04-25T17:58:27Z","isPatch":false,"body":"> The last one was a rhetorical question.  I do not want to see such a\n> configuration variable to implicitly trigger fetching at all.\n\n🤣\n\nGood to clarify that when working with me so that I don't go ahead and\nimplement that!\n\n> If you are merely starting at a single arbitrary\n> commit, instead of anticipating to having to repeatedly sync with\n> the remote-tracking branch that will subsequently move, there is no\n> point jumping to a \"freshest\" commit that you haven't even seen let\n> alone inspected (i.e., you do not even know if it is a good base to\n> build on).\n\nNot sure I understand this sentiment. For better or worse, the latest\ncommit will decide what you have to work with -- unless we expect it to be\nreverted or forced pushed over.\n\nWhat better starting point is there?\n\n> For a starter, if you interact\n> with a repository with two or more branches, should\n> \n>    $ git checkout --track=fetch -b topic origin/main\n> \n> update an unrelated remote-tracking branch origin/maint from the\n> same remote?  As I already said, most Git tools _depend_ on the\n> stability of remote-tracking branches\n\nThis is an interesting question, and it's very likely that I am missing\nsome nuance here. However, with that said what option does the developer\nhave, you have to accept that the upstream changes constantly when others\nare working on it. What good does it do to keep the \"head in the sand\" any\nlonger than necessary?\n\nI'm not sure there is a way to fetch only 'origin/main' and avoid\n'origin/maint'? Maybe, maybe, if that exists it could be useful here.\n\n> still on a leave\n\nEnjoy your vacation! I don't expect any response from you until you're back!\n\n\nHarald\n"},{"id":"542305","messageId":"pull.2281.v2.git.git.1777140755373.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.git.git.1777024991531.gitgitgadget@gmail.com","subject":"[PATCH v2] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-04-25T18:12:35Z","receivedAt":"2026-04-25T18:12:39Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA common workflow is:\n\n    git fetch origin\n    git checkout -b new_branch origin/some-branch\n\nThe first command exists purely so the second sees an up-to-date view\nof the remote. If it is forgotten, origin/some-branch points at a stale\ncommit and the new local branch is created from the wrong start point.\n\nTeach checkout (and switch) a --fetch flag that folds the two steps\ninto one:\n\n    git checkout --fetch -b new_branch origin/some-branch\n\nWhen --fetch is given and <start-point> names a configured remote\n(either bare, like \"origin\", or prefixed, like \"origin/foo\"), fetch\nthat remote before resolving the ref. Abort the checkout if the fetch\nfails.\n\nAlso add a checkout.fetch config to enable this by default.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n     * Rename the config from checkout.autoFetch to checkout.fetch, so it\n       matches the --fetch option name.\n    \n     * Rename the internal struct field from auto_fetch to fetch for\n       consistency with the option and config names.\n    \n     * Reword the commit message to lead with the problem (forgetting 'git\n       fetch' and ending up with a stale start-point) before describing the\n       solution.\n    \n     * Document --fetch / --no-fetch in git-checkout and git-switch, and\n       document checkout.fetch in the config reference.\n    \n     * Use \"remote-tracking branch\" instead of \"remote-tracking ref\" in the\n       option help text.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v2\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v1:\n\n 1:  e2fa50ff40 ! 1:  13074c9fea checkout: add --fetch to fetch remote before resolving start-point\n     @@ Metadata\n       ## Commit message ##\n          checkout: add --fetch to fetch remote before resolving start-point\n      \n     -    Add a --fetch option to git checkout and git switch, plus a\n     -    checkout.autoFetch config to enable it by default. When set and the\n     -    start-point argument names a configured remote (either bare, like\n     -    \"origin\", or prefixed, like \"origin/foo\"), fetch that remote before\n     -    resolving the ref. Aborts the checkout if the fetch fails.\n     +    A common workflow is:\n     +\n     +        git fetch origin\n     +        git checkout -b new_branch origin/some-branch\n     +\n     +    The first command exists purely so the second sees an up-to-date view\n     +    of the remote. If it is forgotten, origin/some-branch points at a stale\n     +    commit and the new local branch is created from the wrong start point.\n     +\n     +    Teach checkout (and switch) a --fetch flag that folds the two steps\n     +    into one:\n     +\n     +        git checkout --fetch -b new_branch origin/some-branch\n     +\n     +    When --fetch is given and <start-point> names a configured remote\n     +    (either bare, like \"origin\", or prefixed, like \"origin/foo\"), fetch\n     +    that remote before resolving the ref. Abort the checkout if the fetch\n     +    fails.\n     +\n     +    Also add a checkout.fetch config to enable this by default.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     + ## Documentation/config/checkout.adoc ##\n     +@@ Documentation/config/checkout.adoc: commands or functionality in the future.\n     + \toption in `git checkout` and `git switch`. See\n     + \tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n     + \n     ++`checkout.fetch`::\n     ++\tProvides the default value for the `--fetch` or `--no-fetch`\n     ++\toption in `git checkout` and `git switch`. See\n     ++\tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n     ++\n     + `checkout.workers`::\n     + \tThe number of parallel workers to use when updating the working tree.\n     + \tThe default is one, i.e. sequential execution. If set to a value less\n     +\n     + ## Documentation/git-checkout.adoc ##\n     +@@ Documentation/git-checkout.adoc: linkgit:git-config[1].\n     + The default behavior can be set via the `checkout.guess` configuration\n     + variable.\n     + \n     ++`--fetch`::\n     ++`--no-fetch`::\n     ++\tIf _<start-point>_ names a configured remote -- either bare,\n     ++\tlike `origin` (which resolves to the remote's default branch),\n     ++\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n     ++\tremote before resolving _<start-point>_. If the fetch fails,\n     ++\tthe checkout is aborted and no local branch is created.\n     +++\n     ++The default behavior can be set via the `checkout.fetch` configuration\n     ++variable.\n     ++\n     + `-l`::\n     + \tCreate the new branch's reflog; see linkgit:git-branch[1] for\n     + \tdetails.\n     +\n     + ## Documentation/git-switch.adoc ##\n     +@@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See also\n     + The default behavior can be set via the `checkout.guess` configuration\n     + variable.\n     + \n     ++`--fetch`::\n     ++`--no-fetch`::\n     ++\tIf _<start-point>_ names a configured remote -- either bare,\n     ++\tlike `origin` (which resolves to the remote's default branch),\n     ++\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n     ++\tremote before resolving _<start-point>_. If the fetch fails,\n     ++\tthe switch is aborted and no local branch is created.\n     +++\n     ++The default behavior can be set via the `checkout.fetch` configuration\n     ++variable.\n     ++\n     + `-f`::\n     + `--force`::\n     + \tAn alias for `--discard-changes`.\n     +\n       ## builtin/checkout.c ##\n      @@\n       #include \"repo-settings.h\"\n     @@ builtin/checkout.c: struct checkout_opts {\n       \tint count_checkout_paths;\n       \tint overlay_mode;\n       \tint dwim_new_local_branch;\n     -+\tint auto_fetch;\n     ++\tint fetch;\n       \tint discard_changes;\n       \tint accept_ref;\n       \tint accept_pathspec;\n     @@ builtin/checkout.c: static int git_checkout_config(const char *var, const char *\n       \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n       \t\treturn 0;\n       \t}\n     -+\tif (!strcmp(var, \"checkout.autofetch\")) {\n     -+\t\topts->auto_fetch = git_config_bool(var, value);\n     ++\tif (!strcmp(var, \"checkout.fetch\")) {\n     ++\t\topts->fetch = git_config_bool(var, value);\n      +\t\treturn 0;\n      +\t}\n       \n     @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const\n      -\t\t\t\t\t     &new_branch_info, opts, &rev);\n      +\t\tint n;\n      +\n     -+\t\tif (opts->auto_fetch)\n     ++\t\tif (opts->fetch)\n      +\t\t\tfetch_remote_for_start_point(argv[0]);\n      +\n      +\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n     @@ builtin/checkout.c: int cmd_checkout(int argc,\n       \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n       \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n       \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n     -+\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n     -+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n     ++\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n     ++\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n       \t\tOPT_END()\n       \t};\n       \n     @@ builtin/checkout.c: int cmd_switch(int argc,\n       \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n       \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n       \t\t\t N_(\"throw away local modifications\")),\n     -+\t\tOPT_BOOL(0, \"fetch\", &opts.auto_fetch,\n     -+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking ref\")),\n     ++\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n     ++\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n       \t\tOPT_END()\n       \t};\n       \n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n      +'\n      +\n     -+test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' '\n     ++test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_cfg &&\n      +\ttest_commit -C fetch_upstream u_cfg &&\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n     -+\tgit -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n     ++\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n      +'\n      +\n\n\n Documentation/config/checkout.adoc |  5 +++\n Documentation/git-checkout.adoc    | 11 +++++++\n Documentation/git-switch.adoc      | 11 +++++++\n builtin/checkout.c                 | 48 ++++++++++++++++++++++++++--\n t/t7201-co.sh                      | 51 ++++++++++++++++++++++++++++++\n t/t9902-completion.sh              |  1 +\n 6 files changed, 125 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc\nindex e35d212969..c95f72b38e 100644\n--- a/Documentation/config/checkout.adoc\n+++ b/Documentation/config/checkout.adoc\n@@ -22,6 +22,11 @@ commands or functionality in the future.\n \toption in `git checkout` and `git switch`. See\n \tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n \n+`checkout.fetch`::\n+\tProvides the default value for the `--fetch` or `--no-fetch`\n+\toption in `git checkout` and `git switch`. See\n+\tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n+\n `checkout.workers`::\n \tThe number of parallel workers to use when updating the working tree.\n \tThe default is one, i.e. sequential execution. If set to a value less\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..f20e2f4c8c 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -201,6 +201,17 @@ linkgit:git-config[1].\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ names a configured remote -- either bare,\n+\tlike `origin` (which resolves to the remote's default branch),\n+\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n+\tremote before resolving _<start-point>_. If the fetch fails,\n+\tthe checkout is aborted and no local branch is created.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-l`::\n \tCreate the new branch's reflog; see linkgit:git-branch[1] for\n \tdetails.\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..3826ed9066 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ names a configured remote -- either bare,\n+\tlike `origin` (which resolves to the remote's default branch),\n+\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n+\tremote before resolving _<start-point>_. If the fetch fails,\n+\tthe switch is aborted and no local branch is created.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-f`::\n `--force`::\n \tAn alias for `--discard-changes`.\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..b2a34f0f00 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,34 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(var, \"checkout.fetch\")) {\n+\t\topts->fetch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n@@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\n@@ -2052,6 +2092,8 @@ int cmd_checkout(int argc,\n \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -2102,6 +2144,8 @@ int cmd_switch(int argc,\n \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n \t\t\t N_(\"throw away local modifications\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..f5729f0831 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n+'\n+\n+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_cfg &&\n+\ttest_commit -C fetch_upstream u_cfg &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n+\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n+'\n+\n+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 2f9a597ec7..dc1d63669f 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2602,6 +2602,7 @@ test_expect_success 'double dash \"git checkout\"' '\n \t--ignore-other-worktrees Z\n \t--recurse-submodules Z\n \t--auto-advance Z\n+\t--fetch Z\n \t--progress Z\n \t--guess Z\n \t--no-guess Z\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542306","messageId":"E5737766-2A41-46A6-A3FD-530CAC5076F4@gmail.com","threadId":"65546","inReplyTo":"20260425175824.48380-1-haraldnordgren@gmail.com","subject":"Re: Multiple remotes","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-04-25T21:57:25Z","receivedAt":"2026-04-25T21:57:38Z","isPatch":false,"body":"\n> Le 25 avr. 2026 à 13:58, Harald Nordgren <haraldnordgren@gmail.com> a écrit :\n> \n> ﻿\n>> \n>> The last one was a rhetorical question.  I do not want to see such a\n>> configuration variable to implicitly trigger fetching at all.\n> \n> 🤣\n> \n> Good to clarify that when working with me so that I don't go ahead and\n> implement that!\n> \n>> If you are merely starting at a single arbitrary\n>> commit, instead of anticipating to having to repeatedly sync with\n>> the remote-tracking branch that will subsequently move, there is no\n>> point jumping to a \"freshest\" commit that you haven't even seen let\n>> alone inspected (i.e., you do not even know if it is a good base to\n>> build on).\n> \n> Not sure I understand this sentiment. For better or worse, the latest\n> commit will decide what you have to work with -- unless we expect it to be\n> reverted or forced pushed over.\n> \n> What better starting point is there?\n> \n>> For a starter, if you interact\n>> with a repository with two or more branches, should\n>> \n>>   $ git checkout --track=fetch -b topic origin/main\n>> \n>> update an unrelated remote-tracking branch origin/maint from the\n>> same remote?  As I already said, most Git tools _depend_ on the\n>> stability of remote-tracking branches\n> \n> This is an interesting question, and it's very likely that I am missing\n> some nuance here. However, with that said what option does the developer\n> have, you have to accept that the upstream changes constantly when others\n> are working on it. What good does it do to keep the \"head in the sand\" any\n> longer than necessary?\n> \n> I'm not sure there is a way to fetch only 'origin/main' and avoid\n> 'origin/maint'? Maybe, maybe, if that exists it could be useful here.\n\nIsn’t that exactly what\n\n    git fetch origin main\n\ndoes? (Might need to expand the refspec.)\n\n> \n>> still on a leave\n> \n> Enjoy your vacation! I don't expect any response from you until you're back!\n> \n> \n> Harald\n> \n"},{"id":"542309","messageId":"20260425225405.7915-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"E5737766-2A41-46A6-A3FD-530CAC5076F4@gmail.com","subject":"gh","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-25T22:54:05Z","receivedAt":"2026-04-25T22:54:09Z","isPatch":false,"body":"> Isn’t that exactly what\n> \n>     git fetch origin main\n> \n> does? (Might need to expand the refspec.)\n\nVery good point, I will update it!\n\n\nHarald\n"},{"id":"542317","messageId":"c61933c9-e489-4f4f-b4f4-74267d5eb76f@app.fastmail.com","threadId":"65546","inReplyTo":"20260425174448.45681-1-haraldnordgren@gmail.com","subject":"Re: Wrong subject line","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-04-26T07:07:52Z","receivedAt":"2026-04-26T07:10:04Z","isPatch":false,"body":"On Sat, Apr 25, 2026, at 19:44, Harald Nordgren wrote:\n> I know you are not Ben. Forgot to change the subject line from a\n> previous message.\n\nYou mean I’m not Phillip. ;)\n\n    Subject: Comments on Phillip's review\n\nI don’t understand why you change the email subjects so often. Right now\nI had three “Wrong subject line” in my inbox with lost threading\n(webmail client) with the only way to distinguish them being that I was\nthe the CC on this one.\n\nMost of the time whole 100-email threads like patch series never change\nthe subject. And to me it is easier to keep track of those “RE: [PATCH\nv5] florb: drop glorb” than if someone changes the subject to e.g.\n“Regarding memory leaks” because someone found a memory leak in a\nreview. Because that was a reply to an email from two days ago, but I’ve\nbeen a away for a week so I think it’s a new thread about\nsomething else.\n\nThat’s just my experience. My amateur webmail setup doesn’t really\nmatter here since I just dip in/interrupt threads when I feel like it.\n"},{"id":"542318","messageId":"pull.2281.v3.git.git.1777188295021.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v2.git.git.1777140755373.gitgitgadget@gmail.com","subject":"[PATCH v3] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-04-26T07:24:54Z","receivedAt":"2026-04-26T07:24:58Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA common workflow is:\n\n    git fetch origin\n    git checkout -b new_branch origin/some-branch\n\nThe first command exists purely so the second sees an up-to-date view\nof the remote. If it is forgotten, origin/some-branch points at a stale\ncommit and the new local branch is created from the wrong start point.\n\nTeach checkout (and switch) a --fetch flag that folds the two steps\ninto one:\n\n    git checkout --fetch -b new_branch origin/some-branch\n\nWhen --fetch is given and <start-point> is in <remote>/<branch> form,\nrun \"git fetch <remote> <branch>\" before resolving the ref. This\nnarrows the fetch to the requested branch so that other\nremote-tracking branches are left untouched -- many tools rely on the\nstability of remote-tracking refs between explicit fetches. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nAlso add a checkout.fetch config to enable this by default.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n    When <start-point> is in <remote>/<branch> form, only fetch that one\n    branch instead of the whole remote, so unrelated remote-tracking\n    branches stay stable. The bare-remote form (e.g. \"origin\") still fetches\n    everything.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v3\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v2:\n\n 1:  13074c9fea ! 1:  df7b63862c checkout: add --fetch to fetch remote before resolving start-point\n     @@ Commit message\n      \n              git checkout --fetch -b new_branch origin/some-branch\n      \n     -    When --fetch is given and <start-point> names a configured remote\n     -    (either bare, like \"origin\", or prefixed, like \"origin/foo\"), fetch\n     -    that remote before resolving the ref. Abort the checkout if the fetch\n     -    fails.\n     +    When --fetch is given and <start-point> is in <remote>/<branch> form,\n     +    run \"git fetch <remote> <branch>\" before resolving the ref. This\n     +    narrows the fetch to the requested branch so that other\n     +    remote-tracking branches are left untouched -- many tools rely on the\n     +    stability of remote-tracking refs between explicit fetches. If\n     +    <start-point> is a bare remote name like \"origin\" (which resolves to\n     +    that remote's default branch), \"git fetch <remote>\" is run instead,\n     +    since the target branch is not known up front. Abort the checkout if\n     +    the fetch fails.\n      \n          Also add a checkout.fetch config to enable this by default.\n      \n     @@ Documentation/git-checkout.adoc: linkgit:git-config[1].\n       \n      +`--fetch`::\n      +`--no-fetch`::\n     -+\tIf _<start-point>_ names a configured remote -- either bare,\n     -+\tlike `origin` (which resolves to the remote's default branch),\n     -+\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n     -+\tremote before resolving _<start-point>_. If the fetch fails,\n     -+\tthe checkout is aborted and no local branch is created.\n     ++\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n     ++\tfrom that remote before resolving it. When _<start-point>_ is\n     ++\tin _<remote>/<branch>_ form, only that branch is updated; when\n     ++\tit is a bare remote name (e.g. `origin`), the whole remote is\n     ++\tfetched. If the fetch fails, the checkout is aborted.\n      ++\n      +The default behavior can be set via the `checkout.fetch` configuration\n      +variable.\n     @@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See\n       \n      +`--fetch`::\n      +`--no-fetch`::\n     -+\tIf _<start-point>_ names a configured remote -- either bare,\n     -+\tlike `origin` (which resolves to the remote's default branch),\n     -+\tor in _<remote>/<branch>_ form -- run `git fetch` on that\n     -+\tremote before resolving _<start-point>_. If the fetch fails,\n     -+\tthe switch is aborted and no local branch is created.\n     ++\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n     ++\tfrom that remote before resolving it. When _<start-point>_ is\n     ++\tin _<remote>/<branch>_ form, only that branch is updated; when\n     ++\tit is a bare remote name (e.g. `origin`), the whole remote is\n     ++\tfetched. If the fetch fails, the switch is aborted.\n      ++\n      +The default behavior can be set via the `checkout.fetch` configuration\n      +variable.\n     @@ builtin/checkout.c: struct branch_info {\n      +\t}\n      +\n      +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n     ++\tif (slash && slash[1])\n     ++\t\tstrvec_push(&cmd.args, slash + 1);\n      +\tcmd.git_cmd = 1;\n      +\tfree(remote_name);\n      +\tif (run_command(&cmd))\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n      +'\n      +\n     ++test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_target &&\n     ++\ttest_commit -C fetch_upstream u_target_pre &&\n     ++\tgit -C fetch_upstream checkout -b fetch_other &&\n     ++\ttest_commit -C fetch_upstream u_other_pre &&\n     ++\tgit fetch fetch_upstream &&\n     ++\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n     ++\tgit -C fetch_upstream checkout fetch_target &&\n     ++\ttest_commit -C fetch_upstream u_target_post &&\n     ++\tgit -C fetch_upstream checkout fetch_other &&\n     ++\ttest_commit -C fetch_upstream u_other_post &&\n     ++\tgit checkout --fetch -b local_target fetch_upstream/fetch_target &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n     ++\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n     ++'\n     ++\n      +test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_new2 &&\n\n\n Documentation/config/checkout.adoc |  5 +++\n Documentation/git-checkout.adoc    | 11 +++++\n Documentation/git-switch.adoc      | 11 +++++\n builtin/checkout.c                 | 50 +++++++++++++++++++++-\n t/t7201-co.sh                      | 68 ++++++++++++++++++++++++++++++\n t/t9902-completion.sh              |  1 +\n 6 files changed, 144 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc\nindex e35d212969..c95f72b38e 100644\n--- a/Documentation/config/checkout.adoc\n+++ b/Documentation/config/checkout.adoc\n@@ -22,6 +22,11 @@ commands or functionality in the future.\n \toption in `git checkout` and `git switch`. See\n \tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n \n+`checkout.fetch`::\n+\tProvides the default value for the `--fetch` or `--no-fetch`\n+\toption in `git checkout` and `git switch`. See\n+\tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n+\n `checkout.workers`::\n \tThe number of parallel workers to use when updating the working tree.\n \tThe default is one, i.e. sequential execution. If set to a value less\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..f5cc1ced74 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -201,6 +201,17 @@ linkgit:git-config[1].\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n+\tfrom that remote before resolving it. When _<start-point>_ is\n+\tin _<remote>/<branch>_ form, only that branch is updated; when\n+\tit is a bare remote name (e.g. `origin`), the whole remote is\n+\tfetched. If the fetch fails, the checkout is aborted.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-l`::\n \tCreate the new branch's reflog; see linkgit:git-branch[1] for\n \tdetails.\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..29743bafea 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n+\tfrom that remote before resolving it. When _<start-point>_ is\n+\tin _<remote>/<branch>_ form, only that branch is updated; when\n+\tit is a bare remote name (e.g. `origin`), the whole remote is\n+\tfetched. If the fetch fails, the switch is aborted.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-f`::\n `--force`::\n \tAn alias for `--discard-changes`.\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..8d810fe2fa 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,36 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (slash && slash[1])\n+\t\tstrvec_push(&cmd.args, slash + 1);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,6 +1270,10 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(var, \"checkout.fetch\")) {\n+\t\topts->fetch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n@@ -1942,8 +1979,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\n@@ -2052,6 +2094,8 @@ int cmd_checkout(int argc,\n \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -2102,6 +2146,8 @@ int cmd_switch(int argc,\n \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n \t\t\t N_(\"throw away local modifications\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..cf2ceb4052 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,72 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n+'\n+\n+test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_cfg &&\n+\ttest_commit -C fetch_upstream u_cfg &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n+\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n+'\n+\n+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 2f9a597ec7..dc1d63669f 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2602,6 +2602,7 @@ test_expect_success 'double dash \"git checkout\"' '\n \t--ignore-other-worktrees Z\n \t--recurse-submodules Z\n \t--auto-advance Z\n+\t--fetch Z\n \t--progress Z\n \t--guess Z\n \t--no-guess Z\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542331","messageId":"20260426151528.49016-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"c61933c9-e489-4f4f-b4f4-74267d5eb76f@app.fastmail.com","subject":"Re: [PATCH] remote: add --set-head option to 'git remote add'","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-26T15:15:28Z","receivedAt":"2026-04-26T15:15:31Z","isPatch":true,"body":"> I don’t understand why you change the email subjects so often. Right now\n> I had three “Wrong subject line” in my inbox with lost threading\n> (webmail client) with the only way to distinguish them being that I was\n> the the CC on this one.\n> \n> Most of the time whole 100-email threads like patch series never change\n> the subject. And to me it is easier to keep track of those “RE: [PATCH\n> v5] florb: drop glorb” than if someone changes the subject to e.g.\n> “Regarding memory leaks” because someone found a memory leak in a\n> review. Because that was a reply to an email from two days ago, but I’ve\n> been a away for a week so I think it’s a new thread about\n> something else.\n\nI got some feedback before, not from you, that subject lines should be\nmore varied, probably there is a golden middle that I need to find.\n\nAnd I fundamentally agree with you.\n\n\nHarald\n"},{"id":"542332","messageId":"0c589c18-e8f6-42ac-842f-ec1f5f6fb3ff@ramsayjones.plus.com","threadId":"65546","inReplyTo":"pull.2281.v3.git.git.1777188295021.gitgitgadget@gmail.com","subject":"Re: [PATCH v3] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsayjones.plus.com","sentAt":"2026-04-26T15:54:14Z","receivedAt":"2026-04-26T15:57:24Z","isPatch":true,"body":"\n\nOn 26/04/2026 8:24 am, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n[snip]\n\n> diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> index 9bcf7c0b40..cf2ceb4052 100755\n> --- a/t/t7201-co.sh\n> +++ b/t/t7201-co.sh\n> @@ -801,4 +801,72 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n>  \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n>  '\n>  \n> +test_expect_success 'setup upstream for --fetch tests' '\n> +\tgit checkout main &&\n> +\tgit init fetch_upstream &&\n> +\ttest_commit -C fetch_upstream u_main &&\n> +\tgit remote add fetch_upstream fetch_upstream &&\n> +\tgit fetch fetch_upstream &&\n> +\tgit -C fetch_upstream checkout -b fetch_new &&\n> +\ttest_commit -C fetch_upstream u_new\n> +'\n> +\n> +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> +\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n> +'\n> +\n> +test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_pre &&\n> +\tgit -C fetch_upstream checkout -b fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_pre &&\n> +\tgit fetch fetch_upstream &&\n> +\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n> +\tgit -C fetch_upstream checkout fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_post &&\n> +\tgit -C fetch_upstream checkout fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_post &&\n> +\tgit checkout --fetch -b local_target fetch_upstream/fetch_target &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n> +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n> +'\n> +\n> +test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_new2 &&\n> +\ttest_commit -C fetch_upstream u_new2 &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n> +\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n> +\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n> +'\n> +\n> +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n> +\tgit checkout main &&\n> +\ttest_might_fail git branch -D bogus &&\n> +\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n> +'\n> +\n> +test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_cfg &&\n> +\ttest_commit -C fetch_upstream u_cfg &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n> +\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n> +'\n> +\n> +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_switch &&\n> +\ttest_commit -C fetch_upstream u_switch &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> +\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> +'\n> +\n>  test_done\n\nStill just skimming the list, so ... ;)\n\nI seem to have missed v2 (but I guess the config name changed in v2), but it seems\nthat there is still no test that confirms '--no-fetch' can countermand the config\nvariable (or, indeed, an earlier command-line '--fetch' etc,.).\n\n[I would have no use for this facility (I have _never_ used git-pull for similar\nreasons) since I would find it odd to 'mash' git-fetch into git-checkout/switch! :) ]\n\nATB,\nRamsay Jones\n\n\n"},{"id":"542333","messageId":"pull.2281.v4.git.git.1777228346809.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v3.git.git.1777188295021.gitgitgadget@gmail.com","subject":"[PATCH v4] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-04-26T18:32:26Z","receivedAt":"2026-04-26T18:32:30Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA common workflow is:\n\n    git fetch origin\n    git checkout -b new_branch origin/some-branch\n\nThe first command exists purely so the second sees an up-to-date view\nof the remote. If it is forgotten, origin/some-branch points at a stale\ncommit and the new local branch is created from the wrong start point.\n\nTeach checkout (and switch) a --fetch flag that folds the two steps\ninto one:\n\n    git checkout --fetch -b new_branch origin/some-branch\n\nWhen --fetch is given and <start-point> is in <remote>/<branch> form,\nrun \"git fetch <remote> <branch>\" before resolving the ref. This\nnarrows the fetch to the requested branch so that other\nremote-tracking branches are left untouched -- many tools rely on the\nstability of remote-tracking refs between explicit fetches. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nAlso add a checkout.fetch config to enable this by default.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n    Adding tests to confirm that '--no-fetch' can countermand the config.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v4\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v3:\n\n 1:  df7b63862c ! 1:  150ccbb621 checkout: add --fetch to fetch remote before resolving start-point\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n      +'\n      +\n     ++test_expect_success '--no-fetch overrides checkout.fetch=true' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_nofetch &&\n     ++\ttest_commit -C fetch_upstream u_nofetch &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n     ++\ttest_must_fail git -c checkout.fetch=true checkout --no-fetch \\\n     ++\t\t-b local_nofetch fetch_upstream/fetch_nofetch &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_nofetch\n     ++'\n     ++\n     ++test_expect_success '--no-fetch overrides earlier --fetch on command line' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_override &&\n     ++\ttest_commit -C fetch_upstream u_override &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n     ++\ttest_must_fail git checkout --fetch --no-fetch \\\n     ++\t\t-b local_override fetch_upstream/fetch_override &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_override\n     ++'\n     ++\n      +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_switch &&\n\n\n Documentation/config/checkout.adoc |  5 ++\n Documentation/git-checkout.adoc    | 11 ++++\n Documentation/git-switch.adoc      | 11 ++++\n builtin/checkout.c                 | 50 ++++++++++++++++-\n t/t7201-co.sh                      | 90 ++++++++++++++++++++++++++++++\n t/t9902-completion.sh              |  1 +\n 6 files changed, 166 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc\nindex e35d212969..c95f72b38e 100644\n--- a/Documentation/config/checkout.adoc\n+++ b/Documentation/config/checkout.adoc\n@@ -22,6 +22,11 @@ commands or functionality in the future.\n \toption in `git checkout` and `git switch`. See\n \tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n \n+`checkout.fetch`::\n+\tProvides the default value for the `--fetch` or `--no-fetch`\n+\toption in `git checkout` and `git switch`. See\n+\tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n+\n `checkout.workers`::\n \tThe number of parallel workers to use when updating the working tree.\n \tThe default is one, i.e. sequential execution. If set to a value less\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..f5cc1ced74 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -201,6 +201,17 @@ linkgit:git-config[1].\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n+\tfrom that remote before resolving it. When _<start-point>_ is\n+\tin _<remote>/<branch>_ form, only that branch is updated; when\n+\tit is a bare remote name (e.g. `origin`), the whole remote is\n+\tfetched. If the fetch fails, the checkout is aborted.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-l`::\n \tCreate the new branch's reflog; see linkgit:git-branch[1] for\n \tdetails.\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..29743bafea 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also\n The default behavior can be set via the `checkout.guess` configuration\n variable.\n \n+`--fetch`::\n+`--no-fetch`::\n+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n+\tfrom that remote before resolving it. When _<start-point>_ is\n+\tin _<remote>/<branch>_ form, only that branch is updated; when\n+\tit is a bare remote name (e.g. `origin`), the whole remote is\n+\tfetched. If the fetch fails, the switch is aborted.\n++\n+The default behavior can be set via the `checkout.fetch` configuration\n+variable.\n+\n `-f`::\n `--force`::\n \tAn alias for `--discard-changes`.\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..8d810fe2fa 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,36 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (slash && slash[1])\n+\t\tstrvec_push(&cmd.args, slash + 1);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,6 +1270,10 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(var, \"checkout.fetch\")) {\n+\t\topts->fetch = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n@@ -1942,8 +1979,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\n@@ -2052,6 +2094,8 @@ int cmd_checkout(int argc,\n \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -2102,6 +2146,8 @@ int cmd_switch(int argc,\n \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n \t\t\t N_(\"throw away local modifications\")),\n+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n \t\tOPT_END()\n \t};\n \ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..731be2680a 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,94 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n+'\n+\n+test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_cfg &&\n+\ttest_commit -C fetch_upstream u_cfg &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n+\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n+'\n+\n+test_expect_success '--no-fetch overrides checkout.fetch=true' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_nofetch &&\n+\ttest_commit -C fetch_upstream u_nofetch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n+\ttest_must_fail git -c checkout.fetch=true checkout --no-fetch \\\n+\t\t-b local_nofetch fetch_upstream/fetch_nofetch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_nofetch\n+'\n+\n+test_expect_success '--no-fetch overrides earlier --fetch on command line' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_override &&\n+\ttest_commit -C fetch_upstream u_override &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n+\ttest_must_fail git checkout --fetch --no-fetch \\\n+\t\t-b local_override fetch_upstream/fetch_override &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_override\n+'\n+\n+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 2f9a597ec7..dc1d63669f 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -2602,6 +2602,7 @@ test_expect_success 'double dash \"git checkout\"' '\n \t--ignore-other-worktrees Z\n \t--recurse-submodules Z\n \t--auto-advance Z\n+\t--fetch Z\n \t--progress Z\n \t--guess Z\n \t--no-guess Z\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542408","messageId":"xmqqwlxrzwid.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v4.git.git.1777228346809.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-04-28T01:47:22Z","receivedAt":"2026-04-28T01:47:25Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n\n> A common workflow is:\n>\n>     git fetch origin\n>     git checkout -b new_branch origin/some-branch\n>\n> The first command exists purely so the second sees an up-to-date view\n> of the remote.\n\nThis is only half true, isn't it?\n\nGit is among projects that encourage forking only from a well-known\npoint in history (like the latest released version), not at a random\n\"tip of the day\" commit from the upstream.\n\nSuch projects also tend to discourage people from constantly pulling\nupdated upstream into their unfinished topic branch, or rebase their\nunfinished topic branch on top of updated upstream, only to \"catch\nup\", and instead encourage them to make a trial merge to notice when\nthe base got too stale to cause eventual merge to conflict too much,\nand when it happens, make such a back-merge, but otherwise keep\nworking on the stable base and avoid such \"catching up\".\n\nAnd when working with such a project, what users who do the above is\nforgetting is to inspect origin/master between the two steps to see\nif it is a good commit to start your topic at.\n\n> If it is forgotten, origin/some-branch points at a stale commit\n> and the new local branch is created from the wrong start point.\n\nSo this part is not quite true.  What makes your topic begin at a\nwrong starting point is not that you forget to fetch, but you forget\nto verify what you fetched and think if it is a good starting point.\n\nAnd for that verification to happen, you do not want \"checkout\" and\n\"fetch\" mixed into one.\n\nOn the other hand, if you are allowed to fork at anywhere (as\nopposed to a latest release), then not fetching and building on top\nof slightly older codebase is not such a huge deal, as you're likely\nto be making the \"catch up\" changes on top of your unfinished work\nlater anyway.\n\nSo as I already said before, I am fairly negative on this topic.  It\nfeels more like a knob to allow and actively encourage people to be\nmore sloppy than anything else.\n\nI may have already pointed this out (but I do not remember), but\nthis option would not make any sense when --track is not in effect,\nso instead of adding a brand new option, making it an extension to\nthe existing --track option might make it slightly more palatable.\n\n"},{"id":"542415","messageId":"20260428084415.10702-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"xmqqwlxrzwid.fsf@gitster.g","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-04-28T08:44:15Z","receivedAt":"2026-04-28T08:44:19Z","isPatch":true,"body":"I hope you had a good leave and is back with renewed energy! ☀️\n\n> Git is among projects that encourage forking only from a well-known\n> point in history (like the latest released version), not at a random\n> \"tip of the day\" commit from the upstream.\n\nAre you talking about the Git project that we are working on right now, or\ntalking about how people use Git \"in the wild\"?\n\nBecause how people use Git in the wild can be a bit different, and merge\nconflicts arguably the worst part of collaborating with a team using Git.\nIn my early days as a professional coder, snubbed my toe countless times on\nforgetting to pull in the latest changes, before starting to work on\nsomething.\n\nI respect that things work differently in a neatly ordered project like Git\nitself, where you do a great work of organizing, but all other projects are\nnot like that. My advice to a junior developer is to pull in the latest\nchanges when starting and to rebase obsessively to prevent a large merge\nconflict down the road.\n\n> So instead of introducing a totally new option that can only be used\n> only when \"--track\" is given, it might make more sense to introduce\n> this as a variant of \"--track\", perhaps \"--track=fetch,[in]direct\"\n> or something like that. \n\n> I may have already pointed this out (but I do not remember), but\n> this option would not make any sense when --track is not in effect,\n> so instead of adding a brand new option, making it an extension to\n> the existing --track option might make it slightly more palatable.\n\nFair enough. You did point it out and I will give that a try!\n\n\nHarald\n"},{"id":"542417","messageId":"pull.2281.v5.git.git.1777367012441.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v4.git.git.1777228346809.gitgitgadget@gmail.com","subject":"[PATCH v5] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-04-28T09:03:32Z","receivedAt":"2026-04-28T09:03:37Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA common workflow is:\n\n    git fetch origin\n    git checkout -b new_branch --track origin/some-branch\n\nThe first command exists so the second sees an up-to-date view of the\nremote. If it is forgotten, origin/some-branch points at a stale\ncommit and the new local branch is created from the wrong start\npoint. This only matters when the user is setting up tracking and\nexpects the new branch to start at the freshest tip; for a one-off\ncheckout of an arbitrary commit there is no reason to \"freshen\" the\nstart-point.\n\nTie the new behavior to --track for that reason: extend its argument\nto take a comma-separated list, where \"fetch\" can be combined with the\nexisting \"direct\" (default) and \"inherit\" modes. Examples:\n\n    git checkout --track=fetch -b new_branch origin/some-branch\n    git checkout --track=fetch,inherit -b new_branch some_local_branch\n    git switch --track=fetch -c new_branch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref. This\nnarrows the fetch to the requested branch so that other\nremote-tracking branches are left untouched -- many tools rely on the\nstability of remote-tracking refs between explicit fetches. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n     * Folded --fetch into --track. The standalone --fetch/--no-fetch flags\n       are gone; the same behavior is now requested via --track=fetch\n       (combinable as --track=fetch,inherit).\n     * Removed the checkout.fetch config. Since fetching is tied to --track,\n       there's no separate config knob anymore.\n     * Docs reworked accordingly. --track's syntax is now\n       (direct|inherit|fetch)[,...] in both git-checkout and git-switch man\n       pages, with the fetch behavior described under it. The old --fetch\n       and checkout.fetch sections are deleted.\n     * New parser callback in checkout.c. A small parse_opt_checkout_track\n       splits the comma-separated argument with string_list_split and sets\n       opts->track and opts->fetch together.\n     * Tests updated and trimmed. All test invocations switched from --fetch\n       to --track=fetch. Dropped the checkout.fetch=true and --no-fetch\n       override tests (those features no longer exist). Added a\n       --track=fetch,inherit test, a --track=bogus error test, and stronger\n       config-assertion checks on the basic test. Two redundant tests\n       (fetch,direct and order-insensitivity) were removed.\n     * Completion list cleaned up. --fetch removed from the expected git\n       checkout option list.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v5\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v4:\n\n 1:  150ccbb621 ! 1:  8ebc2f94b9 checkout: add --fetch to fetch remote before resolving start-point\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    checkout: add --fetch to fetch remote before resolving start-point\n     +    checkout: extend --track with a \"fetch\" mode to refresh start-point\n      \n          A common workflow is:\n      \n              git fetch origin\n     -        git checkout -b new_branch origin/some-branch\n     +        git checkout -b new_branch --track origin/some-branch\n      \n     -    The first command exists purely so the second sees an up-to-date view\n     -    of the remote. If it is forgotten, origin/some-branch points at a stale\n     -    commit and the new local branch is created from the wrong start point.\n     +    The first command exists so the second sees an up-to-date view of the\n     +    remote. If it is forgotten, origin/some-branch points at a stale\n     +    commit and the new local branch is created from the wrong start\n     +    point. This only matters when the user is setting up tracking and\n     +    expects the new branch to start at the freshest tip; for a one-off\n     +    checkout of an arbitrary commit there is no reason to \"freshen\" the\n     +    start-point.\n      \n     -    Teach checkout (and switch) a --fetch flag that folds the two steps\n     -    into one:\n     +    Tie the new behavior to --track for that reason: extend its argument\n     +    to take a comma-separated list, where \"fetch\" can be combined with the\n     +    existing \"direct\" (default) and \"inherit\" modes. Examples:\n      \n     -        git checkout --fetch -b new_branch origin/some-branch\n     +        git checkout --track=fetch -b new_branch origin/some-branch\n     +        git checkout --track=fetch,inherit -b new_branch some_local_branch\n     +        git switch --track=fetch -c new_branch origin/some-branch\n      \n     -    When --fetch is given and <start-point> is in <remote>/<branch> form,\n     -    run \"git fetch <remote> <branch>\" before resolving the ref. This\n     +    When \"fetch\" is requested and <start-point> is in <remote>/<branch>\n     +    form, run \"git fetch <remote> <branch>\" before resolving the ref. This\n          narrows the fetch to the requested branch so that other\n          remote-tracking branches are left untouched -- many tools rely on the\n          stability of remote-tracking refs between explicit fetches. If\n     @@ Commit message\n          since the target branch is not known up front. Abort the checkout if\n          the fetch fails.\n      \n     -    Also add a checkout.fetch config to enable this by default.\n     -\n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     - ## Documentation/config/checkout.adoc ##\n     -@@ Documentation/config/checkout.adoc: commands or functionality in the future.\n     - \toption in `git checkout` and `git switch`. See\n     - \tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n     - \n     -+`checkout.fetch`::\n     -+\tProvides the default value for the `--fetch` or `--no-fetch`\n     -+\toption in `git checkout` and `git switch`. See\n     -+\tlinkgit:git-switch[1] and linkgit:git-checkout[1].\n     -+\n     - `checkout.workers`::\n     - \tThe number of parallel workers to use when updating the working tree.\n     - \tThe default is one, i.e. sequential execution. If set to a value less\n     -\n       ## Documentation/git-checkout.adoc ##\n     -@@ Documentation/git-checkout.adoc: linkgit:git-config[1].\n     - The default behavior can be set via the `checkout.guess` configuration\n     - variable.\n     +@@ Documentation/git-checkout.adoc: of it\").\n     + \tresets _<branch>_ to the start point instead of failing.\n       \n     -+`--fetch`::\n     -+`--no-fetch`::\n     -+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n     -+\tfrom that remote before resolving it. When _<start-point>_ is\n     -+\tin _<remote>/<branch>_ form, only that branch is updated; when\n     -+\tit is a bare remote name (e.g. `origin`), the whole remote is\n     -+\tfetched. If the fetch fails, the checkout is aborted.\n     + `-t`::\n     +-`--track[=(direct|inherit)]`::\n     ++`--track[=(direct|inherit|fetch)[,...]]`::\n     + \tWhen creating a new branch, set up \"upstream\" configuration. See\n     + \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n     + \t--track without -b implies branch creation.\n     + +\n     ++The argument is a comma-separated list. `direct` (the default) and\n     ++`inherit` select the tracking mode. Adding `fetch` requests that the\n     ++remote be fetched before _<start-point>_ is resolved, so the new branch\n     ++starts from a fresh tip: when _<start-point>_ is in\n     ++_<remote>/<branch>_ form, only that branch is updated; when it is a\n     ++bare remote name (e.g. `origin`), the whole remote is fetched. If the\n     ++fetch fails, the checkout is aborted.\n      ++\n     -+The default behavior can be set via the `checkout.fetch` configuration\n     -+variable.\n     -+\n     - `-l`::\n     - \tCreate the new branch's reflog; see linkgit:git-branch[1] for\n     - \tdetails.\n     + If no `-b` option is given, the name of the new branch will be\n     + derived from the remote-tracking branch, by looking at the local part of\n     + the refspec configured for the corresponding remote, and then stripping\n      \n       ## Documentation/git-switch.adoc ##\n     -@@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See also\n     - The default behavior can be set via the `checkout.guess` configuration\n     - variable.\n     +@@ Documentation/git-switch.adoc: should result in deletion of the path).\n     + \tattached to a terminal, regardless of `--quiet`.\n       \n     -+`--fetch`::\n     -+`--no-fetch`::\n     -+\tIf _<start-point>_ refers to a remote-tracking branch, fetch\n     -+\tfrom that remote before resolving it. When _<start-point>_ is\n     -+\tin _<remote>/<branch>_ form, only that branch is updated; when\n     -+\tit is a bare remote name (e.g. `origin`), the whole remote is\n     -+\tfetched. If the fetch fails, the switch is aborted.\n     + `-t`::\n     +-`--track[ (direct|inherit)]`::\n     ++`--track[=(direct|inherit|fetch)[,...]]`::\n     + \tWhen creating a new branch, set up \"upstream\" configuration.\n     + \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n     + \tdetails.\n     + +\n     ++The argument is a comma-separated list. `direct` (the default) and\n     ++`inherit` select the tracking mode. Adding `fetch` requests that the\n     ++remote be fetched before _<start-point>_ is resolved, so the new branch\n     ++starts from a fresh tip: when _<start-point>_ is in\n     ++_<remote>/<branch>_ form, only that branch is updated; when it is a\n     ++bare remote name (e.g. `origin`), the whole remote is fetched. If the\n     ++fetch fails, the switch is aborted.\n      ++\n     -+The default behavior can be set via the `checkout.fetch` configuration\n     -+variable.\n     -+\n     - `-f`::\n     - `--force`::\n     - \tAn alias for `--discard-changes`.\n     + If no `-c` option is given, the name of the new branch will be derived\n     + from the remote-tracking branch, by looking at the local part of the\n     + refspec configured for the corresponding remote, and then stripping\n      \n       ## builtin/checkout.c ##\n      @@\n     @@ builtin/checkout.c: struct branch_info {\n      +\tif (run_command(&cmd))\n      +\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n      +}\n     ++\n     ++static int parse_opt_checkout_track(const struct option *opt,\n     ++\t\t\t\t    const char *arg, int unset)\n     ++{\n     ++\tstruct checkout_opts *opts = opt->value;\n     ++\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n     ++\tstruct string_list_item *item;\n     ++\tint ret = 0;\n     ++\n     ++\tif (unset) {\n     ++\t\topts->track = BRANCH_TRACK_NEVER;\n     ++\t\topts->fetch = 0;\n     ++\t\treturn 0;\n     ++\t}\n     ++\n     ++\topts->track = BRANCH_TRACK_EXPLICIT;\n     ++\tif (!arg)\n     ++\t\treturn 0;\n     ++\n     ++\tstring_list_split(&tokens, arg, \",\", -1);\n     ++\tfor_each_string_list_item(item, &tokens) {\n     ++\t\tif (!strcmp(item->string, \"fetch\")) {\n     ++\t\t\topts->fetch = 1;\n     ++\t\t} else if (!strcmp(item->string, \"direct\")) {\n     ++\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n     ++\t\t} else if (!strcmp(item->string, \"inherit\")) {\n     ++\t\t\topts->track = BRANCH_TRACK_INHERIT;\n     ++\t\t} else {\n     ++\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n     ++\t\t\t\t      \"or \\\"%s\\\"\"),\n     ++\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n     ++\t\t\tbreak;\n     ++\t\t}\n     ++\t}\n     ++\n     ++\tstring_list_clear(&tokens, 0);\n     ++\treturn ret;\n     ++}\n      +\n       static void branch_info_release(struct branch_info *info)\n       {\n     @@ builtin/checkout.c: static int git_checkout_config(const char *var, const char *\n       \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n       \t\treturn 0;\n       \t}\n     -+\tif (!strcmp(var, \"checkout.fetch\")) {\n     -+\t\topts->fetch = git_config_bool(var, value);\n     -+\t\treturn 0;\n     -+\t}\n     - \n     +-\n       \tif (starts_with(var, \"submodule.\"))\n       \t\treturn git_default_submodule_config(var, value, NULL);\n     + \n     +@@ builtin/checkout.c: static struct option *add_common_switch_branch_options(\n     + {\n     + \tstruct option options[] = {\n     + \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n     +-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n     ++\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n     + \t\t\tN_(\"set branch tracking configuration\"),\n     + \t\t\tPARSE_OPT_OPTARG,\n     +-\t\t\tparse_opt_tracking_mode),\n     ++\t\t\tparse_opt_checkout_track),\n     + \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n     + \t\t\t   PARSE_OPT_NOCOMPLETE),\n     + \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n      @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const char *prefix,\n       \t\t\topts->dwim_new_local_branch &&\n       \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n     @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const\n       \t\targv += n;\n       \t\targc -= n;\n       \t} else if (!opts->accept_ref && opts->from_treeish) {\n     -@@ builtin/checkout.c: int cmd_checkout(int argc,\n     - \t\tOPT_BOOL(0, \"overlay\", &opts.overlay_mode, N_(\"use overlay mode (default)\")),\n     - \t\tOPT_BOOL(0, \"auto-advance\", &opts.auto_advance,\n     - \t\t\t N_(\"auto advance to the next file when selecting hunks interactively\")),\n     -+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n     -+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n     - \t\tOPT_END()\n     - \t};\n     - \n     -@@ builtin/checkout.c: int cmd_switch(int argc,\n     - \t\t\t N_(\"second guess 'git switch <no-such-branch>'\")),\n     - \t\tOPT_BOOL(0, \"discard-changes\", &opts.discard_changes,\n     - \t\t\t N_(\"throw away local modifications\")),\n     -+\t\tOPT_BOOL(0, \"fetch\", &opts.fetch,\n     -+\t\t\t N_(\"fetch from the remote first if <start-point> is a remote-tracking branch\")),\n     - \t\tOPT_END()\n     - \t};\n     - \n      \n       ## t/t7201-co.sh ##\n      @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n       \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n       '\n       \n     -+test_expect_success 'setup upstream for --fetch tests' '\n     ++test_expect_success 'setup upstream for --track=fetch tests' '\n      +\tgit checkout main &&\n      +\tgit init fetch_upstream &&\n      +\ttest_commit -C fetch_upstream u_main &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_commit -C fetch_upstream u_new\n      +'\n      +\n     -+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '\n     ++test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n      +\tgit checkout main &&\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n     -+\tgit checkout --fetch -b local_new fetch_upstream/fetch_new &&\n     -+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD\n     ++\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n     ++\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n     ++\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n      +'\n      +\n     -+test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' '\n     ++test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_target &&\n      +\ttest_commit -C fetch_upstream u_target_pre &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_commit -C fetch_upstream u_target_post &&\n      +\tgit -C fetch_upstream checkout fetch_other &&\n      +\ttest_commit -C fetch_upstream u_other_post &&\n     -+\tgit checkout --fetch -b local_target fetch_upstream/fetch_target &&\n     ++\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n      +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n      +'\n      +\n     -+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '\n     ++test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_new2 &&\n      +\ttest_commit -C fetch_upstream u_new2 &&\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n     -+\tgit checkout --fetch -b local_from_remote fetch_upstream &&\n     ++\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n      +\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n      +'\n      +\n     -+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '\n     ++test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' '\n      +\tgit checkout main &&\n      +\ttest_might_fail git branch -D bogus &&\n     -+\ttest_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&\n     ++\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n      +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n      +'\n      +\n     -+test_expect_success 'checkout.fetch=true enables fetching without --fetch' '\n     ++test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_cfg &&\n     -+\ttest_commit -C fetch_upstream u_cfg &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&\n     -+\tgit -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&\n     -+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD\n     -+'\n     -+\n     -+test_expect_success '--no-fetch overrides checkout.fetch=true' '\n     ++\tgit -C fetch_upstream checkout -b fetch_inherit &&\n     ++\ttest_commit -C fetch_upstream u_inherit &&\n     ++\tgit fetch fetch_upstream fetch_inherit &&\n     ++\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n     ++\ttest_commit -C fetch_upstream u_inherit2 &&\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_nofetch &&\n     -+\ttest_commit -C fetch_upstream u_nofetch &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n     -+\ttest_must_fail git -c checkout.fetch=true checkout --no-fetch \\\n     -+\t\t-b local_nofetch fetch_upstream/fetch_nofetch &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch &&\n     -+\ttest_must_fail git rev-parse --verify refs/heads/local_nofetch\n     ++\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n     ++\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n     ++\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n      +'\n      +\n     -+test_expect_success '--no-fetch overrides earlier --fetch on command line' '\n     ++test_expect_success 'checkout --track=bogus reports an error' '\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_override &&\n     -+\ttest_commit -C fetch_upstream u_override &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n     -+\ttest_must_fail git checkout --fetch --no-fetch \\\n     -+\t\t-b local_override fetch_upstream/fetch_override &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override &&\n     -+\ttest_must_fail git rev-parse --verify refs/heads/local_override\n     ++\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n     ++\ttest_grep \"expects\" err\n      +'\n      +\n     -+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '\n     ++test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_switch &&\n      +\ttest_commit -C fetch_upstream u_switch &&\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n     -+\tgit switch --fetch -c local_switch fetch_upstream/fetch_switch &&\n     ++\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n      +'\n      +\n       test_done\n     -\n     - ## t/t9902-completion.sh ##\n     -@@ t/t9902-completion.sh: test_expect_success 'double dash \"git checkout\"' '\n     - \t--ignore-other-worktrees Z\n     - \t--recurse-submodules Z\n     - \t--auto-advance Z\n     -+\t--fetch Z\n     - \t--progress Z\n     - \t--guess Z\n     - \t--no-guess Z\n\n\n Documentation/git-checkout.adoc | 10 +++-\n Documentation/git-switch.adoc   | 10 +++-\n builtin/checkout.c              | 85 +++++++++++++++++++++++++++++++--\n t/t7201-co.sh                   | 81 +++++++++++++++++++++++++++++++\n 4 files changed, 179 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..3b8292612d 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,19 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode. Adding `fetch` requests that the\n+remote be fetched before _<start-point>_ is resolved, so the new branch\n+starts from a fresh tip: when _<start-point>_ is in\n+_<remote>/<branch>_ form, only that branch is updated; when it is a\n+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n+fetch fails, the checkout is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..35a03e8a52 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -154,11 +154,19 @@ should result in deletion of the path).\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode. Adding `fetch` requests that the\n+remote be fetched before _<start-point>_ is resolved, so the new branch\n+starts from a fresh tip: when _<start-point>_ is in\n+_<remote>/<branch>_ form, only that branch is updated; when it is a\n+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n+fetch fails, the switch is aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..de4d7c00c7 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,74 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (slash && slash[1])\n+\t\tstrvec_push(&cmd.args, slash + 1);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint ret = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\topts->fetch = 0;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,7 +1308,6 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n \n@@ -1734,10 +1804,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1942,8 +2012,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..39236dca12 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,85 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542622","messageId":"xmqqse88ryyk.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v5.git.git.1777367012441.gitgitgadget@gmail.com","subject":"Re: [PATCH v5] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-03T20:59:47Z","receivedAt":"2026-05-03T20:59:49Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A common workflow is:\n>\n>     git fetch origin\n>     git checkout -b new_branch --track origin/some-branch\n>\n> The first command exists so the second sees an up-to-date view of the\n> remote. If it is forgotten, origin/some-branch points at a stale\n> commit and the new local branch is created from the wrong start\n> point.\n\nAs I pointed out multiple times, I prefer not to see this called\n\"wrong\".  Even if you did not \"forget\", somebody may be pushing\nafter you fetched and you may end up forking from a \"stale\" commit.\nSo not fetching is not inherently \"wrong\", simply because that is\nhow real world works.  Multiple people working in a distributed\nenvironment does not give you absolute garantee that you will be \"up\nto date\", ever, which makes it wrong to call anything that is not\n\"up to date\" a \"wrong starting point\".\n\n> This only matters when the user is setting up tracking and\n> expects the new branch to start at the freshest tip; for a one-off\n> checkout of an arbitrary commit there is no reason to \"freshen\" the\n> start-point.\n\nI do not think \"arbitrary\" fits in this workflow description.\n\nIf anything, \"I'd take anything that the remote repository happens\nto have at the tip, even without having a chance to sanity checke if\nthat is a good starting point\" is more appropriate workflow to be\ndescribed with a word \"arbitrary commit\".\n\nIf you are checking out without forking from there, you'd more\nlikely be checking out the \"latest\" you have fetched from the other\nside, often knowing exactly what it is after checking it with \"git\nshow origin/$topic\".\n\n> Tie the new behavior to --track for that reason:\n\nNotice that the reader hasn't heard what \"the new behaviour\" is up\nto this point yet?\n\nHow about rewriting everything up to and including this \"Tie the new\n...\" line perhaps like so:\n\n    If you want to fork your topic branch from the very latest of\n    the tip of a branch your remote has, you would do:\n\n\tgit fetch origin some-branch\n\tgit checkout -b new_branch --track origin/some-branch\n\n    Extend the \"--track\" option of \"git checkout\" and allow users to\n    write\n\n\tgit checkout --track=fetch -b new_branch origin/some_branch\n\n    to (1) fetch 'some-branch' from the remote 'origin', updating\n    the remote-tracking branch 'origin/some-branch', (2) arrange\n    subsequent 'git pull' on 'new_branch' to interact with\n    'origin/some_branch' and (3) fork 'new_branch' from it.\n   \n    In the value of the '--track' option, 'fetch' can be combined\n    with ...\n"},{"id":"542625","messageId":"pull.2281.v6.git.git.1777847487823.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v5.git.git.1777367012441.gitgitgadget@gmail.com","subject":"[PATCH v6] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-03T22:31:27Z","receivedAt":"2026-05-03T22:31:30Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n    Commit message only, no code/doc/test changes. Restructured the opening\n    around the user-visible workflow before introducing '--track=fetch',\n    reordered all example invocations to ' -b/-c --track[=...] ', dropped\n    the \"wrong/stale start-point\" and \"arbitrary commit\" framings, and\n    trimmed the over-explanation of the narrowed fetch.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v6\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v5:\n\n 1:  8ebc2f94b9 ! 1:  1b42c648b9 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Metadata\n       ## Commit message ##\n          checkout: extend --track with a \"fetch\" mode to refresh start-point\n      \n     -    A common workflow is:\n     +    If you want to fork your topic branch from the very latest of the\n     +    tip of a branch your remote has, you would do:\n      \n     -        git fetch origin\n     +        git fetch origin some-branch\n              git checkout -b new_branch --track origin/some-branch\n      \n     -    The first command exists so the second sees an up-to-date view of the\n     -    remote. If it is forgotten, origin/some-branch points at a stale\n     -    commit and the new local branch is created from the wrong start\n     -    point. This only matters when the user is setting up tracking and\n     -    expects the new branch to start at the freshest tip; for a one-off\n     -    checkout of an arbitrary commit there is no reason to \"freshen\" the\n     -    start-point.\n     +    Extend the \"--track\" option of \"git checkout\" and allow users to\n     +    write\n      \n     -    Tie the new behavior to --track for that reason: extend its argument\n     -    to take a comma-separated list, where \"fetch\" can be combined with the\n     -    existing \"direct\" (default) and \"inherit\" modes. Examples:\n     +        git checkout -b new_branch --track=fetch origin/some-branch\n      \n     -        git checkout --track=fetch -b new_branch origin/some-branch\n     -        git checkout --track=fetch,inherit -b new_branch some_local_branch\n     -        git switch --track=fetch -c new_branch origin/some-branch\n     +    to (1) fetch 'some-branch' from the remote 'origin', updating the\n     +    remote-tracking branch 'origin/some-branch', (2) arrange subsequent\n     +    'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n     +    (3) fork 'new_branch' from it.\n     +\n     +    In the value of the '--track' option, 'fetch' can be combined with\n     +    the existing 'direct' (default) and 'inherit' modes via a\n     +    comma-separated list. Examples:\n     +\n     +        git checkout -b new_branch --track=fetch,inherit some_local_branch\n     +        git switch -c new_branch --track=fetch origin/some-branch\n      \n          When \"fetch\" is requested and <start-point> is in <remote>/<branch>\n     -    form, run \"git fetch <remote> <branch>\" before resolving the ref. This\n     -    narrows the fetch to the requested branch so that other\n     -    remote-tracking branches are left untouched -- many tools rely on the\n     -    stability of remote-tracking refs between explicit fetches. If\n     +    form, run \"git fetch <remote> <branch>\" before resolving the ref, so\n     +    that other remote-tracking branches are left untouched. If\n          <start-point> is a bare remote name like \"origin\" (which resolves to\n          that remote's default branch), \"git fetch <remote>\" is run instead,\n          since the target branch is not known up front. Abort the checkout if\n\n\n Documentation/git-checkout.adoc | 10 +++-\n Documentation/git-switch.adoc   | 10 +++-\n builtin/checkout.c              | 85 +++++++++++++++++++++++++++++++--\n t/t7201-co.sh                   | 81 +++++++++++++++++++++++++++++++\n 4 files changed, 179 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..3b8292612d 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,19 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode. Adding `fetch` requests that the\n+remote be fetched before _<start-point>_ is resolved, so the new branch\n+starts from a fresh tip: when _<start-point>_ is in\n+_<remote>/<branch>_ form, only that branch is updated; when it is a\n+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n+fetch fails, the checkout is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..35a03e8a52 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -154,11 +154,19 @@ should result in deletion of the path).\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode. Adding `fetch` requests that the\n+remote be fetched before _<start-point>_ is resolved, so the new branch\n+starts from a fresh tip: when _<start-point>_ is in\n+_<remote>/<branch>_ form, only that branch is updated; when it is a\n+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n+fetch fails, the switch is aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..de4d7c00c7 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -30,7 +30,9 @@\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +63,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +115,74 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (!arg || !*arg)\n+\t\treturn;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (slash && slash[1])\n+\t\tstrvec_push(&cmd.args, slash + 1);\n+\tcmd.git_cmd = 1;\n+\tfree(remote_name);\n+\tif (run_command(&cmd))\n+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint ret = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\topts->fetch = 0;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,7 +1308,6 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n \n@@ -1734,10 +1804,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1942,8 +2012,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..39236dca12 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,85 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n+\ttest_commit -C fetch_upstream u_new2 &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"542626","messageId":"20260503223217.990-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"xmqqse88ryyk.fsf@gitster.g","subject":"[PATCH] checkout: add --autostash option for branch switching","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-03T22:32:17Z","receivedAt":"2026-05-03T22:32:21Z","isPatch":true,"body":"> How about rewriting everything up to and including this \"Tie the new\n> ...\" line perhaps like so:\n\nDone!\n\n\nHarald\n"},{"id":"542876","messageId":"20260507201253.41428-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v6.git.git.1777847487823.gitgitgadget@gmail.com","subject":"[PATCH v6] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-07T20:12:53Z","receivedAt":"2026-05-07T20:12:56Z","isPatch":true,"body":"Is this ready to move to next?\n\n\nHarald\n"},{"id":"542916","messageId":"f23eb128-958f-475f-911b-eac4f6daddff@gmail.com","threadId":"65546","inReplyTo":"20260507201253.41428-1-haraldnordgren@gmail.com","subject":"Re: [PATCH v6] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-05-08T13:15:51Z","receivedAt":"2026-05-08T13:15:46Z","isPatch":true,"body":"Hi Harald\n\nOn 07/05/2026 21:12, Harald Nordgren wrote:\n> Is this ready to move to next?\n\nI'm not particularly enthusiastic one way or the other about adding \nthis, but so long as we only try to fetch when the user explicitly asks \nfor it I don't particularly object. However having had a quick scan of \nthe implementation I have a few comments\n\n* \"--track=inherit,direct\" is nonsense and should be rejected\n\n* currently \"--track\" has \"last one wins\" behavior so\n   \"--track=inherit --track=direct\" behaves like \"--track=direct\". We\n   should probably keep that so that \"--track=fetch --track=direct\"\n   behaves like \"--track=direct\", not \"--track=fetch,direct\"\n\n* if \"git fetch\" fails and the remote tracking ref already exists then\n   we should print a warning and carry on rather than dying which is more\n   convenient if the user or remote server are offline.\n\n* \"git checkout --track=fetch origin/branch\" should respect\n   remote.origin.fetch so that we fetch the ref that we're going to\n   checkout. I wonder if we can share this logic with the code that\n   sets the upstream branch.\n\n* \"git checkout --track=fetch origin\" should only fetch the remote\n   ref that we're going to checkout, not all the refs from origin. i.e.\n   it should read origin/HEAD to work out what to fetch.\n\nThanks\n\nPhillip\n\n"},{"id":"542933","messageId":"20260508224050.97883-1-haraldnordgren@gmail.com","threadId":"65546","inReplyTo":"f23eb128-958f-475f-911b-eac4f6daddff@gmail.com","subject":"[PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-08T22:40:50Z","receivedAt":"2026-05-08T22:40:54Z","isPatch":true,"body":"> * \"--track=inherit,direct\" is nonsense and should be rejected\n> \n> * currently \"--track\" has \"last one wins\" behavior so\n>   \"--track=inherit --track=direct\" behaves like \"--track=direct\". We\n>   should probably keep that so that \"--track=fetch --track=direct\"\n>   behaves like \"--track=direct\", not \"--track=fetch,direct\"\n> \n> * if \"git fetch\" fails and the remote tracking ref already exists then\n>   we should print a warning and carry on rather than dying which is more\n>   convenient if the user or remote server are offline.\n> \n> * \"git checkout --track=fetch origin/branch\" should respect\n>   remote.origin.fetch so that we fetch the ref that we're going to\n>   checkout. I wonder if we can share this logic with the code that\n>   sets the upstream branch.\n> \n> * \"git checkout --track=fetch origin\" should only fetch the remote\n>   ref that we're going to checkout, not all the refs from origin. i.e.\n>   it should read origin/HEAD to work out what to fetch.\n\nGood points!\n\n\nHarald\n"},{"id":"542934","messageId":"pull.2281.v7.git.git.1778280727849.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v6.git.git.1777847487823.gitgitgadget@gmail.com","subject":"[PATCH v7] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-08T22:52:07Z","receivedAt":"2026-05-08T22:52:10Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: add --fetch to fetch remote before resolving start-point\n    \n     * Reject --track=inherit,direct as mutually exclusive modes.\n     * Make repeated --track= last-one-wins: --track=fetch --track=direct\n       behaves like --track=direct.\n     * On fetch failure, warn and proceed from the existing remote-tracking\n       ref instead of aborting (friendlier when offline); only abort when\n       there is no existing ref.\n     * For --track=fetch <remote>/<branch>, resolve the source ref through\n       the configured remote.<name>.fetch refspec so custom refspecs fetch\n       the correct ref.\n     * For --track=fetch <remote>, read <remote>/HEAD and fetch only that\n       branch instead of the whole remote.\n     * Tests and docs updated accordingly.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v7\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v6:\n\n 1:  1b42c648b9 ! 1:  de375d55f1 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Documentation/git-checkout.adoc: of it\").\n       \t--track without -b implies branch creation.\n       +\n      +The argument is a comma-separated list. `direct` (the default) and\n     -+`inherit` select the tracking mode. Adding `fetch` requests that the\n     -+remote be fetched before _<start-point>_ is resolved, so the new branch\n     -+starts from a fresh tip: when _<start-point>_ is in\n     -+_<remote>/<branch>_ form, only that branch is updated; when it is a\n     -+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n     -+fetch fails, the checkout is aborted.\n     ++`inherit` select the tracking mode and are mutually exclusive. Adding\n     ++`fetch` requests that the remote be fetched before _<start-point>_ is\n     ++resolved, so the new branch starts from a fresh tip: when\n     ++_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n     ++updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n     ++only the remote's default branch is updated. If the fetch fails and the\n     ++corresponding remote-tracking ref already exists, a warning is printed\n     ++and the checkout proceeds from the existing tip; otherwise the checkout\n     ++is aborted.\n      ++\n       If no `-b` option is given, the name of the new branch will be\n       derived from the remote-tracking branch, by looking at the local part of\n     @@ Documentation/git-switch.adoc: should result in deletion of the path).\n       \tdetails.\n       +\n      +The argument is a comma-separated list. `direct` (the default) and\n     -+`inherit` select the tracking mode. Adding `fetch` requests that the\n     -+remote be fetched before _<start-point>_ is resolved, so the new branch\n     -+starts from a fresh tip: when _<start-point>_ is in\n     -+_<remote>/<branch>_ form, only that branch is updated; when it is a\n     -+bare remote name (e.g. `origin`), the whole remote is fetched. If the\n     -+fetch fails, the switch is aborted.\n     ++`inherit` select the tracking mode and are mutually exclusive. Adding\n     ++`fetch` requests that the remote be fetched before _<start-point>_ is\n     ++resolved, so the new branch starts from a fresh tip: when\n     ++_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n     ++updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n     ++only the remote's default branch is updated. If the fetch fails and the\n     ++corresponding remote-tracking ref already exists, a warning is printed\n     ++and the switch proceeds from the existing tip; otherwise the switch is\n     ++aborted.\n      ++\n       If no `-c` option is given, the name of the new branch will be derived\n       from the remote-tracking branch, by looking at the local part of the\n     @@ Documentation/git-switch.adoc: should result in deletion of the path).\n      \n       ## builtin/checkout.c ##\n      @@\n     + #include \"preload-index.h\"\n     + #include \"read-cache.h\"\n     + #include \"refs.h\"\n     ++#include \"refspec.h\"\n     + #include \"remote.h\"\n       #include \"repo-settings.h\"\n       #include \"resolve-undo.h\"\n       #include \"revision.h\"\n     @@ builtin/checkout.c: struct branch_info {\n       \tchar *checkout;\n       };\n       \n     -+static void fetch_remote_for_start_point(const char *arg)\n     ++static int resolve_fetch_target(const char *arg, char **remote_out,\n     ++\t\t\t\tchar **src_ref_out)\n      +{\n      +\tconst char *slash;\n      +\tchar *remote_name;\n      +\tstruct remote *remote;\n     -+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n     ++\tstruct refspec_item query = { 0 };\n     ++\tstruct strbuf dst = STRBUF_INIT;\n     ++\tconst char *rest;\n     ++\n     ++\t*remote_out = NULL;\n     ++\t*src_ref_out = NULL;\n      +\n      +\tif (!arg || !*arg)\n     -+\t\treturn;\n     ++\t\treturn -1;\n      +\n      +\tslash = strchr(arg, '/');\n      +\tif (slash == arg)\n     -+\t\treturn;\n     ++\t\treturn -1;\n      +\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n      +\n      +\tremote = remote_get(remote_name);\n      +\tif (!remote || !remote_is_configured(remote, 1)) {\n      +\t\tfree(remote_name);\n     ++\t\treturn -1;\n     ++\t}\n     ++\n     ++\trest = (slash && slash[1]) ? slash + 1 : NULL;\n     ++\tif (!rest) {\n     ++\t\tstruct object_id oid;\n     ++\t\tconst char *head_target;\n     ++\t\tconst char *short_target;\n     ++\n     ++\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n     ++\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n     ++\t\t\t\t\t\t      dst.buf,\n     ++\t\t\t\t\t\t      RESOLVE_REF_READING |\n     ++\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n     ++\t\t\t\t\t\t      &oid, NULL);\n     ++\t\tstrbuf_reset(&dst);\n     ++\t\tif (head_target &&\n     ++\t\t    skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n     ++\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n     ++\t\t    *short_target == '/')\n     ++\t\t\trest = short_target + 1;\n     ++\t}\n     ++\n     ++\tif (rest) {\n     ++\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n     ++\t\tquery.dst = dst.buf;\n     ++\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n     ++\t\t\t*src_ref_out = xstrdup(query.src);\n     ++\t\t\tfree(query.src);\n     ++\t\t} else {\n     ++\t\t\t*src_ref_out = xstrdup(rest);\n     ++\t\t}\n     ++\t}\n     ++\n     ++\tstrbuf_release(&dst);\n     ++\t*remote_out = remote_name;\n     ++\treturn 0;\n     ++}\n     ++\n     ++static void fetch_remote_for_start_point(const char *arg)\n     ++{\n     ++\tchar *remote_name = NULL;\n     ++\tchar *src_ref = NULL;\n     ++\tstruct child_process cmd = CHILD_PROCESS_INIT;\n     ++\tstruct strbuf dst_ref = STRBUF_INIT;\n     ++\tint have_existing_ref = 0;\n     ++\n     ++\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n      +\t\treturn;\n     ++\n     ++\tif (src_ref) {\n     ++\t\tconst char *short_src = src_ref;\n     ++\t\tstruct object_id oid;\n     ++\n     ++\t\tskip_prefix(short_src, \"refs/heads/\", &short_src);\n     ++\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/%s\", remote_name, short_src);\n     ++\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     ++\t\t\t\t   dst_ref.buf, &oid))\n     ++\t\t\thave_existing_ref = 1;\n      +\t}\n      +\n      +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n     -+\tif (slash && slash[1])\n     -+\t\tstrvec_push(&cmd.args, slash + 1);\n     ++\tif (src_ref)\n     ++\t\tstrvec_push(&cmd.args, src_ref);\n      +\tcmd.git_cmd = 1;\n     ++\tif (run_command(&cmd)) {\n     ++\t\tif (have_existing_ref)\n     ++\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n     ++\t\t\t\t  \"using existing '%s'\"),\n     ++\t\t\t\targ, dst_ref.buf);\n     ++\t\telse\n     ++\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n     ++\t}\n     ++\n      +\tfree(remote_name);\n     -+\tif (run_command(&cmd))\n     -+\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n     ++\tfree(src_ref);\n     ++\tstrbuf_release(&dst_ref);\n      +}\n      +\n      +static int parse_opt_checkout_track(const struct option *opt,\n     @@ builtin/checkout.c: struct branch_info {\n      +\tstruct checkout_opts *opts = opt->value;\n      +\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n      +\tstruct string_list_item *item;\n     ++\tint saw_direct = 0, saw_inherit = 0;\n      +\tint ret = 0;\n      +\n     ++\topts->fetch = 0;\n     ++\n      +\tif (unset) {\n      +\t\topts->track = BRANCH_TRACK_NEVER;\n     -+\t\topts->fetch = 0;\n      +\t\treturn 0;\n      +\t}\n      +\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\tif (!strcmp(item->string, \"fetch\")) {\n      +\t\t\topts->fetch = 1;\n      +\t\t} else if (!strcmp(item->string, \"direct\")) {\n     ++\t\t\tsaw_direct = 1;\n      +\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n      +\t\t} else if (!strcmp(item->string, \"inherit\")) {\n     ++\t\t\tsaw_inherit = 1;\n      +\t\t\topts->track = BRANCH_TRACK_INHERIT;\n      +\t\t} else {\n      +\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n      +\t\t\t\t      \"or \\\"%s\\\"\"),\n      +\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n     -+\t\t\tbreak;\n     ++\t\t\tgoto out;\n      +\t\t}\n      +\t}\n      +\n     ++\tif (saw_direct && saw_inherit)\n     ++\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n     ++\t\t\t    \"--track\", \"direct\", \"inherit\");\n     ++\n     ++out:\n      +\tstring_list_clear(&tokens, 0);\n      +\treturn ret;\n      +}\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' '\n     ++test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_new2 &&\n     -+\ttest_commit -C fetch_upstream u_new2 &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&\n     ++\tgit -C fetch_upstream checkout main &&\n     ++\tgit remote set-head fetch_upstream main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n     ++\ttest_commit -C fetch_upstream u_unrelated_pre &&\n     ++\tgit fetch fetch_upstream fetch_unrelated &&\n     ++\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n     ++\tgit -C fetch_upstream checkout main &&\n     ++\ttest_commit -C fetch_upstream u_main_post &&\n     ++\tgit -C fetch_upstream checkout fetch_unrelated &&\n     ++\ttest_commit -C fetch_upstream u_unrelated_post &&\n      +\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n     -+\tgit rev-parse --verify refs/remotes/fetch_upstream/fetch_new2\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n     ++\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' '\n     ++test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n      +\tgit checkout main &&\n      +\ttest_might_fail git branch -D bogus &&\n      +\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n      +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n      +'\n      +\n     ++test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_offline &&\n     ++\ttest_commit -C fetch_upstream u_offline &&\n     ++\tgit fetch fetch_upstream fetch_offline &&\n     ++\tsaved_url=$(git config remote.fetch_upstream.url) &&\n     ++\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n     ++\tgit config remote.fetch_upstream.url ./does-not-exist &&\n     ++\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n     ++\ttest_grep \"failed to fetch\" err &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_refspec &&\n     ++\ttest_commit -C fetch_upstream u_refspec &&\n     ++\tgit fetch fetch_upstream fetch_refspec &&\n     ++\tgit remote add fetch_custom ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove fetch_custom\" &&\n     ++\tgit config --replace-all remote.fetch_custom.fetch \\\n     ++\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n     ++\tgit fetch fetch_custom &&\n     ++\ttest_commit -C fetch_upstream u_refspec_post &&\n     ++\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n     ++\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=inherit,direct is rejected' '\n     ++\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n     ++\ttest_grep \"cannot combine\" err\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n     ++\ttest_commit -C fetch_upstream u_lastwin &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n     ++\ttest_must_fail git checkout --track=fetch --track=direct \\\n     ++\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n     ++'\n     ++\n      +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_inherit &&\n\n\n Documentation/git-checkout.adoc |  13 ++-\n Documentation/git-switch.adoc   |  13 ++-\n builtin/checkout.c              | 168 +++++++++++++++++++++++++++++++-\n t/t7201-co.sh                   | 132 +++++++++++++++++++++++++\n 4 files changed, 319 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..28f17f427e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,22 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..3f54cf39e9 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -154,11 +154,22 @@ should result in deletion of the path).\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the switch proceeds from the existing tip; otherwise the switch is\n+aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex e031e61886..8f8d1ecffe 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,11 +26,14 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n+#include \"strvec.h\"\n #include \"submodule.h\"\n #include \"symlinks.h\"\n #include \"trace2.h\"\n@@ -61,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -112,6 +116,156 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static int resolve_fetch_target(const char *arg, char **remote_out,\n+\t\t\t\tchar **src_ref_out)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct refspec_item query = { 0 };\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tconst char *rest;\n+\n+\t*remote_out = NULL;\n+\t*src_ref_out = NULL;\n+\n+\tif (!arg || !*arg)\n+\t\treturn -1;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn -1;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn -1;\n+\t}\n+\n+\trest = (slash && slash[1]) ? slash + 1 : NULL;\n+\tif (!rest) {\n+\t\tstruct object_id oid;\n+\t\tconst char *head_target;\n+\t\tconst char *short_target;\n+\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n+\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t      dst.buf,\n+\t\t\t\t\t\t      RESOLVE_REF_READING |\n+\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t      &oid, NULL);\n+\t\tstrbuf_reset(&dst);\n+\t\tif (head_target &&\n+\t\t    skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n+\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n+\t\t    *short_target == '/')\n+\t\t\trest = short_target + 1;\n+\t}\n+\n+\tif (rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n+\t\tquery.dst = dst.buf;\n+\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n+\t\t\t*src_ref_out = xstrdup(query.src);\n+\t\t\tfree(query.src);\n+\t\t} else {\n+\t\t\t*src_ref_out = xstrdup(rest);\n+\t\t}\n+\t}\n+\n+\tstrbuf_release(&dst);\n+\t*remote_out = remote_name;\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tchar *remote_name = NULL;\n+\tchar *src_ref = NULL;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct strbuf dst_ref = STRBUF_INIT;\n+\tint have_existing_ref = 0;\n+\n+\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n+\t\treturn;\n+\n+\tif (src_ref) {\n+\t\tconst char *short_src = src_ref;\n+\t\tstruct object_id oid;\n+\n+\t\tskip_prefix(short_src, \"refs/heads/\", &short_src);\n+\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/%s\", remote_name, short_src);\n+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t   dst_ref.buf, &oid))\n+\t\t\thave_existing_ref = 1;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (src_ref)\n+\t\tstrvec_push(&cmd.args, src_ref);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (have_existing_ref)\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"),\n+\t\t\t\targ, dst_ref.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfree(remote_name);\n+\tfree(src_ref);\n+\tstrbuf_release(&dst_ref);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0, saw_inherit = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\tsaw_direct = 1;\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\tsaw_inherit = 1;\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (saw_direct && saw_inherit)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1237,7 +1391,6 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n \n@@ -1734,10 +1887,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1942,8 +2095,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..19ac6a1a2e 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\tgit fetch fetch_upstream fetch_refspec &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit fetch fetch_custom &&\n+\ttest_commit -C fetch_upstream u_refspec_post &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n-- \ngitgitgadget\n"},{"id":"543055","messageId":"98410b9c-8ed6-41df-b508-ca9e15d4ee53@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v7.git.git.1778280727849.gitgitgadget@gmail.com","subject":"Re: [PATCH v7] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-05-11T13:16:30Z","receivedAt":"2026-05-11T13:16:36Z","isPatch":true,"body":"Hi Harald\n\nOn 08/05/2026 23:52, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> +static void fetch_remote_for_start_point(const char *arg)\n> +{\n> +\tchar *remote_name = NULL;\n> +\tchar *src_ref = NULL;\n> +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n> +\tstruct strbuf dst_ref = STRBUF_INIT;\n> +\tint have_existing_ref = 0;\n> +\n> +\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n> +\t\treturn;\n> +\n> +\tif (src_ref) {\n> +\t\tconst char *short_src = src_ref;\n> +\t\tstruct object_id oid;\n> +\n> +\t\tskip_prefix(short_src, \"refs/heads/\", &short_src);\n> +\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/%s\", remote_name, short_src);\n> +\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n> +\t\t\t\t   dst_ref.buf, &oid))\n> +\t\t\thave_existing_ref = 1;\n\nsrc_ref is the name of the branch on the remote server, not the name of \nthe remote tracking ref which is given by arg. If arg is a remote name \nthen we need to resolve refs/remotes/$arg/HEAD to find the branch to \ncheck, otherwise we should be checking refs/remotes/$arg\n\nI've only given this version a quick scan through, but I didn't notice \nany other issues.\n\nThanks\n\nPhillip\n\n> +\t}\n> +\n> +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n> +\tif (src_ref)\n> +\t\tstrvec_push(&cmd.args, src_ref);\n> +\tcmd.git_cmd = 1;\n> +\tif (run_command(&cmd)) {\n> +\t\tif (have_existing_ref)\n> +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n> +\t\t\t\t  \"using existing '%s'\"),\n> +\t\t\t\targ, dst_ref.buf);\n> +\t\telse\n> +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n> +\t}\n> +\n> +\tfree(remote_name);\n> +\tfree(src_ref);\n> +\tstrbuf_release(&dst_ref);\n> +}\n> +\n> +static int parse_opt_checkout_track(const struct option *opt,\n> +\t\t\t\t    const char *arg, int unset)\n> +{\n> +\tstruct checkout_opts *opts = opt->value;\n> +\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n> +\tstruct string_list_item *item;\n> +\tint saw_direct = 0, saw_inherit = 0;\n> +\tint ret = 0;\n> +\n> +\topts->fetch = 0;\n> +\n> +\tif (unset) {\n> +\t\topts->track = BRANCH_TRACK_NEVER;\n> +\t\treturn 0;\n> +\t}\n> +\n> +\topts->track = BRANCH_TRACK_EXPLICIT;\n> +\tif (!arg)\n> +\t\treturn 0;\n> +\n> +\tstring_list_split(&tokens, arg, \",\", -1);\n> +\tfor_each_string_list_item(item, &tokens) {\n> +\t\tif (!strcmp(item->string, \"fetch\")) {\n> +\t\t\topts->fetch = 1;\n> +\t\t} else if (!strcmp(item->string, \"direct\")) {\n> +\t\t\tsaw_direct = 1;\n> +\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n> +\t\t} else if (!strcmp(item->string, \"inherit\")) {\n> +\t\t\tsaw_inherit = 1;\n> +\t\t\topts->track = BRANCH_TRACK_INHERIT;\n> +\t\t} else {\n> +\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n> +\t\t\t\t      \"or \\\"%s\\\"\"),\n> +\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n> +\t\t\tgoto out;\n> +\t\t}\n> +\t}\n> +\n> +\tif (saw_direct && saw_inherit)\n> +\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n> +\t\t\t    \"--track\", \"direct\", \"inherit\");\n> +\n> +out:\n> +\tstring_list_clear(&tokens, 0);\n> +\treturn ret;\n> +}\n> +\n>   static void branch_info_release(struct branch_info *info)\n>   {\n>   \tfree(info->name);\n> @@ -1237,7 +1391,6 @@ static int git_checkout_config(const char *var, const char *value,\n>   \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n>   \t\treturn 0;\n>   \t}\n> -\n>   \tif (starts_with(var, \"submodule.\"))\n>   \t\treturn git_default_submodule_config(var, value, NULL);\n>   \n> @@ -1734,10 +1887,10 @@ static struct option *add_common_switch_branch_options(\n>   {\n>   \tstruct option options[] = {\n>   \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n> -\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n> +\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n>   \t\t\tN_(\"set branch tracking configuration\"),\n>   \t\t\tPARSE_OPT_OPTARG,\n> -\t\t\tparse_opt_tracking_mode),\n> +\t\t\tparse_opt_checkout_track),\n>   \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n>   \t\t\t   PARSE_OPT_NOCOMPLETE),\n>   \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n> @@ -1942,8 +2095,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>   \t\t\topts->dwim_new_local_branch &&\n>   \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n>   \t\t\t!opts->new_branch;\n> -\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n> -\t\t\t\t\t     &new_branch_info, opts, &rev);\n> +\t\tint n;\n> +\n> +\t\tif (opts->fetch)\n> +\t\t\tfetch_remote_for_start_point(argv[0]);\n> +\n> +\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n> +\t\t\t\t\t &new_branch_info, opts, &rev);\n>   \t\targv += n;\n>   \t\targc -= n;\n>   \t} else if (!opts->accept_ref && opts->from_treeish) {\n> diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> index 9bcf7c0b40..19ac6a1a2e 100755\n> --- a/t/t7201-co.sh\n> +++ b/t/t7201-co.sh\n> @@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n>   \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n>   '\n>   \n> +test_expect_success 'setup upstream for --track=fetch tests' '\n> +\tgit checkout main &&\n> +\tgit init fetch_upstream &&\n> +\ttest_commit -C fetch_upstream u_main &&\n> +\tgit remote add fetch_upstream fetch_upstream &&\n> +\tgit fetch fetch_upstream &&\n> +\tgit -C fetch_upstream checkout -b fetch_new &&\n> +\ttest_commit -C fetch_upstream u_new\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> +\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n> +\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n> +\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_pre &&\n> +\tgit -C fetch_upstream checkout -b fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_pre &&\n> +\tgit fetch fetch_upstream &&\n> +\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n> +\tgit -C fetch_upstream checkout fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_post &&\n> +\tgit -C fetch_upstream checkout fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_post &&\n> +\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n> +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\tgit remote set-head fetch_upstream main &&\n> +\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n> +\ttest_commit -C fetch_upstream u_unrelated_pre &&\n> +\tgit fetch fetch_upstream fetch_unrelated &&\n> +\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\ttest_commit -C fetch_upstream u_main_post &&\n> +\tgit -C fetch_upstream checkout fetch_unrelated &&\n> +\ttest_commit -C fetch_upstream u_unrelated_post &&\n> +\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n> +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n> +\tgit checkout main &&\n> +\ttest_might_fail git branch -D bogus &&\n> +\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_offline &&\n> +\ttest_commit -C fetch_upstream u_offline &&\n> +\tgit fetch fetch_upstream fetch_offline &&\n> +\tsaved_url=$(git config remote.fetch_upstream.url) &&\n> +\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n> +\tgit config remote.fetch_upstream.url ./does-not-exist &&\n> +\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n> +\ttest_grep \"failed to fetch\" err &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_refspec &&\n> +\ttest_commit -C fetch_upstream u_refspec &&\n> +\tgit fetch fetch_upstream fetch_refspec &&\n> +\tgit remote add fetch_custom ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_custom\" &&\n> +\tgit config --replace-all remote.fetch_custom.fetch \\\n> +\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n> +\tgit fetch fetch_custom &&\n> +\ttest_commit -C fetch_upstream u_refspec_post &&\n> +\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n> +\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=inherit,direct is rejected' '\n> +\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n> +\ttest_grep \"cannot combine\" err\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n> +\ttest_commit -C fetch_upstream u_lastwin &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n> +\ttest_must_fail git checkout --track=fetch --track=direct \\\n> +\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_inherit &&\n> +\ttest_commit -C fetch_upstream u_inherit &&\n> +\tgit fetch fetch_upstream fetch_inherit &&\n> +\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n> +\ttest_commit -C fetch_upstream u_inherit2 &&\n> +\tgit checkout main &&\n> +\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n> +\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n> +\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n> +'\n> +\n> +test_expect_success 'checkout --track=bogus reports an error' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n> +\ttest_grep \"expects\" err\n> +'\n> +\n> +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_switch &&\n> +\ttest_commit -C fetch_upstream u_switch &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> +\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> +'\n> +\n>   test_done\n> \n> base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0\n\n"},{"id":"543058","messageId":"pull.2281.v8.git.git.1778507225500.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v7.git.git.1778280727849.gitgitgadget@gmail.com","subject":"[PATCH v8] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-11T13:47:05Z","receivedAt":"2026-05-11T13:47:09Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: --track=fetch\n    \n    Check the fallback ref using arg directly instead of reconstructing it.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v8\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v8\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v7:\n\n 1:  de375d55f1 ! 1:  61c2199fd5 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ builtin/checkout.c\n       #include \"revision.h\"\n      +#include \"run-command.h\"\n       #include \"setup.h\"\n     -+#include \"strvec.h\"\n     + #include \"strvec.h\"\n       #include \"submodule.h\"\n     - #include \"symlinks.h\"\n     - #include \"trace2.h\"\n      @@ builtin/checkout.c: struct checkout_opts {\n       \tint count_checkout_paths;\n       \tint overlay_mode;\n     @@ builtin/checkout.c: struct branch_info {\n      +\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n      +\t\treturn;\n      +\n     -+\tif (src_ref) {\n     -+\t\tconst char *short_src = src_ref;\n     ++\t{\n      +\t\tstruct object_id oid;\n      +\n     -+\t\tskip_prefix(short_src, \"refs/heads/\", &short_src);\n     -+\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/%s\", remote_name, short_src);\n     ++\t\tif (strchr(arg, '/'))\n     ++\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s\", arg);\n     ++\t\telse\n     ++\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/HEAD\", arg);\n      +\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n      +\t\t\t\t   dst_ref.buf, &oid))\n      +\t\t\thave_existing_ref = 1;\n\n\n Documentation/git-checkout.adoc |  13 ++-\n Documentation/git-switch.adoc   |  13 ++-\n builtin/checkout.c              | 168 +++++++++++++++++++++++++++++++-\n t/t7201-co.sh                   | 132 +++++++++++++++++++++++++\n 4 files changed, 319 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..28f17f427e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,22 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..3f54cf39e9 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -154,11 +154,22 @@ should result in deletion of the path).\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the switch proceeds from the existing tip; otherwise the switch is\n+aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex ac0186a33e..157242bc9f 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,10 +26,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n #include \"submodule.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -113,6 +116,157 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static int resolve_fetch_target(const char *arg, char **remote_out,\n+\t\t\t\tchar **src_ref_out)\n+{\n+\tconst char *slash;\n+\tchar *remote_name;\n+\tstruct remote *remote;\n+\tstruct refspec_item query = { 0 };\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tconst char *rest;\n+\n+\t*remote_out = NULL;\n+\t*src_ref_out = NULL;\n+\n+\tif (!arg || !*arg)\n+\t\treturn -1;\n+\n+\tslash = strchr(arg, '/');\n+\tif (slash == arg)\n+\t\treturn -1;\n+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n+\n+\tremote = remote_get(remote_name);\n+\tif (!remote || !remote_is_configured(remote, 1)) {\n+\t\tfree(remote_name);\n+\t\treturn -1;\n+\t}\n+\n+\trest = (slash && slash[1]) ? slash + 1 : NULL;\n+\tif (!rest) {\n+\t\tstruct object_id oid;\n+\t\tconst char *head_target;\n+\t\tconst char *short_target;\n+\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n+\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t      dst.buf,\n+\t\t\t\t\t\t      RESOLVE_REF_READING |\n+\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t      &oid, NULL);\n+\t\tstrbuf_reset(&dst);\n+\t\tif (head_target &&\n+\t\t    skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n+\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n+\t\t    *short_target == '/')\n+\t\t\trest = short_target + 1;\n+\t}\n+\n+\tif (rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n+\t\tquery.dst = dst.buf;\n+\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n+\t\t\t*src_ref_out = xstrdup(query.src);\n+\t\t\tfree(query.src);\n+\t\t} else {\n+\t\t\t*src_ref_out = xstrdup(rest);\n+\t\t}\n+\t}\n+\n+\tstrbuf_release(&dst);\n+\t*remote_out = remote_name;\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tchar *remote_name = NULL;\n+\tchar *src_ref = NULL;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct strbuf dst_ref = STRBUF_INIT;\n+\tint have_existing_ref = 0;\n+\n+\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n+\t\treturn;\n+\n+\t{\n+\t\tstruct object_id oid;\n+\n+\t\tif (strchr(arg, '/'))\n+\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s\", arg);\n+\t\telse\n+\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/HEAD\", arg);\n+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t   dst_ref.buf, &oid))\n+\t\t\thave_existing_ref = 1;\n+\t}\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (src_ref)\n+\t\tstrvec_push(&cmd.args, src_ref);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (have_existing_ref)\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"),\n+\t\t\t\targ, dst_ref.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfree(remote_name);\n+\tfree(src_ref);\n+\tstrbuf_release(&dst_ref);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0, saw_inherit = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\tsaw_direct = 1;\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\tsaw_inherit = 1;\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (saw_direct && saw_inherit)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value,\n \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\n \tif (starts_with(var, \"submodule.\"))\n \t\treturn git_default_submodule_config(var, value, NULL);\n \n@@ -1741,10 +1894,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1949,8 +2102,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..19ac6a1a2e 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\tgit fetch fetch_upstream fetch_refspec &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit fetch fetch_custom &&\n+\ttest_commit -C fetch_upstream u_refspec_post &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 7760f83b59750c27df653c5c46d0f80e44cfe02c\n-- \ngitgitgadget\n"},{"id":"543102","messageId":"xmqqh5odqxh2.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v8.git.git.1778507225500.gitgitgadget@gmail.com","subject":"Re: [PATCH v8] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-12T00:32:09Z","receivedAt":"2026-05-12T00:32:12Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\n> index 43ccf47cf6..28f17f427e 100644\n> --- a/Documentation/git-checkout.adoc\n> +++ b/Documentation/git-checkout.adoc\n> @@ -158,11 +158,22 @@ of it\").\n>  \tresets _<branch>_ to the start point instead of failing.\n>  \n>  `-t`::\n> -`--track[=(direct|inherit)]`::\n> +`--track[=(direct|inherit|fetch)[,...]]`::\n>  \tWhen creating a new branch, set up \"upstream\" configuration. See\n>  \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n>  \t--track without -b implies branch creation.\n>  +\n> +The argument is a comma-separated list. `direct` (the default) and\n> +`inherit` select the tracking mode and are mutually exclusive. Adding\n> +`fetch` requests that the remote be fetched before _<start-point>_ is\n> +resolved, so the new branch starts from a fresh tip: when\n> +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n> +updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n> +only the remote's default branch is updated.\n\nThe latter is because \"checkout -t -b new remote/origin\" makes \"new\"\ntrack \"remote/origin/HEAD\", which makes sense.  And of course this\nfetch can fail.\n\n> +static int resolve_fetch_target(const char *arg, char **remote_out,\n> +\t\t\t\tchar **src_ref_out)\n> +{\n> +\tconst char *slash;\n> +\tchar *remote_name;\n> +\tstruct remote *remote;\n> +\tstruct refspec_item query = { 0 };\n> +\tstruct strbuf dst = STRBUF_INIT;\n> +\tconst char *rest;\n> +\n> +\t*remote_out = NULL;\n> +\t*src_ref_out = NULL;\n> +\n> +\tif (!arg || !*arg)\n> +\t\treturn -1;\n> +\n> +\tslash = strchr(arg, '/');\n> +\tif (slash == arg)\n> +\t\treturn -1;\n> +\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n> +\n> +\tremote = remote_get(remote_name);\n> +\tif (!remote || !remote_is_configured(remote, 1)) {\n> +\t\tfree(remote_name);\n> +\t\treturn -1;\n> +\t}\n> +\n> +\trest = (slash && slash[1]) ? slash + 1 : NULL;\n\nThere is no slash when asking for \"origin\", and the control goes\ninto the \"if (!rest)\" block.\n\n> +\tif (!rest) {\n> +\t\tstruct object_id oid;\n> +\t\tconst char *head_target;\n> +\t\tconst char *short_target;\n> +\n> +\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n> +\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\t      dst.buf,\n> +\t\t\t\t\t\t      RESOLVE_REF_READING |\n> +\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n> +\t\t\t\t\t\t      &oid, NULL);\n> +\t\tstrbuf_reset(&dst);\n> +\t\tif (head_target &&\n> +\t\t    skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n> +\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n> +\t\t    *short_target == '/')\n> +\t\t\trest = short_target + 1;\n> +\t}\n\nIf refs/remotes/origin/HEAD points at refs/remotes/origin/main, rest\ngets \"main\".  Otherwise (i.e. unexpected contents in HEAD or lacking\nHEAD), rest remains NULL.  And then we have the \"if (rest)\" block.\n\n> +\tif (rest) {\n> +\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n> +\t\tquery.dst = dst.buf;\n> +\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n> +\t\t\t*src_ref_out = xstrdup(query.src);\n> +\t\t\tfree(query.src);\n> +\t\t} else {\n> +\t\t\t*src_ref_out = xstrdup(rest);\n> +\t\t}\n> +\t}\n> +\n> +\tstrbuf_release(&dst);\n> +\t*remote_out = remote_name;\n> +\treturn 0;\n> +}\n\nIt is not a new problem but I do not remember how we explicitly\nforbid \"hierarchical\" remote names.  refs/remotes/a/b/c might be\ntheir branch \"b/c\" at remote we call \"a\", or branch \"c\" at remote\n\"a/b\".  Unless we forbid slashes in remote names, the \"there is no\nslash so we got only the name of the remote to mean its HEAD\" logic\nwould not work well, so we may want to double check.\n\n> +static void fetch_remote_for_start_point(const char *arg)\n> +{\n> +\tchar *remote_name = NULL;\n> +\tchar *src_ref = NULL;\n> +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n> +\tstruct strbuf dst_ref = STRBUF_INIT;\n> +\tint have_existing_ref = 0;\n> +\n> +\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n> +\t\treturn;\n> +\n> +\t{\n> +\t\tstruct object_id oid;\n> +\n> +\t\tif (strchr(arg, '/'))\n> +\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s\", arg);\n> +\t\telse\n> +\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/HEAD\", arg);\n> +\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n> +\t\t\t\t   dst_ref.buf, &oid))\n> +\t\t\thave_existing_ref = 1;\n> +\t}\n\nI do not quite see the point of this extra block.  Can we do without\nit (and move the def of oid up near the beginning of the function,\nof course)?\n\nEven better, as resolve_fetch_target() already looks at \"arg\" and\npoked at the remote-tracking ref hierarchy, wouldn't it make more\nsense to make that helper function responsible for finding out if\nthere already is a usable, albeit potentially stale, ref?  \n\n\n> +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n> +\tif (src_ref)\n> +\t\tstrvec_push(&cmd.args, src_ref);\n> +\tcmd.git_cmd = 1;\n> +\tif (run_command(&cmd)) {\n> +\t\tif (have_existing_ref)\n> +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n> +\t\t\t\t  \"using existing '%s'\"),\n> +\t\t\t\targ, dst_ref.buf);\n> +\t\telse\n> +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n> +\t}\n> +\n> +\tfree(remote_name);\n> +\tfree(src_ref);\n> +\tstrbuf_release(&dst_ref);\n> +}\n\n> @@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value,\n>  \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n>  \t\treturn 0;\n>  \t}\n> -\n>  \tif (starts_with(var, \"submodule.\"))\n>  \t\treturn git_default_submodule_config(var, value, NULL);\n\nUnrelated patch noise?\n\n"},{"id":"543165","messageId":"pull.2281.v9.git.git.1778583307774.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v8.git.git.1778507225500.gitgitgadget@gmail.com","subject":"[PATCH v9] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-12T10:55:07Z","receivedAt":"2026-05-12T10:55:11Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: --track=fetch\n    \n     * Support hierarchical remote names (e.g. nested/remote) by trying the\n       longest prefix first.\n     * Fold the existing-ref lookup into resolve_fetch_target() so it\n       returns the ref name directly.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v9\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v9\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v8:\n\n 1:  61c2199fd5 ! 1:  021375e4cc checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ builtin/checkout.c: struct branch_info {\n       };\n       \n      +static int resolve_fetch_target(const char *arg, char **remote_out,\n     -+\t\t\t\tchar **src_ref_out)\n     ++\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n      +{\n      +\tconst char *slash;\n     -+\tchar *remote_name;\n     -+\tstruct remote *remote;\n     ++\tchar *remote_name = NULL;\n     ++\tstruct remote *remote = NULL;\n      +\tstruct refspec_item query = { 0 };\n      +\tstruct strbuf dst = STRBUF_INIT;\n     -+\tconst char *rest;\n     ++\tstruct object_id oid;\n     ++\tconst char *rest = NULL;\n     ++\tconst char *head_target = NULL;\n     ++\tconst char *short_target;\n      +\n      +\t*remote_out = NULL;\n      +\t*src_ref_out = NULL;\n     ++\t*existing_ref_out = NULL;\n      +\n     -+\tif (!arg || !*arg)\n     -+\t\treturn -1;\n     -+\n     -+\tslash = strchr(arg, '/');\n     -+\tif (slash == arg)\n     ++\tif (!arg || !*arg || *arg == '/')\n      +\t\treturn -1;\n     -+\tremote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);\n      +\n     -+\tremote = remote_get(remote_name);\n     -+\tif (!remote || !remote_is_configured(remote, 1)) {\n     ++\tslash = arg + strlen(arg);\n     ++\twhile (1) {\n      +\t\tfree(remote_name);\n     -+\t\treturn -1;\n     ++\t\tremote_name = xstrndup(arg, slash - arg);\n     ++\t\tremote = remote_get(remote_name);\n     ++\t\tif (remote && remote_is_configured(remote, 1))\n     ++\t\t\tbreak;\n     ++\t\twhile (slash > arg && *--slash != '/')\n     ++\t\t\t;\n     ++\t\tif (slash == arg) {\n     ++\t\t\tfree(remote_name);\n     ++\t\t\treturn -1;\n     ++\t\t}\n      +\t}\n      +\n     -+\trest = (slash && slash[1]) ? slash + 1 : NULL;\n     ++\tif (*slash == '/' && slash[1])\n     ++\t\trest = slash + 1;\n      +\tif (!rest) {\n     -+\t\tstruct object_id oid;\n     -+\t\tconst char *head_target;\n     -+\t\tconst char *short_target;\n     -+\n      +\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n      +\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n      +\t\t\t\t\t\t      dst.buf,\n      +\t\t\t\t\t\t      RESOLVE_REF_READING |\n      +\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n      +\t\t\t\t\t\t      &oid, NULL);\n     ++\t\tif (head_target) {\n     ++\t\t\t*existing_ref_out = xstrdup(dst.buf);\n     ++\t\t\tif (skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n     ++\t\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n     ++\t\t\t    *short_target == '/')\n     ++\t\t\t\trest = short_target + 1;\n     ++\t\t}\n      +\t\tstrbuf_reset(&dst);\n     -+\t\tif (head_target &&\n     -+\t\t    skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n     -+\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n     -+\t\t    *short_target == '/')\n     -+\t\t\trest = short_target + 1;\n      +\t}\n      +\n      +\tif (rest) {\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\t} else {\n      +\t\t\t*src_ref_out = xstrdup(rest);\n      +\t\t}\n     ++\t\tif (!*existing_ref_out) {\n     ++\t\t\tstrbuf_reset(&dst);\n     ++\t\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n     ++\t\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     ++\t\t\t\t\t   dst.buf, &oid))\n     ++\t\t\t\t*existing_ref_out = xstrdup(dst.buf);\n     ++\t\t}\n      +\t}\n      +\n      +\tstrbuf_release(&dst);\n     @@ builtin/checkout.c: struct branch_info {\n      +{\n      +\tchar *remote_name = NULL;\n      +\tchar *src_ref = NULL;\n     ++\tchar *existing_ref = NULL;\n      +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n     -+\tstruct strbuf dst_ref = STRBUF_INIT;\n     -+\tint have_existing_ref = 0;\n      +\n     -+\tif (resolve_fetch_target(arg, &remote_name, &src_ref))\n     ++\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n      +\t\treturn;\n      +\n     -+\t{\n     -+\t\tstruct object_id oid;\n     -+\n     -+\t\tif (strchr(arg, '/'))\n     -+\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s\", arg);\n     -+\t\telse\n     -+\t\t\tstrbuf_addf(&dst_ref, \"refs/remotes/%s/HEAD\", arg);\n     -+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     -+\t\t\t\t   dst_ref.buf, &oid))\n     -+\t\t\thave_existing_ref = 1;\n     -+\t}\n     -+\n      +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n      +\tif (src_ref)\n      +\t\tstrvec_push(&cmd.args, src_ref);\n      +\tcmd.git_cmd = 1;\n      +\tif (run_command(&cmd)) {\n     -+\t\tif (have_existing_ref)\n     ++\t\tif (existing_ref)\n      +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n      +\t\t\t\t  \"using existing '%s'\"),\n     -+\t\t\t\targ, dst_ref.buf);\n     ++\t\t\t\targ, existing_ref);\n      +\t\telse\n      +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n      +\t}\n      +\n      +\tfree(remote_name);\n      +\tfree(src_ref);\n     -+\tstrbuf_release(&dst_ref);\n     ++\tfree(existing_ref);\n      +}\n      +\n      +static int parse_opt_checkout_track(const struct option *opt,\n     @@ builtin/checkout.c: struct branch_info {\n       static void branch_info_release(struct branch_info *info)\n       {\n       \tfree(info->name);\n     -@@ builtin/checkout.c: static int git_checkout_config(const char *var, const char *value,\n     - \t\topts->dwim_new_local_branch = git_config_bool(var, value);\n     - \t\treturn 0;\n     - \t}\n     --\n     - \tif (starts_with(var, \"submodule.\"))\n     - \t\treturn git_default_submodule_config(var, value, NULL);\n     - \n      @@ builtin/checkout.c: static struct option *add_common_switch_branch_options(\n       {\n       \tstruct option options[] = {\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n      +'\n      +\n     ++test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_hier &&\n     ++\ttest_commit -C fetch_upstream u_hier &&\n     ++\tgit remote add nested/remote ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove nested/remote\" &&\n     ++\tgit fetch nested/remote fetch_hier &&\n     ++\ttest_commit -C fetch_upstream u_hier_post &&\n     ++\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n     ++\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n     ++'\n     ++\n      +test_expect_success 'checkout --track=inherit,direct is rejected' '\n      +\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n      +\ttest_grep \"cannot combine\" err\n\n\n Documentation/git-checkout.adoc |  13 ++-\n Documentation/git-switch.adoc   |  13 ++-\n builtin/checkout.c              | 168 +++++++++++++++++++++++++++++++-\n t/t7201-co.sh                   | 144 +++++++++++++++++++++++++++\n 4 files changed, 332 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex 43ccf47cf6..28f17f427e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,22 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex 87707e9265..3f54cf39e9 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -154,11 +154,22 @@ should result in deletion of the path).\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the switch proceeds from the existing tip; otherwise the switch is\n+aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex ac0186a33e..aff442c526 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,10 +26,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n #include \"submodule.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -113,6 +116,158 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static int resolve_fetch_target(const char *arg, char **remote_out,\n+\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n+{\n+\tconst char *slash;\n+\tchar *remote_name = NULL;\n+\tstruct remote *remote = NULL;\n+\tstruct refspec_item query = { 0 };\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct object_id oid;\n+\tconst char *rest = NULL;\n+\tconst char *head_target = NULL;\n+\tconst char *short_target;\n+\n+\t*remote_out = NULL;\n+\t*src_ref_out = NULL;\n+\t*existing_ref_out = NULL;\n+\n+\tif (!arg || !*arg || *arg == '/')\n+\t\treturn -1;\n+\n+\tslash = arg + strlen(arg);\n+\twhile (1) {\n+\t\tfree(remote_name);\n+\t\tremote_name = xstrndup(arg, slash - arg);\n+\t\tremote = remote_get(remote_name);\n+\t\tif (remote && remote_is_configured(remote, 1))\n+\t\t\tbreak;\n+\t\twhile (slash > arg && *--slash != '/')\n+\t\t\t;\n+\t\tif (slash == arg) {\n+\t\t\tfree(remote_name);\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\tif (*slash == '/' && slash[1])\n+\t\trest = slash + 1;\n+\tif (!rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n+\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t      dst.buf,\n+\t\t\t\t\t\t      RESOLVE_REF_READING |\n+\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t      &oid, NULL);\n+\t\tif (head_target) {\n+\t\t\t*existing_ref_out = xstrdup(dst.buf);\n+\t\t\tif (skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n+\t\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n+\t\t\t    *short_target == '/')\n+\t\t\t\trest = short_target + 1;\n+\t\t}\n+\t\tstrbuf_reset(&dst);\n+\t}\n+\n+\tif (rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n+\t\tquery.dst = dst.buf;\n+\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n+\t\t\t*src_ref_out = xstrdup(query.src);\n+\t\t\tfree(query.src);\n+\t\t} else {\n+\t\t\t*src_ref_out = xstrdup(rest);\n+\t\t}\n+\t\tif (!*existing_ref_out) {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\t\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t\t   dst.buf, &oid))\n+\t\t\t\t*existing_ref_out = xstrdup(dst.buf);\n+\t\t}\n+\t}\n+\n+\tstrbuf_release(&dst);\n+\t*remote_out = remote_name;\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tchar *remote_name = NULL;\n+\tchar *src_ref = NULL;\n+\tchar *existing_ref = NULL;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n+\t\treturn;\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (src_ref)\n+\t\tstrvec_push(&cmd.args, src_ref);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (existing_ref)\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"),\n+\t\t\t\targ, existing_ref);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfree(remote_name);\n+\tfree(src_ref);\n+\tfree(existing_ref);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0, saw_inherit = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\tsaw_direct = 1;\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\tsaw_inherit = 1;\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (saw_direct && saw_inherit)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1741,10 +1896,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1949,8 +2104,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 9bcf7c0b40..6dfe9ec931 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -801,4 +801,148 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\tgit fetch fetch_upstream fetch_refspec &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit fetch fetch_custom &&\n+\ttest_commit -C fetch_upstream u_refspec_post &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit fetch nested/remote fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier_post &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 29bd7ed5127255713c1ac2f43b7c6f257d7b4594\n-- \ngitgitgadget\n"},{"id":"543502","messageId":"pull.2281.v10.git.git.1779091483321.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v9.git.git.1778583307774.gitgitgadget@gmail.com","subject":"[PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-18T08:04:43Z","receivedAt":"2026-05-18T08:04:46Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: --track=fetch\n    \n    Rebased to fix merge conflict with master.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v10\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v10\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v9:\n\n 1:  021375e4cc ! 1:  a773fb6bdf checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Documentation/git-checkout.adoc: of it\").\n       the refspec configured for the corresponding remote, and then stripping\n      \n       ## Documentation/git-switch.adoc ##\n     -@@ Documentation/git-switch.adoc: should result in deletion of the path).\n     +@@ Documentation/git-switch.adoc: variable.\n       \tattached to a terminal, regardless of `--quiet`.\n       \n       `-t`::\n     @@ builtin/checkout.c\n       #include \"resolve-undo.h\"\n       #include \"revision.h\"\n      +#include \"run-command.h\"\n     + #include \"sequencer.h\"\n       #include \"setup.h\"\n       #include \"strvec.h\"\n     - #include \"submodule.h\"\n      @@ builtin/checkout.c: struct checkout_opts {\n       \tint count_checkout_paths;\n       \tint overlay_mode;\n\n\n Documentation/git-checkout.adoc |  13 ++-\n Documentation/git-switch.adoc   |  13 ++-\n builtin/checkout.c              | 168 +++++++++++++++++++++++++++++++-\n t/t7201-co.sh                   | 144 +++++++++++++++++++++++++++\n 4 files changed, 332 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..ec63434159 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,22 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..b5e79435cd 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,11 +155,22 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the switch proceeds from the existing tip; otherwise the switch is\n+aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 1345e8574a..fc58456546 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -115,6 +118,158 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static int resolve_fetch_target(const char *arg, char **remote_out,\n+\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n+{\n+\tconst char *slash;\n+\tchar *remote_name = NULL;\n+\tstruct remote *remote = NULL;\n+\tstruct refspec_item query = { 0 };\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct object_id oid;\n+\tconst char *rest = NULL;\n+\tconst char *head_target = NULL;\n+\tconst char *short_target;\n+\n+\t*remote_out = NULL;\n+\t*src_ref_out = NULL;\n+\t*existing_ref_out = NULL;\n+\n+\tif (!arg || !*arg || *arg == '/')\n+\t\treturn -1;\n+\n+\tslash = arg + strlen(arg);\n+\twhile (1) {\n+\t\tfree(remote_name);\n+\t\tremote_name = xstrndup(arg, slash - arg);\n+\t\tremote = remote_get(remote_name);\n+\t\tif (remote && remote_is_configured(remote, 1))\n+\t\t\tbreak;\n+\t\twhile (slash > arg && *--slash != '/')\n+\t\t\t;\n+\t\tif (slash == arg) {\n+\t\t\tfree(remote_name);\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\tif (*slash == '/' && slash[1])\n+\t\trest = slash + 1;\n+\tif (!rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n+\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t      dst.buf,\n+\t\t\t\t\t\t      RESOLVE_REF_READING |\n+\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t      &oid, NULL);\n+\t\tif (head_target) {\n+\t\t\t*existing_ref_out = xstrdup(dst.buf);\n+\t\t\tif (skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n+\t\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n+\t\t\t    *short_target == '/')\n+\t\t\t\trest = short_target + 1;\n+\t\t}\n+\t\tstrbuf_reset(&dst);\n+\t}\n+\n+\tif (rest) {\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n+\t\tquery.dst = dst.buf;\n+\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n+\t\t\t*src_ref_out = xstrdup(query.src);\n+\t\t\tfree(query.src);\n+\t\t} else {\n+\t\t\t*src_ref_out = xstrdup(rest);\n+\t\t}\n+\t\tif (!*existing_ref_out) {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\t\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t\t   dst.buf, &oid))\n+\t\t\t\t*existing_ref_out = xstrdup(dst.buf);\n+\t\t}\n+\t}\n+\n+\tstrbuf_release(&dst);\n+\t*remote_out = remote_name;\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tchar *remote_name = NULL;\n+\tchar *src_ref = NULL;\n+\tchar *existing_ref = NULL;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n+\t\treturn;\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (src_ref)\n+\t\tstrvec_push(&cmd.args, src_ref);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (existing_ref)\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"),\n+\t\t\t\targ, existing_ref);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfree(remote_name);\n+\tfree(src_ref);\n+\tfree(existing_ref);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0, saw_inherit = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\tsaw_direct = 1;\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\tsaw_inherit = 1;\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (saw_direct && saw_inherit)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1733,10 +1888,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1941,8 +2096,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..75b46494c0 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,148 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\tgit fetch fetch_upstream fetch_refspec &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit fetch fetch_custom &&\n+\ttest_commit -C fetch_upstream u_refspec_post &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit fetch nested/remote fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier_post &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 68aca6b91299738150f71035d0033af6987fe455\n-- \ngitgitgadget\n"},{"id":"543503","messageId":"CAHwyqnXew558xQYyXvM=fRVETyMt3MVDubBkwSNLQCs7WE7PqA@mail.gmail.com","threadId":"65546","inReplyTo":"xmqqh5odqxh2.fsf@gitster.g","subject":"Re: [PATCH v8] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-18T08:06:02Z","receivedAt":"2026-05-18T08:06:40Z","isPatch":true,"body":"> I do not quite see the point of this extra block.  Can we do without\n> it (and move the def of oid up near the beginning of the function,\n> of course)?\n>\n> Even better, as resolve_fetch_target() already looks at \"arg\" and\n> poked at the remote-tracking ref hierarchy, wouldn't it make more\n> sense to make that helper function responsible for finding out if\n> there already is a usable, albeit potentially stale, ref?\n\nDone. Can I get another review here please?\n\n> > @@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value,\n> >               opts->dwim_new_local_branch = git_config_bool(var, value);\n> >               return 0;\n> >       }\n> > -\n> >       if (starts_with(var, \"submodule.\"))\n> >               return git_default_submodule_config(var, value, NULL);\n>\n> Unrelated patch noise?\n\nRemoved.\n\n\nHarald\n"},{"id":"543591","messageId":"xmqq8q9f9b5w.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v10.git.git.1779091483321.gitgitgadget@gmail.com","subject":"Re: [PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-19T06:16:27Z","receivedAt":"2026-05-19T06:16:30Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> If you want to fork your topic branch from the very latest of the\n> tip of a branch your remote has, you would do:\n>\n>     git fetch origin some-branch\n>     git checkout -b new_branch --track origin/some-branch\n>\n> Extend the \"--track\" option of \"git checkout\" and allow users to\n> write\n>\n>     git checkout -b new_branch --track=fetch origin/some-branch\n>\n> to (1) fetch 'some-branch' from the remote 'origin', updating the\n> remote-tracking branch 'origin/some-branch', (2) arrange subsequent\n> 'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n> (3) fork 'new_branch' from it.\n>\n> In the value of the '--track' option, 'fetch' can be combined with\n> the existing 'direct' (default) and 'inherit' modes via a\n> comma-separated list. Examples:\n>\n>     git checkout -b new_branch --track=fetch,inherit some_local_branch\n>     git switch -c new_branch --track=fetch origin/some-branch\n>\n> When \"fetch\" is requested and <start-point> is in <remote>/<branch>\n> form, run \"git fetch <remote> <branch>\" before resolving the ref, so\n> that other remote-tracking branches are left untouched. If\n> <start-point> is a bare remote name like \"origin\" (which resolves to\n> that remote's default branch), \"git fetch <remote>\" is run instead,\n> since the target branch is not known up front. Abort the checkout if\n> the fetch fails.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>     checkout: --track=fetch\n>     \n>     Rebased to fix merge conflict with master.\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v10\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v10\n> Pull-Request: https://github.com/git/git/pull/2281\n>\n> Range-diff vs v9:\n>\n>  1:  021375e4cc ! 1:  a773fb6bdf checkout: extend --track with a \"fetch\" mode to refresh start-point\n>      @@ Documentation/git-checkout.adoc: of it\").\n>        the refspec configured for the corresponding remote, and then stripping\n>       \n>        ## Documentation/git-switch.adoc ##\n>      -@@ Documentation/git-switch.adoc: should result in deletion of the path).\n>      +@@ Documentation/git-switch.adoc: variable.\n>        \tattached to a terminal, regardless of `--quiet`.\n>        \n>        `-t`::\n>      @@ builtin/checkout.c\n>        #include \"resolve-undo.h\"\n>        #include \"revision.h\"\n>       +#include \"run-command.h\"\n>      + #include \"sequencer.h\"\n>        #include \"setup.h\"\n>        #include \"strvec.h\"\n>      - #include \"submodule.h\"\n>       @@ builtin/checkout.c: struct checkout_opts {\n>        \tint count_checkout_paths;\n>        \tint overlay_mode;\n>\n>\n>  Documentation/git-checkout.adoc |  13 ++-\n>  Documentation/git-switch.adoc   |  13 ++-\n>  builtin/checkout.c              | 168 +++++++++++++++++++++++++++++++-\n>  t/t7201-co.sh                   | 144 +++++++++++++++++++++++++++\n>  4 files changed, 332 insertions(+), 6 deletions(-)\n>\n> diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\n> index a8b3b8c2e2..ec63434159 100644\n> --- a/Documentation/git-checkout.adoc\n> +++ b/Documentation/git-checkout.adoc\n> @@ -158,11 +158,22 @@ of it\").\n>  \tresets _<branch>_ to the start point instead of failing.\n>  \n>  `-t`::\n> -`--track[=(direct|inherit)]`::\n> +`--track[=(direct|inherit|fetch)[,...]]`::\n>  \tWhen creating a new branch, set up \"upstream\" configuration. See\n>  \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n>  \t--track without -b implies branch creation.\n>  +\n> +The argument is a comma-separated list. `direct` (the default) and\n> +`inherit` select the tracking mode and are mutually exclusive. Adding\n> +`fetch` requests that the remote be fetched before _<start-point>_ is\n> +resolved, so the new branch starts from a fresh tip: when\n> +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n> +updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n> +only the remote's default branch is updated. If the fetch fails and the\n> +corresponding remote-tracking ref already exists, a warning is printed\n> +and the checkout proceeds from the existing tip; otherwise the checkout\n> +is aborted.\n> ++\n>  If no `-b` option is given, the name of the new branch will be\n>  derived from the remote-tracking branch, by looking at the local part of\n>  the refspec configured for the corresponding remote, and then stripping\n> diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\n> index d6c4f229a5..b5e79435cd 100644\n> --- a/Documentation/git-switch.adoc\n> +++ b/Documentation/git-switch.adoc\n> @@ -155,11 +155,22 @@ variable.\n>  \tattached to a terminal, regardless of `--quiet`.\n>  \n>  `-t`::\n> -`--track[ (direct|inherit)]`::\n> +`--track[=(direct|inherit|fetch)[,...]]`::\n>  \tWhen creating a new branch, set up \"upstream\" configuration.\n>  \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n>  \tdetails.\n>  +\n> +The argument is a comma-separated list. `direct` (the default) and\n> +`inherit` select the tracking mode and are mutually exclusive. Adding\n> +`fetch` requests that the remote be fetched before _<start-point>_ is\n> +resolved, so the new branch starts from a fresh tip: when\n> +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n> +updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n> +only the remote's default branch is updated. If the fetch fails and the\n> +corresponding remote-tracking ref already exists, a warning is printed\n> +and the switch proceeds from the existing tip; otherwise the switch is\n> +aborted.\n> ++\n>  If no `-c` option is given, the name of the new branch will be derived\n>  from the remote-tracking branch, by looking at the local part of the\n>  refspec configured for the corresponding remote, and then stripping\n> diff --git a/builtin/checkout.c b/builtin/checkout.c\n> index 1345e8574a..fc58456546 100644\n> --- a/builtin/checkout.c\n> +++ b/builtin/checkout.c\n> @@ -25,10 +25,12 @@\n>  #include \"preload-index.h\"\n>  #include \"read-cache.h\"\n>  #include \"refs.h\"\n> +#include \"refspec.h\"\n>  #include \"remote.h\"\n>  #include \"repo-settings.h\"\n>  #include \"resolve-undo.h\"\n>  #include \"revision.h\"\n> +#include \"run-command.h\"\n>  #include \"sequencer.h\"\n>  #include \"setup.h\"\n>  #include \"strvec.h\"\n> @@ -62,6 +64,7 @@ struct checkout_opts {\n>  \tint count_checkout_paths;\n>  \tint overlay_mode;\n>  \tint dwim_new_local_branch;\n> +\tint fetch;\n>  \tint discard_changes;\n>  \tint accept_ref;\n>  \tint accept_pathspec;\n> @@ -115,6 +118,158 @@ struct branch_info {\n>  \tchar *checkout;\n>  };\n>  \n> +static int resolve_fetch_target(const char *arg, char **remote_out,\n> +\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n> +{\n> +\tconst char *slash;\n> +\tchar *remote_name = NULL;\n> +\tstruct remote *remote = NULL;\n> +\tstruct refspec_item query = { 0 };\n> +\tstruct strbuf dst = STRBUF_INIT;\n> +\tstruct object_id oid;\n> +\tconst char *rest = NULL;\n> +\tconst char *head_target = NULL;\n> +\tconst char *short_target;\n> +\n> +\t*remote_out = NULL;\n> +\t*src_ref_out = NULL;\n> +\t*existing_ref_out = NULL;\n> +\n> +\tif (!arg || !*arg || *arg == '/')\n> +\t\treturn -1;\n> +\n> +\tslash = arg + strlen(arg);\n> +\twhile (1) {\n> +\t\tfree(remote_name);\n> +\t\tremote_name = xstrndup(arg, slash - arg);\n> +\t\tremote = remote_get(remote_name);\n> +\t\tif (remote && remote_is_configured(remote, 1))\n> +\t\t\tbreak;\n> +\t\twhile (slash > arg && *--slash != '/')\n> +\t\t\t;\n> +\t\tif (slash == arg) {\n> +\t\t\tfree(remote_name);\n> +\t\t\treturn -1;\n> +\t\t}\n> +\t}\n\nOK.  So the caller gives \"foo/bar/baz\" when \"foo/bar\" is the name of\nthe remote that uses \"refs/remotes/foo/bar\" to store remote-tracking\nbranches from there.  You check \"foo/bar/baz\", which fails to be a\nremote, then \"foo/bar\", which is a configured remote and break.\n\n> +\n> +\tif (*slash == '/' && slash[1])\n> +\t\trest = slash + 1;\n\nAnd \"baz\" becomes the \"rest\"; if the user gave us \"foo/bar\" and the\nabobve loop found it as the name of the remote, we would want to use\n\"refs/remotes/foo/bar/HEAD\", which is the \"if (!rest)\" below is\nabout.\n\n> +\tif (!rest) {\n> +\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n> +\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\t      dst.buf,\n> +\t\t\t\t\t\t      RESOLVE_REF_READING |\n> +\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n> +\t\t\t\t\t\t      &oid, NULL);\n> +\t\tif (head_target) {\n> +\t\t\t*existing_ref_out = xstrdup(dst.buf);\n> +\t\t\tif (skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n> +\t\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n> +\t\t\t    *short_target == '/')\n> +\t\t\t\trest = short_target + 1;\n> +\t\t}\n> +\t\tstrbuf_reset(&dst);\n> +\t}\n\nSo we may have been given \"foo/bar\" and after resolving HEAD there,\nhave \"baz\" in rest.  Or \"foo/bar/baz\" was given and we may have\nfigured out that that is \"baz\" in \"foo/bar\".\n\n> +\tif (rest) {\n> +\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n> +\t\tquery.dst = dst.buf;\n> +\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n> +\t\t\t*src_ref_out = xstrdup(query.src);\n> +\t\t\tfree(query.src);\n> +\t\t} else {\n> +\t\t\t*src_ref_out = xstrdup(rest);\n> +\t\t}\n> +\t\tif (!*existing_ref_out) {\n> +\t\t\tstrbuf_reset(&dst);\n> +\t\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n> +\t\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n> +\t\t\t\t\t   dst.buf, &oid))\n> +\t\t\t\t*existing_ref_out = xstrdup(dst.buf);\n> +\t\t}\n> +\t}\n\nWhat happens if \"HEAD\" was not there, though.  If \"refs/remotes/foo/bar/\"\ndid not exist, we would have already returned -1 after trying to\nfind which part in arg is the remote name.  But if \"refs/remotes/foo/bar\"\nis valid, the user gave us \"foo/bar\", and we cannot find HEAD, then\nthe above \"if (rest)\" is skipped.  We give \"foo/bar\" to *remote_out,\nand return 0 without touching *src_ref_out or *existing_ref_out at\nall.\n\n> +\tstrbuf_release(&dst);\n> +\t*remote_out = remote_name;\n> +\treturn 0;\n> +}\n> +\n> +static void fetch_remote_for_start_point(const char *arg)\n> +{\n> +\tchar *remote_name = NULL;\n> +\tchar *src_ref = NULL;\n> +\tchar *existing_ref = NULL;\n> +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n> +\n> +\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n> +\t\treturn;\n> +\n> +\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n> +\tif (src_ref)\n> +\t\tstrvec_push(&cmd.args, src_ref);\n\nWhat should happen with this configuration\n\n    [remote \"origin\"]\n        fetch = refs/heads/*:refs/upstream/*\n\nand the user says either of these two:\n\n    $ git checkout --track=fetch upstream\n    $ git checkout --track=fetch upstream/master\n\nWe fail to find in \"where does the remote name ends and branch name\nstart?\" loop that this request is about remote \"origin\" at all, no?\nWe may see in the former case that there is\nrefs/remotes/upstream/HEAD >that points at \"master\" in the same\nhierarchy, but the code thinks \"upstream\" is the remote name, which\nwould mean you would \"git fetch upstream\", when the remote you need\nto fetch from is \"origin\".\n\n> +\tcmd.git_cmd = 1;\n> +\tif (run_command(&cmd)) {\n> +\t\tif (existing_ref)\n> +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n> +\t\t\t\t  \"using existing '%s'\"),\n> +\t\t\t\targ, existing_ref);\n> +\t\telse\n> +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n\nIf we failed to set *existing_ref_out, shouldn't we fail without\neven attempting to call run_command() here, as we will have to die()\nanyway even if \"git fetch\" succeeds.  For that matter, it may be\nsimpler and more correct for resolve_fetch_target() to fail (return\n-1) when it happens, by making the lat \"if (rest) {...}\" to have a\ncorresponding \"else { return -1 }\" after it.\n\n> +\t}\n> +\n> +\tfree(remote_name);\n> +\tfree(src_ref);\n> +\tfree(existing_ref);\n> +}\n\nI'll stop here.\n"},{"id":"543596","messageId":"CAHwyqnUx=59MK5zKL0uuFXKrZ6PEc1j_2WT-_xtsGewVH3gBKQ@mail.gmail.com","threadId":"65546","inReplyTo":"xmqq8q9f9b5w.fsf@gitster.g","subject":"Re: [PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-19T07:52:34Z","receivedAt":"2026-05-19T07:53:12Z","isPatch":true,"body":"> > +     strbuf_release(&dst);\n> > +     *remote_out = remote_name;\n> > +     return 0;\n> > +}\n> > +\n> > +static void fetch_remote_for_start_point(const char *arg)\n> > +{\n> > +     char *remote_name = NULL;\n> > +     char *src_ref = NULL;\n> > +     char *existing_ref = NULL;\n> > +     struct child_process cmd = CHILD_PROCESS_INIT;\n> > +\n> > +     if (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n> > +             return;\n> > +\n> > +     strvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n> > +     if (src_ref)\n> > +             strvec_push(&cmd.args, src_ref);\n>\n> What should happen with this configuration\n>\n>     [remote \"origin\"]\n>         fetch = refs/heads/*:refs/upstream/*\n>\n> and the user says either of these two:\n>\n>     $ git checkout --track=fetch upstream\n>     $ git checkout --track=fetch upstream/master\n>\n> We fail to find in \"where does the remote name ends and branch name\n> start?\" loop that this request is about remote \"origin\" at all, no?\n> We may see in the former case that there is\n> refs/remotes/upstream/HEAD >that points at \"master\" in the same\n> hierarchy, but the code thinks \"upstream\" is the remote name, which\n> would mean you would \"git fetch upstream\", when the remote you need\n> to fetch from is \"origin\".\n>\n> > +     cmd.git_cmd = 1;\n> > +     if (run_command(&cmd)) {\n> > +             if (existing_ref)\n> > +                     warning(_(\"failed to fetch start-point '%s'; \"\n> > +                               \"using existing '%s'\"),\n> > +                             arg, existing_ref);\n> > +             else\n> > +                     die(_(\"failed to fetch start-point '%s'\"), arg);\n>\n> If we failed to set *existing_ref_out, shouldn't we fail without\n> even attempting to call run_command() here, as we will have to die()\n> anyway even if \"git fetch\" succeeds.  For that matter, it may be\n> simpler and more correct for resolve_fetch_target() to fail (return\n> -1) when it happens, by making the lat \"if (rest) {...}\" to have a\n> corresponding \"else { return -1 }\" after it.\n\nYeah, good point. I will try to address this and send a new patch.\n\n\nHarald\n"},{"id":"543597","messageId":"pull.2281.v11.git.git.1779177508772.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v10.git.git.1779091483321.gitgitgadget@gmail.com","subject":"[PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-19T07:58:28Z","receivedAt":"2026-05-19T07:58:31Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIf you want to fork your topic branch from the very latest of the\ntip of a branch your remote has, you would do:\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nExtend the \"--track\" option of \"git checkout\" and allow users to\nwrite\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nto (1) fetch 'some-branch' from the remote 'origin', updating the\nremote-tracking branch 'origin/some-branch', (2) arrange subsequent\n'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n(3) fork 'new_branch' from it.\n\nIn the value of the '--track' option, 'fetch' can be combined with\nthe existing 'direct' (default) and 'inherit' modes via a\ncomma-separated list. Examples:\n\n    git checkout -b new_branch --track=fetch,inherit some_local_branch\n    git switch -c new_branch --track=fetch origin/some-branch\n\nWhen \"fetch\" is requested and <start-point> is in <remote>/<branch>\nform, run \"git fetch <remote> <branch>\" before resolving the ref, so\nthat other remote-tracking branches are left untouched. If\n<start-point> is a bare remote name like \"origin\" (which resolves to\nthat remote's default branch), \"git fetch <remote>\" is run instead,\nsince the target branch is not known up front. Abort the checkout if\nthe fetch fails.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: --track=fetch\n    \n     * Find the right remote by checking which remote's fetch refspec maps\n       to the user's start-point, instead of assuming the start-point begins\n       with the remote's name. This fixes cases where the user has a custom\n       refspec mapping into a namespace whose name differs from the remote\n       (e.g. fetching from origin into refs/remotes/upstream/*).\n     * For a bare namespace name, follow <namespace>/HEAD first to figure\n       out which branch to fetch.\n     * Strengthen the custom-refspec test so it actually exercises the fetch\n       (no prior git fetch).\n     * New test for the bare-namespace case.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v11\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v11\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v10:\n\n 1:  a773fb6bdf ! 1:  d0c9e3e879 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ builtin/checkout.c: struct branch_info {\n       \tchar *checkout;\n       };\n       \n     ++struct fetch_target_cb {\n     ++\tstruct refspec_item query;\n     ++\tconst char *remote_name;\n     ++\tint matches;\n     ++};\n     ++\n     ++static int match_fetch_target(struct remote *remote, void *priv)\n     ++{\n     ++\tstruct fetch_target_cb *cb = priv;\n     ++\tstruct refspec_item q = { .dst = cb->query.dst };\n     ++\n     ++\tif (!remote_find_tracking(remote, &q) && q.src) {\n     ++\t\tif (++cb->matches == 1) {\n     ++\t\t\tcb->remote_name = remote->name;\n     ++\t\t\tfree(cb->query.src);\n     ++\t\t\tcb->query.src = q.src;\n     ++\t\t} else {\n     ++\t\t\tfree(q.src);\n     ++\t\t}\n     ++\t}\n     ++\treturn 0;\n     ++}\n     ++\n      +static int resolve_fetch_target(const char *arg, char **remote_out,\n      +\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n      +{\n     -+\tconst char *slash;\n     -+\tchar *remote_name = NULL;\n     -+\tstruct remote *remote = NULL;\n     -+\tstruct refspec_item query = { 0 };\n      +\tstruct strbuf dst = STRBUF_INIT;\n     ++\tstruct strbuf head_path = STRBUF_INIT;\n     ++\tstruct fetch_target_cb cb = { 0 };\n      +\tstruct object_id oid;\n     -+\tconst char *rest = NULL;\n     -+\tconst char *head_target = NULL;\n     -+\tconst char *short_target;\n     ++\tconst char *head_target;\n      +\n      +\t*remote_out = NULL;\n      +\t*src_ref_out = NULL;\n      +\t*existing_ref_out = NULL;\n      +\n     -+\tif (!arg || !*arg || *arg == '/')\n     ++\tif (!arg || !*arg)\n      +\t\treturn -1;\n      +\n     -+\tslash = arg + strlen(arg);\n     -+\twhile (1) {\n     -+\t\tfree(remote_name);\n     -+\t\tremote_name = xstrndup(arg, slash - arg);\n     -+\t\tremote = remote_get(remote_name);\n     -+\t\tif (remote && remote_is_configured(remote, 1))\n     -+\t\t\tbreak;\n     -+\t\twhile (slash > arg && *--slash != '/')\n     -+\t\t\t;\n     -+\t\tif (slash == arg) {\n     -+\t\t\tfree(remote_name);\n     -+\t\t\treturn -1;\n     -+\t\t}\n     -+\t}\n     -+\n     -+\tif (*slash == '/' && slash[1])\n     -+\t\trest = slash + 1;\n     -+\tif (!rest) {\n     -+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/HEAD\", remote_name);\n     -+\t\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n     -+\t\t\t\t\t\t      dst.buf,\n     -+\t\t\t\t\t\t      RESOLVE_REF_READING |\n     -+\t\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n     -+\t\t\t\t\t\t      &oid, NULL);\n     -+\t\tif (head_target) {\n     -+\t\t\t*existing_ref_out = xstrdup(dst.buf);\n     -+\t\t\tif (skip_prefix(head_target, \"refs/remotes/\", &short_target) &&\n     -+\t\t\t    skip_prefix(short_target, remote_name, &short_target) &&\n     -+\t\t\t    *short_target == '/')\n     -+\t\t\t\trest = short_target + 1;\n     -+\t\t}\n     -+\t\tstrbuf_reset(&dst);\n     ++\tstrbuf_addf(&head_path, \"refs/remotes/%s/HEAD\", arg);\n     ++\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n     ++\t\t\t\t\t      head_path.buf,\n     ++\t\t\t\t\t      RESOLVE_REF_READING |\n     ++\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n     ++\t\t\t\t\t      &oid, NULL);\n     ++\tif (head_target)\n     ++\t\tstrbuf_addstr(&dst, head_target);\n     ++\telse\n     ++\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n     ++\n     ++\tcb.query.dst = dst.buf;\n     ++\tfor_each_remote(match_fetch_target, &cb);\n     ++\n     ++\tif (cb.matches != 1) {\n     ++\t\tfree(cb.query.src);\n     ++\t\tstrbuf_release(&dst);\n     ++\t\tstrbuf_release(&head_path);\n     ++\t\treturn -1;\n      +\t}\n      +\n     -+\tif (rest) {\n     -+\t\tstrbuf_addf(&dst, \"refs/remotes/%s/%s\", remote_name, rest);\n     -+\t\tquery.dst = dst.buf;\n     -+\t\tif (!remote_find_tracking(remote, &query) && query.src) {\n     -+\t\t\t*src_ref_out = xstrdup(query.src);\n     -+\t\t\tfree(query.src);\n     -+\t\t} else {\n     -+\t\t\t*src_ref_out = xstrdup(rest);\n     -+\t\t}\n     -+\t\tif (!*existing_ref_out) {\n     -+\t\t\tstrbuf_reset(&dst);\n     -+\t\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n     -+\t\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     -+\t\t\t\t\t   dst.buf, &oid))\n     -+\t\t\t\t*existing_ref_out = xstrdup(dst.buf);\n     -+\t\t}\n     -+\t}\n     ++\t*remote_out = xstrdup(cb.remote_name);\n     ++\t*src_ref_out = cb.query.src;\n     ++\tif (head_target)\n     ++\t\t*existing_ref_out = strbuf_detach(&head_path, NULL);\n     ++\telse if (!refs_read_ref(get_main_ref_store(the_repository),\n     ++\t\t\t\tdst.buf, &oid))\n     ++\t\t*existing_ref_out = strbuf_detach(&dst, NULL);\n      +\n      +\tstrbuf_release(&dst);\n     -+\t*remote_out = remote_name;\n     ++\tstrbuf_release(&head_path);\n      +\treturn 0;\n      +}\n      +\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\n      +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n     -+\ttest_commit -C fetch_upstream u_refspec &&\n     -+\tgit fetch fetch_upstream fetch_refspec &&\n      +\tgit remote add fetch_custom ./fetch_upstream &&\n      +\ttest_when_finished \"git remote remove fetch_custom\" &&\n      +\tgit config --replace-all remote.fetch_custom.fetch \\\n      +\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n     -+\tgit fetch fetch_custom &&\n     -+\ttest_commit -C fetch_upstream u_refspec_post &&\n     ++\tgit -C fetch_upstream checkout -b fetch_refspec &&\n     ++\ttest_commit -C fetch_upstream u_refspec &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n      +\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n      +\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n      +'\n      +\n     ++test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n     ++\tgit checkout main &&\n     ++\tgit remote add fetch_ns ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove fetch_ns\" &&\n     ++\tgit config --replace-all remote.fetch_ns.fetch \\\n     ++\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n     ++\tgit fetch fetch_ns &&\n     ++\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n     ++\tgit -C fetch_upstream checkout main &&\n     ++\ttest_commit -C fetch_upstream u_ns_post &&\n     ++\tgit checkout --track=fetch -b local_ns ns_alias &&\n     ++\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n     ++\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n     ++\ttest_cmp_config refs/heads/main branch.local_ns.merge\n     ++'\n     ++\n      +test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_hier &&\n\n\n Documentation/git-checkout.adoc |  13 ++-\n Documentation/git-switch.adoc   |  13 ++-\n builtin/checkout.c              | 164 +++++++++++++++++++++++++++++++-\n t/t7201-co.sh                   | 158 ++++++++++++++++++++++++++++++\n 4 files changed, 342 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..ec63434159 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,22 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..b5e79435cd 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,11 +155,22 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n \tdetails.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n+only the remote's default branch is updated. If the fetch fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the switch proceeds from the existing tip; otherwise the switch is\n+aborted.\n++\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\n refspec configured for the corresponding remote, and then stripping\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 1345e8574a..5b3a06c0cd 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -115,6 +118,154 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+struct fetch_target_cb {\n+\tstruct refspec_item query;\n+\tconst char *remote_name;\n+\tint matches;\n+};\n+\n+static int match_fetch_target(struct remote *remote, void *priv)\n+{\n+\tstruct fetch_target_cb *cb = priv;\n+\tstruct refspec_item q = { .dst = cb->query.dst };\n+\n+\tif (!remote_find_tracking(remote, &q) && q.src) {\n+\t\tif (++cb->matches == 1) {\n+\t\t\tcb->remote_name = remote->name;\n+\t\t\tfree(cb->query.src);\n+\t\t\tcb->query.src = q.src;\n+\t\t} else {\n+\t\t\tfree(q.src);\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n+\n+static int resolve_fetch_target(const char *arg, char **remote_out,\n+\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n+{\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct strbuf head_path = STRBUF_INIT;\n+\tstruct fetch_target_cb cb = { 0 };\n+\tstruct object_id oid;\n+\tconst char *head_target;\n+\n+\t*remote_out = NULL;\n+\t*src_ref_out = NULL;\n+\t*existing_ref_out = NULL;\n+\n+\tif (!arg || !*arg)\n+\t\treturn -1;\n+\n+\tstrbuf_addf(&head_path, \"refs/remotes/%s/HEAD\", arg);\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t      head_path.buf,\n+\t\t\t\t\t      RESOLVE_REF_READING |\n+\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      &oid, NULL);\n+\tif (head_target)\n+\t\tstrbuf_addstr(&dst, head_target);\n+\telse\n+\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\n+\tcb.query.dst = dst.buf;\n+\tfor_each_remote(match_fetch_target, &cb);\n+\n+\tif (cb.matches != 1) {\n+\t\tfree(cb.query.src);\n+\t\tstrbuf_release(&dst);\n+\t\tstrbuf_release(&head_path);\n+\t\treturn -1;\n+\t}\n+\n+\t*remote_out = xstrdup(cb.remote_name);\n+\t*src_ref_out = cb.query.src;\n+\tif (head_target)\n+\t\t*existing_ref_out = strbuf_detach(&head_path, NULL);\n+\telse if (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\tdst.buf, &oid))\n+\t\t*existing_ref_out = strbuf_detach(&dst, NULL);\n+\n+\tstrbuf_release(&dst);\n+\tstrbuf_release(&head_path);\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg)\n+{\n+\tchar *remote_name = NULL;\n+\tchar *src_ref = NULL;\n+\tchar *existing_ref = NULL;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\n+\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n+\t\treturn;\n+\n+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n+\tif (src_ref)\n+\t\tstrvec_push(&cmd.args, src_ref);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (existing_ref)\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"),\n+\t\t\t\targ, existing_ref);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfree(remote_name);\n+\tfree(src_ref);\n+\tfree(existing_ref);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0, saw_inherit = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\")) {\n+\t\t\topts->fetch = 1;\n+\t\t} else if (!strcmp(item->string, \"direct\")) {\n+\t\t\tsaw_direct = 1;\n+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n+\t\t\tsaw_inherit = 1;\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\t} else {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (saw_direct && saw_inherit)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1733,10 +1884,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1941,8 +2092,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0]);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..d4f5467903 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,162 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ns\" &&\n+\tgit config --replace-all remote.fetch_ns.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n+\tgit fetch fetch_ns &&\n+\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_ns_post &&\n+\tgit checkout --track=fetch -b local_ns ns_alias &&\n+\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n+\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n+\ttest_cmp_config refs/heads/main branch.local_ns.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit fetch nested/remote fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier_post &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\tgit fetch fetch_upstream fetch_inherit &&\n+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit2 &&\n+\tgit checkout main &&\n+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: 7bcaabddcf68bd0702697da5904c3b68c52f94cf\n-- \ngitgitgadget\n"},{"id":"543601","messageId":"xmqqfr3n7r1a.fsf@gitster.g","threadId":"65546","inReplyTo":"CAHwyqnUx=59MK5zKL0uuFXKrZ6PEc1j_2WT-_xtsGewVH3gBKQ@mail.gmail.com","subject":"Re: [PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-19T08:16:33Z","receivedAt":"2026-05-19T08:16:36Z","isPatch":true,"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> Yeah, good point. I will try to address this and send a new patch.\n\nPlease don't.\n\nNext time, think deeply yourself and do not rob my time to think\nthese things for you.  I do not have infinite amount of time.  A\ngood balance may be if I find one issue in your current code, it is\nlikely that you'd better three more issues and fix them before\nsending the next round, or something like that.\n\nThanks.\n"},{"id":"543606","messageId":"CAHwyqnVtZDsBiGSk5rvMJGGk_KRg7XY_rJO4Q37hOxgoO_SJaA@mail.gmail.com","threadId":"65546","inReplyTo":"xmqqfr3n7r1a.fsf@gitster.g","subject":"Re: [PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-19T08:32:57Z","receivedAt":"2026-05-19T08:33:36Z","isPatch":true,"body":"> > Yeah, good point. I will try to address this and send a new patch.\n>\n> Please don't.\n>\n> Next time, think deeply yourself and do not rob my time to think\n> these things for you.  I do not have infinite amount of time.  A\n> good balance may be if I find one issue in your current code, it is\n> likely that you'd better three more issues and fix them before\n> sending the next round, or something like that.\n>\n> Thanks.\n\nI already sent the patch. Sorry about that!\n\n\nHarald\n"},{"id":"543608","messageId":"CAHwyqnXMWMz+_8Yccgje1FuzcxK2x26eNd9i7+_H1xHLs-VA+g@mail.gmail.com","threadId":"65546","inReplyTo":"CAHwyqnVtZDsBiGSk5rvMJGGk_KRg7XY_rJO4Q37hOxgoO_SJaA@mail.gmail.com","subject":"Re: [PATCH v10] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-19T08:38:18Z","receivedAt":"2026-05-19T08:38:57Z","isPatch":true,"body":"> > > Yeah, good point. I will try to address this and send a new patch.\n> >\n> > Please don't.\n> >\n> > Next time, think deeply yourself and do not rob my time to think\n> > these things for you.  I do not have infinite amount of time.  A\n> > good balance may be if I find one issue in your current code, it is\n> > likely that you'd better three more issues and fix them before\n> > sending the next round, or something like that.\n> >\n> > Thanks.\n>\n> I already sent the patch. Sorry about that!\n\nI didn't realize how much time this took away from you. My motivation\nwas always to put the topic into a better position ASAP, to avoid\nanyone else reviewing an old version.\n\nBut I do know that it lands in your inbox each time there is a new\npatch. So I will try to cut down on the amount of patches from now\none, since they are not free in terms of yours and other time. Sounds\nok?\n\n\nHarald\n"},{"id":"543635","messageId":"xmqq1pf77kml.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v11.git.git.1779177508772.gitgitgadget@gmail.com","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-19T10:34:58Z","receivedAt":"2026-05-19T10:35:03Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n>     checkout: --track=fetch\n>     \n>      * Find the right remote by checking which remote's fetch refspec maps\n>        to the user's start-point, instead of assuming the start-point begins\n>        with the remote's name. This fixes cases where the user has a custom\n>        refspec mapping into a namespace whose name differs from the remote\n>        (e.g. fetching from origin into refs/remotes/upstream/*).\n\nThis comment is even before looking at the patch text.  After\ngetting one issue pointed out, I'd expect you to think about related\nissues before sending a new round out.\n\nOne.  Have you considered the case where the remote-tracking refs\nare overlapping, e.g., where \"origin\" and \"upstream\" point at\ndifferent URLs but they both store in \"refs/remotes/upstream/*\"?\nPerhaps their URLs may textually be different but are pointing\nlogically at the same place (e.g., one ssh:// the other https:// for\nexample).\n\nWhat should happen?  What does happen after you apply this patch?\n\n>      * For a bare namespace name, follow <namespace>/HEAD first to figure\n>        out which branch to fetch.\n\nWhat should happen if HEAD does not exist?  What does happen after\nyou apply this patch?\n\nThanks.\n"},{"id":"543812","messageId":"b8932b27-8006-4b43-b7e5-1fac0fbf42c7@gmail.com","threadId":"65546","inReplyTo":"xmqq1pf77kml.fsf@gitster.g","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-05-21T09:49:13Z","receivedAt":"2026-05-21T09:49:19Z","isPatch":true,"body":"On 19/05/2026 11:34, Junio C Hamano wrote:\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> \n>>      checkout: --track=fetch\n>>      \n>>       * Find the right remote by checking which remote's fetch refspec maps\n>>         to the user's start-point, instead of assuming the start-point begins\n>>         with the remote's name. This fixes cases where the user has a custom\n>>         refspec mapping into a namespace whose name differs from the remote\n>>         (e.g. fetching from origin into refs/remotes/upstream/*).\n> \n> This comment is even before looking at the patch text.  After\n> getting one issue pointed out, I'd expect you to think about related\n> issues before sending a new round out.\n> \n> One.  Have you considered the case where the remote-tracking refs\n> are overlapping, e.g., where \"origin\" and \"upstream\" point at\n> different URLs but they both store in \"refs/remotes/upstream/*\"?\n> Perhaps their URLs may textually be different but are pointing\n> logically at the same place (e.g., one ssh:// the other https:// for\n> example).\n> \n> What should happen?  What does happen after you apply this patch?\n\nIt would be worth looking at what \"git checkout --track\" does in that \ncase and seeing if we can share the code.\n\nThanks\n\nPhillip\n\n> \n>>       * For a bare namespace name, follow <namespace>/HEAD first to figure\n>>         out which branch to fetch.\n> \n> What should happen if HEAD does not exist?  What does happen after\n> you apply this patch?\n> \n> Thanks.\n> \n\n"},{"id":"543814","messageId":"pull.2281.v12.git.git.1779358803652.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v11.git.git.1779177508772.gitgitgadget@gmail.com","subject":"[PATCH v12] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-21T10:20:03Z","receivedAt":"2026-05-21T10:20:08Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\nswitch\" that refreshes <start-point> before checking it out:\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nis shorthand for\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nIdentify the remote whose configured fetch refspec maps to\n<start-point>, then run \"git fetch <remote> <src-ref>\" for just that\nref so other remote-tracking branches are left untouched. When\n<start-point> is a bare <remote> (e.g. \"origin\"), follow\nrefs/remotes/<remote>/HEAD to learn which branch to refresh. If\n\"git fetch\" fails but the remote-tracking ref already exists locally,\nwarn and proceed from the existing tip; otherwise abort.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    checkout: --track=fetch\n    \n     * Find the right remote by matching fetch refspecs. Instead of assuming\n       the start-point begins with a remote's name, ask each configured\n       remote whether its fetch refspec maps to refs/remotes/<start-point>.\n     * Die clearly when no remote matches. Previously, if we couldn't figure\n       out the remote we'd silently skip the fetch and fall through to a\n       confusing later error.\n     * Die clearly when more than one remote matches. If two remotes both\n       map their fetches into the same refs/remotes/<ns>/* namespace, there\n       is no unambiguous choice.\n     * Bare-namespace form (--track=fetch origin) requires <ns>/HEAD. When\n       the user passes just a namespace, we now follow\n       refs/remotes/<ns>/HEAD to learn which branch to refresh. If that\n       symref is missing, die with a hint to run git remote set-head <ns>\n       --auto instead of guessing or fetching everything. If <ns>/HEAD\n       points outside the namespace, reject it.\n     * Validate the refname before doing anything. Reject obviously invalid\n       start-points like foo..bar up front, so we don't run a fetch we know\n       cannot succeed.\n     * Forward --quiet to the underlying fetch. checkout -q --track=fetch\n       ... now suppresses the fetch progress output, matching the user's\n       intent.\n     * More tests coverage.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v12\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v12\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v11:\n\n 1:  d0c9e3e879 ! 1:  bcd034dbed checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Metadata\n       ## Commit message ##\n          checkout: extend --track with a \"fetch\" mode to refresh start-point\n      \n     -    If you want to fork your topic branch from the very latest of the\n     -    tip of a branch your remote has, you would do:\n     -\n     -        git fetch origin some-branch\n     -        git checkout -b new_branch --track origin/some-branch\n     -\n     -    Extend the \"--track\" option of \"git checkout\" and allow users to\n     -    write\n     +    Add a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\n     +    switch\" that refreshes <start-point> before checking it out:\n      \n              git checkout -b new_branch --track=fetch origin/some-branch\n      \n     -    to (1) fetch 'some-branch' from the remote 'origin', updating the\n     -    remote-tracking branch 'origin/some-branch', (2) arrange subsequent\n     -    'git pull' on 'new_branch' to interact with 'origin/some-branch' and\n     -    (3) fork 'new_branch' from it.\n     -\n     -    In the value of the '--track' option, 'fetch' can be combined with\n     -    the existing 'direct' (default) and 'inherit' modes via a\n     -    comma-separated list. Examples:\n     +    is shorthand for\n      \n     -        git checkout -b new_branch --track=fetch,inherit some_local_branch\n     -        git switch -c new_branch --track=fetch origin/some-branch\n     +        git fetch origin some-branch\n     +        git checkout -b new_branch --track origin/some-branch\n      \n     -    When \"fetch\" is requested and <start-point> is in <remote>/<branch>\n     -    form, run \"git fetch <remote> <branch>\" before resolving the ref, so\n     -    that other remote-tracking branches are left untouched. If\n     -    <start-point> is a bare remote name like \"origin\" (which resolves to\n     -    that remote's default branch), \"git fetch <remote>\" is run instead,\n     -    since the target branch is not known up front. Abort the checkout if\n     -    the fetch fails.\n     +    Identify the remote whose configured fetch refspec maps to\n     +    <start-point>, then run \"git fetch <remote> <src-ref>\" for just that\n     +    ref so other remote-tracking branches are left untouched. When\n     +    <start-point> is a bare <remote> (e.g. \"origin\"), follow\n     +    refs/remotes/<remote>/HEAD to learn which branch to refresh. If\n     +    \"git fetch\" fails but the remote-tracking ref already exists locally,\n     +    warn and proceed from the existing tip; otherwise abort.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/git-checkout.adoc: of it\").\n      +`fetch` requests that the remote be fetched before _<start-point>_ is\n      +resolved, so the new branch starts from a fresh tip: when\n      +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n     -+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n     -+only the remote's default branch is updated. If the fetch fails and the\n     ++updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n     ++branch named by _<remote>/HEAD_ is updated, and the checkout fails\n     ++with a hint to configure that symref if it is not set. The checkout\n     ++also fails if no configured remote's fetch refspec maps to\n     ++_<start-point>_, or if more than one does (in which case the `fetch`\n     ++cannot be unambiguously routed). If the fetch itself fails and the\n      +corresponding remote-tracking ref already exists, a warning is printed\n      +and the checkout proceeds from the existing tip; otherwise the checkout\n      +is aborted.\n     @@ Documentation/git-switch.adoc: variable.\n      +`--track[=(direct|inherit|fetch)[,...]]`::\n       \tWhen creating a new branch, set up \"upstream\" configuration.\n       \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n     - \tdetails.\n     +-\tdetails.\n     ++\tdetails, and `--track` in linkgit:git-checkout[1] for the\n     ++\t`fetch` mode.\n       +\n     -+The argument is a comma-separated list. `direct` (the default) and\n     -+`inherit` select the tracking mode and are mutually exclusive. Adding\n     -+`fetch` requests that the remote be fetched before _<start-point>_ is\n     -+resolved, so the new branch starts from a fresh tip: when\n     -+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n     -+updated; when _<start-point>_ is a bare remote name (e.g. `origin`),\n     -+only the remote's default branch is updated. If the fetch fails and the\n     -+corresponding remote-tracking ref already exists, a warning is printed\n     -+and the switch proceeds from the existing tip; otherwise the switch is\n     -+aborted.\n     -++\n       If no `-c` option is given, the name of the new branch will be derived\n       from the remote-tracking branch, by looking at the local part of the\n     - refspec configured for the corresponding remote, and then stripping\n      \n       ## builtin/checkout.c ##\n      @@\n     @@ builtin/checkout.c: struct branch_info {\n       };\n       \n      +struct fetch_target_cb {\n     -+\tstruct refspec_item query;\n     -+\tconst char *remote_name;\n     -+\tint matches;\n     ++\tchar *dst;\n     ++\tstruct string_list matches;\n      +};\n      +\n      +static int match_fetch_target(struct remote *remote, void *priv)\n      +{\n      +\tstruct fetch_target_cb *cb = priv;\n     -+\tstruct refspec_item q = { .dst = cb->query.dst };\n     -+\n     -+\tif (!remote_find_tracking(remote, &q) && q.src) {\n     -+\t\tif (++cb->matches == 1) {\n     -+\t\t\tcb->remote_name = remote->name;\n     -+\t\t\tfree(cb->query.src);\n     -+\t\t\tcb->query.src = q.src;\n     -+\t\t} else {\n     -+\t\t\tfree(q.src);\n     -+\t\t}\n     -+\t}\n     ++\tstruct refspec_item q = { .dst = cb->dst };\n     ++\n     ++\tif (!remote_find_tracking(remote, &q) && q.src)\n     ++\t\tstring_list_append(&cb->matches, remote->name)->util = q.src;\n      +\treturn 0;\n      +}\n      +\n     -+static int resolve_fetch_target(const char *arg, char **remote_out,\n     -+\t\t\t\tchar **src_ref_out, char **existing_ref_out)\n     ++static void fetch_remote_for_start_point(const char *arg, int quiet)\n      +{\n      +\tstruct strbuf dst = STRBUF_INIT;\n     -+\tstruct strbuf head_path = STRBUF_INIT;\n     -+\tstruct fetch_target_cb cb = { 0 };\n     ++\tstruct fetch_target_cb cb = { .matches = STRING_LIST_INIT_NODUP };\n     ++\tstruct child_process cmd = CHILD_PROCESS_INIT;\n      +\tstruct object_id oid;\n     -+\tconst char *head_target;\n     -+\n     -+\t*remote_out = NULL;\n     -+\t*src_ref_out = NULL;\n     -+\t*existing_ref_out = NULL;\n     -+\n     -+\tif (!arg || !*arg)\n     -+\t\treturn -1;\n     -+\n     -+\tstrbuf_addf(&head_path, \"refs/remotes/%s/HEAD\", arg);\n     -+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n     -+\t\t\t\t\t      head_path.buf,\n     -+\t\t\t\t\t      RESOLVE_REF_READING |\n     -+\t\t\t\t\t      RESOLVE_REF_NO_RECURSE,\n     -+\t\t\t\t\t      &oid, NULL);\n     -+\tif (head_target)\n     -+\t\tstrbuf_addstr(&dst, head_target);\n     -+\telse\n     -+\t\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n     -+\n     -+\tcb.query.dst = dst.buf;\n     -+\tfor_each_remote(match_fetch_target, &cb);\n     -+\n     -+\tif (cb.matches != 1) {\n     -+\t\tfree(cb.query.src);\n     -+\t\tstrbuf_release(&dst);\n     -+\t\tstrbuf_release(&head_path);\n     -+\t\treturn -1;\n     ++\tstruct remote *named_remote;\n     ++\tint bare_ns;\n     ++\tsize_t i;\n     ++\n     ++\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n     ++\tif (check_refname_format(dst.buf, 0))\n     ++\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n     ++\t\t      \"remote-tracking name\"), arg);\n     ++\n     ++\tnamed_remote = remote_get(arg);\n     ++\tbare_ns = !strchr(arg, '/') ||\n     ++\t\t(named_remote && remote_is_configured(named_remote, 1));\n     ++\tif (bare_ns) {\n     ++\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n     ++\t\tconst char *head_target =\n     ++\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n     ++\t\t\t\t\t\thead_path,\n     ++\t\t\t\t\t\tRESOLVE_REF_READING |\n     ++\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n     ++\t\t\t\t\t\t&oid, NULL);\n     ++\t\tif (head_target &&\n     ++\t\t    starts_with(head_target, dst.buf) &&\n     ++\t\t    head_target[dst.len] == '/' &&\n     ++\t\t    !check_refname_format(head_target, 0)) {\n     ++\t\t\tstrbuf_reset(&dst);\n     ++\t\t\tstrbuf_addstr(&dst, head_target);\n     ++\t\t\tbare_ns = 0;\n     ++\t\t}\n     ++\t\tfree(head_path);\n      +\t}\n      +\n     -+\t*remote_out = xstrdup(cb.remote_name);\n     -+\t*src_ref_out = cb.query.src;\n     -+\tif (head_target)\n     -+\t\t*existing_ref_out = strbuf_detach(&head_path, NULL);\n     -+\telse if (!refs_read_ref(get_main_ref_store(the_repository),\n     -+\t\t\t\tdst.buf, &oid))\n     -+\t\t*existing_ref_out = strbuf_detach(&dst, NULL);\n     -+\n     -+\tstrbuf_release(&dst);\n     -+\tstrbuf_release(&head_path);\n     -+\treturn 0;\n     -+}\n     ++\tcb.dst = dst.buf;\n     ++\tfor_each_remote(match_fetch_target, &cb);\n      +\n     -+static void fetch_remote_for_start_point(const char *arg)\n     -+{\n     -+\tchar *remote_name = NULL;\n     -+\tchar *src_ref = NULL;\n     -+\tchar *existing_ref = NULL;\n     -+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n     ++\tif (cb.matches.nr > 1) {\n     ++\t\tstruct strbuf msg = STRBUF_INIT;\n     ++\n     ++\t\tstrbuf_addf(&msg,\n     ++\t\t\t    _(\"cannot fetch start-point '%s': fetch refspecs \"\n     ++\t\t\t      \"of multiple remotes map to the same destination:\"),\n     ++\t\t\t    arg);\n     ++\t\tfor (i = 0; i < cb.matches.nr; i++)\n     ++\t\t\tstrbuf_addf(&msg, \"\\n  %s\", cb.matches.items[i].string);\n     ++\t\tstrbuf_addstr(&msg,\n     ++\t\t\t      _(\"\\nadjust 'remote.<name>.fetch' so only one \"\n     ++\t\t\t\t\"remote maps there, or omit '=fetch'\"));\n     ++\t\tdie(\"%s\", msg.buf);\n     ++\t}\n      +\n     -+\tif (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref))\n     -+\t\treturn;\n     ++\tif (!cb.matches.nr) {\n     ++\t\tif (bare_ns && named_remote &&\n     ++\t\t    remote_is_configured(named_remote, 1))\n     ++\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n     ++\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n     ++\t\t\t      \"'git remote set-head %s --auto' to set it\"),\n     ++\t\t\t    arg, arg, arg);\n     ++\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n     ++\t\t      \"fetch refspec matches it\"), arg);\n     ++\t}\n      +\n     -+\tstrvec_pushl(&cmd.args, \"fetch\", remote_name, NULL);\n     -+\tif (src_ref)\n     -+\t\tstrvec_push(&cmd.args, src_ref);\n     ++\tstrvec_push(&cmd.args, \"fetch\");\n     ++\tif (quiet)\n     ++\t\tstrvec_push(&cmd.args, \"--quiet\");\n     ++\tstrvec_pushl(&cmd.args, cb.matches.items[0].string,\n     ++\t\t     (char *)cb.matches.items[0].util, NULL);\n      +\tcmd.git_cmd = 1;\n      +\tif (run_command(&cmd)) {\n     -+\t\tif (existing_ref)\n     ++\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     ++\t\t\t\t   dst.buf, &oid))\n      +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n     -+\t\t\t\t  \"using existing '%s'\"),\n     -+\t\t\t\targ, existing_ref);\n     ++\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n      +\t\telse\n      +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n      +\t}\n      +\n     -+\tfree(remote_name);\n     -+\tfree(src_ref);\n     -+\tfree(existing_ref);\n     ++\tfor (i = 0; i < cb.matches.nr; i++)\n     ++\t\tfree(cb.matches.items[i].util);\n     ++\tstring_list_clear(&cb.matches, 0);\n     ++\tstrbuf_release(&dst);\n      +}\n      +\n      +static int parse_opt_checkout_track(const struct option *opt,\n     @@ builtin/checkout.c: struct branch_info {\n      +\tstruct checkout_opts *opts = opt->value;\n      +\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n      +\tstruct string_list_item *item;\n     -+\tint saw_direct = 0, saw_inherit = 0;\n     ++\tint saw_direct = 0;\n      +\tint ret = 0;\n      +\n      +\topts->fetch = 0;\n     -+\n      +\tif (unset) {\n      +\t\topts->track = BRANCH_TRACK_NEVER;\n      +\t\treturn 0;\n      +\t}\n     -+\n      +\topts->track = BRANCH_TRACK_EXPLICIT;\n      +\tif (!arg)\n      +\t\treturn 0;\n      +\n      +\tstring_list_split(&tokens, arg, \",\", -1);\n      +\tfor_each_string_list_item(item, &tokens) {\n     -+\t\tif (!strcmp(item->string, \"fetch\")) {\n     ++\t\tif (!strcmp(item->string, \"fetch\"))\n      +\t\t\topts->fetch = 1;\n     -+\t\t} else if (!strcmp(item->string, \"direct\")) {\n     ++\t\telse if (!strcmp(item->string, \"direct\"))\n      +\t\t\tsaw_direct = 1;\n     -+\t\t\topts->track = BRANCH_TRACK_EXPLICIT;\n     -+\t\t} else if (!strcmp(item->string, \"inherit\")) {\n     -+\t\t\tsaw_inherit = 1;\n     ++\t\telse if (!strcmp(item->string, \"inherit\"))\n      +\t\t\topts->track = BRANCH_TRACK_INHERIT;\n     -+\t\t} else {\n     ++\t\telse {\n      +\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n      +\t\t\t\t      \"or \\\"%s\\\"\"),\n      +\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n      +\t\t\tgoto out;\n      +\t\t}\n      +\t}\n     -+\n     -+\tif (saw_direct && saw_inherit)\n     ++\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n      +\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n      +\t\t\t    \"--track\", \"direct\", \"inherit\");\n     -+\n      +out:\n      +\tstring_list_clear(&tokens, 0);\n      +\treturn ret;\n     @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const\n      +\t\tint n;\n      +\n      +\t\tif (opts->fetch)\n     -+\t\t\tfetch_remote_for_start_point(argv[0]);\n     ++\t\t\tfetch_remote_for_start_point(argv[0], opts->quiet);\n      +\n      +\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n      +\t\t\t\t\t &new_branch_info, opts, &rev);\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\tgit checkout main &&\n      +\tgit remote add fetch_ns ./fetch_upstream &&\n      +\ttest_when_finished \"git remote remove fetch_ns\" &&\n     ++\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n      +\tgit config --replace-all remote.fetch_ns.fetch \\\n      +\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n      +\tgit fetch fetch_ns &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_config refs/heads/main branch.local_ns.merge\n      +'\n      +\n     ++test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n     ++\tgit checkout main &&\n     ++\tgit remote add nested/bare ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove nested/bare\" &&\n     ++\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n     ++\tgit fetch nested/bare &&\n     ++\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n     ++\t\trefs/remotes/nested/bare/main &&\n     ++\tgit -C fetch_upstream checkout main &&\n     ++\ttest_commit -C fetch_upstream u_nested_bare_post &&\n     ++\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n     ++\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n     ++'\n     ++\n      +test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n      +\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_hier &&\n     -+\ttest_commit -C fetch_upstream u_hier &&\n      +\tgit remote add nested/remote ./fetch_upstream &&\n      +\ttest_when_finished \"git remote remove nested/remote\" &&\n     -+\tgit fetch nested/remote fetch_hier &&\n     -+\ttest_commit -C fetch_upstream u_hier_post &&\n     ++\tgit -C fetch_upstream checkout -b fetch_hier &&\n     ++\ttest_commit -C fetch_upstream u_hier &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n      +\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n      +\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n      +'\n      +\n     ++test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n     ++\tgit checkout main &&\n     ++\tgit remote add fetch_nohead ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove fetch_nohead\" &&\n     ++\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n     ++\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n     ++\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n     ++\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n     ++\tgit checkout main &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n     ++\ttest_must_fail git config --get remote.no_such_ns.url &&\n     ++\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n     ++\ttest_grep \"no configured remote\" err &&\n     ++\ttest_grep ! \"set-head\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n     ++\tgit checkout main &&\n     ++\tgit remote add fetch_crossns ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove fetch_crossns\" &&\n     ++\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n     ++\tgit fetch fetch_crossns &&\n     ++\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n     ++\t\trefs/remotes/fetch_upstream/u_main &&\n     ++\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n     ++\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n     ++\tgit checkout main &&\n     ++\tgit remote add fetch_ambig_a ./fetch_upstream &&\n     ++\tgit remote add fetch_ambig_b ./fetch_upstream &&\n     ++\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n     ++\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n     ++\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n     ++\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n     ++\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n     ++\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n     ++\tgit -C fetch_upstream checkout -b fetch_ambig &&\n     ++\ttest_commit -C fetch_upstream u_ambig &&\n     ++\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n     ++\ttest_grep \"fetch_ambig_a\" err &&\n     ++\ttest_grep \"fetch_ambig_b\" err &&\n     ++\ttest_grep \"remote.<name>.fetch\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n     ++\tgit checkout main &&\n     ++\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n     ++\ttest_grep \"valid\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n     ++\tgit checkout main &&\n     ++\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n     ++\t\t\"foo..bar\" 2>err &&\n     ++\ttest_grep \"valid\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n     ++'\n     ++\n      +test_expect_success 'checkout --track=inherit,direct is rejected' '\n      +\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n      +\ttest_grep \"cannot combine\" err\n      +'\n      +\n     ++test_expect_success 'checkout --track=direct,inherit is rejected' '\n     ++\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n     ++\ttest_grep \"cannot combine\" err\n     ++'\n     ++\n      +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch,inherit fetches and inherits' '\n     ++test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_notrack &&\n     ++\ttest_commit -C fetch_upstream u_notrack &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n     ++\ttest_must_fail git checkout --track=fetch --no-track \\\n     ++\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_inherit &&\n      +\ttest_commit -C fetch_upstream u_inherit &&\n     -+\tgit fetch fetch_upstream fetch_inherit &&\n     -+\tgit checkout -b base_inherit fetch_upstream/fetch_inherit &&\n     -+\ttest_commit -C fetch_upstream u_inherit2 &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n     ++\tgit checkout --track=fetch,inherit -b local_inherit \\\n     ++\t\tfetch_upstream/fetch_inherit &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n      +\tgit checkout main &&\n     -+\tgit checkout --track=fetch,inherit -b local_inherit base_inherit &&\n     -+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD &&\n     -+\ttest_cmp_config fetch_upstream branch.local_inherit.remote &&\n     -+\ttest_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge\n     ++\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n     ++\ttest_grep \"no configured remote\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/bad\n     ++'\n     ++\n     ++test_expect_success 'checkout --track=fetch on local start-point errors' '\n     ++\tgit checkout main &&\n     ++\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n     ++\ttest_grep \"no configured remote\" err &&\n     ++\ttest_must_fail git rev-parse --verify refs/heads/bad\n      +'\n      +\n      +test_expect_success 'checkout --track=bogus reports an error' '\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_grep \"expects\" err\n      +'\n      +\n     ++test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n     ++\tgit checkout main &&\n     ++\tgit -C fetch_upstream checkout -b fetch_quiet &&\n     ++\ttest_commit -C fetch_upstream u_quiet &&\n     ++\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n     ++\tgit checkout -q --track=fetch -b local_quiet \\\n     ++\t\tfetch_upstream/fetch_quiet 2>err &&\n     ++\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n     ++'\n     ++\n      +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_switch &&\n\n\n Documentation/git-checkout.adoc |  17 +-\n Documentation/git-switch.adoc   |   5 +-\n builtin/checkout.c              | 159 +++++++++++++++++-\n t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n 4 files changed, 450 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..20b6cae60e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,26 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n+branch named by _<remote>/HEAD_ is updated, and the checkout fails\n+with a hint to configure that symref if it is not set. The checkout\n+also fails if no configured remote's fetch refspec maps to\n+_<start-point>_, or if more than one does (in which case the `fetch`\n+cannot be unambiguously routed). If the fetch itself fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..a8730b1da8 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,10 +155,11 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n-\tdetails.\n+\tdetails, and `--track` in linkgit:git-checkout[1] for the\n+\t`fetch` mode.\n +\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 1345e8574a..9c5c4f1c2e 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -115,6 +118,149 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+struct fetch_target_cb {\n+\tchar *dst;\n+\tstruct string_list matches;\n+};\n+\n+static int match_fetch_target(struct remote *remote, void *priv)\n+{\n+\tstruct fetch_target_cb *cb = priv;\n+\tstruct refspec_item q = { .dst = cb->dst };\n+\n+\tif (!remote_find_tracking(remote, &q) && q.src)\n+\t\tstring_list_append(&cb->matches, remote->name)->util = q.src;\n+\treturn 0;\n+}\n+\n+static void fetch_remote_for_start_point(const char *arg, int quiet)\n+{\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct fetch_target_cb cb = { .matches = STRING_LIST_INIT_NODUP };\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct object_id oid;\n+\tstruct remote *named_remote;\n+\tint bare_ns;\n+\tsize_t i;\n+\n+\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\tif (check_refname_format(dst.buf, 0))\n+\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n+\t\t      \"remote-tracking name\"), arg);\n+\n+\tnamed_remote = remote_get(arg);\n+\tbare_ns = !strchr(arg, '/') ||\n+\t\t(named_remote && remote_is_configured(named_remote, 1));\n+\tif (bare_ns) {\n+\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n+\t\tconst char *head_target =\n+\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\thead_path,\n+\t\t\t\t\t\tRESOLVE_REF_READING |\n+\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t&oid, NULL);\n+\t\tif (head_target &&\n+\t\t    starts_with(head_target, dst.buf) &&\n+\t\t    head_target[dst.len] == '/' &&\n+\t\t    !check_refname_format(head_target, 0)) {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addstr(&dst, head_target);\n+\t\t\tbare_ns = 0;\n+\t\t}\n+\t\tfree(head_path);\n+\t}\n+\n+\tcb.dst = dst.buf;\n+\tfor_each_remote(match_fetch_target, &cb);\n+\n+\tif (cb.matches.nr > 1) {\n+\t\tstruct strbuf msg = STRBUF_INIT;\n+\n+\t\tstrbuf_addf(&msg,\n+\t\t\t    _(\"cannot fetch start-point '%s': fetch refspecs \"\n+\t\t\t      \"of multiple remotes map to the same destination:\"),\n+\t\t\t    arg);\n+\t\tfor (i = 0; i < cb.matches.nr; i++)\n+\t\t\tstrbuf_addf(&msg, \"\\n  %s\", cb.matches.items[i].string);\n+\t\tstrbuf_addstr(&msg,\n+\t\t\t      _(\"\\nadjust 'remote.<name>.fetch' so only one \"\n+\t\t\t\t\"remote maps there, or omit '=fetch'\"));\n+\t\tdie(\"%s\", msg.buf);\n+\t}\n+\n+\tif (!cb.matches.nr) {\n+\t\tif (bare_ns && named_remote &&\n+\t\t    remote_is_configured(named_remote, 1))\n+\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n+\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n+\t\t\t      \"'git remote set-head %s --auto' to set it\"),\n+\t\t\t    arg, arg, arg);\n+\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n+\t\t      \"fetch refspec matches it\"), arg);\n+\t}\n+\n+\tstrvec_push(&cmd.args, \"fetch\");\n+\tif (quiet)\n+\t\tstrvec_push(&cmd.args, \"--quiet\");\n+\tstrvec_pushl(&cmd.args, cb.matches.items[0].string,\n+\t\t     (char *)cb.matches.items[0].util, NULL);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t   dst.buf, &oid))\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tfor (i = 0; i < cb.matches.nr; i++)\n+\t\tfree(cb.matches.items[i].util);\n+\tstring_list_clear(&cb.matches, 0);\n+\tstrbuf_release(&dst);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\"))\n+\t\t\topts->fetch = 1;\n+\t\telse if (!strcmp(item->string, \"direct\"))\n+\t\t\tsaw_direct = 1;\n+\t\telse if (!strcmp(item->string, \"inherit\"))\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\telse {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1733,10 +1879,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1941,8 +2087,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0], opts->quiet);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..c4a165cb1d 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,280 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n+\tgit config --replace-all remote.fetch_ns.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n+\tgit fetch fetch_ns &&\n+\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_ns_post &&\n+\tgit checkout --track=fetch -b local_ns ns_alias &&\n+\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n+\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n+\ttest_cmp_config refs/heads/main branch.local_ns.merge\n+'\n+\n+test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add nested/bare ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/bare\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n+\tgit fetch nested/bare &&\n+\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n+\t\trefs/remotes/nested/bare/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_nested_bare_post &&\n+\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n+\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_nohead ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_nohead\" &&\n+\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n+\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n+\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n+\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n+'\n+\n+test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n+\ttest_must_fail git config --get remote.no_such_ns.url &&\n+\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_grep ! \"set-head\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n+\tgit checkout main &&\n+\tgit remote add fetch_crossns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_crossns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n+\tgit fetch fetch_crossns &&\n+\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n+\t\trefs/remotes/fetch_upstream/u_main &&\n+\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n+\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ambig_a ./fetch_upstream &&\n+\tgit remote add fetch_ambig_b ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n+\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n+\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_ambig &&\n+\ttest_commit -C fetch_upstream u_ambig &&\n+\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n+\ttest_grep \"fetch_ambig_a\" err &&\n+\ttest_grep \"fetch_ambig_b\" err &&\n+\ttest_grep \"remote.<name>.fetch\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n+\t\t\"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=direct,inherit is rejected' '\n+\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_notrack &&\n+\ttest_commit -C fetch_upstream u_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git checkout --track=fetch --no-track \\\n+\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n+\tgit checkout --track=fetch,inherit -b local_inherit \\\n+\t\tfetch_upstream/fetch_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=fetch on local start-point errors' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_quiet &&\n+\ttest_commit -C fetch_upstream u_quiet &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n+\tgit checkout -q --track=fetch -b local_quiet \\\n+\t\tfetch_upstream/fetch_quiet 2>err &&\n+\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n\nbase-commit: aec3f587505a472db67e9462d0702e7d463a449d\n-- \ngitgitgadget\n"},{"id":"543815","messageId":"CAHwyqnWeqQEoTM=pum0_Ui6+=cXdxFJgWEWxNWoFpFo8xaW8eg@mail.gmail.com","threadId":"65546","inReplyTo":"b8932b27-8006-4b43-b7e5-1fac0fbf42c7@gmail.com","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-05-21T10:24:43Z","receivedAt":"2026-05-21T10:25:22Z","isPatch":true,"body":"Interesting idea! I will think about it.\n\n\nHarald\n\nOn Thu, May 21, 2026 at 11:49 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> On 19/05/2026 11:34, Junio C Hamano wrote:\n> > \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> >\n> >>      checkout: --track=fetch\n> >>\n> >>       * Find the right remote by checking which remote's fetch refspec maps\n> >>         to the user's start-point, instead of assuming the start-point begins\n> >>         with the remote's name. This fixes cases where the user has a custom\n> >>         refspec mapping into a namespace whose name differs from the remote\n> >>         (e.g. fetching from origin into refs/remotes/upstream/*).\n> >\n> > This comment is even before looking at the patch text.  After\n> > getting one issue pointed out, I'd expect you to think about related\n> > issues before sending a new round out.\n> >\n> > One.  Have you considered the case where the remote-tracking refs\n> > are overlapping, e.g., where \"origin\" and \"upstream\" point at\n> > different URLs but they both store in \"refs/remotes/upstream/*\"?\n> > Perhaps their URLs may textually be different but are pointing\n> > logically at the same place (e.g., one ssh:// the other https:// for\n> > example).\n> >\n> > What should happen?  What does happen after you apply this patch?\n>\n> It would be worth looking at what \"git checkout --track\" does in that\n> case and seeing if we can share the code.\n>\n> Thanks\n>\n> Phillip\n>\n> >\n> >>       * For a bare namespace name, follow <namespace>/HEAD first to figure\n> >>         out which branch to fetch.\n> >\n> > What should happen if HEAD does not exist?  What does happen after\n> > you apply this patch?\n> >\n> > Thanks.\n> >\n>\n"},{"id":"543825","messageId":"xmqqtss02a2o.fsf@gitster.g","threadId":"65546","inReplyTo":"b8932b27-8006-4b43-b7e5-1fac0fbf42c7@gmail.com","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-21T12:58:39Z","receivedAt":"2026-05-21T12:58:43Z","isPatch":true,"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> One.  Have you considered the case where the remote-tracking refs\n>> are overlapping, e.g., where \"origin\" and \"upstream\" point at\n>> different URLs but they both store in \"refs/remotes/upstream/*\"?\n>> Perhaps their URLs may textually be different but are pointing\n>> logically at the same place (e.g., one ssh:// the other https:// for\n>> example).\n>> \n>> What should happen?  What does happen after you apply this patch?\n>\n> It would be worth looking at what \"git checkout --track\" does in that \n> case and seeing if we can share the code.\n\nIt always is a good idea to think how we can share code for\ndifferent purposes to solve a new problem, but in this particular\none, I am not sure if \"git checkout -t -b topic upstream/main\"\ncodepath has much to offer to solve what the new \"before the\ncheckout, update from the remote\" feature wants to do.  To the\nformer, it does not matter how refs/remotes/upstream/* are updated\nand by fetching which remote at all.  The only thing it cares about\nis to leave the record that this new \"topic\" branch works with\nrefs/remotes/upstrea/main.  But the latter needs to be able to\ncompute which remote it should fetch from.  It is a problem that\nexisting code had no need to solve.\n\n"},{"id":"543831","messageId":"01526f43-86aa-466f-a1e8-054284e1a2e1@gmail.com","threadId":"65546","inReplyTo":"xmqqtss02a2o.fsf@gitster.g","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-05-21T14:06:35Z","receivedAt":"2026-05-21T14:06:43Z","isPatch":true,"body":"On 21/05/2026 13:58, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> \n>>> One.  Have you considered the case where the remote-tracking refs\n>>> are overlapping, e.g., where \"origin\" and \"upstream\" point at\n>>> different URLs but they both store in \"refs/remotes/upstream/*\"?\n>>> Perhaps their URLs may textually be different but are pointing\n>>> logically at the same place (e.g., one ssh:// the other https:// for\n>>> example).\n>>>\n>>> What should happen?  What does happen after you apply this patch?\n>>\n>> It would be worth looking at what \"git checkout --track\" does in that\n>> case and seeing if we can share the code.\n> \n> It always is a good idea to think how we can share code for\n> different purposes to solve a new problem, but in this particular\n> one, I am not sure if \"git checkout -t -b topic upstream/main\"\n> codepath has much to offer to solve what the new \"before the\n> checkout, update from the remote\" feature wants to do.  To the\n> former, it does not matter how refs/remotes/upstream/* are updated\n> and by fetching which remote at all.\n\nDon't we want to avoid creating a branch with an ambiguous upstream so \nthat a subsequent \"git pull\" works though? Looking at \nbranch.c:setup_tracking() it seems to reject upstream branches that \nmatch more than one remote.\n\nThanks\n\nPhillip\n\n>  The only thing it cares about\n> is to leave the record that this new \"topic\" branch works with\n> refs/remotes/upstrea/main.  But the latter needs to be able to\n> compute which remote it should fetch from.  It is a problem that\n> existing code had no need to solve.\n> \n\n"},{"id":"543863","messageId":"xmqqzf1sxqu4.fsf@gitster.g","threadId":"65546","inReplyTo":"01526f43-86aa-466f-a1e8-054284e1a2e1@gmail.com","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-21T23:53:07Z","receivedAt":"2026-05-21T23:53:14Z","isPatch":true,"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n> Don't we want to avoid creating a branch with an ambiguous upstream so \n> that a subsequent \"git pull\" works though? Looking at \n> branch.c:setup_tracking() it seems to reject upstream branches that \n> match more than one remote.\n\nYes.  We may try to avoid creating misconfigured branches in the\nfirst place, but we'd want to make sure other parts of the system\nthat consumes existing branches to behave as sensible as possible,\nno?\n\nThanks.\n"},{"id":"543985","messageId":"pull.2281.v13.git.git.1779565714.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v12.git.git.1779358803652.gitgitgadget@gmail.com","subject":"[PATCH v13 0/2] checkout: --track=fetch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-23T19:48:32Z","receivedAt":"2026-05-23T19:48:37Z","isPatch":true,"body":" * Create a preparatory commit that exposes find_tracking_remote_for_ref()\n   and advise_ambiguous_fetch_refspec() from branch.c, so checkout can reuse\n   the same lookup git branch --track uses.\n * Use advise_ambiguous_fetch_refspec() for the \"multiple remotes match\"\n   case, so the wording matches git branch --track.\n\nHarald Nordgren (2):\n  branch: expose helpers for finding the remote owning a tracking ref\n  checkout: extend --track with a \"fetch\" mode to refresh start-point\n\n Documentation/git-checkout.adoc |  17 +-\n Documentation/git-switch.adoc   |   5 +-\n branch.c                        |  96 ++++++-----\n branch.h                        |  16 ++\n builtin/checkout.c              | 139 +++++++++++++++-\n t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n 6 files changed, 498 insertions(+), 51 deletions(-)\n\n\nbase-commit: aec3f587505a472db67e9462d0702e7d463a449d\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v13\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v13\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v12:\n\n -:  ---------- > 1:  2369afad24 branch: expose helpers for finding the remote owning a tracking ref\n 1:  bcd034dbed ! 2:  60adf0e67d checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Commit message\n              git checkout -b new_branch --track origin/some-branch\n      \n          Identify the remote whose configured fetch refspec maps to\n     -    <start-point>, then run \"git fetch <remote> <src-ref>\" for just that\n     -    ref so other remote-tracking branches are left untouched. When\n     -    <start-point> is a bare <remote> (e.g. \"origin\"), follow\n     +    <start-point> using find_tracking_remote_for_ref() (the same lookup\n     +    \"--track\" uses to pick which remote to record in\n     +    branch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\n     +    just that ref so other remote-tracking branches are left untouched.\n     +    When <start-point> is a bare <remote> (e.g. \"origin\"), follow\n          refs/remotes/<remote>/HEAD to learn which branch to refresh. If\n          \"git fetch\" fails but the remote-tracking ref already exists locally,\n          warn and proceed from the existing tip; otherwise abort.\n     @@ builtin/checkout.c: struct branch_info {\n       \tchar *checkout;\n       };\n       \n     -+struct fetch_target_cb {\n     -+\tchar *dst;\n     -+\tstruct string_list matches;\n     -+};\n     -+\n     -+static int match_fetch_target(struct remote *remote, void *priv)\n     -+{\n     -+\tstruct fetch_target_cb *cb = priv;\n     -+\tstruct refspec_item q = { .dst = cb->dst };\n     -+\n     -+\tif (!remote_find_tracking(remote, &q) && q.src)\n     -+\t\tstring_list_append(&cb->matches, remote->name)->util = q.src;\n     -+\treturn 0;\n     -+}\n     -+\n      +static void fetch_remote_for_start_point(const char *arg, int quiet)\n      +{\n      +\tstruct strbuf dst = STRBUF_INIT;\n     -+\tstruct fetch_target_cb cb = { .matches = STRING_LIST_INIT_NODUP };\n     ++\tstruct tracking tracking;\n     ++\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n     ++\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n      +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n      +\tstruct object_id oid;\n      +\tstruct remote *named_remote;\n      +\tint bare_ns;\n     -+\tsize_t i;\n      +\n      +\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n      +\tif (check_refname_format(dst.buf, 0))\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\tfree(head_path);\n      +\t}\n      +\n     -+\tcb.dst = dst.buf;\n     -+\tfor_each_remote(match_fetch_target, &cb);\n     -+\n     -+\tif (cb.matches.nr > 1) {\n     -+\t\tstruct strbuf msg = STRBUF_INIT;\n     -+\n     -+\t\tstrbuf_addf(&msg,\n     -+\t\t\t    _(\"cannot fetch start-point '%s': fetch refspecs \"\n     -+\t\t\t      \"of multiple remotes map to the same destination:\"),\n     -+\t\t\t    arg);\n     -+\t\tfor (i = 0; i < cb.matches.nr; i++)\n     -+\t\t\tstrbuf_addf(&msg, \"\\n  %s\", cb.matches.items[i].string);\n     -+\t\tstrbuf_addstr(&msg,\n     -+\t\t\t      _(\"\\nadjust 'remote.<name>.fetch' so only one \"\n     -+\t\t\t\t\"remote maps there, or omit '=fetch'\"));\n     -+\t\tdie(\"%s\", msg.buf);\n     ++\tmemset(&tracking, 0, sizeof(tracking));\n     ++\ttracking.spec.dst = dst.buf;\n     ++\ttracking.srcs = &tracking_srcs;\n     ++\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n     ++\n     ++\tif (tracking.matches > 1) {\n     ++\t\tint status = die_message(_(\"cannot fetch start-point '%s': \"\n     ++\t\t\t\t\t   \"fetch refspecs of multiple remotes \"\n     ++\t\t\t\t\t   \"map to '%s'\"), arg, dst.buf);\n     ++\t\tadvise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n     ++\t\texit(status);\n      +\t}\n      +\n     -+\tif (!cb.matches.nr) {\n     ++\tif (!tracking.matches) {\n      +\t\tif (bare_ns && named_remote &&\n      +\t\t    remote_is_configured(named_remote, 1))\n      +\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n     @@ builtin/checkout.c: struct branch_info {\n      +\tstrvec_push(&cmd.args, \"fetch\");\n      +\tif (quiet)\n      +\t\tstrvec_push(&cmd.args, \"--quiet\");\n     -+\tstrvec_pushl(&cmd.args, cb.matches.items[0].string,\n     -+\t\t     (char *)cb.matches.items[0].util, NULL);\n     ++\tstrvec_pushl(&cmd.args, tracking.remote,\n     ++\t\t     tracking_srcs.items[0].string, NULL);\n      +\tcmd.git_cmd = 1;\n      +\tif (run_command(&cmd)) {\n      +\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n      +\t}\n      +\n     -+\tfor (i = 0; i < cb.matches.nr; i++)\n     -+\t\tfree(cb.matches.items[i].util);\n     -+\tstring_list_clear(&cb.matches, 0);\n     ++\tstring_list_clear(&tracking_srcs, 0);\n     ++\tstring_list_clear(&ambiguous_remotes, 0);\n      +\tstrbuf_release(&dst);\n      +}\n      +\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n      +\ttest_grep \"fetch_ambig_a\" err &&\n      +\ttest_grep \"fetch_ambig_b\" err &&\n     -+\ttest_grep \"remote.<name>.fetch\" err &&\n     ++\ttest_grep \"tracking namespaces\" err &&\n      +\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n      +'\n      +\n\n-- \ngitgitgadget\n"},{"id":"543986","messageId":"2369afad24a0983d2095eb554cfc7a8b4af5e519.1779565714.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v13.git.git.1779565714.gitgitgadget@gmail.com","subject":"[PATCH v13 1/2] branch: expose helpers for finding the remote owning a tracking ref","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-23T19:48:33Z","receivedAt":"2026-05-23T19:48:38Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe remote-lookup that setup_tracking() does is useful outside\nbranch.c too; for example, deciding which remote to \"git fetch\"\nfrom given a remote-tracking ref.\n\nMove 'struct tracking' to branch.h and add two helpers backed by the\nexisting for_each_remote walk: find_tracking_remote_for_ref() and\nadvise_ambiguous_fetch_refspec(). setup_tracking() uses both. No\nbehavior change.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n branch.c | 96 ++++++++++++++++++++++++++++++--------------------------\n branch.h | 16 ++++++++++\n 2 files changed, 68 insertions(+), 44 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 243db7d0fc..46ae7f0035 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -20,16 +20,9 @@\n #include \"run-command.h\"\n #include \"strmap.h\"\n \n-struct tracking {\n-\tstruct refspec_item spec;\n-\tstruct string_list *srcs;\n-\tconst char *remote;\n-\tint matches;\n-};\n-\n struct find_tracked_branch_cb {\n \tstruct tracking *tracking;\n-\tstruct string_list ambiguous_remotes;\n+\tstruct string_list *ambiguous_remotes;\n };\n \n static int find_tracked_branch(struct remote *remote, void *priv)\n@@ -45,10 +38,10 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \t\t\tbreak;\n \t\tcase 2:\n \t\t\t/* there are at least two remotes; backfill the first one */\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, tracking->remote);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, tracking->remote);\n \t\t\t/* fall through */\n \t\tdefault:\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, remote->name);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, remote->name);\n \t\t\tfree(tracking->spec.src);\n \t\t\tstring_list_clear(tracking->srcs, 0);\n \t\tbreak;\n@@ -59,6 +52,51 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \treturn 0;\n }\n \n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes)\n+{\n+\tstruct find_tracked_branch_cb ftb_cb = {\n+\t\t.tracking = tracking,\n+\t\t.ambiguous_remotes = ambiguous_remotes,\n+\t};\n+\n+\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+}\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes)\n+{\n+\tstruct strbuf remotes_advice = STRBUF_INIT;\n+\tstruct string_list_item *item;\n+\n+\tif (!advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC))\n+\t\treturn;\n+\n+\tfor_each_string_list_item(item, ambiguous_remotes)\n+\t\t/*\n+\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n+\t\t * refspecs in the advice message below. For RTL languages you'll\n+\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n+\t\t */\n+\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n+\n+\t/*\n+\t * TRANSLATORS: The second argument is a \\n-delimited list of\n+\t * duplicate refspecs, composed above.\n+\t */\n+\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n+\t\t \"tracking ref '%s':\\n\"\n+\t\t \"%s\"\n+\t\t \"\\n\"\n+\t\t \"This is typically a configuration error.\\n\"\n+\t\t \"\\n\"\n+\t\t \"To support setting up tracking branches, ensure that\\n\"\n+\t\t \"different remotes' fetch refspecs map into different\\n\"\n+\t\t \"tracking namespaces.\"), dst,\n+\t       remotes_advice.buf);\n+\tstrbuf_release(&remotes_advice);\n+}\n+\n static int should_setup_rebase(const char *origin)\n {\n \tswitch (autorebase) {\n@@ -254,11 +292,8 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n {\n \tstruct tracking tracking;\n \tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n \tint config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;\n-\tstruct find_tracked_branch_cb ftb_cb = {\n-\t\t.tracking = &tracking,\n-\t\t.ambiguous_remotes = STRING_LIST_INIT_DUP,\n-\t};\n \n \tif (!track)\n \t\tBUG(\"asked to set up tracking, but tracking is disallowed\");\n@@ -267,7 +302,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \ttracking.spec.dst = (char *)orig_ref;\n \ttracking.srcs = &tracking_srcs;\n \tif (track != BRANCH_TRACK_INHERIT)\n-\t\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+\t\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n \telse if (inherit_tracking(&tracking, orig_ref))\n \t\tgoto cleanup;\n \n@@ -293,34 +328,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \tif (tracking.matches > 1) {\n \t\tint status = die_message(_(\"not tracking: ambiguous information for ref '%s'\"),\n \t\t\t\t\t    orig_ref);\n-\t\tif (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {\n-\t\t\tstruct strbuf remotes_advice = STRBUF_INIT;\n-\t\t\tstruct string_list_item *item;\n-\n-\t\t\tfor_each_string_list_item(item, &ftb_cb.ambiguous_remotes)\n-\t\t\t\t/*\n-\t\t\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n-\t\t\t\t * refspecs in the advice message below. For RTL languages you'll\n-\t\t\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n-\t\t\t\t */\n-\t\t\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n-\n-\t\t\t/*\n-\t\t\t * TRANSLATORS: The second argument is a \\n-delimited list of\n-\t\t\t * duplicate refspecs, composed above.\n-\t\t\t */\n-\t\t\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n-\t\t\t\t \"tracking ref '%s':\\n\"\n-\t\t\t\t \"%s\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"This is typically a configuration error.\\n\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"To support setting up tracking branches, ensure that\\n\"\n-\t\t\t\t \"different remotes' fetch refspecs map into different\\n\"\n-\t\t\t\t \"tracking namespaces.\"), orig_ref,\n-\t\t\t       remotes_advice.buf);\n-\t\t\tstrbuf_release(&remotes_advice);\n-\t\t}\n+\t\tadvise_ambiguous_fetch_refspec(orig_ref, &ambiguous_remotes);\n \t\texit(status);\n \t}\n \n@@ -347,7 +355,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \n cleanup:\n \tstring_list_clear(&tracking_srcs, 0);\n-\tstring_list_clear(&ftb_cb.ambiguous_remotes, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n }\n \n int read_branch_desc(struct strbuf *buf, const char *branch_name)\ndiff --git a/branch.h b/branch.h\nindex 3dc6e2a0ff..0aafa1673f 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -1,9 +1,25 @@\n #ifndef BRANCH_H\n #define BRANCH_H\n \n+#include \"refspec.h\"\n+#include \"string-list.h\"\n+\n struct repository;\n struct strbuf;\n \n+struct tracking {\n+\tstruct refspec_item spec;\n+\tstruct string_list *srcs;\n+\tconst char *remote;\n+\tint matches;\n+};\n+\n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes);\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes);\n+\n enum branch_track {\n \tBRANCH_TRACK_UNSPECIFIED = -1,\n \tBRANCH_TRACK_NEVER = 0,\n-- \ngitgitgadget\n\n"},{"id":"543987","messageId":"60adf0e67d97a1162e8c2e27e7a01fa8a8345d27.1779565714.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v13.git.git.1779565714.gitgitgadget@gmail.com","subject":"[PATCH v13 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-05-23T19:48:34Z","receivedAt":"2026-05-23T19:48:41Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\nswitch\" that refreshes <start-point> before checking it out:\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nis shorthand for\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nIdentify the remote whose configured fetch refspec maps to\n<start-point> using find_tracking_remote_for_ref() (the same lookup\n\"--track\" uses to pick which remote to record in\nbranch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\njust that ref so other remote-tracking branches are left untouched.\nWhen <start-point> is a bare <remote> (e.g. \"origin\"), follow\nrefs/remotes/<remote>/HEAD to learn which branch to refresh. If\n\"git fetch\" fails but the remote-tracking ref already exists locally,\nwarn and proceed from the existing tip; otherwise abort.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-checkout.adoc |  17 +-\n Documentation/git-switch.adoc   |   5 +-\n builtin/checkout.c              | 139 +++++++++++++++-\n t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n 4 files changed, 430 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..20b6cae60e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,26 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n+branch named by _<remote>/HEAD_ is updated, and the checkout fails\n+with a hint to configure that symref if it is not set. The checkout\n+also fails if no configured remote's fetch refspec maps to\n+_<start-point>_, or if more than one does (in which case the `fetch`\n+cannot be unambiguously routed). If the fetch itself fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..a8730b1da8 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,10 +155,11 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n-\tdetails.\n+\tdetails, and `--track` in linkgit:git-checkout[1] for the\n+\t`fetch` mode.\n +\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 1345e8574a..25b511f22a 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"strvec.h\"\n@@ -62,6 +64,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -115,6 +118,129 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg, int quiet)\n+{\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct tracking tracking;\n+\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct object_id oid;\n+\tstruct remote *named_remote;\n+\tint bare_ns;\n+\n+\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\tif (check_refname_format(dst.buf, 0))\n+\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n+\t\t      \"remote-tracking name\"), arg);\n+\n+\tnamed_remote = remote_get(arg);\n+\tbare_ns = !strchr(arg, '/') ||\n+\t\t(named_remote && remote_is_configured(named_remote, 1));\n+\tif (bare_ns) {\n+\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n+\t\tconst char *head_target =\n+\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\thead_path,\n+\t\t\t\t\t\tRESOLVE_REF_READING |\n+\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t&oid, NULL);\n+\t\tif (head_target &&\n+\t\t    starts_with(head_target, dst.buf) &&\n+\t\t    head_target[dst.len] == '/' &&\n+\t\t    !check_refname_format(head_target, 0)) {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addstr(&dst, head_target);\n+\t\t\tbare_ns = 0;\n+\t\t}\n+\t\tfree(head_path);\n+\t}\n+\n+\tmemset(&tracking, 0, sizeof(tracking));\n+\ttracking.spec.dst = dst.buf;\n+\ttracking.srcs = &tracking_srcs;\n+\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n+\n+\tif (tracking.matches > 1) {\n+\t\tint status = die_message(_(\"cannot fetch start-point '%s': \"\n+\t\t\t\t\t   \"fetch refspecs of multiple remotes \"\n+\t\t\t\t\t   \"map to '%s'\"), arg, dst.buf);\n+\t\tadvise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n+\t\texit(status);\n+\t}\n+\n+\tif (!tracking.matches) {\n+\t\tif (bare_ns && named_remote &&\n+\t\t    remote_is_configured(named_remote, 1))\n+\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n+\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n+\t\t\t      \"'git remote set-head %s --auto' to set it\"),\n+\t\t\t    arg, arg, arg);\n+\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n+\t\t      \"fetch refspec matches it\"), arg);\n+\t}\n+\n+\tstrvec_push(&cmd.args, \"fetch\");\n+\tif (quiet)\n+\t\tstrvec_push(&cmd.args, \"--quiet\");\n+\tstrvec_pushl(&cmd.args, tracking.remote,\n+\t\t     tracking_srcs.items[0].string, NULL);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t   dst.buf, &oid))\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tstring_list_clear(&tracking_srcs, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n+\tstrbuf_release(&dst);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\"))\n+\t\t\topts->fetch = 1;\n+\t\telse if (!strcmp(item->string, \"direct\"))\n+\t\t\tsaw_direct = 1;\n+\t\telse if (!strcmp(item->string, \"inherit\"))\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\telse {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1733,10 +1859,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1941,8 +2067,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0], opts->quiet);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..1e321b1512 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,280 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n+\tgit config --replace-all remote.fetch_ns.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n+\tgit fetch fetch_ns &&\n+\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_ns_post &&\n+\tgit checkout --track=fetch -b local_ns ns_alias &&\n+\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n+\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n+\ttest_cmp_config refs/heads/main branch.local_ns.merge\n+'\n+\n+test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add nested/bare ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/bare\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n+\tgit fetch nested/bare &&\n+\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n+\t\trefs/remotes/nested/bare/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_nested_bare_post &&\n+\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n+\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_nohead ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_nohead\" &&\n+\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n+\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n+\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n+\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n+'\n+\n+test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n+\ttest_must_fail git config --get remote.no_such_ns.url &&\n+\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_grep ! \"set-head\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n+\tgit checkout main &&\n+\tgit remote add fetch_crossns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_crossns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n+\tgit fetch fetch_crossns &&\n+\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n+\t\trefs/remotes/fetch_upstream/u_main &&\n+\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n+\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ambig_a ./fetch_upstream &&\n+\tgit remote add fetch_ambig_b ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n+\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n+\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_ambig &&\n+\ttest_commit -C fetch_upstream u_ambig &&\n+\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n+\ttest_grep \"fetch_ambig_a\" err &&\n+\ttest_grep \"fetch_ambig_b\" err &&\n+\ttest_grep \"tracking namespaces\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n+\t\t\"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=direct,inherit is rejected' '\n+\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_notrack &&\n+\ttest_commit -C fetch_upstream u_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git checkout --track=fetch --no-track \\\n+\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n+\tgit checkout --track=fetch,inherit -b local_inherit \\\n+\t\tfetch_upstream/fetch_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=fetch on local start-point errors' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_quiet &&\n+\ttest_commit -C fetch_upstream u_quiet &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n+\tgit checkout -q --track=fetch -b local_quiet \\\n+\t\tfetch_upstream/fetch_quiet 2>err &&\n+\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"545464","messageId":"CAHwyqnVjmwakmb+TF0Eg-KZMrEHihXZbaVkkS3WVJao-So2SKA@mail.gmail.com","threadId":"65546","inReplyTo":"01526f43-86aa-466f-a1e8-054284e1a2e1@gmail.com","subject":"Re: [PATCH v11] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-13T17:38:03Z","receivedAt":"2026-06-13T17:38:41Z","isPatch":true,"body":"Hi Phillip and JCH!\n\nWould it be possible to get another look here to know if it's worth\ncontinuing with this topic. I think it's a useful feature, but the\nfeedback from this list has been a bit lukewarm.\n\n\nHarald\n\nOn Thu, May 21, 2026 at 4:06 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> On 21/05/2026 13:58, Junio C Hamano wrote:\n> > Phillip Wood <phillip.wood123@gmail.com> writes:\n> >\n> >>> One.  Have you considered the case where the remote-tracking refs\n> >>> are overlapping, e.g., where \"origin\" and \"upstream\" point at\n> >>> different URLs but they both store in \"refs/remotes/upstream/*\"?\n> >>> Perhaps their URLs may textually be different but are pointing\n> >>> logically at the same place (e.g., one ssh:// the other https:// for\n> >>> example).\n> >>>\n> >>> What should happen?  What does happen after you apply this patch?\n> >>\n> >> It would be worth looking at what \"git checkout --track\" does in that\n> >> case and seeing if we can share the code.\n> >\n> > It always is a good idea to think how we can share code for\n> > different purposes to solve a new problem, but in this particular\n> > one, I am not sure if \"git checkout -t -b topic upstream/main\"\n> > codepath has much to offer to solve what the new \"before the\n> > checkout, update from the remote\" feature wants to do.  To the\n> > former, it does not matter how refs/remotes/upstream/* are updated\n> > and by fetching which remote at all.\n>\n> Don't we want to avoid creating a branch with an ambiguous upstream so\n> that a subsequent \"git pull\" works though? Looking at\n> branch.c:setup_tracking() it seems to reject upstream branches that\n> match more than one remote.\n>\n> Thanks\n>\n> Phillip\n>\n> >  The only thing it cares about\n> > is to leave the record that this new \"topic\" branch works with\n> > refs/remotes/upstrea/main.  But the latter needs to be able to\n> > compute which remote it should fetch from.  It is a problem that\n> > existing code had no need to solve.\n> >\n>\n"},{"id":"545792","messageId":"xmqqmrwtuggb.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v13.git.git.1779565714.gitgitgadget@gmail.com","subject":"Re: [PATCH v13 0/2] checkout: --track=fetch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-17T19:15:00Z","receivedAt":"2026-06-17T19:15:09Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n>  * Create a preparatory commit that exposes find_tracking_remote_for_ref()\n>    and advise_ambiguous_fetch_refspec() from branch.c, so checkout can reuse\n>    the same lookup git branch --track uses.\n>  * Use advise_ambiguous_fetch_refspec() for the \"multiple remotes match\"\n>    case, so the wording matches git branch --track.\n>\n> Harald Nordgren (2):\n>   branch: expose helpers for finding the remote owning a tracking ref\n>   checkout: extend --track with a \"fetch\" mode to refresh start-point\n>\n>  Documentation/git-checkout.adoc |  17 +-\n>  Documentation/git-switch.adoc   |   5 +-\n>  branch.c                        |  96 ++++++-----\n>  branch.h                        |  16 ++\n>  builtin/checkout.c              | 139 +++++++++++++++-\n>  t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n>  6 files changed, 498 insertions(+), 51 deletions(-)\n\nI was scanning \"What's cooking\" and this topic was the oldest one\namong the ones marked as \"Needs review\".  Nobody seems to have\ncommented on this iteration.\n\nI am still not convinced that it is a good idea to allow \"checkout\"\nto go to the network and muck with remote-tracking branches.  The\nremote-tracking branches are meant to give us solid reference\npoints, and such an on-demand update to move them (which by itself\nis not bad) and then use the updated result without first seeing\nwhat it contains (which is the part I disagree with) cannot lead us\nto a good place. I suspect that the feature encourages a bad\nworkflow to our end-users.\n\nHaving said all that, the changes since v12, in response to earlier\nreview comments to avoid duplicating the remote lookup and ambiguity\nadvice logic, look well executed in this round.  This also ensures\nconsistent error messages and behavior between 'git branch --track'\nand 'git checkout --track=fetch'.\n\nIOW, I find the mechanical implementation fairly solid.  I am not\nsure if we are implementing a good thing, though.\n\nOne small thing about [1/2];\n\ndiff --git a/branch.h b/branch.h\nindex 3dc6e2a0ff..0aafa1673f 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -1,9 +1,25 @@\n #ifndef BRANCH_H\n #define BRANCH_H\n \n+#include \"refspec.h\"\n+#include \"string-list.h\"\n+\n struct repository;\n struct strbuf;\n \n+struct tracking {\n+\tstruct refspec_item spec;\n+\tstruct string_list *srcs;\n+\tconst char *remote;\n+\tint matches;\n+};\n+\n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes);\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes);\n+\n\nAs we are not embedding any \"string_list\" instance into any of our\nstruct (we only have a pointer to a struct), unlike the way we embed\n\"struct refspec_item\" that requires us to include \"refspec.h\", we do\nnot need to include \"string-list.h\".  Instead, we only need to\ndeclare \"struct string_list\", just like we declare repository and\nstrbuf.\n\ndiff --git i/branch.h w/branch.h\nindex 0aafa1673f..c2e6725491 100644\n--- i/branch.h\n+++ w/branch.h\n@@ -2,8 +2,8 @@\n #define BRANCH_H\n \n #include \"refspec.h\"\n-#include \"string-list.h\"\n \n+struct string_list;\n struct repository;\n struct strbuf;\n \n"},{"id":"545807","messageId":"CAHwyqnXLceLXzRrW_7TB8JM+Ur92gw5QkYeKjzOGbWX+f_yLjw@mail.gmail.com","threadId":"65546","inReplyTo":"xmqqmrwtuggb.fsf@gitster.g","subject":"Re: [PATCH v13 0/2] checkout: --track=fetch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-17T22:10:36Z","receivedAt":"2026-06-17T22:11:15Z","isPatch":true,"body":"Hi!\n\nThanks for your continued attention here, I appreciate the help on all\nmy topics.\n\nBut can I offer some (unsolicited) feedback on this review process in\nparticular? Given that it seems unlikely to hit 'master' at this\npoint, I want to say that it's the wrong order of things to dig into\ncode specific feedback, before deciding if we even want the feature at\nall. We are wasting each other's time. I have pushed on despite\ninitial negative feedback, that's on me. But I also cannot lay flat, I\nlike the idea so I keep pushing. Now we have v13, maybe soon v14, of a\ntopic that has slim chances of passing.\n\nI would have been much happier if you shut this topic down directly.\n\nImagine all the review time spent on this that could have been better\nspent elsewhere.\n\n\nHarald\n"},{"id":"545808","messageId":"xmqqtsr0tvci.fsf@gitster.g","threadId":"65546","inReplyTo":"CAHwyqnXLceLXzRrW_7TB8JM+Ur92gw5QkYeKjzOGbWX+f_yLjw@mail.gmail.com","subject":"Re: [PATCH v13 0/2] checkout: --track=fetch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-18T02:50:53Z","receivedAt":"2026-06-18T02:50:57Z","isPatch":true,"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> But can I offer some (unsolicited) feedback on this review process in\n> particular? Given that it seems unlikely to hit 'master' at this\n> point, I want to say that it's the wrong order of things to dig into\n> code specific feedback, before deciding if we even want the feature at\n> all. We are wasting each other's time. I have pushed on despite\n> initial negative feedback, that's on me. But I also cannot lay flat, I\n> like the idea so I keep pushing. Now we have v13, maybe soon v14, of a\n> topic that has slim chances of passing.\n>\n> I would have been much happier if you shut this topic down directly.\n>\n> Imagine all the review time spent on this that could have been better\n> spent elsewhere.\n\nYou are forgetting that there are two things a topic author can and\nshould do: addressing higher-level design issues (e.g., Counter \"is\nit a good idea to begin with?\" with \"it is, because in this context\nyou haven't thought about, it would make the user experience\nbetter\", to help reviewers understand and hopefully agree with your\nviewpoint) and addressing mechanical implementation issues (e.g.,\nRespond to \"it is wasteful and adds maintenance burden to dupplicate\nthe logic here and there\" with \"Here is an updated implementation\nbased on better refactoring\").  The latter is easier in a sense,\nespecially when the reviewers say something like \"I am not sure if\nthis is a good idea to begin with, but assuming it is, here are the\nthings I find in your implementation\".  But the former needs to be\naddressed eventually before the topic can gain wider support\n(remember, it does not have to be sold to _me_ 100% and make me say\n\"oh, I cannot believe Git did not have this feature for 20 years!\";\nyou only need to convince me that it is OK to have it as an opt-in\nfeature, that is a lot lower bar to cross.  In addition, I do not\nhave to agree with it at all, as long as others whose judgement I\ntrust well enough support the feature).\n\n\n"},{"id":"545838","messageId":"CAHwyqnU4xuw9ZDjarWKKua_s1Qywt07GyP1kJO2HM+XQTcE8hA@mail.gmail.com","threadId":"65546","inReplyTo":"CALnO6CCNoo8y2V5KmE0KQ6qDurZELipFowcr=ZpZ3ocVB-uLjA@mail.gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-18T12:36:13Z","receivedAt":"2026-06-18T12:36:51Z","isPatch":true,"body":"Hi Ben!\n\nTrying to shore up some support for this topic. How do you feel about this now?\n\n\nHarald\n"},{"id":"545839","messageId":"CAHwyqnWU9DaaxkLhzq-8ADBTXshC0zJUFN4amUh3xDgvCqad3Q@mail.gmail.com","threadId":"65546","inReplyTo":"f23eb128-958f-475f-911b-eac4f6daddff@gmail.com","subject":"Re: [PATCH v6] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-18T12:38:59Z","receivedAt":"2026-06-18T12:39:38Z","isPatch":true,"body":"Hi Phillip!\n\nHow do you feel now, is it worth it for us to move forward with this\ntopic or not?\n\n\nHarald\n\nOn Fri, May 8, 2026 at 3:15 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Harald\n>\n> On 07/05/2026 21:12, Harald Nordgren wrote:\n> > Is this ready to move to next?\n>\n> I'm not particularly enthusiastic one way or the other about adding\n> this, but so long as we only try to fetch when the user explicitly asks\n> for it I don't particularly object. However having had a quick scan of\n> the implementation I have a few comments\n>\n> * \"--track=inherit,direct\" is nonsense and should be rejected\n>\n> * currently \"--track\" has \"last one wins\" behavior so\n>    \"--track=inherit --track=direct\" behaves like \"--track=direct\". We\n>    should probably keep that so that \"--track=fetch --track=direct\"\n>    behaves like \"--track=direct\", not \"--track=fetch,direct\"\n>\n> * if \"git fetch\" fails and the remote tracking ref already exists then\n>    we should print a warning and carry on rather than dying which is more\n>    convenient if the user or remote server are offline.\n>\n> * \"git checkout --track=fetch origin/branch\" should respect\n>    remote.origin.fetch so that we fetch the ref that we're going to\n>    checkout. I wonder if we can share this logic with the code that\n>    sets the upstream branch.\n>\n> * \"git checkout --track=fetch origin\" should only fetch the remote\n>    ref that we're going to checkout, not all the refs from origin. i.e.\n>    it should read origin/HEAD to work out what to fetch.\n>\n> Thanks\n>\n> Phillip\n>\n"},{"id":"545841","messageId":"pull.2281.v14.git.git.1781786652.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v13.git.git.1779565714.gitgitgadget@gmail.com","subject":"[PATCH v14 0/2] checkout: --track=fetch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-18T12:44:10Z","receivedAt":"2026-06-18T12:44:14Z","isPatch":true,"body":"Extend checkout --track with a fetch mode to refresh start-point.\n\nChanges in v14:\n\n * Handle .h files in a better way.\n\nChanges in v13:\n\n * Create a preparatory commit that exposes find_tracking_remote_for_ref()\n   and advise_ambiguous_fetch_refspec() from branch.c, so checkout can reuse\n   the same lookup git branch --track uses.\n * Use advise_ambiguous_fetch_refspec() for the \"multiple remotes match\"\n   case, so the wording matches git branch --track.\n\nHarald Nordgren (2):\n  branch: expose helpers for finding the remote owning a tracking ref\n  checkout: extend --track with a \"fetch\" mode to refresh start-point\n\n Documentation/git-checkout.adoc |  17 +-\n Documentation/git-switch.adoc   |   5 +-\n branch.c                        |  96 ++++++-----\n branch.h                        |  16 ++\n builtin/checkout.c              | 139 +++++++++++++++-\n t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n 6 files changed, 498 insertions(+), 51 deletions(-)\n\n\nbase-commit: 4621f8ce5e9b97aa2e8d0d9ffe9d25df2471074d\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v14\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v14\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v13:\n\n 1:  2369afad24 ! 1:  f79689c23d branch: expose helpers for finding the remote owning a tracking ref\n     @@ branch.h\n       #define BRANCH_H\n       \n      +#include \"refspec.h\"\n     -+#include \"string-list.h\"\n      +\n     ++struct string_list;\n       struct repository;\n       struct strbuf;\n       \n 2:  60adf0e67d ! 2:  8518f090b1 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ builtin/checkout.c\n      +#include \"run-command.h\"\n       #include \"sequencer.h\"\n       #include \"setup.h\"\n     - #include \"strvec.h\"\n     + #include \"sparse-index.h\"\n      @@ builtin/checkout.c: struct checkout_opts {\n       \tint count_checkout_paths;\n       \tint overlay_mode;\n\n-- \ngitgitgadget\n"},{"id":"545842","messageId":"f79689c23d962c117e374970686ae408f0163e67.1781786652.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v14.git.git.1781786652.gitgitgadget@gmail.com","subject":"[PATCH v14 1/2] branch: expose helpers for finding the remote owning a tracking ref","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-18T12:44:11Z","receivedAt":"2026-06-18T12:44:15Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe remote-lookup that setup_tracking() does is useful outside\nbranch.c too; for example, deciding which remote to \"git fetch\"\nfrom given a remote-tracking ref.\n\nMove 'struct tracking' to branch.h and add two helpers backed by the\nexisting for_each_remote walk: find_tracking_remote_for_ref() and\nadvise_ambiguous_fetch_refspec(). setup_tracking() uses both. No\nbehavior change.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n branch.c | 96 ++++++++++++++++++++++++++++++--------------------------\n branch.h | 16 ++++++++++\n 2 files changed, 68 insertions(+), 44 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 243db7d0fc..46ae7f0035 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -20,16 +20,9 @@\n #include \"run-command.h\"\n #include \"strmap.h\"\n \n-struct tracking {\n-\tstruct refspec_item spec;\n-\tstruct string_list *srcs;\n-\tconst char *remote;\n-\tint matches;\n-};\n-\n struct find_tracked_branch_cb {\n \tstruct tracking *tracking;\n-\tstruct string_list ambiguous_remotes;\n+\tstruct string_list *ambiguous_remotes;\n };\n \n static int find_tracked_branch(struct remote *remote, void *priv)\n@@ -45,10 +38,10 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \t\t\tbreak;\n \t\tcase 2:\n \t\t\t/* there are at least two remotes; backfill the first one */\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, tracking->remote);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, tracking->remote);\n \t\t\t/* fall through */\n \t\tdefault:\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, remote->name);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, remote->name);\n \t\t\tfree(tracking->spec.src);\n \t\t\tstring_list_clear(tracking->srcs, 0);\n \t\tbreak;\n@@ -59,6 +52,51 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \treturn 0;\n }\n \n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes)\n+{\n+\tstruct find_tracked_branch_cb ftb_cb = {\n+\t\t.tracking = tracking,\n+\t\t.ambiguous_remotes = ambiguous_remotes,\n+\t};\n+\n+\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+}\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes)\n+{\n+\tstruct strbuf remotes_advice = STRBUF_INIT;\n+\tstruct string_list_item *item;\n+\n+\tif (!advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC))\n+\t\treturn;\n+\n+\tfor_each_string_list_item(item, ambiguous_remotes)\n+\t\t/*\n+\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n+\t\t * refspecs in the advice message below. For RTL languages you'll\n+\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n+\t\t */\n+\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n+\n+\t/*\n+\t * TRANSLATORS: The second argument is a \\n-delimited list of\n+\t * duplicate refspecs, composed above.\n+\t */\n+\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n+\t\t \"tracking ref '%s':\\n\"\n+\t\t \"%s\"\n+\t\t \"\\n\"\n+\t\t \"This is typically a configuration error.\\n\"\n+\t\t \"\\n\"\n+\t\t \"To support setting up tracking branches, ensure that\\n\"\n+\t\t \"different remotes' fetch refspecs map into different\\n\"\n+\t\t \"tracking namespaces.\"), dst,\n+\t       remotes_advice.buf);\n+\tstrbuf_release(&remotes_advice);\n+}\n+\n static int should_setup_rebase(const char *origin)\n {\n \tswitch (autorebase) {\n@@ -254,11 +292,8 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n {\n \tstruct tracking tracking;\n \tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n \tint config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;\n-\tstruct find_tracked_branch_cb ftb_cb = {\n-\t\t.tracking = &tracking,\n-\t\t.ambiguous_remotes = STRING_LIST_INIT_DUP,\n-\t};\n \n \tif (!track)\n \t\tBUG(\"asked to set up tracking, but tracking is disallowed\");\n@@ -267,7 +302,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \ttracking.spec.dst = (char *)orig_ref;\n \ttracking.srcs = &tracking_srcs;\n \tif (track != BRANCH_TRACK_INHERIT)\n-\t\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+\t\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n \telse if (inherit_tracking(&tracking, orig_ref))\n \t\tgoto cleanup;\n \n@@ -293,34 +328,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \tif (tracking.matches > 1) {\n \t\tint status = die_message(_(\"not tracking: ambiguous information for ref '%s'\"),\n \t\t\t\t\t    orig_ref);\n-\t\tif (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {\n-\t\t\tstruct strbuf remotes_advice = STRBUF_INIT;\n-\t\t\tstruct string_list_item *item;\n-\n-\t\t\tfor_each_string_list_item(item, &ftb_cb.ambiguous_remotes)\n-\t\t\t\t/*\n-\t\t\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n-\t\t\t\t * refspecs in the advice message below. For RTL languages you'll\n-\t\t\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n-\t\t\t\t */\n-\t\t\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n-\n-\t\t\t/*\n-\t\t\t * TRANSLATORS: The second argument is a \\n-delimited list of\n-\t\t\t * duplicate refspecs, composed above.\n-\t\t\t */\n-\t\t\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n-\t\t\t\t \"tracking ref '%s':\\n\"\n-\t\t\t\t \"%s\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"This is typically a configuration error.\\n\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"To support setting up tracking branches, ensure that\\n\"\n-\t\t\t\t \"different remotes' fetch refspecs map into different\\n\"\n-\t\t\t\t \"tracking namespaces.\"), orig_ref,\n-\t\t\t       remotes_advice.buf);\n-\t\t\tstrbuf_release(&remotes_advice);\n-\t\t}\n+\t\tadvise_ambiguous_fetch_refspec(orig_ref, &ambiguous_remotes);\n \t\texit(status);\n \t}\n \n@@ -347,7 +355,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \n cleanup:\n \tstring_list_clear(&tracking_srcs, 0);\n-\tstring_list_clear(&ftb_cb.ambiguous_remotes, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n }\n \n int read_branch_desc(struct strbuf *buf, const char *branch_name)\ndiff --git a/branch.h b/branch.h\nindex 3dc6e2a0ff..c2e6725491 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -1,9 +1,25 @@\n #ifndef BRANCH_H\n #define BRANCH_H\n \n+#include \"refspec.h\"\n+\n+struct string_list;\n struct repository;\n struct strbuf;\n \n+struct tracking {\n+\tstruct refspec_item spec;\n+\tstruct string_list *srcs;\n+\tconst char *remote;\n+\tint matches;\n+};\n+\n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes);\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes);\n+\n enum branch_track {\n \tBRANCH_TRACK_UNSPECIFIED = -1,\n \tBRANCH_TRACK_NEVER = 0,\n-- \ngitgitgadget\n\n"},{"id":"545843","messageId":"8518f090b1069a02d40c710975528ad118776b67.1781786652.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v14.git.git.1781786652.gitgitgadget@gmail.com","subject":"[PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-18T12:44:12Z","receivedAt":"2026-06-18T12:44:16Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\nswitch\" that refreshes <start-point> before checking it out:\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nis shorthand for\n\n    git fetch origin some-branch\n    git checkout -b new_branch --track origin/some-branch\n\nIdentify the remote whose configured fetch refspec maps to\n<start-point> using find_tracking_remote_for_ref() (the same lookup\n\"--track\" uses to pick which remote to record in\nbranch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\njust that ref so other remote-tracking branches are left untouched.\nWhen <start-point> is a bare <remote> (e.g. \"origin\"), follow\nrefs/remotes/<remote>/HEAD to learn which branch to refresh. If\n\"git fetch\" fails but the remote-tracking ref already exists locally,\nwarn and proceed from the existing tip; otherwise abort.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-checkout.adoc |  17 +-\n Documentation/git-switch.adoc   |   5 +-\n builtin/checkout.c              | 139 +++++++++++++++-\n t/t7201-co.sh                   | 276 ++++++++++++++++++++++++++++++++\n 4 files changed, 430 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..20b6cae60e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,26 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n+branch named by _<remote>/HEAD_ is updated, and the checkout fails\n+with a hint to configure that symref if it is not set. The checkout\n+also fails if no configured remote's fetch refspec maps to\n+_<start-point>_, or if more than one does (in which case the `fetch`\n+cannot be unambiguously routed). If the fetch itself fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..a8730b1da8 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,10 +155,11 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n-\tdetails.\n+\tdetails, and `--track` in linkgit:git-checkout[1] for the\n+\t`fetch` mode.\n +\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex b78b3a1d16..37caceaefd 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"sparse-index.h\"\n@@ -63,6 +65,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -116,6 +119,129 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg, int quiet)\n+{\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct tracking tracking;\n+\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct object_id oid;\n+\tstruct remote *named_remote;\n+\tint bare_ns;\n+\n+\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\tif (check_refname_format(dst.buf, 0))\n+\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n+\t\t      \"remote-tracking name\"), arg);\n+\n+\tnamed_remote = remote_get(arg);\n+\tbare_ns = !strchr(arg, '/') ||\n+\t\t(named_remote && remote_is_configured(named_remote, 1));\n+\tif (bare_ns) {\n+\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n+\t\tconst char *head_target =\n+\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\thead_path,\n+\t\t\t\t\t\tRESOLVE_REF_READING |\n+\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t\t&oid, NULL);\n+\t\tif (head_target &&\n+\t\t    starts_with(head_target, dst.buf) &&\n+\t\t    head_target[dst.len] == '/' &&\n+\t\t    !check_refname_format(head_target, 0)) {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addstr(&dst, head_target);\n+\t\t\tbare_ns = 0;\n+\t\t}\n+\t\tfree(head_path);\n+\t}\n+\n+\tmemset(&tracking, 0, sizeof(tracking));\n+\ttracking.spec.dst = dst.buf;\n+\ttracking.srcs = &tracking_srcs;\n+\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n+\n+\tif (tracking.matches > 1) {\n+\t\tint status = die_message(_(\"cannot fetch start-point '%s': \"\n+\t\t\t\t\t   \"fetch refspecs of multiple remotes \"\n+\t\t\t\t\t   \"map to '%s'\"), arg, dst.buf);\n+\t\tadvise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n+\t\texit(status);\n+\t}\n+\n+\tif (!tracking.matches) {\n+\t\tif (bare_ns && named_remote &&\n+\t\t    remote_is_configured(named_remote, 1))\n+\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n+\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n+\t\t\t      \"'git remote set-head %s --auto' to set it\"),\n+\t\t\t    arg, arg, arg);\n+\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n+\t\t      \"fetch refspec matches it\"), arg);\n+\t}\n+\n+\tstrvec_push(&cmd.args, \"fetch\");\n+\tif (quiet)\n+\t\tstrvec_push(&cmd.args, \"--quiet\");\n+\tstrvec_pushl(&cmd.args, tracking.remote,\n+\t\t     tracking_srcs.items[0].string, NULL);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n+\t\t\t\t   dst.buf, &oid))\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tstring_list_clear(&tracking_srcs, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n+\tstrbuf_release(&dst);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\"))\n+\t\t\topts->fetch = 1;\n+\t\telse if (!strcmp(item->string, \"direct\"))\n+\t\t\tsaw_direct = 1;\n+\t\telse if (!strcmp(item->string, \"inherit\"))\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\telse {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1786,10 +1912,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1994,8 +2120,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0], opts->quiet);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..1e321b1512 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,280 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n+\tgit config --replace-all remote.fetch_ns.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n+\tgit fetch fetch_ns &&\n+\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_ns_post &&\n+\tgit checkout --track=fetch -b local_ns ns_alias &&\n+\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n+\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n+\ttest_cmp_config refs/heads/main branch.local_ns.merge\n+'\n+\n+test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add nested/bare ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/bare\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n+\tgit fetch nested/bare &&\n+\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n+\t\trefs/remotes/nested/bare/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_nested_bare_post &&\n+\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n+\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n+\tgit checkout main &&\n+\tgit remote add nested/remote ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove nested/remote\" &&\n+\tgit -C fetch_upstream checkout -b fetch_hier &&\n+\ttest_commit -C fetch_upstream u_hier &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_nohead ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_nohead\" &&\n+\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n+\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n+\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n+\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n+'\n+\n+test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n+\ttest_must_fail git config --get remote.no_such_ns.url &&\n+\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_grep ! \"set-head\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n+\tgit checkout main &&\n+\tgit remote add fetch_crossns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_crossns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n+\tgit fetch fetch_crossns &&\n+\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n+\t\trefs/remotes/fetch_upstream/u_main &&\n+\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n+\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ambig_a ./fetch_upstream &&\n+\tgit remote add fetch_ambig_b ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n+\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n+\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_ambig &&\n+\ttest_commit -C fetch_upstream u_ambig &&\n+\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n+\ttest_grep \"fetch_ambig_a\" err &&\n+\ttest_grep \"fetch_ambig_b\" err &&\n+\ttest_grep \"tracking namespaces\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n+\t\t\"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=direct,inherit is rejected' '\n+\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_notrack &&\n+\ttest_commit -C fetch_upstream u_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git checkout --track=fetch --no-track \\\n+\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n+\tgit checkout --track=fetch,inherit -b local_inherit \\\n+\t\tfetch_upstream/fetch_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=fetch on local start-point errors' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_quiet &&\n+\ttest_commit -C fetch_upstream u_quiet &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n+\tgit checkout -q --track=fetch -b local_quiet \\\n+\t\tfetch_upstream/fetch_quiet 2>err &&\n+\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"545873","messageId":"CALnO6CAVHAt8wDMNGabY=krpOs4=JNUXYrhnNdKKcyJ_heMaXQ@mail.gmail.com","threadId":"65546","inReplyTo":"CAHwyqnU4xuw9ZDjarWKKua_s1Qywt07GyP1kJO2HM+XQTcE8hA@mail.gmail.com","subject":"Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-06-18T17:47:57Z","receivedAt":"2026-06-18T17:48:10Z","isPatch":true,"body":"Hi Harald,\n\nOn Thu, Jun 18, 2026 at 8:36 AM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> Hi Ben!\n>\n> Trying to shore up some support for this topic. How do you feel about this now?\n>\n>\n> Harald\n\nI haven't followed the series too closely; it sounds like the code is\nin decent shape.\n\nI stand by my thought that this could be convenient, but that's not\nmuch one way or another in terms of how it fits in overall with Git's\nvision.\n\nThere are some conveniences I recommend universally. This one requires\ncaveats, as pointed out by others, depending on the project, so… from\nthat point of view I wonder if keeping \"fetch + switch\" as 2 distinct\nsteps is better? I truly don't know.\n\n-- \nD. Ben Knoble\n"},{"id":"546233","messageId":"12998c3a-ff69-4a98-9ed6-18aa0224e75e@gmail.com","threadId":"65546","inReplyTo":"8518f090b1069a02d40c710975528ad118776b67.1781786652.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-23T13:49:29Z","receivedAt":"2026-06-23T13:49:32Z","isPatch":true,"body":"Hi Harald\n\nOn 18/06/2026 13:44, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> Add a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\n> switch\" that refreshes <start-point> before checking it out:\n> \n>      git checkout -b new_branch --track=fetch origin/some-branch\n> \n> is shorthand for\n> \n>      git fetch origin some-branch\n>      git checkout -b new_branch --track origin/some-branch\n> \n> Identify the remote whose configured fetch refspec maps to\n> <start-point> using find_tracking_remote_for_ref() (the same lookup\n> \"--track\" uses to pick which remote to record in\n> branch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\n> just that ref so other remote-tracking branches are left untouched.\n> When <start-point> is a bare <remote> (e.g. \"origin\"), follow\n> refs/remotes/<remote>/HEAD to learn which branch to refresh. If\n> \"git fetch\" fails but the remote-tracking ref already exists locally,\n> warn and proceed from the existing tip; otherwise abort.\n\nThis describes the feature well, but does not really explain why it is \nconvenient to have a shorthand for \"git fetch ... && git checkout -b \n...\". For example if the reason is that in a fast-moving project you \nwant to start your new work off the latest upstream changes to minimize \nthe chance of merge conflicts or duplicated work it would be useful to \nsay that. As Junio has said the implementation looks pretty solid I've \nleft a few comments below, but the important thing to do first is to \nconvince others that this is a useful feature and why it is worth \nblurring the separation between fetch and checkout. You can do that \nwithout sending a new version.\n\n> diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\n> index a8b3b8c2e2..20b6cae60e 100644\n> --- a/Documentation/git-checkout.adoc\n> +++ b/Documentation/git-checkout.adoc\n> @@ -158,11 +158,26 @@ of it\").\n>   \tresets _<branch>_ to the start point instead of failing.\n>   \n>   `-t`::\n> -`--track[=(direct|inherit)]`::\n> +`--track[=(direct|inherit|fetch)[,...]]`::\n>   \tWhen creating a new branch, set up \"upstream\" configuration. See\n>   \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n>   \t--track without -b implies branch creation.\n>   +\n> +The argument is a comma-separated list. `direct` (the default) and\n> +`inherit` select the tracking mode and are mutually exclusive. Adding\n> +`fetch` requests that the remote be fetched before _<start-point>_ is\n> +resolved, so the new branch starts from a fresh tip: when\n> +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n> +updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n> +branch named by _<remote>/HEAD_ is updated, and the checkout fails\n> +with a hint to configure that symref if it is not set. The checkout\n> +also fails if no configured remote's fetch refspec maps to\n> +_<start-point>_, or if more than one does (in which case the `fetch`\n> +cannot be unambiguously routed). If the fetch itself fails and the\n> +corresponding remote-tracking ref already exists, a warning is printed\n> +and the checkout proceeds from the existing tip; otherwise the checkout\n> +is aborted.\n\nNicely explained\n\n> +static void fetch_remote_for_start_point(const char *arg, int quiet)\n> +{\n> +\tstruct strbuf dst = STRBUF_INIT;\n> +\tstruct tracking tracking;\n> +\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n> +\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n> +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n> +\tstruct object_id oid;\n> +\tstruct remote *named_remote;\n> +\tint bare_ns;\n> +\n> +\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n> +\tif (check_refname_format(dst.buf, 0))\n> +\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n> +\t\t      \"remote-tracking name\"), arg);\n> +\n> +\tnamed_remote = remote_get(arg);\n> +\tbare_ns = !strchr(arg, '/') ||\n> +\t\t(named_remote && remote_is_configured(named_remote, 1));\n> +\tif (bare_ns) {\n> +\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n> +\t\tconst char *head_target =\n> +\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\thead_path,\n> +\t\t\t\t\t\tRESOLVE_REF_READING |\n> +\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n\nWhy do we use RESOLVE_REF_NO_RECURSE here? This should match whatever \n\"git checkout -b <remote>\" does.\n\n> +\t\t\t\t\t\t&oid, NULL);\n> +\t\tif (head_target &&\n> +\t\t    starts_with(head_target, dst.buf) &&\n> +\t\t    head_target[dst.len] == '/' &&\n> +\t\t    !check_refname_format(head_target, 0)) {\n\nI don't think there is any need to call check_refname_format() here - \nyou're using the result of reading a ref, not some untrusted input.\n\n> +\t\t\tstrbuf_reset(&dst);\n> +\t\t\tstrbuf_addstr(&dst, head_target);\n> +\t\t\tbare_ns = 0;\n> +\t\t}\n> +\t\tfree(head_path);\n> +\t}\n> +\n> +\tmemset(&tracking, 0, sizeof(tracking));\n\nWhen you want to zero initialize a stack variable it is easier, clearer \nand less error-prone to initialize it by adding \"= {0};\" where it is \ndeclared.\n\n> +\ttracking.spec.dst = dst.buf;\n> +\ttracking.srcs = &tracking_srcs;\n> +\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n> +\n> +\tif (tracking.matches > 1) {\n> +\t\tint status = die_message(_(\"cannot fetch start-point '%s': \"\n> +\t\t\t\t\t   \"fetch refspecs of multiple remotes \"\n> +\t\t\t\t\t   \"map to '%s'\"), arg, dst.buf);\n> +\t\tadvise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n> +\t\texit(status);\n> +\t}\n> +\n> +\tif (!tracking.matches) {\n> +\t\tif (bare_ns && named_remote &&\n> +\t\t    remote_is_configured(named_remote, 1))\n> +\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n> +\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n> +\t\t\t      \"'git remote set-head %s --auto' to set it\")\n\nThis is quite a long message for a single line - breaking the line and \nputting the suggested command on a separate line would make it clearer. \nSomething like\n\ncannot fetch start-point 'origin' because 'refs/remotes/origin/HEAD'\ndoes not exist. To create it run\n\n     git remote set-head origin --auto\n\n> +\t\t\t    arg, arg, arg);\n> +\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n> +\t\t      \"fetch refspec matches it\"), arg);\n> +\t}\n> +\n> +\tstrvec_push(&cmd.args, \"fetch\");\n> +\tif (quiet)\n> +\t\tstrvec_push(&cmd.args, \"--quiet\");\n> +\tstrvec_pushl(&cmd.args, tracking.remote,\n> +\t\t     tracking_srcs.items[0].string, NULL);\n> +\tcmd.git_cmd = 1;\n> +\tif (run_command(&cmd)) {\n> +\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n> +\t\t\t\t   dst.buf, &oid))\n\nYou can use refs_ref_exists() to check a ref exists which avoids \ndeclaring \"oid\" which we're not interested in here.\n\n> +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n> +\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n> +\t\telse\n> +\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n> +\t}\n> +\n> +\tstring_list_clear(&tracking_srcs, 0);\n> +\tstring_list_clear(&ambiguous_remotes, 0);\n> +\tstrbuf_release(&dst);\n> +}\n> +\n> +static int parse_opt_checkout_track(const struct option *opt,\n> +\t\t\t\t    const char *arg, int unset)\n> +{\n> +\tstruct checkout_opts *opts = opt->value;\n> +\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n> +\tstruct string_list_item *item;\n> +\tint saw_direct = 0;\n> +\tint ret = 0;\n> +\n> +\topts->fetch = 0;\n> +\tif (unset) {\n> +\t\topts->track = BRANCH_TRACK_NEVER;\n> +\t\treturn 0;\n> +\t}\n> +\topts->track = BRANCH_TRACK_EXPLICIT;\n> +\tif (!arg)\n> +\t\treturn 0;\n> +\n> +\tstring_list_split(&tokens, arg, \",\", -1);\n> +\tfor_each_string_list_item(item, &tokens) {\n> +\t\tif (!strcmp(item->string, \"fetch\"))\n> +\t\t\topts->fetch = 1;\n> +\t\telse if (!strcmp(item->string, \"direct\"))\n> +\t\t\tsaw_direct = 1;\n> +\t\telse if (!strcmp(item->string, \"inherit\"))\n> +\t\t\topts->track = BRANCH_TRACK_INHERIT;\n> +\t\telse {\n> +\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n> +\t\t\t\t      \"or \\\"%s\\\"\"),\n> +\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n> +\t\t\tgoto out;\n> +\t\t}\n> +\t}\n> +\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n> +\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n> +\t\t\t    \"--track\", \"direct\", \"inherit\");\n\nThis parsing looks good\n> diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> index 7613b1d2a4..1e321b1512 100755\n> --- a/t/t7201-co.sh\n> +++ b/t/t7201-co.sh\n> @@ -870,4 +870,280 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n>   \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n>   '\n\nI've not read the tests in detail but there seem to be an awful lot of \nthem. We only need to test each thing once so for example if we test\n\n     git checkout --track=fetch -b <remote-ref>\n\nwith a fetch refspec, that maps refs/heads/*:refs/remotes/origin/xxx/* \nthen we don't need to test it without that refspec. I notice you use \n\"namespace\" below with is confusing because it is not referring to the \nfeature described in the gitnamespaces(7) man page.\n\nTry and avoid\n\n     test $a = $b\n\nas it makes it hard to debug failing tests. Instead I think you can use \ntest_cmp_rev in this case.\n\nThanks\n\nPhillip\n\n> +test_expect_success 'setup upstream for --track=fetch tests' '\n> +\tgit checkout main &&\n> +\tgit init fetch_upstream &&\n> +\ttest_commit -C fetch_upstream u_main &&\n> +\tgit remote add fetch_upstream fetch_upstream &&\n> +\tgit fetch fetch_upstream &&\n> +\tgit -C fetch_upstream checkout -b fetch_new &&\n> +\ttest_commit -C fetch_upstream u_new\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> +\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n> +\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n> +\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_pre &&\n> +\tgit -C fetch_upstream checkout -b fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_pre &&\n> +\tgit fetch fetch_upstream &&\n> +\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n> +\tgit -C fetch_upstream checkout fetch_target &&\n> +\ttest_commit -C fetch_upstream u_target_post &&\n> +\tgit -C fetch_upstream checkout fetch_other &&\n> +\ttest_commit -C fetch_upstream u_other_post &&\n> +\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n> +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\tgit remote set-head fetch_upstream main &&\n> +\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n> +\ttest_commit -C fetch_upstream u_unrelated_pre &&\n> +\tgit fetch fetch_upstream fetch_unrelated &&\n> +\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\ttest_commit -C fetch_upstream u_main_post &&\n> +\tgit -C fetch_upstream checkout fetch_unrelated &&\n> +\ttest_commit -C fetch_upstream u_unrelated_post &&\n> +\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n> +\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n> +\tgit checkout main &&\n> +\ttest_might_fail git branch -D bogus &&\n> +\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bogus\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_offline &&\n> +\ttest_commit -C fetch_upstream u_offline &&\n> +\tgit fetch fetch_upstream fetch_offline &&\n> +\tsaved_url=$(git config remote.fetch_upstream.url) &&\n> +\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n> +\tgit config remote.fetch_upstream.url ./does-not-exist &&\n> +\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n> +\ttest_grep \"failed to fetch\" err &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n> +\tgit checkout main &&\n> +\tgit remote add fetch_custom ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_custom\" &&\n> +\tgit config --replace-all remote.fetch_custom.fetch \\\n> +\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n> +\tgit -C fetch_upstream checkout -b fetch_refspec &&\n> +\ttest_commit -C fetch_upstream u_refspec &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n> +\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n> +\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n> +\tgit checkout main &&\n> +\tgit remote add fetch_ns ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_ns\" &&\n> +\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n> +\tgit config --replace-all remote.fetch_ns.fetch \\\n> +\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n> +\tgit fetch fetch_ns &&\n> +\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\ttest_commit -C fetch_upstream u_ns_post &&\n> +\tgit checkout --track=fetch -b local_ns ns_alias &&\n> +\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n> +\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n> +\ttest_cmp_config refs/heads/main branch.local_ns.merge\n> +'\n> +\n> +test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n> +\tgit checkout main &&\n> +\tgit remote add nested/bare ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove nested/bare\" &&\n> +\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n> +\tgit fetch nested/bare &&\n> +\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n> +\t\trefs/remotes/nested/bare/main &&\n> +\tgit -C fetch_upstream checkout main &&\n> +\ttest_commit -C fetch_upstream u_nested_bare_post &&\n> +\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n> +\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n> +\tgit checkout main &&\n> +\tgit remote add nested/remote ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove nested/remote\" &&\n> +\tgit -C fetch_upstream checkout -b fetch_hier &&\n> +\ttest_commit -C fetch_upstream u_hier &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n> +\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n> +\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n> +\tgit checkout main &&\n> +\tgit remote add fetch_nohead ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_nohead\" &&\n> +\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n> +\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n> +\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n> +\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n> +\ttest_must_fail git config --get remote.no_such_ns.url &&\n> +\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n> +\ttest_grep \"no configured remote\" err &&\n> +\ttest_grep ! \"set-head\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n> +\tgit checkout main &&\n> +\tgit remote add fetch_crossns ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_crossns\" &&\n> +\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n> +\tgit fetch fetch_crossns &&\n> +\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n> +\t\trefs/remotes/fetch_upstream/u_main &&\n> +\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n> +\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n> +\tgit checkout main &&\n> +\tgit remote add fetch_ambig_a ./fetch_upstream &&\n> +\tgit remote add fetch_ambig_b ./fetch_upstream &&\n> +\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n> +\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n> +\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n> +\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n> +\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n> +\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n> +\tgit -C fetch_upstream checkout -b fetch_ambig &&\n> +\ttest_commit -C fetch_upstream u_ambig &&\n> +\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n> +\ttest_grep \"fetch_ambig_a\" err &&\n> +\ttest_grep \"fetch_ambig_b\" err &&\n> +\ttest_grep \"tracking namespaces\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n> +\ttest_grep \"valid\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n> +\t\t\"foo..bar\" 2>err &&\n> +\ttest_grep \"valid\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n> +'\n> +\n> +test_expect_success 'checkout --track=inherit,direct is rejected' '\n> +\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n> +\ttest_grep \"cannot combine\" err\n> +'\n> +\n> +test_expect_success 'checkout --track=direct,inherit is rejected' '\n> +\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n> +\ttest_grep \"cannot combine\" err\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n> +\ttest_commit -C fetch_upstream u_lastwin &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n> +\ttest_must_fail git checkout --track=fetch --track=direct \\\n> +\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_notrack &&\n> +\ttest_commit -C fetch_upstream u_notrack &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n> +\ttest_must_fail git checkout --track=fetch --no-track \\\n> +\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_inherit &&\n> +\ttest_commit -C fetch_upstream u_inherit &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n> +\tgit checkout --track=fetch,inherit -b local_inherit \\\n> +\t\tfetch_upstream/fetch_inherit &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n> +\ttest_grep \"no configured remote\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bad\n> +'\n> +\n> +test_expect_success 'checkout --track=fetch on local start-point errors' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n> +\ttest_grep \"no configured remote\" err &&\n> +\ttest_must_fail git rev-parse --verify refs/heads/bad\n> +'\n> +\n> +test_expect_success 'checkout --track=bogus reports an error' '\n> +\tgit checkout main &&\n> +\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n> +\ttest_grep \"expects\" err\n> +'\n> +\n> +test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_quiet &&\n> +\ttest_commit -C fetch_upstream u_quiet &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n> +\tgit checkout -q --track=fetch -b local_quiet \\\n> +\t\tfetch_upstream/fetch_quiet 2>err &&\n> +\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n> +'\n> +\n> +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n> +\tgit checkout main &&\n> +\tgit -C fetch_upstream checkout -b fetch_switch &&\n> +\ttest_commit -C fetch_upstream u_switch &&\n> +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> +\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n> +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> +'\n> +\n>   test_done\n\n"},{"id":"546250","messageId":"CAHwyqnWwyPHiaOW+rz-Z9ZvRf=OjXWw2T+rB3cSsxXWXkeRm=Q@mail.gmail.com","threadId":"65546","inReplyTo":"12998c3a-ff69-4a98-9ed6-18aa0224e75e@gmail.com","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-23T17:47:19Z","receivedAt":"2026-06-23T17:47:59Z","isPatch":true,"body":"Ok, let's focus on the need for the feature before talking code:\n\nIn an active project, forking from \"origin/master\" without refreshing\nfirst often has consequences: you start work that has already been\ndone, or you build on an old version of the code which causes big\nconflicts only later when you pull. The fix is simple (\"git fetch\norigin master && git checkout -b topic origin/master\"), but it is\nstill a mouthful. Other tools exist because this is annoying enough\nthat people automate it.\n\nConsider instead that the cost of a fetch is nothing.\n\nWhen a new user types \"git checkout -b topic origin/master\", I assume\ntheir mental model is already \"start from the LATEST origin/master\".\nThe fetch is implicit, automating it matches what they probably\nalready meant.\n\n\nHarald\n\nOn Tue, Jun 23, 2026 at 3:49 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Harald\n>\n> On 18/06/2026 13:44, Harald Nordgren via GitGitGadget wrote:\n> > From: Harald Nordgren <haraldnordgren@gmail.com>\n> >\n> > Add a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\n> > switch\" that refreshes <start-point> before checking it out:\n> >\n> >      git checkout -b new_branch --track=fetch origin/some-branch\n> >\n> > is shorthand for\n> >\n> >      git fetch origin some-branch\n> >      git checkout -b new_branch --track origin/some-branch\n> >\n> > Identify the remote whose configured fetch refspec maps to\n> > <start-point> using find_tracking_remote_for_ref() (the same lookup\n> > \"--track\" uses to pick which remote to record in\n> > branch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\n> > just that ref so other remote-tracking branches are left untouched.\n> > When <start-point> is a bare <remote> (e.g. \"origin\"), follow\n> > refs/remotes/<remote>/HEAD to learn which branch to refresh. If\n> > \"git fetch\" fails but the remote-tracking ref already exists locally,\n> > warn and proceed from the existing tip; otherwise abort.\n>\n> This describes the feature well, but does not really explain why it is\n> convenient to have a shorthand for \"git fetch ... && git checkout -b\n> ...\". For example if the reason is that in a fast-moving project you\n> want to start your new work off the latest upstream changes to minimize\n> the chance of merge conflicts or duplicated work it would be useful to\n> say that. As Junio has said the implementation looks pretty solid I've\n> left a few comments below, but the important thing to do first is to\n> convince others that this is a useful feature and why it is worth\n> blurring the separation between fetch and checkout. You can do that\n> without sending a new version.\n>\n> > diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\n> > index a8b3b8c2e2..20b6cae60e 100644\n> > --- a/Documentation/git-checkout.adoc\n> > +++ b/Documentation/git-checkout.adoc\n> > @@ -158,11 +158,26 @@ of it\").\n> >       resets _<branch>_ to the start point instead of failing.\n> >\n> >   `-t`::\n> > -`--track[=(direct|inherit)]`::\n> > +`--track[=(direct|inherit|fetch)[,...]]`::\n> >       When creating a new branch, set up \"upstream\" configuration. See\n> >       `--track` in linkgit:git-branch[1] for details. As a convenience,\n> >       --track without -b implies branch creation.\n> >   +\n> > +The argument is a comma-separated list. `direct` (the default) and\n> > +`inherit` select the tracking mode and are mutually exclusive. Adding\n> > +`fetch` requests that the remote be fetched before _<start-point>_ is\n> > +resolved, so the new branch starts from a fresh tip: when\n> > +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n> > +updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n> > +branch named by _<remote>/HEAD_ is updated, and the checkout fails\n> > +with a hint to configure that symref if it is not set. The checkout\n> > +also fails if no configured remote's fetch refspec maps to\n> > +_<start-point>_, or if more than one does (in which case the `fetch`\n> > +cannot be unambiguously routed). If the fetch itself fails and the\n> > +corresponding remote-tracking ref already exists, a warning is printed\n> > +and the checkout proceeds from the existing tip; otherwise the checkout\n> > +is aborted.\n>\n> Nicely explained\n>\n> > +static void fetch_remote_for_start_point(const char *arg, int quiet)\n> > +{\n> > +     struct strbuf dst = STRBUF_INIT;\n> > +     struct tracking tracking;\n> > +     struct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n> > +     struct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n> > +     struct child_process cmd = CHILD_PROCESS_INIT;\n> > +     struct object_id oid;\n> > +     struct remote *named_remote;\n> > +     int bare_ns;\n> > +\n> > +     strbuf_addf(&dst, \"refs/remotes/%s\", arg);\n> > +     if (check_refname_format(dst.buf, 0))\n> > +             die(_(\"cannot fetch start-point '%s': not a valid \"\n> > +                   \"remote-tracking name\"), arg);\n> > +\n> > +     named_remote = remote_get(arg);\n> > +     bare_ns = !strchr(arg, '/') ||\n> > +             (named_remote && remote_is_configured(named_remote, 1));\n> > +     if (bare_ns) {\n> > +             char *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n> > +             const char *head_target =\n> > +                     refs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n> > +                                             head_path,\n> > +                                             RESOLVE_REF_READING |\n> > +                                             RESOLVE_REF_NO_RECURSE,\n>\n> Why do we use RESOLVE_REF_NO_RECURSE here? This should match whatever\n> \"git checkout -b <remote>\" does.\n>\n> > +                                             &oid, NULL);\n> > +             if (head_target &&\n> > +                 starts_with(head_target, dst.buf) &&\n> > +                 head_target[dst.len] == '/' &&\n> > +                 !check_refname_format(head_target, 0)) {\n>\n> I don't think there is any need to call check_refname_format() here -\n> you're using the result of reading a ref, not some untrusted input.\n>\n> > +                     strbuf_reset(&dst);\n> > +                     strbuf_addstr(&dst, head_target);\n> > +                     bare_ns = 0;\n> > +             }\n> > +             free(head_path);\n> > +     }\n> > +\n> > +     memset(&tracking, 0, sizeof(tracking));\n>\n> When you want to zero initialize a stack variable it is easier, clearer\n> and less error-prone to initialize it by adding \"= {0};\" where it is\n> declared.\n>\n> > +     tracking.spec.dst = dst.buf;\n> > +     tracking.srcs = &tracking_srcs;\n> > +     find_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n> > +\n> > +     if (tracking.matches > 1) {\n> > +             int status = die_message(_(\"cannot fetch start-point '%s': \"\n> > +                                        \"fetch refspecs of multiple remotes \"\n> > +                                        \"map to '%s'\"), arg, dst.buf);\n> > +             advise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n> > +             exit(status);\n> > +     }\n> > +\n> > +     if (!tracking.matches) {\n> > +             if (bare_ns && named_remote &&\n> > +                 remote_is_configured(named_remote, 1))\n> > +                     die(_(\"cannot fetch start-point '%s': \"\n> > +                           \"'refs/remotes/%s/HEAD' is not set; run \"\n> > +                           \"'git remote set-head %s --auto' to set it\")\n>\n> This is quite a long message for a single line - breaking the line and\n> putting the suggested command on a separate line would make it clearer.\n> Something like\n>\n> cannot fetch start-point 'origin' because 'refs/remotes/origin/HEAD'\n> does not exist. To create it run\n>\n>      git remote set-head origin --auto\n>\n> > +                         arg, arg, arg);\n> > +             die(_(\"cannot fetch start-point '%s': no configured remote's \"\n> > +                   \"fetch refspec matches it\"), arg);\n> > +     }\n> > +\n> > +     strvec_push(&cmd.args, \"fetch\");\n> > +     if (quiet)\n> > +             strvec_push(&cmd.args, \"--quiet\");\n> > +     strvec_pushl(&cmd.args, tracking.remote,\n> > +                  tracking_srcs.items[0].string, NULL);\n> > +     cmd.git_cmd = 1;\n> > +     if (run_command(&cmd)) {\n> > +             if (!refs_read_ref(get_main_ref_store(the_repository),\n> > +                                dst.buf, &oid))\n>\n> You can use refs_ref_exists() to check a ref exists which avoids\n> declaring \"oid\" which we're not interested in here.\n>\n> > +                     warning(_(\"failed to fetch start-point '%s'; \"\n> > +                               \"using existing '%s'\"), arg, dst.buf);\n> > +             else\n> > +                     die(_(\"failed to fetch start-point '%s'\"), arg);\n> > +     }\n> > +\n> > +     string_list_clear(&tracking_srcs, 0);\n> > +     string_list_clear(&ambiguous_remotes, 0);\n> > +     strbuf_release(&dst);\n> > +}\n> > +\n> > +static int parse_opt_checkout_track(const struct option *opt,\n> > +                                 const char *arg, int unset)\n> > +{\n> > +     struct checkout_opts *opts = opt->value;\n> > +     struct string_list tokens = STRING_LIST_INIT_DUP;\n> > +     struct string_list_item *item;\n> > +     int saw_direct = 0;\n> > +     int ret = 0;\n> > +\n> > +     opts->fetch = 0;\n> > +     if (unset) {\n> > +             opts->track = BRANCH_TRACK_NEVER;\n> > +             return 0;\n> > +     }\n> > +     opts->track = BRANCH_TRACK_EXPLICIT;\n> > +     if (!arg)\n> > +             return 0;\n> > +\n> > +     string_list_split(&tokens, arg, \",\", -1);\n> > +     for_each_string_list_item(item, &tokens) {\n> > +             if (!strcmp(item->string, \"fetch\"))\n> > +                     opts->fetch = 1;\n> > +             else if (!strcmp(item->string, \"direct\"))\n> > +                     saw_direct = 1;\n> > +             else if (!strcmp(item->string, \"inherit\"))\n> > +                     opts->track = BRANCH_TRACK_INHERIT;\n> > +             else {\n> > +                     ret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n> > +                                   \"or \\\"%s\\\"\"),\n> > +                                 \"--track\", \"direct\", \"inherit\", \"fetch\");\n> > +                     goto out;\n> > +             }\n> > +     }\n> > +     if (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n> > +             ret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n> > +                         \"--track\", \"direct\", \"inherit\");\n>\n> This parsing looks good\n> > diff --git a/t/t7201-co.sh b/t/t7201-co.sh\n> > index 7613b1d2a4..1e321b1512 100755\n> > --- a/t/t7201-co.sh\n> > +++ b/t/t7201-co.sh\n> > @@ -870,4 +870,280 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n> >       test_cmp_config \"\" --default \"\" branch.main2.merge\n> >   '\n>\n> I've not read the tests in detail but there seem to be an awful lot of\n> them. We only need to test each thing once so for example if we test\n>\n>      git checkout --track=fetch -b <remote-ref>\n>\n> with a fetch refspec, that maps refs/heads/*:refs/remotes/origin/xxx/*\n> then we don't need to test it without that refspec. I notice you use\n> \"namespace\" below with is confusing because it is not referring to the\n> feature described in the gitnamespaces(7) man page.\n>\n> Try and avoid\n>\n>      test $a = $b\n>\n> as it makes it hard to debug failing tests. Instead I think you can use\n> test_cmp_rev in this case.\n>\n> Thanks\n>\n> Phillip\n>\n> > +test_expect_success 'setup upstream for --track=fetch tests' '\n> > +     git checkout main &&\n> > +     git init fetch_upstream &&\n> > +     test_commit -C fetch_upstream u_main &&\n> > +     git remote add fetch_upstream fetch_upstream &&\n> > +     git fetch fetch_upstream &&\n> > +     git -C fetch_upstream checkout -b fetch_new &&\n> > +     test_commit -C fetch_upstream u_new\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n> > +     git checkout main &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n> > +     git checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n> > +     test_cmp_config fetch_upstream branch.local_new.remote &&\n> > +     test_cmp_config refs/heads/fetch_new branch.local_new.merge\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_target &&\n> > +     test_commit -C fetch_upstream u_target_pre &&\n> > +     git -C fetch_upstream checkout -b fetch_other &&\n> > +     test_commit -C fetch_upstream u_other_pre &&\n> > +     git fetch fetch_upstream &&\n> > +     other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n> > +     git -C fetch_upstream checkout fetch_target &&\n> > +     test_commit -C fetch_upstream u_target_post &&\n> > +     git -C fetch_upstream checkout fetch_other &&\n> > +     test_commit -C fetch_upstream u_other_post &&\n> > +     git checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n> > +     test \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout main &&\n> > +     git remote set-head fetch_upstream main &&\n> > +     git -C fetch_upstream checkout -b fetch_unrelated &&\n> > +     test_commit -C fetch_upstream u_unrelated_pre &&\n> > +     git fetch fetch_upstream fetch_unrelated &&\n> > +     unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n> > +     git -C fetch_upstream checkout main &&\n> > +     test_commit -C fetch_upstream u_main_post &&\n> > +     git -C fetch_upstream checkout fetch_unrelated &&\n> > +     test_commit -C fetch_upstream u_unrelated_post &&\n> > +     git checkout --track=fetch -b local_from_remote fetch_upstream &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n> > +     test \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n> > +     git checkout main &&\n> > +     test_might_fail git branch -D bogus &&\n> > +     test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n> > +     test_must_fail git rev-parse --verify refs/heads/bogus\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_offline &&\n> > +     test_commit -C fetch_upstream u_offline &&\n> > +     git fetch fetch_upstream fetch_offline &&\n> > +     saved_url=$(git config remote.fetch_upstream.url) &&\n> > +     test_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n> > +     git config remote.fetch_upstream.url ./does-not-exist &&\n> > +     git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n> > +     test_grep \"failed to fetch\" err &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n> > +     git checkout main &&\n> > +     git remote add fetch_custom ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove fetch_custom\" &&\n> > +     git config --replace-all remote.fetch_custom.fetch \\\n> > +             \"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n> > +     git -C fetch_upstream checkout -b fetch_refspec &&\n> > +     test_commit -C fetch_upstream u_refspec &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n> > +     git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n> > +     test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n> > +     git checkout main &&\n> > +     git remote add fetch_ns ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove fetch_ns\" &&\n> > +     test_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n> > +     git config --replace-all remote.fetch_ns.fetch \\\n> > +             \"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n> > +     git fetch fetch_ns &&\n> > +     git symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n> > +     git -C fetch_upstream checkout main &&\n> > +     test_commit -C fetch_upstream u_ns_post &&\n> > +     git checkout --track=fetch -b local_ns ns_alias &&\n> > +     test_cmp_rev refs/remotes/ns_alias/main HEAD &&\n> > +     test_cmp_config fetch_ns branch.local_ns.remote &&\n> > +     test_cmp_config refs/heads/main branch.local_ns.merge\n> > +'\n> > +\n> > +test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n> > +     git checkout main &&\n> > +     git remote add nested/bare ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove nested/bare\" &&\n> > +     test_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n> > +     git fetch nested/bare &&\n> > +     git symbolic-ref refs/remotes/nested/bare/HEAD \\\n> > +             refs/remotes/nested/bare/main &&\n> > +     git -C fetch_upstream checkout main &&\n> > +     test_commit -C fetch_upstream u_nested_bare_post &&\n> > +     git checkout --track=fetch -b local_nested_bare nested/bare &&\n> > +     test_cmp_rev refs/remotes/nested/bare/main HEAD\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n> > +     git checkout main &&\n> > +     git remote add nested/remote ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove nested/remote\" &&\n> > +     git -C fetch_upstream checkout -b fetch_hier &&\n> > +     test_commit -C fetch_upstream u_hier &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n> > +     git checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n> > +     test_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n> > +     git checkout main &&\n> > +     git remote add fetch_nohead ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove fetch_nohead\" &&\n> > +     test_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n> > +     test_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n> > +     test_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n> > +     test_grep \"git remote set-head fetch_nohead --auto\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_nohead\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n> > +     git checkout main &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n> > +     test_must_fail git config --get remote.no_such_ns.url &&\n> > +     test_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n> > +     test_grep \"no configured remote\" err &&\n> > +     test_grep ! \"set-head\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_unknown\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n> > +     git checkout main &&\n> > +     git remote add fetch_crossns ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove fetch_crossns\" &&\n> > +     test_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n> > +     git fetch fetch_crossns &&\n> > +     git symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n> > +             refs/remotes/fetch_upstream/u_main &&\n> > +     test_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n> > +     test_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_crossns\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n> > +     git checkout main &&\n> > +     git remote add fetch_ambig_a ./fetch_upstream &&\n> > +     git remote add fetch_ambig_b ./fetch_upstream &&\n> > +     test_when_finished \"git remote remove fetch_ambig_a\" &&\n> > +     test_when_finished \"git remote remove fetch_ambig_b\" &&\n> > +     git config --replace-all remote.fetch_ambig_a.fetch \\\n> > +             \"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n> > +     git config --replace-all remote.fetch_ambig_b.fetch \\\n> > +             \"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n> > +     git -C fetch_upstream checkout -b fetch_ambig &&\n> > +     test_commit -C fetch_upstream u_ambig &&\n> > +     test_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n> > +     test_grep \"fetch_ambig_a\" err &&\n> > +     test_grep \"fetch_ambig_b\" err &&\n> > +     test_grep \"tracking namespaces\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_ambig\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n> > +     git checkout main &&\n> > +     test_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n> > +     test_grep \"valid\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_invalid\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n> > +     git checkout main &&\n> > +     test_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n> > +             \"foo..bar\" 2>err &&\n> > +     test_grep \"valid\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/local_invalid\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=inherit,direct is rejected' '\n> > +     test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n> > +     test_grep \"cannot combine\" err\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=direct,inherit is rejected' '\n> > +     test_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n> > +     test_grep \"cannot combine\" err\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_lastwin &&\n> > +     test_commit -C fetch_upstream u_lastwin &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n> > +     test_must_fail git checkout --track=fetch --track=direct \\\n> > +             -b local_lastwin fetch_upstream/fetch_lastwin &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_notrack &&\n> > +     test_commit -C fetch_upstream u_notrack &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n> > +     test_must_fail git checkout --track=fetch --no-track \\\n> > +             -b local_notrack fetch_upstream/fetch_notrack &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_inherit &&\n> > +     test_commit -C fetch_upstream u_inherit &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n> > +     git checkout --track=fetch,inherit -b local_inherit \\\n> > +             fetch_upstream/fetch_inherit &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n> > +     git checkout main &&\n> > +     test_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n> > +     test_grep \"no configured remote\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/bad\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=fetch on local start-point errors' '\n> > +     git checkout main &&\n> > +     test_must_fail git checkout --track=fetch -b bad main 2>err &&\n> > +     test_grep \"no configured remote\" err &&\n> > +     test_must_fail git rev-parse --verify refs/heads/bad\n> > +'\n> > +\n> > +test_expect_success 'checkout --track=bogus reports an error' '\n> > +     git checkout main &&\n> > +     test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n> > +     test_grep \"expects\" err\n> > +'\n> > +\n> > +test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_quiet &&\n> > +     test_commit -C fetch_upstream u_quiet &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n> > +     git checkout -q --track=fetch -b local_quiet \\\n> > +             fetch_upstream/fetch_quiet 2>err &&\n> > +     test_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n> > +'\n> > +\n> > +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n> > +     git checkout main &&\n> > +     git -C fetch_upstream checkout -b fetch_switch &&\n> > +     test_commit -C fetch_upstream u_switch &&\n> > +     test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n> > +     git switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n> > +     test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n> > +'\n> > +\n> >   test_done\n>\n"},{"id":"546347","messageId":"pull.2281.v15.git.git.1782338098.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v14.git.git.1781786652.gitgitgadget@gmail.com","subject":"[PATCH v15 0/2] checkout: --track=fetch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-24T21:54:56Z","receivedAt":"2026-06-24T21:55:00Z","isPatch":true,"body":"Extend checkout --track with a fetch mode to refresh start-point.\n\nChanges in v15:\n\n * Reword commit message to lead with motivation.\n * Drop RESOLVE_REF_NO_RECURSE so <ns>/HEAD lookup matches what git checkout\n   does for the same ref. Drop redundant check_refname_format on a ref we\n   just read. Replace memset with brace initializer. Use refs_ref_exists and\n   pass NULL for the OID out-params.\n * Split the \"set HEAD\" error onto its own line via die_message and advise,\n   matching the suggested format.\n * Remove 6 redundant tests, replace test $a = $b with test_cmp_rev, and\n   rename test titles to avoid \"namespace\".\n\nChanges in v14:\n\n * Handle .h files in a better way.\n\nChanges in v13:\n\n * Create a preparatory commit that exposes find_tracking_remote_for_ref()\n   and advise_ambiguous_fetch_refspec() from branch.c, so checkout can reuse\n   the same lookup git branch --track uses.\n * Use advise_ambiguous_fetch_refspec() for the \"multiple remotes match\"\n   case, so the wording matches git branch --track.\n\nHarald Nordgren (2):\n  branch: expose helpers for finding the remote owning a tracking ref\n  checkout: extend --track with a \"fetch\" mode to refresh start-point\n\n Documentation/git-checkout.adoc |  17 ++-\n Documentation/git-switch.adoc   |   5 +-\n branch.c                        |  96 +++++++-------\n branch.h                        |  16 +++\n builtin/checkout.c              | 138 +++++++++++++++++++-\n t/t7201-co.sh                   | 222 ++++++++++++++++++++++++++++++++\n 6 files changed, 443 insertions(+), 51 deletions(-)\n\n\nbase-commit: ab776a62a78576513ee121424adb19597fbb7613\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v15\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v15\nPull-Request: https://github.com/git/git/pull/2281\n\nRange-diff vs v14:\n\n 1:  f79689c23d = 1:  8139490c36 branch: expose helpers for finding the remote owning a tracking ref\n 2:  8518f090b1 ! 2:  b7e387f123 checkout: extend --track with a \"fetch\" mode to refresh start-point\n     @@ Metadata\n       ## Commit message ##\n          checkout: extend --track with a \"fetch\" mode to refresh start-point\n      \n     -    Add a \"fetch\" mode to the \"--track\" option of \"git checkout\" / \"git\n     -    switch\" that refreshes <start-point> before checking it out:\n     +    Forking from an existing remote branch without refreshing first often\n     +    has consequences: you start work that has already been done, or you\n     +    build on an old version of the code which causes big conflicts later\n     +    when you pull. The workaround is two commands (\"git fetch <remote>\n     +    <branch> && git checkout -b <topic> <remote>/<branch>\"), and when\n     +    the fetch is skipped the checkout silently starts from a stale tip.\n      \n     -        git checkout -b new_branch --track=fetch origin/some-branch\n     +    Users may already expect \"<remote>/<branch>\" to refer to the latest\n     +    tip on the remote. While this blurs the line between fetch and\n     +    checkout, git already does this in places where it pays off: \"git\n     +    clone\" fetches and checks out, and \"git pull\" fetches and merges.\n      \n     -    is shorthand for\n     +    Add a \"fetch\" mode to \"--track\" that refreshes <start-point> before\n     +    checking it out:\n      \n     -        git fetch origin some-branch\n     -        git checkout -b new_branch --track origin/some-branch\n     +        git checkout -b new_branch --track=fetch origin/some-branch\n      \n     -    Identify the remote whose configured fetch refspec maps to\n     -    <start-point> using find_tracking_remote_for_ref() (the same lookup\n     -    \"--track\" uses to pick which remote to record in\n     -    branch.<name>.remote), then run \"git fetch <remote> <src-ref>\" for\n     -    just that ref so other remote-tracking branches are left untouched.\n     -    When <start-point> is a bare <remote> (e.g. \"origin\"), follow\n     -    refs/remotes/<remote>/HEAD to learn which branch to refresh. If\n     -    \"git fetch\" fails but the remote-tracking ref already exists locally,\n     -    warn and proceed from the existing tip; otherwise abort.\n     +    Only the requested branch is fetched so other remote-tracking\n     +    branches are left untouched. When <start-point> is a bare <remote>\n     +    (e.g. \"origin\"), follow refs/remotes/<remote>/HEAD to learn which\n     +    branch to refresh. If \"git fetch\" fails but the remote-tracking ref\n     +    already exists locally, warn and proceed from the existing tip,\n     +    otherwise abort.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ builtin/checkout.c: struct branch_info {\n      +static void fetch_remote_for_start_point(const char *arg, int quiet)\n      +{\n      +\tstruct strbuf dst = STRBUF_INIT;\n     -+\tstruct tracking tracking;\n     ++\tstruct tracking tracking = { 0 };\n      +\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n      +\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n      +\tstruct child_process cmd = CHILD_PROCESS_INIT;\n     -+\tstruct object_id oid;\n      +\tstruct remote *named_remote;\n      +\tint bare_ns;\n      +\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\tconst char *head_target =\n      +\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n      +\t\t\t\t\t\thead_path,\n     -+\t\t\t\t\t\tRESOLVE_REF_READING |\n     -+\t\t\t\t\t\tRESOLVE_REF_NO_RECURSE,\n     -+\t\t\t\t\t\t&oid, NULL);\n     ++\t\t\t\t\t\tRESOLVE_REF_READING,\n     ++\t\t\t\t\t\tNULL, NULL);\n      +\t\tif (head_target &&\n      +\t\t    starts_with(head_target, dst.buf) &&\n     -+\t\t    head_target[dst.len] == '/' &&\n     -+\t\t    !check_refname_format(head_target, 0)) {\n     ++\t\t    head_target[dst.len] == '/') {\n      +\t\t\tstrbuf_reset(&dst);\n      +\t\t\tstrbuf_addstr(&dst, head_target);\n      +\t\t\tbare_ns = 0;\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\tfree(head_path);\n      +\t}\n      +\n     -+\tmemset(&tracking, 0, sizeof(tracking));\n      +\ttracking.spec.dst = dst.buf;\n      +\ttracking.srcs = &tracking_srcs;\n      +\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n     @@ builtin/checkout.c: struct branch_info {\n      +\n      +\tif (!tracking.matches) {\n      +\t\tif (bare_ns && named_remote &&\n     -+\t\t    remote_is_configured(named_remote, 1))\n     -+\t\t\tdie(_(\"cannot fetch start-point '%s': \"\n     -+\t\t\t      \"'refs/remotes/%s/HEAD' is not set; run \"\n     -+\t\t\t      \"'git remote set-head %s --auto' to set it\"),\n     -+\t\t\t    arg, arg, arg);\n     ++\t\t    remote_is_configured(named_remote, 1)) {\n     ++\t\t\tint status = die_message(_(\"cannot fetch start-point '%s' \"\n     ++\t\t\t\t\t\t   \"because 'refs/remotes/%s/HEAD' \"\n     ++\t\t\t\t\t\t   \"does not exist.\"), arg, arg);\n     ++\t\t\tadvise(_(\"To create it run\\n\"\n     ++\t\t\t\t \"\\n\"\n     ++\t\t\t\t \"    git remote set-head %s --auto\\n\"), arg);\n     ++\t\t\texit(status);\n     ++\t\t}\n      +\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n      +\t\t      \"fetch refspec matches it\"), arg);\n      +\t}\n     @@ builtin/checkout.c: struct branch_info {\n      +\t\t     tracking_srcs.items[0].string, NULL);\n      +\tcmd.git_cmd = 1;\n      +\tif (run_command(&cmd)) {\n     -+\t\tif (!refs_read_ref(get_main_ref_store(the_repository),\n     -+\t\t\t\t   dst.buf, &oid))\n     ++\t\tif (refs_ref_exists(get_main_ref_store(the_repository), dst.buf))\n      +\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n      +\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n      +\t\telse\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\tgit -C fetch_upstream checkout -b fetch_other &&\n      +\ttest_commit -C fetch_upstream u_other_pre &&\n      +\tgit fetch fetch_upstream &&\n     -+\tother_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) &&\n     ++\tgit update-ref refs/heads/snapshot_other refs/remotes/fetch_upstream/fetch_other &&\n      +\tgit -C fetch_upstream checkout fetch_target &&\n      +\ttest_commit -C fetch_upstream u_target_post &&\n      +\tgit -C fetch_upstream checkout fetch_other &&\n      +\ttest_commit -C fetch_upstream u_other_post &&\n      +\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n     -+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_other)\" = \"$other_before\"\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_other snapshot_other\n      +'\n      +\n      +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n      +\ttest_commit -C fetch_upstream u_unrelated_pre &&\n      +\tgit fetch fetch_upstream fetch_unrelated &&\n     -+\tunrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) &&\n     ++\tgit update-ref refs/heads/snapshot_unrelated \\\n     ++\t\trefs/remotes/fetch_upstream/fetch_unrelated &&\n      +\tgit -C fetch_upstream checkout main &&\n      +\ttest_commit -C fetch_upstream u_main_post &&\n      +\tgit -C fetch_upstream checkout fetch_unrelated &&\n      +\ttest_commit -C fetch_upstream u_unrelated_post &&\n      +\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n     -+\ttest \"$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)\" = \"$unrelated_before\"\n     ++\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_unrelated snapshot_unrelated\n      +'\n      +\n      +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch on namespace bare name follows <ns>/HEAD' '\n     ++test_expect_success 'checkout --track=fetch on bare remote-tracking prefix follows <prefix>/HEAD' '\n      +\tgit checkout main &&\n      +\tgit remote add fetch_ns ./fetch_upstream &&\n      +\ttest_when_finished \"git remote remove fetch_ns\" &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_config refs/heads/main branch.local_ns.merge\n      +'\n      +\n     -+test_expect_success '--track=fetch on bare hierarchical remote name follows <ns>/HEAD' '\n     -+\tgit checkout main &&\n     -+\tgit remote add nested/bare ./fetch_upstream &&\n     -+\ttest_when_finished \"git remote remove nested/bare\" &&\n     -+\ttest_when_finished \"git update-ref -d refs/remotes/nested/bare/HEAD\" &&\n     -+\tgit fetch nested/bare &&\n     -+\tgit symbolic-ref refs/remotes/nested/bare/HEAD \\\n     -+\t\trefs/remotes/nested/bare/main &&\n     -+\tgit -C fetch_upstream checkout main &&\n     -+\ttest_commit -C fetch_upstream u_nested_bare_post &&\n     -+\tgit checkout --track=fetch -b local_nested_bare nested/bare &&\n     -+\ttest_cmp_rev refs/remotes/nested/bare/main HEAD\n     -+'\n     -+\n     -+test_expect_success 'checkout --track=fetch handles hierarchical remote name' '\n     -+\tgit checkout main &&\n     -+\tgit remote add nested/remote ./fetch_upstream &&\n     -+\ttest_when_finished \"git remote remove nested/remote\" &&\n     -+\tgit -C fetch_upstream checkout -b fetch_hier &&\n     -+\ttest_commit -C fetch_upstream u_hier &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/nested/remote/fetch_hier &&\n     -+\tgit checkout --track=fetch -b local_hier nested/remote/fetch_hier &&\n     -+\ttest_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD\n     -+'\n     -+\n      +test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n      +\tgit checkout main &&\n      +\tgit remote add fetch_nohead ./fetch_upstream &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside namespace' '\n     ++test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside the tracking prefix' '\n      +\tgit checkout main &&\n      +\tgit remote add fetch_crossns ./fetch_upstream &&\n      +\ttest_when_finished \"git remote remove fetch_crossns\" &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch,inherit rejects invalid refname components' '\n     -+\tgit checkout main &&\n     -+\ttest_must_fail git checkout --track=fetch,inherit -b local_invalid \\\n     -+\t\t\"foo..bar\" 2>err &&\n     -+\ttest_grep \"valid\" err &&\n     -+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n     -+'\n     -+\n      +test_expect_success 'checkout --track=inherit,direct is rejected' '\n      +\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n      +\ttest_grep \"cannot combine\" err\n      +'\n      +\n     -+test_expect_success 'checkout --track=direct,inherit is rejected' '\n     -+\ttest_must_fail git checkout --track=direct,inherit -b bad fetch_upstream/fetch_new 2>err &&\n     -+\ttest_grep \"cannot combine\" err\n     -+'\n     -+\n      +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch then --no-track drops fetch' '\n     -+\tgit checkout main &&\n     -+\tgit -C fetch_upstream checkout -b fetch_notrack &&\n     -+\ttest_commit -C fetch_upstream u_notrack &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack &&\n     -+\ttest_must_fail git checkout --track=fetch --no-track \\\n     -+\t\t-b local_notrack fetch_upstream/fetch_notrack &&\n     -+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_notrack\n     -+'\n     -+\n      +test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n      +\tgit checkout main &&\n      +\tgit -C fetch_upstream checkout -b fetch_inherit &&\n     @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh\n      +\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n      +'\n      +\n     -+test_expect_success 'checkout --track=fetch,inherit errors when start-point does not map to a remote' '\n     -+\tgit checkout main &&\n     -+\ttest_must_fail git checkout --track=fetch,inherit -b bad main 2>err &&\n     -+\ttest_grep \"no configured remote\" err &&\n     -+\ttest_must_fail git rev-parse --verify refs/heads/bad\n     -+'\n     -+\n      +test_expect_success 'checkout --track=fetch on local start-point errors' '\n      +\tgit checkout main &&\n      +\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n\n-- \ngitgitgadget\n"},{"id":"546348","messageId":"8139490c369eb858085118893975cf05efeae142.1782338098.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v15.git.git.1782338098.gitgitgadget@gmail.com","subject":"[PATCH v15 1/2] branch: expose helpers for finding the remote owning a tracking ref","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-24T21:54:57Z","receivedAt":"2026-06-24T21:55:02Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe remote-lookup that setup_tracking() does is useful outside\nbranch.c too; for example, deciding which remote to \"git fetch\"\nfrom given a remote-tracking ref.\n\nMove 'struct tracking' to branch.h and add two helpers backed by the\nexisting for_each_remote walk: find_tracking_remote_for_ref() and\nadvise_ambiguous_fetch_refspec(). setup_tracking() uses both. No\nbehavior change.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n branch.c | 96 ++++++++++++++++++++++++++++++--------------------------\n branch.h | 16 ++++++++++\n 2 files changed, 68 insertions(+), 44 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 243db7d0fc..46ae7f0035 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -20,16 +20,9 @@\n #include \"run-command.h\"\n #include \"strmap.h\"\n \n-struct tracking {\n-\tstruct refspec_item spec;\n-\tstruct string_list *srcs;\n-\tconst char *remote;\n-\tint matches;\n-};\n-\n struct find_tracked_branch_cb {\n \tstruct tracking *tracking;\n-\tstruct string_list ambiguous_remotes;\n+\tstruct string_list *ambiguous_remotes;\n };\n \n static int find_tracked_branch(struct remote *remote, void *priv)\n@@ -45,10 +38,10 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \t\t\tbreak;\n \t\tcase 2:\n \t\t\t/* there are at least two remotes; backfill the first one */\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, tracking->remote);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, tracking->remote);\n \t\t\t/* fall through */\n \t\tdefault:\n-\t\t\tstring_list_append(&ftb->ambiguous_remotes, remote->name);\n+\t\t\tstring_list_append(ftb->ambiguous_remotes, remote->name);\n \t\t\tfree(tracking->spec.src);\n \t\t\tstring_list_clear(tracking->srcs, 0);\n \t\tbreak;\n@@ -59,6 +52,51 @@ static int find_tracked_branch(struct remote *remote, void *priv)\n \treturn 0;\n }\n \n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes)\n+{\n+\tstruct find_tracked_branch_cb ftb_cb = {\n+\t\t.tracking = tracking,\n+\t\t.ambiguous_remotes = ambiguous_remotes,\n+\t};\n+\n+\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+}\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes)\n+{\n+\tstruct strbuf remotes_advice = STRBUF_INIT;\n+\tstruct string_list_item *item;\n+\n+\tif (!advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC))\n+\t\treturn;\n+\n+\tfor_each_string_list_item(item, ambiguous_remotes)\n+\t\t/*\n+\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n+\t\t * refspecs in the advice message below. For RTL languages you'll\n+\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n+\t\t */\n+\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n+\n+\t/*\n+\t * TRANSLATORS: The second argument is a \\n-delimited list of\n+\t * duplicate refspecs, composed above.\n+\t */\n+\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n+\t\t \"tracking ref '%s':\\n\"\n+\t\t \"%s\"\n+\t\t \"\\n\"\n+\t\t \"This is typically a configuration error.\\n\"\n+\t\t \"\\n\"\n+\t\t \"To support setting up tracking branches, ensure that\\n\"\n+\t\t \"different remotes' fetch refspecs map into different\\n\"\n+\t\t \"tracking namespaces.\"), dst,\n+\t       remotes_advice.buf);\n+\tstrbuf_release(&remotes_advice);\n+}\n+\n static int should_setup_rebase(const char *origin)\n {\n \tswitch (autorebase) {\n@@ -254,11 +292,8 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n {\n \tstruct tracking tracking;\n \tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n \tint config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;\n-\tstruct find_tracked_branch_cb ftb_cb = {\n-\t\t.tracking = &tracking,\n-\t\t.ambiguous_remotes = STRING_LIST_INIT_DUP,\n-\t};\n \n \tif (!track)\n \t\tBUG(\"asked to set up tracking, but tracking is disallowed\");\n@@ -267,7 +302,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \ttracking.spec.dst = (char *)orig_ref;\n \ttracking.srcs = &tracking_srcs;\n \tif (track != BRANCH_TRACK_INHERIT)\n-\t\tfor_each_remote(find_tracked_branch, &ftb_cb);\n+\t\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n \telse if (inherit_tracking(&tracking, orig_ref))\n \t\tgoto cleanup;\n \n@@ -293,34 +328,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \tif (tracking.matches > 1) {\n \t\tint status = die_message(_(\"not tracking: ambiguous information for ref '%s'\"),\n \t\t\t\t\t    orig_ref);\n-\t\tif (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {\n-\t\t\tstruct strbuf remotes_advice = STRBUF_INIT;\n-\t\t\tstruct string_list_item *item;\n-\n-\t\t\tfor_each_string_list_item(item, &ftb_cb.ambiguous_remotes)\n-\t\t\t\t/*\n-\t\t\t\t * TRANSLATORS: This is a line listing a remote with duplicate\n-\t\t\t\t * refspecs in the advice message below. For RTL languages you'll\n-\t\t\t\t * probably want to swap the \"%s\" and leading \"  \" space around.\n-\t\t\t\t */\n-\t\t\t\tstrbuf_addf(&remotes_advice, _(\"  %s\\n\"), item->string);\n-\n-\t\t\t/*\n-\t\t\t * TRANSLATORS: The second argument is a \\n-delimited list of\n-\t\t\t * duplicate refspecs, composed above.\n-\t\t\t */\n-\t\t\tadvise(_(\"There are multiple remotes whose fetch refspecs map to the remote\\n\"\n-\t\t\t\t \"tracking ref '%s':\\n\"\n-\t\t\t\t \"%s\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"This is typically a configuration error.\\n\"\n-\t\t\t\t \"\\n\"\n-\t\t\t\t \"To support setting up tracking branches, ensure that\\n\"\n-\t\t\t\t \"different remotes' fetch refspecs map into different\\n\"\n-\t\t\t\t \"tracking namespaces.\"), orig_ref,\n-\t\t\t       remotes_advice.buf);\n-\t\t\tstrbuf_release(&remotes_advice);\n-\t\t}\n+\t\tadvise_ambiguous_fetch_refspec(orig_ref, &ambiguous_remotes);\n \t\texit(status);\n \t}\n \n@@ -347,7 +355,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \n cleanup:\n \tstring_list_clear(&tracking_srcs, 0);\n-\tstring_list_clear(&ftb_cb.ambiguous_remotes, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n }\n \n int read_branch_desc(struct strbuf *buf, const char *branch_name)\ndiff --git a/branch.h b/branch.h\nindex 3dc6e2a0ff..c2e6725491 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -1,9 +1,25 @@\n #ifndef BRANCH_H\n #define BRANCH_H\n \n+#include \"refspec.h\"\n+\n+struct string_list;\n struct repository;\n struct strbuf;\n \n+struct tracking {\n+\tstruct refspec_item spec;\n+\tstruct string_list *srcs;\n+\tconst char *remote;\n+\tint matches;\n+};\n+\n+void find_tracking_remote_for_ref(struct tracking *tracking,\n+\t\t\t\t  struct string_list *ambiguous_remotes);\n+\n+void advise_ambiguous_fetch_refspec(const char *dst,\n+\t\t\t\t    const struct string_list *ambiguous_remotes);\n+\n enum branch_track {\n \tBRANCH_TRACK_UNSPECIFIED = -1,\n \tBRANCH_TRACK_NEVER = 0,\n-- \ngitgitgadget\n\n"},{"id":"546351","messageId":"b7e387f1239de376a7934c84e4b60b331b212321.1782338098.git.gitgitgadget@gmail.com","threadId":"65546","inReplyTo":"pull.2281.v15.git.git.1782338098.gitgitgadget@gmail.com","subject":"[PATCH v15 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-24T21:54:58Z","receivedAt":"2026-06-24T21:55:04Z","isPatch":true,"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nForking from an existing remote branch without refreshing first often\nhas consequences: you start work that has already been done, or you\nbuild on an old version of the code which causes big conflicts later\nwhen you pull. The workaround is two commands (\"git fetch <remote>\n<branch> && git checkout -b <topic> <remote>/<branch>\"), and when\nthe fetch is skipped the checkout silently starts from a stale tip.\n\nUsers may already expect \"<remote>/<branch>\" to refer to the latest\ntip on the remote. While this blurs the line between fetch and\ncheckout, git already does this in places where it pays off: \"git\nclone\" fetches and checks out, and \"git pull\" fetches and merges.\n\nAdd a \"fetch\" mode to \"--track\" that refreshes <start-point> before\nchecking it out:\n\n    git checkout -b new_branch --track=fetch origin/some-branch\n\nOnly the requested branch is fetched so other remote-tracking\nbranches are left untouched. When <start-point> is a bare <remote>\n(e.g. \"origin\"), follow refs/remotes/<remote>/HEAD to learn which\nbranch to refresh. If \"git fetch\" fails but the remote-tracking ref\nalready exists locally, warn and proceed from the existing tip,\notherwise abort.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-checkout.adoc |  17 ++-\n Documentation/git-switch.adoc   |   5 +-\n builtin/checkout.c              | 138 +++++++++++++++++++-\n t/t7201-co.sh                   | 222 ++++++++++++++++++++++++++++++++\n 4 files changed, 375 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..20b6cae60e 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -158,11 +158,26 @@ of it\").\n \tresets _<branch>_ to the start point instead of failing.\n \n `-t`::\n-`--track[=(direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration. See\n \t`--track` in linkgit:git-branch[1] for details. As a convenience,\n \t--track without -b implies branch creation.\n +\n+The argument is a comma-separated list. `direct` (the default) and\n+`inherit` select the tracking mode and are mutually exclusive. Adding\n+`fetch` requests that the remote be fetched before _<start-point>_ is\n+resolved, so the new branch starts from a fresh tip: when\n+_<start-point>_ is in _<remote>/<branch>_ form, only that branch is\n+updated; when _<start-point>_ is a bare _<remote>_ (e.g. `origin`), the\n+branch named by _<remote>/HEAD_ is updated, and the checkout fails\n+with a hint to configure that symref if it is not set. The checkout\n+also fails if no configured remote's fetch refspec maps to\n+_<start-point>_, or if more than one does (in which case the `fetch`\n+cannot be unambiguously routed). If the fetch itself fails and the\n+corresponding remote-tracking ref already exists, a warning is printed\n+and the checkout proceeds from the existing tip; otherwise the checkout\n+is aborted.\n++\n If no `-b` option is given, the name of the new branch will be\n derived from the remote-tracking branch, by looking at the local part of\n the refspec configured for the corresponding remote, and then stripping\ndiff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc\nindex d6c4f229a5..a8730b1da8 100644\n--- a/Documentation/git-switch.adoc\n+++ b/Documentation/git-switch.adoc\n@@ -155,10 +155,11 @@ variable.\n \tattached to a terminal, regardless of `--quiet`.\n \n `-t`::\n-`--track[ (direct|inherit)]`::\n+`--track[=(direct|inherit|fetch)[,...]]`::\n \tWhen creating a new branch, set up \"upstream\" configuration.\n \t`-c` is implied. See `--track` in linkgit:git-branch[1] for\n-\tdetails.\n+\tdetails, and `--track` in linkgit:git-checkout[1] for the\n+\t`fetch` mode.\n +\n If no `-c` option is given, the name of the new branch will be derived\n from the remote-tracking branch, by looking at the local part of the\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex b78b3a1d16..c6154fbbb0 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,10 +25,12 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"refspec.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n #include \"revision.h\"\n+#include \"run-command.h\"\n #include \"sequencer.h\"\n #include \"setup.h\"\n #include \"sparse-index.h\"\n@@ -63,6 +65,7 @@ struct checkout_opts {\n \tint count_checkout_paths;\n \tint overlay_mode;\n \tint dwim_new_local_branch;\n+\tint fetch;\n \tint discard_changes;\n \tint accept_ref;\n \tint accept_pathspec;\n@@ -116,6 +119,128 @@ struct branch_info {\n \tchar *checkout;\n };\n \n+static void fetch_remote_for_start_point(const char *arg, int quiet)\n+{\n+\tstruct strbuf dst = STRBUF_INIT;\n+\tstruct tracking tracking = { 0 };\n+\tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n+\tstruct string_list ambiguous_remotes = STRING_LIST_INIT_DUP;\n+\tstruct child_process cmd = CHILD_PROCESS_INIT;\n+\tstruct remote *named_remote;\n+\tint bare_ns;\n+\n+\tstrbuf_addf(&dst, \"refs/remotes/%s\", arg);\n+\tif (check_refname_format(dst.buf, 0))\n+\t\tdie(_(\"cannot fetch start-point '%s': not a valid \"\n+\t\t      \"remote-tracking name\"), arg);\n+\n+\tnamed_remote = remote_get(arg);\n+\tbare_ns = !strchr(arg, '/') ||\n+\t\t(named_remote && remote_is_configured(named_remote, 1));\n+\tif (bare_ns) {\n+\t\tchar *head_path = xstrfmt(\"refs/remotes/%s/HEAD\", arg);\n+\t\tconst char *head_target =\n+\t\t\trefs_resolve_ref_unsafe(get_main_ref_store(the_repository),\n+\t\t\t\t\t\thead_path,\n+\t\t\t\t\t\tRESOLVE_REF_READING,\n+\t\t\t\t\t\tNULL, NULL);\n+\t\tif (head_target &&\n+\t\t    starts_with(head_target, dst.buf) &&\n+\t\t    head_target[dst.len] == '/') {\n+\t\t\tstrbuf_reset(&dst);\n+\t\t\tstrbuf_addstr(&dst, head_target);\n+\t\t\tbare_ns = 0;\n+\t\t}\n+\t\tfree(head_path);\n+\t}\n+\n+\ttracking.spec.dst = dst.buf;\n+\ttracking.srcs = &tracking_srcs;\n+\tfind_tracking_remote_for_ref(&tracking, &ambiguous_remotes);\n+\n+\tif (tracking.matches > 1) {\n+\t\tint status = die_message(_(\"cannot fetch start-point '%s': \"\n+\t\t\t\t\t   \"fetch refspecs of multiple remotes \"\n+\t\t\t\t\t   \"map to '%s'\"), arg, dst.buf);\n+\t\tadvise_ambiguous_fetch_refspec(dst.buf, &ambiguous_remotes);\n+\t\texit(status);\n+\t}\n+\n+\tif (!tracking.matches) {\n+\t\tif (bare_ns && named_remote &&\n+\t\t    remote_is_configured(named_remote, 1)) {\n+\t\t\tint status = die_message(_(\"cannot fetch start-point '%s' \"\n+\t\t\t\t\t\t   \"because 'refs/remotes/%s/HEAD' \"\n+\t\t\t\t\t\t   \"does not exist.\"), arg, arg);\n+\t\t\tadvise(_(\"To create it run\\n\"\n+\t\t\t\t \"\\n\"\n+\t\t\t\t \"    git remote set-head %s --auto\\n\"), arg);\n+\t\t\texit(status);\n+\t\t}\n+\t\tdie(_(\"cannot fetch start-point '%s': no configured remote's \"\n+\t\t      \"fetch refspec matches it\"), arg);\n+\t}\n+\n+\tstrvec_push(&cmd.args, \"fetch\");\n+\tif (quiet)\n+\t\tstrvec_push(&cmd.args, \"--quiet\");\n+\tstrvec_pushl(&cmd.args, tracking.remote,\n+\t\t     tracking_srcs.items[0].string, NULL);\n+\tcmd.git_cmd = 1;\n+\tif (run_command(&cmd)) {\n+\t\tif (refs_ref_exists(get_main_ref_store(the_repository), dst.buf))\n+\t\t\twarning(_(\"failed to fetch start-point '%s'; \"\n+\t\t\t\t  \"using existing '%s'\"), arg, dst.buf);\n+\t\telse\n+\t\t\tdie(_(\"failed to fetch start-point '%s'\"), arg);\n+\t}\n+\n+\tstring_list_clear(&tracking_srcs, 0);\n+\tstring_list_clear(&ambiguous_remotes, 0);\n+\tstrbuf_release(&dst);\n+}\n+\n+static int parse_opt_checkout_track(const struct option *opt,\n+\t\t\t\t    const char *arg, int unset)\n+{\n+\tstruct checkout_opts *opts = opt->value;\n+\tstruct string_list tokens = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tint saw_direct = 0;\n+\tint ret = 0;\n+\n+\topts->fetch = 0;\n+\tif (unset) {\n+\t\topts->track = BRANCH_TRACK_NEVER;\n+\t\treturn 0;\n+\t}\n+\topts->track = BRANCH_TRACK_EXPLICIT;\n+\tif (!arg)\n+\t\treturn 0;\n+\n+\tstring_list_split(&tokens, arg, \",\", -1);\n+\tfor_each_string_list_item(item, &tokens) {\n+\t\tif (!strcmp(item->string, \"fetch\"))\n+\t\t\topts->fetch = 1;\n+\t\telse if (!strcmp(item->string, \"direct\"))\n+\t\t\tsaw_direct = 1;\n+\t\telse if (!strcmp(item->string, \"inherit\"))\n+\t\t\topts->track = BRANCH_TRACK_INHERIT;\n+\t\telse {\n+\t\t\tret = error(_(\"option `%s' expects \\\"%s\\\", \\\"%s\\\", \"\n+\t\t\t\t      \"or \\\"%s\\\"\"),\n+\t\t\t\t    \"--track\", \"direct\", \"inherit\", \"fetch\");\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (saw_direct && opts->track == BRANCH_TRACK_INHERIT)\n+\t\tret = error(_(\"option `%s' cannot combine \\\"%s\\\" and \\\"%s\\\"\"),\n+\t\t\t    \"--track\", \"direct\", \"inherit\");\n+out:\n+\tstring_list_clear(&tokens, 0);\n+\treturn ret;\n+}\n+\n static void branch_info_release(struct branch_info *info)\n {\n \tfree(info->name);\n@@ -1786,10 +1911,10 @@ static struct option *add_common_switch_branch_options(\n {\n \tstruct option options[] = {\n \t\tOPT_BOOL('d', \"detach\", &opts->force_detach, N_(\"detach HEAD at named commit\")),\n-\t\tOPT_CALLBACK_F('t', \"track\",  &opts->track, \"(direct|inherit)\",\n+\t\tOPT_CALLBACK_F('t', \"track\",  opts, \"(direct|inherit|fetch)[,...]\",\n \t\t\tN_(\"set branch tracking configuration\"),\n \t\t\tPARSE_OPT_OPTARG,\n-\t\t\tparse_opt_tracking_mode),\n+\t\t\tparse_opt_checkout_track),\n \t\tOPT__FORCE(&opts->force, N_(\"force checkout (throw away local modifications)\"),\n \t\t\t   PARSE_OPT_NOCOMPLETE),\n \t\tOPT_STRING(0, \"orphan\", &opts->new_orphan_branch, N_(\"new-branch\"), N_(\"new unborn branch\")),\n@@ -1994,8 +2119,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\topts->dwim_new_local_branch &&\n \t\t\topts->track == BRANCH_TRACK_UNSPECIFIED &&\n \t\t\t!opts->new_branch;\n-\t\tint n = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n-\t\t\t\t\t     &new_branch_info, opts, &rev);\n+\t\tint n;\n+\n+\t\tif (opts->fetch)\n+\t\t\tfetch_remote_for_start_point(argv[0], opts->quiet);\n+\n+\t\tn = parse_branchname_arg(argc, argv, dwim_ok, which_command,\n+\t\t\t\t\t &new_branch_info, opts, &rev);\n \t\targv += n;\n \t\targc -= n;\n \t} else if (!opts->accept_ref && opts->from_treeish) {\ndiff --git a/t/t7201-co.sh b/t/t7201-co.sh\nindex 7613b1d2a4..bc0c72c49d 100755\n--- a/t/t7201-co.sh\n+++ b/t/t7201-co.sh\n@@ -870,4 +870,226 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'setup upstream for --track=fetch tests' '\n+\tgit checkout main &&\n+\tgit init fetch_upstream &&\n+\ttest_commit -C fetch_upstream u_main &&\n+\tgit remote add fetch_upstream fetch_upstream &&\n+\tgit fetch fetch_upstream &&\n+\tgit -C fetch_upstream checkout -b fetch_new &&\n+\ttest_commit -C fetch_upstream u_new\n+'\n+\n+test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&\n+\tgit checkout --track=fetch -b local_new fetch_upstream/fetch_new &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD &&\n+\ttest_cmp_config fetch_upstream branch.local_new.remote &&\n+\ttest_cmp_config refs/heads/fetch_new branch.local_new.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_pre &&\n+\tgit -C fetch_upstream checkout -b fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_pre &&\n+\tgit fetch fetch_upstream &&\n+\tgit update-ref refs/heads/snapshot_other refs/remotes/fetch_upstream/fetch_other &&\n+\tgit -C fetch_upstream checkout fetch_target &&\n+\ttest_commit -C fetch_upstream u_target_post &&\n+\tgit -C fetch_upstream checkout fetch_other &&\n+\ttest_commit -C fetch_upstream u_other_post &&\n+\tgit checkout --track=fetch -b local_target fetch_upstream/fetch_target &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_other snapshot_other\n+'\n+\n+test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout main &&\n+\tgit remote set-head fetch_upstream main &&\n+\tgit -C fetch_upstream checkout -b fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_pre &&\n+\tgit fetch fetch_upstream fetch_unrelated &&\n+\tgit update-ref refs/heads/snapshot_unrelated \\\n+\t\trefs/remotes/fetch_upstream/fetch_unrelated &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_main_post &&\n+\tgit -C fetch_upstream checkout fetch_unrelated &&\n+\ttest_commit -C fetch_upstream u_unrelated_post &&\n+\tgit checkout --track=fetch -b local_from_remote fetch_upstream &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/main HEAD &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_unrelated snapshot_unrelated\n+'\n+\n+test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' '\n+\tgit checkout main &&\n+\ttest_might_fail git branch -D bogus &&\n+\ttest_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bogus\n+'\n+\n+test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_offline &&\n+\ttest_commit -C fetch_upstream u_offline &&\n+\tgit fetch fetch_upstream fetch_offline &&\n+\tsaved_url=$(git config remote.fetch_upstream.url) &&\n+\ttest_when_finished \"git config remote.fetch_upstream.url \\\"$saved_url\\\"\" &&\n+\tgit config remote.fetch_upstream.url ./does-not-exist &&\n+\tgit checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err &&\n+\ttest_grep \"failed to fetch\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' '\n+\tgit checkout main &&\n+\tgit remote add fetch_custom ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_custom\" &&\n+\tgit config --replace-all remote.fetch_custom.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/custom-ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_refspec &&\n+\ttest_commit -C fetch_upstream u_refspec &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/custom-ns/fetch_refspec &&\n+\tgit checkout --track=fetch -b local_refspec custom-ns/fetch_refspec &&\n+\ttest_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on bare remote-tracking prefix follows <prefix>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/ns_alias/HEAD\" &&\n+\tgit config --replace-all remote.fetch_ns.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ns_alias/*\" &&\n+\tgit fetch fetch_ns &&\n+\tgit symbolic-ref refs/remotes/ns_alias/HEAD refs/remotes/ns_alias/main &&\n+\tgit -C fetch_upstream checkout main &&\n+\ttest_commit -C fetch_upstream u_ns_post &&\n+\tgit checkout --track=fetch -b local_ns ns_alias &&\n+\ttest_cmp_rev refs/remotes/ns_alias/main HEAD &&\n+\ttest_cmp_config fetch_ns branch.local_ns.remote &&\n+\ttest_cmp_config refs/heads/main branch.local_ns.merge\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on bare remote name with no <ns>/HEAD' '\n+\tgit checkout main &&\n+\tgit remote add fetch_nohead ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_nohead\" &&\n+\ttest_might_fail git symbolic-ref -d refs/remotes/fetch_nohead/HEAD &&\n+\ttest_must_fail git checkout --track=fetch -b local_nohead fetch_nohead 2>err &&\n+\ttest_grep \"refs/remotes/fetch_nohead/HEAD\" err &&\n+\ttest_grep \"git remote set-head fetch_nohead --auto\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_nohead\n+'\n+\n+test_expect_success 'checkout --track=fetch on bare unknown name does not suggest set-head' '\n+\tgit checkout main &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/no_such_ns/HEAD &&\n+\ttest_must_fail git config --get remote.no_such_ns.url &&\n+\ttest_must_fail git checkout --track=fetch -b local_unknown no_such_ns 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_grep ! \"set-head\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_unknown\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects <ns>/HEAD pointing outside the tracking prefix' '\n+\tgit checkout main &&\n+\tgit remote add fetch_crossns ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_crossns\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/fetch_crossns/HEAD\" &&\n+\tgit fetch fetch_crossns &&\n+\tgit symbolic-ref refs/remotes/fetch_crossns/HEAD \\\n+\t\trefs/remotes/fetch_upstream/u_main &&\n+\ttest_must_fail git checkout --track=fetch -b local_crossns fetch_crossns 2>err &&\n+\ttest_grep \"refs/remotes/fetch_crossns/HEAD\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_crossns\n+'\n+\n+test_expect_success 'checkout --track=fetch dies on ambiguous fetch refspec match' '\n+\tgit checkout main &&\n+\tgit remote add fetch_ambig_a ./fetch_upstream &&\n+\tgit remote add fetch_ambig_b ./fetch_upstream &&\n+\ttest_when_finished \"git remote remove fetch_ambig_a\" &&\n+\ttest_when_finished \"git remote remove fetch_ambig_b\" &&\n+\tgit config --replace-all remote.fetch_ambig_a.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit config --replace-all remote.fetch_ambig_b.fetch \\\n+\t\t\"+refs/heads/*:refs/remotes/ambig_ns/*\" &&\n+\tgit -C fetch_upstream checkout -b fetch_ambig &&\n+\ttest_commit -C fetch_upstream u_ambig &&\n+\ttest_must_fail git checkout --track=fetch -b local_ambig ambig_ns/fetch_ambig 2>err &&\n+\ttest_grep \"fetch_ambig_a\" err &&\n+\ttest_grep \"fetch_ambig_b\" err &&\n+\ttest_grep \"tracking namespaces\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_ambig\n+'\n+\n+test_expect_success 'checkout --track=fetch rejects invalid refname components' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b local_invalid \"foo..bar\" 2>err &&\n+\ttest_grep \"valid\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/local_invalid\n+'\n+\n+test_expect_success 'checkout --track=inherit,direct is rejected' '\n+\ttest_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"cannot combine\" err\n+'\n+\n+test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_lastwin &&\n+\ttest_commit -C fetch_upstream u_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git checkout --track=fetch --track=direct \\\n+\t\t-b local_lastwin fetch_upstream/fetch_lastwin &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin\n+'\n+\n+test_expect_success 'checkout --track=fetch,inherit fetches remote-tracking start-point' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_inherit &&\n+\ttest_commit -C fetch_upstream u_inherit &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_inherit &&\n+\tgit checkout --track=fetch,inherit -b local_inherit \\\n+\t\tfetch_upstream/fetch_inherit &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD\n+'\n+\n+test_expect_success 'checkout --track=fetch on local start-point errors' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=fetch -b bad main 2>err &&\n+\ttest_grep \"no configured remote\" err &&\n+\ttest_must_fail git rev-parse --verify refs/heads/bad\n+'\n+\n+test_expect_success 'checkout --track=bogus reports an error' '\n+\tgit checkout main &&\n+\ttest_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err &&\n+\ttest_grep \"expects\" err\n+'\n+\n+test_expect_success 'checkout -q --track=fetch silences the fetch output' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_quiet &&\n+\ttest_commit -C fetch_upstream u_quiet &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_quiet &&\n+\tgit checkout -q --track=fetch -b local_quiet \\\n+\t\tfetch_upstream/fetch_quiet 2>err &&\n+\ttest_grep ! \"-> fetch_upstream/fetch_quiet\" err &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_quiet HEAD\n+'\n+\n+test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' '\n+\tgit checkout main &&\n+\tgit -C fetch_upstream checkout -b fetch_switch &&\n+\ttest_commit -C fetch_upstream u_switch &&\n+\ttest_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&\n+\tgit switch --track=fetch -c local_switch fetch_upstream/fetch_switch &&\n+\ttest_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"546370","messageId":"xmqq5x37h6fj.fsf@gitster.g","threadId":"65546","inReplyTo":"CAHwyqnWwyPHiaOW+rz-Z9ZvRf=OjXWw2T+rB3cSsxXWXkeRm=Q@mail.gmail.com","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-24T23:20:16Z","receivedAt":"2026-06-24T23:20:20Z","isPatch":true,"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> Ok, let's focus on the need for the feature before talking code:\n>\n> In an active project, forking from \"origin/master\" without refreshing\n> first often has consequences: you start work that has already been\n> done, or you build on an old version of the code which causes big\n> conflicts only later when you pull. The fix is simple ...\n\nThe above only argues that contributors should not start work on top\nof a stale codebase without looking at reasonably recent codebase.\n\nI am not sure if automated fetch immediately before forking to start\nwork will be a good fix for that, especially if the fork of a new\nbranch is done blindly _without_ looking at what the updated\nupstream contains.\n\n> ... (\"git fetch\n> origin master && git checkout -b topic origin/master\"), but it is\n> still a mouthful. Other tools exist because this is annoying enough\n> that people automate it.\n\nAnd to actually look at the recent codebase, one would probably need\n\n\tgit fetch\n\tgit log [-p] ..origin -- your-area-of-interest/\n\t... other inspection of the recent changes to refresh your\n\t... understanding of the base code comes here\n\tgit checkout -b topic origin\n\nor something like that.  Wouldn't folding the first and the third\nstep into one operation encourage omitting the second step?  In a\nsense, having a tool to let people blindly fetch and fork without\nlooking at what changed recently (i.e., they had a reason to think\nthat what they had was stale, so has a fetch actually resolved that\nstaleness?  what new things did the fetch bring in?) may encourage\na bad workflow.\n\nAn obvious complaint against \"update and always inspect and\nunderstand\" would be \"it would slow us down!\", but that is why\nprojects encourage forking your topic at a well known release tags,\nnot from a random \"tip of the tree of the day\".\n\nI think most of the above has already been communicated earlier in\ndiscussions before we got to v14, but I may be wrong.  Are there any\nnew arguments in support of the feature?\n"},{"id":"546371","messageId":"43C04FB5-7FE5-4535-A79A-C35449EB38C0@gmail.com","threadId":"65546","inReplyTo":"xmqq5x37h6fj.fsf@gitster.g","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-06-25T01:17:05Z","receivedAt":"2026-06-25T01:17:17Z","isPatch":true,"body":"\n> Le 24 juin 2026 à 19:20, Junio C Hamano <gitster@pobox.com> a écrit :\n> \n> ﻿Harald Nordgren <haraldnordgren@gmail.com> writes:\n> \n>> Ok, let's focus on the need for the feature before talking code:\n>> \n>> In an active project, forking from \"origin/master\" without refreshing\n>> first often has consequences: you start work that has already been\n>> done, or you build on an old version of the code which causes big\n>> conflicts only later when you pull. The fix is simple ...\n> \n> The above only argues that contributors should not start work on top\n> of a stale codebase without looking at reasonably recent codebase.\n> \n> I am not sure if automated fetch immediately before forking to start\n> work will be a good fix for that, especially if the fork of a new\n> branch is done blindly _without_ looking at what the updated\n> upstream contains.\n> \n>> ... (\"git fetch\n>> origin master && git checkout -b topic origin/master\"), but it is\n>> still a mouthful. Other tools exist because this is annoying enough\n>> that people automate it.\n> \n> And to actually look at the recent codebase, one would probably need\n> \n>    git fetch\n>    git log [-p] ..origin -- your-area-of-interest/\n>    ... other inspection of the recent changes to refresh your\n>    ... understanding of the base code comes here\n>    git checkout -b topic origin\n> \n> or something like that.  Wouldn't folding the first and the third\n> step into one operation encourage omitting the second step?  In a\n> sense, having a tool to let people blindly fetch and fork without\n> looking at what changed recently (i.e., they had a reason to think\n> that what they had was stale, so has a fetch actually resolved that\n> staleness?  what new things did the fetch bring in?) may encourage\n> a bad workflow.\n\nI think I remain overall ambivalent, but as anecdata: when I’m working on my employer’scode, it is the default (90%+?) for folks to omit step 2 and have the kind of blind fetch + branch that this would facilitate.\n\nI myself do this when I’m reasonably sure the other changes can’t be of interest (common), or when I suspect the change I’m going to start on will conflict with recent changes I haven’t fetched. At other times I’m more interested in step 2, but generally I omit it at work.\n\nNow, as to why: I spend a lot more time reviewing PRs at work (and making sure they merge quickly when they are in the right direction), and so I’m usually fairly confident of what a fetch is going to bring! [I also fetch several times a day to keep up to date locally, to facilitate various maintenance, admin, and archaeology tasks.] Contrast with distributed open source projects, where I might not have fetched for weeks and can’t predict what might fall out (let alone how it might it interact with local WIP).\n\nSo « bad workflow » I agree with, but am plenty guilty of :)\n\nTo wrap up, I wonder if the convenience of this proposal is especially aimed at folks like my corporate environment (where « build near the tip and integrate quickly » is the norm), but less than useful for those same folks in a different situation?\n\n(OTOH, I suppose I might use something similar when starting a new topic branch from Git’s master branch, since there’s probably no harm in working on a recent copy of master unless I already have older code that needs updated?)\n\n> An obvious complaint against \"update and always inspect and\n> understand\" would be \"it would slow us down!\", but that is why\n> projects encourage forking your topic at a well known release tags,\n> not from a random \"tip of the tree of the day\".\n> \n> I think most of the above has already been communicated earlier in\n> discussions before we got to v14, but I may be wrong.  Are there any\n> new arguments in support of the feature?\n"},{"id":"546383","messageId":"CAHwyqnWdsYGHMcMT=B-Vdrb_DUK61QBs0-YvWkdeq958v_r35w@mail.gmail.com","threadId":"65546","inReplyTo":"43C04FB5-7FE5-4535-A79A-C35449EB38C0@gmail.com","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-06-25T08:53:42Z","receivedAt":"2026-06-25T08:54:20Z","isPatch":true,"body":"Hi!\n\nI think you are touching on the critical point here; Git is likely one\nof the most ordered software projects that exist (I haven't worked on\nLinux kernel but that's the only one I can imagine is more stringent),\nand every \"real job\" I had has been fast-paced and chaotic.\n\nI believe that we must leave the ivory tower and make Git great for\nthe real world.\n\n> I also fetch several times a day to keep up to date locally\n\nI think this is also something that makes you not feel the pain as\nmuch. As a technical leader who spends time on reviewing, it gives a\nmore of a aloof/holistic view. And more likely to have already fetched\nthe latest master at any given point. When I worked as an IC, I forked\nfrom a stale branch 1000's of times and often felt that pain, I would\nhave wished for this feature.\n\n> So « bad workflow » I agree with, but am plenty guilty of :)\n\nIs it \"bad\", or is it just a completely normal workflow for 99% of\ndevelopers? Maybe we should cater to the normal workflow rather than\nchastising them.\n\n> To wrap up, I wonder if the convenience of this proposal is especially aimed at folks like my corporate environment (where « build near the tip and integrate quickly » is the norm), but less than useful for those same folks in a different situation?\n\nSure, we can say that, but that's likely 99% of all Git users?\n\n\nHarald\n\n\n\nHarald\n"},{"id":"546384","messageId":"CAL71e4MiijEiM26TKJcOYT7L4pfQeMM_F2oT3U3igP-wOZm2Ag@mail.gmail.com","threadId":"65546","inReplyTo":"CAHwyqnWdsYGHMcMT=B-Vdrb_DUK61QBs0-YvWkdeq958v_r35w@mail.gmail.com","subject":"Re: [PATCH v14 2/2] checkout: extend --track with a \"fetch\" mode to refresh start-point","fromName":"Kristofer Karlsson","fromEmail":"krka@spotify.com","sentAt":"2026-06-25T09:15:35Z","receivedAt":"2026-06-25T09:15:47Z","isPatch":true,"body":"On Thu, 25 Jun 2026 at 10:54, Harald Nordgren <haraldnordgren@gmail.com> wrote:\n>\n> Hi!\n>\n> I think you are touching on the critical point here; Git is likely one\n> of the most ordered software projects that exist (I haven't worked on\n> Linux kernel but that's the only one I can imagine is more stringent),\n> and every \"real job\" I had has been fast-paced and chaotic.\n>\n> I believe that we must leave the ivory tower and make Git great for\n> the real world.\n>\n\nI wouldn't call it an ivory tower, but rather a very solid foundation\nof useful primitives that can be composed and extended by higher level\nworkflows. I think this is a good thing. The git core is kept clean\nand simple and it provides hooks and tooling to allow for customization.\n\nFor the combination of fetch and checkout, it seems easy enough to add\nas a git alias:\n\n  [alias]\n      newtopic = \"!f() { git fetch origin master && git checkout -b\n\\\"$1\\\" FETCH_HEAD; }; f\"\n\nI think the usecase is valid, but I think it can be cleanly added on top of git\ninstead of changing git.\n\n- Kristofer\n"},{"id":"548841","messageId":"xmqq8q71clob.fsf@gitster.g","threadId":"65546","inReplyTo":"pull.2281.v15.git.git.1782338098.gitgitgadget@gmail.com","subject":"Re: [PATCH v15 0/2] checkout: --track=fetch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-07-23T23:48:20Z","receivedAt":"2026-07-23T23:48:23Z","isPatch":true,"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Extend checkout --track with a fetch mode to refresh start-point.\n> Changes in v15:\n>\n>  * Reword commit message to lead with motivation.\n\nI was reviewing the draft of the What's Cooking report and noticed  \nthat this topic is among a handful of stalled efforts going nowhere.\n\n>      +    Forking from an existing remote branch without refreshing first often\n>      +    has consequences: you start work that has already been done, or you\n>      +    build on an old version of the code which causes big conflicts later\n>      +    when you pull. The workaround is two commands (\"git fetch <remote>\n>      +    <branch> && git checkout -b <topic> <remote>/<branch>\"), and when\n>      +    the fetch is skipped the checkout silently starts from a stale tip.\n\nAutomatically updating the tip of the remote tracking branch also\nhas consequences.  It might be fine for the current branch, but it\nwould also make the other branches that forked from the same place\ngo \"stale\" and without the user realizing (e.g., \"git diff @{u}\"\nwhile they are checked out would show what the user does not\nexpect).\n\nAs long as you keep calling this \"workaround\", I remain skeptical\nthat the merit of this proposal would outweigh its downside.  As\nthis topic is about to become more than a month old, let me mark\nmark the topic to be discarded in my draft of the whats-cooking\nreport.\n\n"}]}