{"thread":{"id":"66353","subject":"[PATCH] fetch: add config to avoid fetching every branch in shallow repo","startedAt":"2026-09-19T14:47:28Z","lastAt":"2026-11-20T14:24:42Z","messageCount":157,"participants":["Harald Nordgren via GitGitGadget","Phillip Wood","Harald Nordgren","Junio C Hamano","D. Ben Knoble","Ben Knoble","Matt Hunter","Kristoffer Haugsbakk"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"552881","messageId":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":null,"subject":"[PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-19T14:47:26Z","receivedAt":"2026-09-19T14:47:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nIn a shallow, sparsely checked out clone of a repository with many\nbranches, plain git pull can take minutes or hang outright, even\nthough only one branch is actually being worked on.\n\nAdd fetch.shallow, off by default. When enabled, a fetch or pull for\na shallow repository that isn't already scoped to specific refs\nfetches only the current branch's tracked upstream, instead of every\nbranch the remote has. git pull ultimately runs such a fetch under\nthe hood, so this fixes pull the same way. It has no effect once the\nrepository is no longer shallow, and no effect on a fetch of a remote\nthe current branch doesn't track, both fall back to the existing\nbehavior.\n\nThis is opt-in rather than automatic because it changes what a plain\nfetch or pull leaves in refs/remotes/<name>/ for anyone who currently\nrelies on it syncing every branch of a shallow remote, not just the\none they are on. Scoping remote.<name>.fetch by hand already covers\nthis for a single remote, but that requires knowing the config exists\nand applies it permanently, even to branches that are not currently\nchecked out.\n\nThe remote's recorded default branch (remotes/<name>/HEAD) is kept up\nto date the same way it always is, only the other branches are\nskipped.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    fetch: add fetch.shallow so pull doesn't fetch every branch on shallow\n    repo\n    \n    Add fetch.shallow config for big shallow repo, so git fetch/pull doesn't\n    hang by fetching every branch.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v1\nPull-Request: https://github.com/git/git/pull/2412\n\n Documentation/config/fetch.adoc |  15 ++++\n builtin/fetch.c                 |  18 +++-\n t/t5537-fetch-shallow.sh        | 146 ++++++++++++++++++++++++++++++++\n 3 files changed, 175 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config/fetch.adoc b/Documentation/config/fetch.adoc\nindex 00435e9a16..e63e54a463 100644\n--- a/Documentation/config/fetch.adoc\n+++ b/Documentation/config/fetch.adoc\n@@ -145,3 +145,18 @@ remove the value for the `fetch.bundleCreationToken` value before fetching.\n `never`;;\n \tNever create or modify the `remotes/<name>/HEAD` symbolic-ref.\n --\n+\n+`fetch.shallow`::\n+\tIf true, and the repository is a shallow repository (see\n+\tlinkgit:git-clone[1] `--depth`), a fetch or `git pull` that names no\n+\texplicit refspec and would otherwise fall back to the remote's\n+\tconfigured `remote.<name>.fetch` refspec instead fetches only the\n+\tcurrent branch's upstream, when that upstream is on the remote being\n+\tfetched. This avoids negotiating history for every branch the remote\n+\tadvertises, which can be slow on a shallow repository that tracks\n+\tmany disjoint shallow histories. It has no effect on a fetch that\n+\tnames an explicit remote or refspec, and no effect on a repository\n+\tthat is not shallow. Defaults to false.\n++\n+`remotes/<name>/HEAD` is still kept up to date per `fetch.followRemoteHEAD`\n+while this is in effect, only the other branches are skipped.\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 533fdfe7d8..b22f7fe5f4 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -111,6 +111,7 @@ struct fetch_config {\n \tint recurse_submodules;\n \tint parallel;\n \tint submodule_fetch_jobs;\n+\tint shallow;\n };\n \n static int git_fetch_config(const char *k, const char *v,\n@@ -175,6 +176,11 @@ static int git_fetch_config(const char *k, const char *v,\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(k, \"fetch.shallow\")) {\n+\t\tfetch_config->shallow = git_config_bool(k, v);\n+\t\treturn 0;\n+\t}\n+\n \tif (!strcmp(k, \"fetch.followremotehead\")) {\n \t\tif (!v)\n \t\t\treturn config_error_nonbool(k);\n@@ -1958,15 +1964,19 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tint narrow_to_tracked_ref = config->shallow &&\n+\t\t\tis_repository_shallow(the_repository) && tracks_this_remote;\n \n \t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\t\tif (!narrow_to_tracked_ref)\n+\t\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n \t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\ndiff --git a/t/t5537-fetch-shallow.sh b/t/t5537-fetch-shallow.sh\nindex f323ceebd2..8143af9fc3 100755\n--- a/t/t5537-fetch-shallow.sh\n+++ b/t/t5537-fetch-shallow.sh\n@@ -13,6 +13,24 @@ commit() {\n \tgit commit -m \"$1\"\n }\n \n+check_upstream_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+check_upstream_head () {\n+\tgit symbolic-ref refs/remotes/upstream/HEAD >actual &&\n+\techo \"refs/remotes/upstream/$1\" >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+check_same_tip () {\n+\tgit log --oneline -1 \"$1\" >expect &&\n+\tgit -C \"$2\" log --oneline -1 \"$3\" >actual &&\n+\ttest_cmp expect actual\n+}\n+\n test_expect_success 'setup' '\n \tcommit 1 &&\n \tcommit 2 &&\n@@ -261,6 +279,134 @@ test_expect_success 'fetch --deepen does not truncate' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'fetch.shallow setup' '\n+\tgit branch narrow-side &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\t.git narrow-default &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\t.git narrow-enabled &&\n+\t(\n+\t\tcd narrow-default &&\n+\t\tgit remote add upstream ../.git &&\n+\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n+\t\tgit branch --set-upstream-to=upstream/main main\n+\t) &&\n+\t(\n+\t\tcd narrow-enabled &&\n+\t\tgit remote add upstream ../.git &&\n+\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n+\t\tgit branch --set-upstream-to=upstream/main main &&\n+\t\tgit config fetch.shallow true\n+\t)\n+'\n+\n+test_expect_success 'a refspec-less fetch expands to the configured refspec by default' '\n+\t(\n+\t\tcd narrow-default &&\n+\t\tgit fetch upstream &&\n+\t\tcheck_upstream_refs <<-\\EOF\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/narrow-side\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'fetch.shallow=true limits a refspec-less fetch to the tracked branch' '\n+\t(\n+\t\tcd narrow-enabled &&\n+\t\tgit fetch upstream &&\n+\t\tcheck_upstream_refs <<-\\EOF\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'fetch.shallow=true still creates refs/remotes/<remote>/HEAD' '\n+\t(\n+\t\tcd narrow-enabled &&\n+\t\tgit symbolic-ref -d refs/remotes/upstream/HEAD &&\n+\t\tgit fetch upstream &&\n+\t\tcheck_upstream_head main\n+\t)\n+'\n+\n+test_expect_success 'fetch.shallow=true with followRemoteHEAD=always corrects a stale HEAD' '\n+\ttest_when_finished \\\n+\t\t\"git -C narrow-enabled update-ref -d refs/remotes/upstream/stale-branch\" &&\n+\t(\n+\t\tcd narrow-enabled &&\n+\t\tgit update-ref refs/remotes/upstream/stale-branch refs/remotes/upstream/main &&\n+\t\tgit symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/stale-branch &&\n+\t\tgit -c fetch.followRemoteHEAD=always fetch upstream &&\n+\t\tcheck_upstream_head main\n+\t)\n+'\n+\n+test_expect_success 'fetch.shallow=true still updates the tracked branch' '\n+\tcommit 5 &&\n+\tgit -C narrow-enabled fetch upstream &&\n+\tcheck_same_tip main narrow-enabled refs/remotes/upstream/main\n+'\n+\n+test_expect_success 'fetch.shallow=true keeps git pull narrowed too' '\n+\ttest_when_finished \"git branch -D narrow-side\" &&\n+\tcommit 6 &&\n+\t(\n+\t\tcd narrow-enabled &&\n+\t\tgit pull &&\n+\t\tcheck_upstream_refs <<-\\EOF\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t) &&\n+\tcheck_same_tip main narrow-enabled HEAD\n+'\n+\n+test_expect_success 'fetch.shallow=true has no effect on a non-shallow repository' '\n+\tgit clone --no-local --branch main --single-branch .git narrow-full &&\n+\t(\n+\t\tcd narrow-full &&\n+\t\tgit rev-parse --is-shallow-repository >actual &&\n+\t\techo false >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit remote add upstream ../.git &&\n+\t\tgit fetch upstream &&\n+\t\tgit branch --set-upstream-to=upstream/main main &&\n+\t\tgit config fetch.shallow true\n+\t) &&\n+\ttest_when_finished \"git branch -D narrow-full-side\" &&\n+\tgit branch narrow-full-side &&\n+\t(\n+\t\tcd narrow-full &&\n+\t\tgit fetch upstream &&\n+\t\tcheck_upstream_refs <<-\\EOF\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/narrow-full-side\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'fetch.shallow=true only narrows a fetch of the tracked remote' '\n+\ttest_when_finished \"git branch -D other-side\" &&\n+\tgit branch other-side &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\t.git narrow-other-remote &&\n+\t(\n+\t\tcd narrow-other-remote &&\n+\t\tgit remote add upstream ../.git &&\n+\t\tgit config fetch.shallow true &&\n+\t\tgit fetch upstream &&\n+\t\tcheck_upstream_refs <<-\\EOF\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/other-side\n+\t\tEOF\n+\t)\n+'\n+\n . \"$TEST_DIRECTORY\"/lib-httpd.sh\n start_httpd\n \n\nbase-commit: d38352cd43ab9745686d697872408bc3249a153f\n-- \ngitgitgadget\n"},{"id":"552930","messageId":"7f084e4d-f738-4bd4-9b4d-cad995f04be8@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-09-21T13:28:08Z","receivedAt":"2026-09-21T13:28:13Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 19/09/2026 15:47, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> \n> In a shallow, sparsely checked out clone of a repository with many\n> branches, plain git pull can take minutes or hang outright, even\n> though only one branch is actually being worked on.\n\nI think the sparse checkout is irrelevant? It is unclear to me if this \nis talking about a case where there are many branches in the remote \nrepository and only one of them was cloned, then adding a second remote \ncreated a wildcard fetch refspec; or if there are intentionally lots of \nremote tracking branches in the local repository and you don't want to \nwait for them all to update. If it is the former then we should think \nhow we can improve the behavior of \"git remote add\" in a sparse \nrepository to prevent it adding a wildcard fetch refspec and instead \nsetup the new remote to fetch only the branch(es) we're interested in.\n\nThanks\n\nPhillip\n\n> Add fetch.shallow, off by default. When enabled, a fetch or pull for\n> a shallow repository that isn't already scoped to specific refs\n> fetches only the current branch's tracked upstream, instead of every\n> branch the remote has. git pull ultimately runs such a fetch under\n> the hood, so this fixes pull the same way. It has no effect once the\n> repository is no longer shallow, and no effect on a fetch of a remote\n> the current branch doesn't track, both fall back to the existing\n> behavior.\n> \n> This is opt-in rather than automatic because it changes what a plain\n> fetch or pull leaves in refs/remotes/<name>/ for anyone who currently\n> relies on it syncing every branch of a shallow remote, not just the\n> one they are on. Scoping remote.<name>.fetch by hand already covers\n> this for a single remote, but that requires knowing the config exists\n> and applies it permanently, even to branches that are not currently\n> checked out.\n> \n> The remote's recorded default branch (remotes/<name>/HEAD) is kept up\n> to date the same way it always is, only the other branches are\n> skipped.\n> \n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>      fetch: add fetch.shallow so pull doesn't fetch every branch on shallow\n>      repo\n>      \n>      Add fetch.shallow config for big shallow repo, so git fetch/pull doesn't\n>      hang by fetching every branch.\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v1\n> Pull-Request: https://github.com/git/git/pull/2412\n> \n>   Documentation/config/fetch.adoc |  15 ++++\n>   builtin/fetch.c                 |  18 +++-\n>   t/t5537-fetch-shallow.sh        | 146 ++++++++++++++++++++++++++++++++\n>   3 files changed, 175 insertions(+), 4 deletions(-)\n> \n> diff --git a/Documentation/config/fetch.adoc b/Documentation/config/fetch.adoc\n> index 00435e9a16..e63e54a463 100644\n> --- a/Documentation/config/fetch.adoc\n> +++ b/Documentation/config/fetch.adoc\n> @@ -145,3 +145,18 @@ remove the value for the `fetch.bundleCreationToken` value before fetching.\n>   `never`;;\n>   \tNever create or modify the `remotes/<name>/HEAD` symbolic-ref.\n>   --\n> +\n> +`fetch.shallow`::\n> +\tIf true, and the repository is a shallow repository (see\n> +\tlinkgit:git-clone[1] `--depth`), a fetch or `git pull` that names no\n> +\texplicit refspec and would otherwise fall back to the remote's\n> +\tconfigured `remote.<name>.fetch` refspec instead fetches only the\n> +\tcurrent branch's upstream, when that upstream is on the remote being\n> +\tfetched. This avoids negotiating history for every branch the remote\n> +\tadvertises, which can be slow on a shallow repository that tracks\n> +\tmany disjoint shallow histories. It has no effect on a fetch that\n> +\tnames an explicit remote or refspec, and no effect on a repository\n> +\tthat is not shallow. Defaults to false.\n> ++\n> +`remotes/<name>/HEAD` is still kept up to date per `fetch.followRemoteHEAD`\n> +while this is in effect, only the other branches are skipped.\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 533fdfe7d8..b22f7fe5f4 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -111,6 +111,7 @@ struct fetch_config {\n>   \tint recurse_submodules;\n>   \tint parallel;\n>   \tint submodule_fetch_jobs;\n> +\tint shallow;\n>   };\n>   \n>   static int git_fetch_config(const char *k, const char *v,\n> @@ -175,6 +176,11 @@ static int git_fetch_config(const char *k, const char *v,\n>   \t\treturn 0;\n>   \t}\n>   \n> +\tif (!strcmp(k, \"fetch.shallow\")) {\n> +\t\tfetch_config->shallow = git_config_bool(k, v);\n> +\t\treturn 0;\n> +\t}\n> +\n>   \tif (!strcmp(k, \"fetch.followremotehead\")) {\n>   \t\tif (!v)\n>   \t\t\treturn config_error_nonbool(k);\n> @@ -1958,15 +1964,19 @@ static int do_fetch(struct transport *transport,\n>   \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n>   \t} else {\n>   \t\tstruct branch *branch = branch_get(NULL);\n> +\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n> +\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n> +\t\tint narrow_to_tracked_ref = config->shallow &&\n> +\t\t\tis_repository_shallow(the_repository) && tracks_this_remote;\n>   \n>   \t\tif (transport->remote->fetch.nr) {\n> -\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n> -\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n> +\t\t\tif (!narrow_to_tracked_ref)\n> +\t\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n> +\t\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n>   \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n>   \t\t\t\tdo_set_head = 1;\n>   \t\t}\n> -\t\tif (branch && branch_has_merge_config(branch) &&\n> -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n> +\t\tif (tracks_this_remote) {\n>   \t\t\tint i;\n>   \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n>   \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> diff --git a/t/t5537-fetch-shallow.sh b/t/t5537-fetch-shallow.sh\n> index f323ceebd2..8143af9fc3 100755\n> --- a/t/t5537-fetch-shallow.sh\n> +++ b/t/t5537-fetch-shallow.sh\n> @@ -13,6 +13,24 @@ commit() {\n>   \tgit commit -m \"$1\"\n>   }\n>   \n> +check_upstream_refs () {\n> +\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream/ >actual &&\n> +\tcat >expect &&\n> +\ttest_cmp expect actual\n> +}\n> +\n> +check_upstream_head () {\n> +\tgit symbolic-ref refs/remotes/upstream/HEAD >actual &&\n> +\techo \"refs/remotes/upstream/$1\" >expect &&\n> +\ttest_cmp expect actual\n> +}\n> +\n> +check_same_tip () {\n> +\tgit log --oneline -1 \"$1\" >expect &&\n> +\tgit -C \"$2\" log --oneline -1 \"$3\" >actual &&\n> +\ttest_cmp expect actual\n> +}\n> +\n>   test_expect_success 'setup' '\n>   \tcommit 1 &&\n>   \tcommit 2 &&\n> @@ -261,6 +279,134 @@ test_expect_success 'fetch --deepen does not truncate' '\n>   \ttest_cmp expect actual\n>   '\n>   \n> +test_expect_success 'fetch.shallow setup' '\n> +\tgit branch narrow-side &&\n> +\tgit clone --no-local --depth=1 --branch main --single-branch \\\n> +\t\t.git narrow-default &&\n> +\tgit clone --no-local --depth=1 --branch main --single-branch \\\n> +\t\t.git narrow-enabled &&\n> +\t(\n> +\t\tcd narrow-default &&\n> +\t\tgit remote add upstream ../.git &&\n> +\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n> +\t\tgit branch --set-upstream-to=upstream/main main\n> +\t) &&\n> +\t(\n> +\t\tcd narrow-enabled &&\n> +\t\tgit remote add upstream ../.git &&\n> +\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n> +\t\tgit branch --set-upstream-to=upstream/main main &&\n> +\t\tgit config fetch.shallow true\n> +\t)\n> +'\n> +\n> +test_expect_success 'a refspec-less fetch expands to the configured refspec by default' '\n> +\t(\n> +\t\tcd narrow-default &&\n> +\t\tgit fetch upstream &&\n> +\t\tcheck_upstream_refs <<-\\EOF\n> +\t\trefs/remotes/upstream/HEAD\n> +\t\trefs/remotes/upstream/main\n> +\t\trefs/remotes/upstream/narrow-side\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true limits a refspec-less fetch to the tracked branch' '\n> +\t(\n> +\t\tcd narrow-enabled &&\n> +\t\tgit fetch upstream &&\n> +\t\tcheck_upstream_refs <<-\\EOF\n> +\t\trefs/remotes/upstream/HEAD\n> +\t\trefs/remotes/upstream/main\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true still creates refs/remotes/<remote>/HEAD' '\n> +\t(\n> +\t\tcd narrow-enabled &&\n> +\t\tgit symbolic-ref -d refs/remotes/upstream/HEAD &&\n> +\t\tgit fetch upstream &&\n> +\t\tcheck_upstream_head main\n> +\t)\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true with followRemoteHEAD=always corrects a stale HEAD' '\n> +\ttest_when_finished \\\n> +\t\t\"git -C narrow-enabled update-ref -d refs/remotes/upstream/stale-branch\" &&\n> +\t(\n> +\t\tcd narrow-enabled &&\n> +\t\tgit update-ref refs/remotes/upstream/stale-branch refs/remotes/upstream/main &&\n> +\t\tgit symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/stale-branch &&\n> +\t\tgit -c fetch.followRemoteHEAD=always fetch upstream &&\n> +\t\tcheck_upstream_head main\n> +\t)\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true still updates the tracked branch' '\n> +\tcommit 5 &&\n> +\tgit -C narrow-enabled fetch upstream &&\n> +\tcheck_same_tip main narrow-enabled refs/remotes/upstream/main\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true keeps git pull narrowed too' '\n> +\ttest_when_finished \"git branch -D narrow-side\" &&\n> +\tcommit 6 &&\n> +\t(\n> +\t\tcd narrow-enabled &&\n> +\t\tgit pull &&\n> +\t\tcheck_upstream_refs <<-\\EOF\n> +\t\trefs/remotes/upstream/HEAD\n> +\t\trefs/remotes/upstream/main\n> +\t\tEOF\n> +\t) &&\n> +\tcheck_same_tip main narrow-enabled HEAD\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true has no effect on a non-shallow repository' '\n> +\tgit clone --no-local --branch main --single-branch .git narrow-full &&\n> +\t(\n> +\t\tcd narrow-full &&\n> +\t\tgit rev-parse --is-shallow-repository >actual &&\n> +\t\techo false >expect &&\n> +\t\ttest_cmp expect actual &&\n> +\t\tgit remote add upstream ../.git &&\n> +\t\tgit fetch upstream &&\n> +\t\tgit branch --set-upstream-to=upstream/main main &&\n> +\t\tgit config fetch.shallow true\n> +\t) &&\n> +\ttest_when_finished \"git branch -D narrow-full-side\" &&\n> +\tgit branch narrow-full-side &&\n> +\t(\n> +\t\tcd narrow-full &&\n> +\t\tgit fetch upstream &&\n> +\t\tcheck_upstream_refs <<-\\EOF\n> +\t\trefs/remotes/upstream/HEAD\n> +\t\trefs/remotes/upstream/main\n> +\t\trefs/remotes/upstream/narrow-full-side\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success 'fetch.shallow=true only narrows a fetch of the tracked remote' '\n> +\ttest_when_finished \"git branch -D other-side\" &&\n> +\tgit branch other-side &&\n> +\tgit clone --no-local --depth=1 --branch main --single-branch \\\n> +\t\t.git narrow-other-remote &&\n> +\t(\n> +\t\tcd narrow-other-remote &&\n> +\t\tgit remote add upstream ../.git &&\n> +\t\tgit config fetch.shallow true &&\n> +\t\tgit fetch upstream &&\n> +\t\tcheck_upstream_refs <<-\\EOF\n> +\t\trefs/remotes/upstream/HEAD\n> +\t\trefs/remotes/upstream/main\n> +\t\trefs/remotes/upstream/other-side\n> +\t\tEOF\n> +\t)\n> +'\n> +\n>   . \"$TEST_DIRECTORY\"/lib-httpd.sh\n>   start_httpd\n>   \n> \n> base-commit: d38352cd43ab9745686d697872408bc3249a153f\n\n"},{"id":"552953","messageId":"CAHwyqnWbbCK40qAU1vhCFmN86J-dCY3tv_N_nBYgK2Bxf3o1fg@mail.gmail.com","threadId":"66353","inReplyTo":"7f084e4d-f738-4bd4-9b4d-cad995f04be8@gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-21T21:45:45Z","receivedAt":"2026-09-21T21:46:24Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Mon, Sep 21, 2026 at 3:28 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Harald\n>\n> On 19/09/2026 15:47, Harald Nordgren via GitGitGadget wrote:\n> > From: Harald Nordgren <haraldnordgren@gmail.com>\n> >\n> > In a shallow, sparsely checked out clone of a repository with many\n> > branches, plain git pull can take minutes or hang outright, even\n> > though only one branch is actually being worked on.\n>\n> I think the sparse checkout is irrelevant? It is unclear to me if this\n> is talking about a case where there are many branches in the remote\n> repository and only one of them was cloned, then adding a second remote\n> created a wildcard fetch refspec\n\nYeah likely. I cloned my fork of homebrew-core with `--depth=1` and\nthen added real upstream as a remote. Then I made my work on a feature\nbranch.\n\nThen tried to set my branch to track upstream, but it didn't work.\n\nThen 'git pull' and it hung.\n\n> If it is the former then we should think\n> how we can improve the behavior of \"git remote add\" in a sparse\n> repository to prevent it adding a wildcard fetch refspec and instead\n> setup the new remote to fetch only the branch(es) we're interested in.\n\nI didn't even consider that, that sounds like an interesting idea!\n\n\nHarald\n"},{"id":"552982","messageId":"CAHwyqnUVkVUjKCiN7YT3tkvHEVCQwb3jDGM0OTojPxptFKrsLQ@mail.gmail.com","threadId":"66353","inReplyTo":"CAHwyqnWbbCK40qAU1vhCFmN86J-dCY3tv_N_nBYgK2Bxf3o1fg@mail.gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-22T13:00:11Z","receivedAt":"2026-09-22T13:00:51Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > If it is the former then we should think\n> > how we can improve the behavior of \"git remote add\" in a sparse\n> > repository to prevent it adding a wildcard fetch refspec and instead\n> > setup the new remote to fetch only the branch(es) we're interested in.\n>\n> I didn't even consider that, that sounds like an interesting idea!\n\nWould you suggest we do that instead of this, or in addition to this\nfix?\n\nSeems maybe we only need the fix on \"git remote add\".\n\n\n\n\nHarald\n"},{"id":"552995","messageId":"15c85d2d-4fb0-427d-b52e-8e4dd958dcb9@gmail.com","threadId":"66353","inReplyTo":"CAHwyqnUVkVUjKCiN7YT3tkvHEVCQwb3jDGM0OTojPxptFKrsLQ@mail.gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-09-22T14:53:36Z","receivedAt":"2026-09-22T14:53:44Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 22/09/2026 14:00, Harald Nordgren wrote:\n>>> If it is the former then we should think\n>>> how we can improve the behavior of \"git remote add\" in a sparse\n>>> repository to prevent it adding a wildcard fetch refspec and instead\n>>> setup the new remote to fetch only the branch(es) we're interested in.\n>>\n>> I didn't even consider that, that sounds like an interesting idea!\n> \n> Would you suggest we do that instead of this, or in addition to this\n> fix?\n\nI'd say instead - if we can fix \"git remote add\" then the problem \nshouldn't arise in the first place which is better than having an opt-in \nconfig option that most users are probably unaware of.\n\nThanks\n\nPhillip\n\n> Seems maybe we only need the fix on \"git remote add\".\n> \n> \n> \n> \n> Harald\n\n"},{"id":"552997","messageId":"CAHwyqnUjDJNjER2AxFXOR6G3sOJKpsXj-agzmm7410_QAFu8PQ@mail.gmail.com","threadId":"66353","inReplyTo":"15c85d2d-4fb0-427d-b52e-8e4dd958dcb9@gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-22T15:37:17Z","receivedAt":"2026-09-22T15:37:56Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Tue, Sep 22, 2026 at 4:53 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> On 22/09/2026 14:00, Harald Nordgren wrote:\n> >>> If it is the former then we should think\n> >>> how we can improve the behavior of \"git remote add\" in a sparse\n> >>> repository to prevent it adding a wildcard fetch refspec and instead\n> >>> setup the new remote to fetch only the branch(es) we're interested in.\n> >>\n> >> I didn't even consider that, that sounds like an interesting idea!\n> >\n> > Would you suggest we do that instead of this, or in addition to this\n> > fix?\n>\n> I'd say instead - if we can fix \"git remote add\" then the problem\n> shouldn't arise in the first place which is better than having an opt-in\n> config option that most users are probably unaware of.\n\nThat makes a lot of sense.\n\nHowever, I think there is info missing on the local side that we can't\nget without making a network call to the newly added remote, like its\nbranches (and default branch). How do we handle that?\n\n\nHarald\n"},{"id":"553003","messageId":"xmqqh5jhfbyw.fsf@gitster.g","threadId":"66353","inReplyTo":"7f084e4d-f738-4bd4-9b4d-cad995f04be8@gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-22T17:11:03Z","receivedAt":"2026-09-22T17:11:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> I think the sparse checkout is irrelevant? It is unclear to me if this \n> is talking about a case where there are many branches in the remote \n> repository and only one of them was cloned, then adding a second remote \n> created a wildcard fetch refspec; or if there are intentionally lots of \n> remote tracking branches in the local repository and you don't want to \n> wait for them all to update. If it is the former then we should think \n> how we can improve the behavior of \"git remote add\" in a sparse \n> repository to prevent it adding a wildcard fetch refspec and instead \n> setup the new remote to fetch only the branch(es) we're interested in.\n\nVery good suggestions.  \"Avoid wildcards\" is easy, but designing a\nsuitable alternative (\"only the ones we are interested in\") may be\nharder.\n\nPerhaps we want to have something similar in spirit to the\n\"matching\" mode 'git push' has, where the set of local branches we\nhave defines the set of branches we are interested in?  That is,\nwhen 'remote.*.fetch' is configured to signal that special mode,\n'git fetch' would:\n\n - Find each local branch that has its '@{upstream}' set to a branch\n   at the remote we are fetching from.\n\n - Fetch these branches at the remote that our local branches care\n   about.\n\nI said \"in spirit\" above, and I find it tempting to use ':' and '+:'\nas the special '<refspec>' to trigger this mode, to mimic what 'git\npush' does when using the local branches we have as the set of\nbranches we care about.  But there are important differences:\n\n (1) The correspondence between local and remote-tracking branches\n     is not one-to-one, as you can fork multiple local topics out\n     of the same upstream branch.  Maybe our 7 local branches build\n     on top of only 2 branches we fetch from the remote, for\n     example.\n\n (2) Corollary.  Unlike the matching mode in 'git push' where local\n     branch 'B' is used to update branch 'B' at the remote (if it\n     exists), this new mode in 'git fetch' only uses local branches\n     as a guide to determine which branches to fetch from the\n     remote.  If our local branch 'B' builds on top of branch 'U' at\n     the remote, it is branch 'U' we fetch and store as the\n     'refs/remotes/R/U' remote-tracking branch, where 'R' is the\n     remote, and 'B' as the name does not get anywhere in the\n     picture.\n\nIn other words, this is not \"matching\" at all, even though it takes\ninspiration from it.  I do not know what it should be called, but I\nthink it would be a useful addition.\n\nThanks.\n"},{"id":"553021","messageId":"CAHwyqnVwQRv3TKDtkZ2iAO3hNtOeC464qdv6GCxFYE_v-rLq6Q@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqh5jhfbyw.fsf@gitster.g","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-22T21:40:09Z","receivedAt":"2026-09-22T21:40:51Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Tue, Sep 22, 2026 at 7:11 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> > I think the sparse checkout is irrelevant? It is unclear to me if this\n> > is talking about a case where there are many branches in the remote\n> > repository and only one of them was cloned, then adding a second remote\n> > created a wildcard fetch refspec; or if there are intentionally lots of\n> > remote tracking branches in the local repository and you don't want to\n> > wait for them all to update. If it is the former then we should think\n> > how we can improve the behavior of \"git remote add\" in a sparse\n> > repository to prevent it adding a wildcard fetch refspec and instead\n> > setup the new remote to fetch only the branch(es) we're interested in.\n>\n> Very good suggestions.  \"Avoid wildcards\" is easy, but designing a\n> suitable alternative (\"only the ones we are interested in\") may be\n> harder.\n>\n> Perhaps we want to have something similar in spirit to the\n> \"matching\" mode 'git push' has, where the set of local branches we\n> have defines the set of branches we are interested in?  That is,\n> when 'remote.*.fetch' is configured to signal that special mode,\n> 'git fetch' would:\n>\n>  - Find each local branch that has its '@{upstream}' set to a branch\n>    at the remote we are fetching from.\n>\n>  - Fetch these branches at the remote that our local branches care\n>    about.\n>\n> I said \"in spirit\" above, and I find it tempting to use ':' and '+:'\n> as the special '<refspec>' to trigger this mode, to mimic what 'git\n> push' does when using the local branches we have as the set of\n> branches we care about.  But there are important differences:\n>\n>  (1) The correspondence between local and remote-tracking branches\n>      is not one-to-one, as you can fork multiple local topics out\n>      of the same upstream branch.  Maybe our 7 local branches build\n>      on top of only 2 branches we fetch from the remote, for\n>      example.\n>\n>  (2) Corollary.  Unlike the matching mode in 'git push' where local\n>      branch 'B' is used to update branch 'B' at the remote (if it\n>      exists), this new mode in 'git fetch' only uses local branches\n>      as a guide to determine which branches to fetch from the\n>      remote.  If our local branch 'B' builds on top of branch 'U' at\n>      the remote, it is branch 'U' we fetch and store as the\n>      'refs/remotes/R/U' remote-tracking branch, where 'R' is the\n>      remote, and 'B' as the name does not get anywhere in the\n>      picture.\n>\n> In other words, this is not \"matching\" at all, even though it takes\n> inspiration from it.  I do not know what it should be called, but I\n> think it would be a useful addition.\n\nVery interesting idea! I would like to suggest that the default branch\nof the upstream is always included as well, even if it's not the\nupstream of any branch yet.\n\nI run this on every repo I work with\n\n    git branch --set-upstream-to=upstream # upstream's default branch\n\nand noticed on the shallow repo that it didn't work unless I first ran this\n\n    git remote set-head upstream --auto\n\nwhich was very annoying.\n\n\n\nHarald\n"},{"id":"553066","messageId":"c5710264-d901-4b23-81f9-fc69f8268826@gmail.com","threadId":"66353","inReplyTo":"CAHwyqnUjDJNjER2AxFXOR6G3sOJKpsXj-agzmm7410_QAFu8PQ@mail.gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-09-23T15:14:32Z","receivedAt":"2026-09-23T15:14:38Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 22/09/2026 16:37, Harald Nordgren wrote:\n> On Tue, Sep 22, 2026 at 4:53 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>>\n>> On 22/09/2026 14:00, Harald Nordgren wrote:\n>>>>> If it is the former then we should think\n>>>>> how we can improve the behavior of \"git remote add\" in a sparse\n>>>>> repository to prevent it adding a wildcard fetch refspec and instead\n>>>>> setup the new remote to fetch only the branch(es) we're interested in.\n>>>>\n>>>> I didn't even consider that, that sounds like an interesting idea!\n>>>\n>>> Would you suggest we do that instead of this, or in addition to this\n>>> fix?\n>>\n>> I'd say instead - if we can fix \"git remote add\" then the problem\n>> shouldn't arise in the first place which is better than having an opt-in\n>> config option that most users are probably unaware of.\n> \n> That makes a lot of sense.\n> \n> However, I think there is info missing on the local side that we can't\n> get without making a network call to the newly added remote, like its\n> branches (and default branch). How do we handle that?\n\nThere is a \"--track\" option for \"git remote add\" to set up the branches \nthat are fetched. Without \"--fetch\" we could refuse to add a new remote \nin a shallow repository unless the user tells us which branches to \ntrack. That is a bit of a pain, but I think it is better than adding a \nwildcard refspec that makes \"git fetch\" unusably slow. We could also \nperhaps look at the refspecs for the existing remotes and, if they are \nall fetching the same branch(es?) copy those.\n\nWith \"--fetch\" we could lookup the default branch on the remote and \nfetch that if \"--track\" isn't given.\n\nThanks\n\nPhillip\n"},{"id":"553068","messageId":"05d0e6e0-e156-4a2a-95a0-4986ab18ef49@gmail.com","threadId":"66353","inReplyTo":"xmqqh5jhfbyw.fsf@gitster.g","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-09-23T15:19:06Z","receivedAt":"2026-09-23T15:19:12Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 22/09/2026 18:11, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> \n>> I think the sparse checkout is irrelevant? It is unclear to me if this\n>> is talking about a case where there are many branches in the remote\n>> repository and only one of them was cloned, then adding a second remote\n>> created a wildcard fetch refspec; or if there are intentionally lots of\n>> remote tracking branches in the local repository and you don't want to\n>> wait for them all to update. If it is the former then we should think\n>> how we can improve the behavior of \"git remote add\" in a sparse\n>> repository to prevent it adding a wildcard fetch refspec and instead\n>> setup the new remote to fetch only the branch(es) we're interested in.\n> \n> Very good suggestions.  \"Avoid wildcards\" is easy, but designing a\n> suitable alternative (\"only the ones we are interested in\") may be\n> harder.\n> \n> Perhaps we want to have something similar in spirit to the\n> \"matching\" mode 'git push' has, where the set of local branches we\n> have defines the set of branches we are interested in?  That is,\n> when 'remote.*.fetch' is configured to signal that special mode,\n> 'git fetch' would:\n> \n>   - Find each local branch that has its '@{upstream}' set to a branch\n>     at the remote we are fetching from.\n> \n>   - Fetch these branches at the remote that our local branches care\n>     about.\n\nI can see that being useful fetch mode for a remote that we've already \nfetched from, but for a newly added remote there will be no local \nbranches with their upstream set to it because \"git branch \n--set-upstream-to\" fails if the upstream does not already exist. So I \nlike the idea for fetching from existing remotes, but it leaves us with \na chicken-and-egg problem when adding new remotes, so I'm not sure how \nit would work in practice.\n\nThanks\n\nPhillip\n  > I said \"in spirit\" above, and I find it tempting to use ':' and '+:'\n> as the special '<refspec>' to trigger this mode, to mimic what 'git\n> push' does when using the local branches we have as the set of\n> branches we care about.  But there are important differences:\n> \n>   (1) The correspondence between local and remote-tracking branches\n>       is not one-to-one, as you can fork multiple local topics out\n>       of the same upstream branch.  Maybe our 7 local branches build\n>       on top of only 2 branches we fetch from the remote, for\n>       example.\n> \n>   (2) Corollary.  Unlike the matching mode in 'git push' where local\n>       branch 'B' is used to update branch 'B' at the remote (if it\n>       exists), this new mode in 'git fetch' only uses local branches\n>       as a guide to determine which branches to fetch from the\n>       remote.  If our local branch 'B' builds on top of branch 'U' at\n>       the remote, it is branch 'U' we fetch and store as the\n>       'refs/remotes/R/U' remote-tracking branch, where 'R' is the\n>       remote, and 'B' as the name does not get anywhere in the\n>       picture.\n> \n> In other words, this is not \"matching\" at all, even though it takes\n> inspiration from it.  I do not know what it should be called, but I\n> think it would be a useful addition.\n> \n> Thanks.\n\n"},{"id":"553069","messageId":"xmqq5wzwc76w.fsf@gitster.g","threadId":"66353","inReplyTo":"05d0e6e0-e156-4a2a-95a0-4986ab18ef49@gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-23T15:34:47Z","receivedAt":"2026-09-23T15:34:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> when 'remote.*.fetch' is configured to signal that special mode,\n>> 'git fetch' would:\n>> \n>>   - Find each local branch that has its '@{upstream}' set to a branch\n>>     at the remote we are fetching from.\n>> \n>>   - Fetch these branches at the remote that our local branches care\n>>     about.\n>\n> I can see that being useful fetch mode for a remote that we've already \n> fetched from, but for a newly added remote there will be no local \n> branches with their upstream set to it because \"git branch \n> --set-upstream-to\" fails if the upstream does not already exist. So I \n> like the idea for fetching from existing remotes, but it leaves us with \n> a chicken-and-egg problem when adding new remotes, so I'm not sure how \n> it would work in practice.\n\nJust like with \"git push there :\", you prime the pump by explicitly\ndoing something (for \"push\", you do \"git push there mine\" to express\nyour preference to work with branch 'mine' and share it with the\nremote).\n\nSo if we are to allow customizing the refspec used for fetch with\n\"git remote add\", you might do:\n\n    $ git remote add --fetch=: second https://ho.st/git/second\n\nwhich creates:\n\n        [remote \"second\"]\n                url = https://ho.st/git/second\n                fetch = :\n\n(Note: I am not sure if \":\" is a good special token to express this\nmode of fetching, as I said earlier).\n\nYour initial \"git fetch second\" without any other arguments will be\na no-op.  You may decide to work on top of their 'main' branch by\nrunning:\n\n    $ git fetch second main:refs/remotes/second/main\n    $ git checkout -b topic -t second/main\n\nAt that point, the local branch 'topic' is built on top of their\n'main' branch by having its @{upstream} set to that remote-tracking\nbranch.  After that, running \"git fetch\" will update 'second/main' and\nno other remote-tracking branch.\n"},{"id":"553073","messageId":"CALnO6CA2DXvyOO+fu04sozg2=E0JoymAqyhs_heHzExgRSEzVw@mail.gmail.com","threadId":"66353","inReplyTo":"xmqq5wzwc76w.fsf@gitster.g","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-09-23T16:55:07Z","receivedAt":"2026-09-23T16:55:19Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Wed, Sep 23, 2026 at 11:49 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> >> when 'remote.*.fetch' is configured to signal that special mode,\n> >> 'git fetch' would:\n> >>\n> >>   - Find each local branch that has its '@{upstream}' set to a branch\n> >>     at the remote we are fetching from.\n> >>\n> >>   - Fetch these branches at the remote that our local branches care\n> >>     about.\n> >\n> > I can see that being useful fetch mode for a remote that we've already\n> > fetched from, but for a newly added remote there will be no local\n> > branches with their upstream set to it because \"git branch\n> > --set-upstream-to\" fails if the upstream does not already exist. So I\n> > like the idea for fetching from existing remotes, but it leaves us with\n> > a chicken-and-egg problem when adding new remotes, so I'm not sure how\n> > it would work in practice.\n>\n> Just like with \"git push there :\", you prime the pump by explicitly\n> doing something (for \"push\", you do \"git push there mine\" to express\n> your preference to work with branch 'mine' and share it with the\n> remote).\n>\n> So if we are to allow customizing the refspec used for fetch with\n> \"git remote add\", you might do:\n>\n>     $ git remote add --fetch=: second https://ho.st/git/second\n>\n> which creates:\n>\n>         [remote \"second\"]\n>                 url = https://ho.st/git/second\n>                 fetch = :\n>\n> (Note: I am not sure if \":\" is a good special token to express this\n> mode of fetching, as I said earlier).\n>\n> Your initial \"git fetch second\" without any other arguments will be\n> a no-op.  You may decide to work on top of their 'main' branch by\n> running:\n>\n>     $ git fetch second main:refs/remotes/second/main\n\nI've wanted something similar for notes, so allow me to interject from\nthe sidelines: it would be even nicer to still have the ability to map\nfetches (so that \"git fetch second main\" did the right thing, creating\na useful remote tracking branch with the usual hierarchy;\nconfigurable, of course) on top of this. So, the first \"git fetch\nsecond\" does nothing, but \"git fetch second main\" + creating the\nbranch does as you describe.\n\n>     $ git checkout -b topic -t second/main\n>\n> At that point, the local branch 'topic' is built on top of their\n> 'main' branch by having its @{upstream} set to that remote-tracking\n> branch.  After that, running \"git fetch\" will update 'second/main' and\n> no other remote-tracking branch.\n\n\n-- \nD. Ben Knoble\n"},{"id":"553105","messageId":"xmqqbj9nagt2.fsf@gitster.g","threadId":"66353","inReplyTo":"CALnO6CA2DXvyOO+fu04sozg2=E0JoymAqyhs_heHzExgRSEzVw@mail.gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-23T19:50:01Z","receivedAt":"2026-09-23T19:50:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"D. Ben Knoble\" <ben.knoble@gmail.com> writes:\n\n>>     $ git fetch second main:refs/remotes/second/main\n>\n> I've wanted something similar for notes, so allow me to interject from\n> the sidelines: it would be even nicer to still have the ability to map\n> fetches (so that \"git fetch second main\" did the right thing, creating\n\nThe thing is, the command line\n\n    $ git fetch second main\n\nhas been used for the past 20 years as a \"single-shot fetch\" syntax\nthat expresses that the user does not intend to keep interacting\nwith the same 'main' branch or even the same 'second' repository,\nand for the \"single-shot fetch\", it is absolutely the wrong thing to\ncreate a remote-tracking branch.\n\nIt would be even worse if we created a remote 'second' and\nremote-tracking branch 'refs/remotes/second/main' when you ran\n\n    $ git fetch https://ho.st/second main\n\nHaving said that, I suspect that the fact that you have the\nshorthand 'second' (i.e., you have \"[remote \"second\"] url = ...\"\ndefined) may be a good enough sign that you expect to keep\ninteracting with that repository, and some people might appreciate\nit if\n\n    $ git fetch second main\n\ncreated a remote-tracking branch \"refs/remotes/second/main\"\nautomatically.\n\nBut we cannot suddenly start doing so without breaking people's\nexpectations, and without a good transition plan.  We need at least\nan escape hatch for users to say \"No, this is a single-shot fetch;\ndo not write the object anywhere other than FETCH_HEAD as we have\nalways done\".\n"},{"id":"553109","messageId":"pull.2412.v2.git.git.1790195720941.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v2] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-23T20:35:20Z","receivedAt":"2026-09-23T20:35:25Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\ngit remote add sets a new remote up to fetch every branch by default.\nIn an already shallow repository, that turns the next plain fetch or\npull into a slow or hanging one, even when only one or two branches\nare ever used.\n\nAdd a special fetch refspec, \"+:\", that fetches only the branches our\nlocal branches are built on, plus the remote's default branch so a\nbrand new remote is usable right away, without needing to first set\nanything up to track it. git remote add now uses it instead of the\nusual wildcard refspec whenever the repository is already shallow.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    fetch: avoid fetching every branch of a new remote in a shallow repo\n    \n    Avoid fetching every branch of a new remote in a shallow repo.\n    \n    Changes in v2:\n    \n     * Replaced the opt-in fetch.shallow config entirely with a new special\n       fetch refspec (+:) that git remote add now defaults new remotes to in\n       a shallow repository. The new refspec fetches whichever branches any\n       local branch tracks at that remote, plus the remote's default branch.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v2\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v1:\n\n 1:  ec05d3e2d6 ! 1:  f04f5ce219 fetch: add config to avoid fetching every branch in shallow repo\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    fetch: add config to avoid fetching every branch in shallow repo\n     +    fetch: avoid fetching every branch of a new remote in a shallow repo\n      \n     -    In a shallow, sparsely checked out clone of a repository with many\n     -    branches, plain git pull can take minutes or hang outright, even\n     -    though only one branch is actually being worked on.\n     +    git remote add sets a new remote up to fetch every branch by default.\n     +    In an already shallow repository, that turns the next plain fetch or\n     +    pull into a slow or hanging one, even when only one or two branches\n     +    are ever used.\n      \n     -    Add fetch.shallow, off by default. When enabled, a fetch or pull for\n     -    a shallow repository that isn't already scoped to specific refs\n     -    fetches only the current branch's tracked upstream, instead of every\n     -    branch the remote has. git pull ultimately runs such a fetch under\n     -    the hood, so this fixes pull the same way. It has no effect once the\n     -    repository is no longer shallow, and no effect on a fetch of a remote\n     -    the current branch doesn't track, both fall back to the existing\n     -    behavior.\n     -\n     -    This is opt-in rather than automatic because it changes what a plain\n     -    fetch or pull leaves in refs/remotes/<name>/ for anyone who currently\n     -    relies on it syncing every branch of a shallow remote, not just the\n     -    one they are on. Scoping remote.<name>.fetch by hand already covers\n     -    this for a single remote, but that requires knowing the config exists\n     -    and applies it permanently, even to branches that are not currently\n     -    checked out.\n     -\n     -    The remote's recorded default branch (remotes/<name>/HEAD) is kept up\n     -    to date the same way it always is, only the other branches are\n     -    skipped.\n     +    Add a special fetch refspec, \"+:\", that fetches only the branches our\n     +    local branches are built on, plus the remote's default branch so a\n     +    brand new remote is usable right away, without needing to first set\n     +    anything up to track it. git remote add now uses it instead of the\n     +    usual wildcard refspec whenever the repository is already shallow.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     - ## Documentation/config/fetch.adoc ##\n     -@@ Documentation/config/fetch.adoc: remove the value for the `fetch.bundleCreationToken` value before fetching.\n     - `never`;;\n     - \tNever create or modify the `remotes/<name>/HEAD` symbolic-ref.\n     - --\n     -+\n     -+`fetch.shallow`::\n     -+\tIf true, and the repository is a shallow repository (see\n     -+\tlinkgit:git-clone[1] `--depth`), a fetch or `git pull` that names no\n     -+\texplicit refspec and would otherwise fall back to the remote's\n     -+\tconfigured `remote.<name>.fetch` refspec instead fetches only the\n     -+\tcurrent branch's upstream, when that upstream is on the remote being\n     -+\tfetched. This avoids negotiating history for every branch the remote\n     -+\tadvertises, which can be slow on a shallow repository that tracks\n     -+\tmany disjoint shallow histories. It has no effect on a fetch that\n     -+\tnames an explicit remote or refspec, and no effect on a repository\n     -+\tthat is not shallow. Defaults to false.\n     + ## Documentation/git-remote.adoc ##\n     +@@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at\n     + _<URL>_.  The command `git fetch <name>` can then be used to create and\n     + update remote-tracking branches `<name>/<branch>`.\n     + +\n     ++If the repository is already a shallow repository (see linkgit:git-clone[1]\n     ++`--depth`) and neither `-t` nor `--mirror` is given, the remote is set up\n     ++with the special tracking refspec (see the _<refspec>_ entry in\n     ++linkgit:git-fetch[1]) instead of the default glob refspec, so that\n     ++`git fetch <name>` does not need to negotiate history for every branch\n     ++the remote has.\n     +++\n     + With `-f` option, `git fetch <name>` is run immediately after\n     + the remote information is set up.\n     + +\n     +\n     + ## Documentation/pull-fetch-param.adoc ##\n     +@@ Documentation/pull-fetch-param.adoc: Negative refspecs can themselves be pattern refspecs. However, they may only\n     + contain a _<src>_ and do not specify a _<dst>_. Fully spelled out hex object\n     + names are also not supported.\n     + +\n     ++The special refspec `:` (or `+:` to allow non-fast-forward updates)\n     ++directs Git to fetch whichever branches at the remote our local branches\n     ++have as their `@{upstream}`, plus the remote's default branch, which is\n     ++always included so that it is available even before anything is set up\n     ++to track it. Unlike the same syntax when pushing, the local branch name\n     ++plays no role here beyond identifying which remote branch to fetch: the\n     ++result is written to `refs/remotes/<remote>/<branch>` using the remote's\n     ++own branch name, and several local branches built on the same remote\n     ++branch only cause it to be fetched once.\n      ++\n     -+`remotes/<name>/HEAD` is still kept up to date per `fetch.followRemoteHEAD`\n     -+while this is in effect, only the other branches are skipped.\n     + `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;\n     + it requests fetching everything up to the given tag.\n     + +\n      \n       ## builtin/fetch.c ##\n     -@@ builtin/fetch.c: struct fetch_config {\n     - \tint recurse_submodules;\n     - \tint parallel;\n     - \tint submodule_fetch_jobs;\n     -+\tint shallow;\n     - };\n     - \n     - static int git_fetch_config(const char *k, const char *v,\n     -@@ builtin/fetch.c: static int git_fetch_config(const char *k, const char *v,\n     - \t\treturn 0;\n     +@@ builtin/fetch.c: static void filter_prefetch_refspec(struct refspec *rs)\n       \t}\n     + }\n       \n     -+\tif (!strcmp(k, \"fetch.shallow\")) {\n     -+\t\tfetch_config->shallow = git_config_bool(k, v);\n     -+\t\treturn 0;\n     ++static void fetch_tracked_branches(struct remote *remote,\n     ++\t\t\t\t   const struct ref *remote_refs,\n     ++\t\t\t\t   struct ref ***tail,\n     ++\t\t\t\t   char **default_branch_out,\n     ++\t\t\t\t   int force)\n     ++{\n     ++\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     ++\tstruct string_list_item *item;\n     ++\tstruct ref *head;\n     ++\tchar *default_branch = NULL;\n     ++\n     ++\tbranches_tracking_remote(remote, &tracked);\n     ++\n     ++\thead = get_remote_ref(remote_refs, \"HEAD\");\n     ++\tif (head && head->symref && *head->symref) {\n     ++\t\tdefault_branch = xstrdup(head->symref);\n     ++\t\tstring_list_insert(&tracked, default_branch);\n     ++\t}\n     ++\tfree_one_ref(head);\n     ++\n     ++\tfor_each_string_list_item(item, &tracked) {\n     ++\t\tstruct refspec_item ref = { .force = force };\n     ++\t\tconst char *branch_name = item->string;\n     ++\t\tint is_default_branch = default_branch &&\n     ++\t\t\t!strcmp(item->string, default_branch);\n     ++\n     ++\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     ++\t\tref.src = xstrdup(is_default_branch ? \"HEAD\" : item->string);\n     ++\t\tref.dst = xstrfmt(\"refs/remotes/%s/%s\", remote->name, branch_name);\n     ++\t\tget_fetch_map(remote_refs, &ref, tail, 1);\n     ++\t\tfree(ref.src);\n     ++\t\tfree(ref.dst);\n     ++\t}\n     ++\n     ++\tif (default_branch && default_branch_out) {\n     ++\t\tconst char *branch_name = default_branch;\n     ++\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     ++\t\t*default_branch_out = xstrdup(branch_name);\n      +\t}\n      +\n     - \tif (!strcmp(k, \"fetch.followremotehead\")) {\n     - \t\tif (!v)\n     - \t\t\treturn config_error_nonbool(k);\n     ++\tfree(default_branch);\n     ++\tstring_list_clear(&tracked, 0);\n     ++}\n     ++\n     + static struct ref *get_ref_map(struct remote *remote,\n     + \t\t\t       const struct ref *remote_refs,\n     + \t\t\t       struct refspec *rs,\n     +-\t\t\t       int tags, int *autotags)\n     ++\t\t\t       int tags, int *autotags,\n     ++\t\t\t       char **bootstrap_head_branch)\n     + {\n     + \tint i;\n     + \tstruct ref *rm;\n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \t\t     /* Note: has_merge implies non-NULL branch->remote_name */\n     + \t\t     (has_merge && !strcmp(branch->remote_name, remote->name)))) {\n     + \t\t\tfor (i = 0; i < remote->fetch.nr; i++) {\n     ++\t\t\t\tif (remote->fetch.items[i].tracking) {\n     ++\t\t\t\t\tfetch_tracked_branches(remote, remote_refs, &tail,\n     ++\t\t\t\t\t\t\t\tbootstrap_head_branch,\n     ++\t\t\t\t\t\t\t\tremote->fetch.items[i].force);\n     ++\t\t\t\t\tcontinue;\n     ++\t\t\t\t}\n     + \t\t\t\tget_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);\n     + \t\t\t\tif (remote->fetch.items[i].dst &&\n     + \t\t\t\t    remote->fetch.items[i].dst[0])\n     +@@ builtin/fetch.c: static void warn_set_head(const char *remote, const char *head_name,\n     + }\n     + \n     + static int set_head(const struct ref *remote_refs, struct remote *remote,\n     +-\t\t\tint follow_remote_head)\n     ++\t\t\tint follow_remote_head, const char *known_head_branch)\n     + {\n     + \tint result = 0, create_only, baremirror, was_detached;\n     + \tstruct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,\n     + \t\t      b_local_head = STRBUF_INIT;\n     + \tconst char *no_warn_branch = remote->no_warn_branch;\n     + \tchar *head_name = NULL;\n     +-\tstruct ref *ref, *matches;\n     ++\tstruct ref *ref, *matches = NULL;\n     + \tstruct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;\n     + \tstruct refspec_item refspec = {\n     + \t\t.force = 0,\n     +@@ builtin/fetch.c: static int set_head(const struct ref *remote_refs, struct remote *remote,\n     + \tstruct string_list heads = STRING_LIST_INIT_DUP;\n     + \tstruct ref_store *refs = get_main_ref_store(the_repository);\n     + \n     +-\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n     +-\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n     +-\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n     +-\tfor (ref = matches; ref; ref = ref->next) {\n     +-\t\tstring_list_append(&heads, strip_refshead(ref->name));\n     +-\t}\n     ++\tif (known_head_branch) {\n     ++\t\thead_name = xstrdup(known_head_branch);\n     ++\t} else {\n     ++\t\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n     ++\t\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n     ++\t\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n     ++\t\tfor (ref = matches; ref; ref = ref->next) {\n     ++\t\t\tstring_list_append(&heads, strip_refshead(ref->name));\n     ++\t\t}\n     + \n     +-\tif (!heads.nr)\n     +-\t\tresult = 1;\n     +-\telse if (heads.nr > 1)\n     +-\t\tresult = 1;\n     +-\telse\n     +-\t\thead_name = xstrdup(heads.items[0].string);\n     ++\t\tif (!heads.nr)\n     ++\t\t\tresult = 1;\n     ++\t\telse if (heads.nr > 1)\n     ++\t\t\tresult = 1;\n     ++\t\telse\n     ++\t\t\thead_name = xstrdup(heads.items[0].string);\n     ++\t}\n     + \n     + \tif (!head_name)\n     + \t\tgoto cleanup;\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \tstruct strmap rejected_refs = STRMAP_INIT;\n     + \tint summary_width = 0;\n     + \tint follow_remote_head;\n     ++\tchar *bootstrap_head_branch = NULL;\n     + \n     + \tif (tags == TAGS_DEFAULT) {\n     + \t\tif (transport->remote->fetch_tags == 2)\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n       \t} else {\n       \t\tstruct branch *branch = branch_get(NULL);\n      +\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n      +\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n     -+\t\tint narrow_to_tracked_ref = config->shallow &&\n     -+\t\t\tis_repository_shallow(the_repository) && tracks_this_remote;\n     ++\t\tint tracking_refspec = transport->remote->fetch.nr == 1 &&\n     ++\t\t\ttransport->remote->fetch.items[0].tracking;\n       \n     - \t\tif (transport->remote->fetch.nr) {\n     --\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     --\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     -+\t\t\tif (!narrow_to_tracked_ref)\n     -+\t\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     -+\t\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     - \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     - \t\t\t\tdo_set_head = 1;\n     +-\t\tif (transport->remote->fetch.nr) {\n     ++\t\tif (tracking_refspec) {\n     ++\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     ++\t\t\tstruct string_list_item *item;\n     ++\n     ++\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n     ++\t\t\tfor_each_string_list_item(item, &tracked)\n     ++\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     ++\t\t\t\t\t    item->string);\n     ++\t\t\tstring_list_clear(&tracked, 0);\n     ++\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes, \"HEAD\");\n     ++\t\t} else if (transport->remote->fetch.nr) {\n     + \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     + \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     +-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     +-\t\t\t\tdo_set_head = 1;\n       \t\t}\n      -\t\tif (branch && branch_has_merge_config(branch) &&\n      -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n     ++\t\tif (transport->remote->fetch.nr &&\n     ++\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n     ++\t\t\tdo_set_head = 1;\n      +\t\tif (tracks_this_remote) {\n       \t\t\tint i;\n       \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n       \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \ttransport_ls_refs_options_release(&transport_ls_refs_options);\n     + \n     + \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n     +-\t\t\t      tags, &autotags);\n     ++\t\t\t      tags, &autotags, &bootstrap_head_branch);\n     ++\n     + \tif (!update_head_ok)\n     + \t\tcheck_not_current_branch(ref_map);\n     + \n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t * Way too many cases where this can go wrong so let's just\n     + \t\t * ignore errors and fail silently for now.\n     + \t\t */\n     +-\t\tset_head(remote_refs, transport->remote, follow_remote_head);\n     ++\t\tset_head(remote_refs, transport->remote, follow_remote_head,\n     ++\t\t\t bootstrap_head_branch);\n     + \t}\n     + \n     + cleanup:\n     ++\tfree(bootstrap_head_branch);\n     + \t/*\n     + \t * When using batched updates, we want to commit the non-rejected\n     + \t * updates and also handle the rejections.\n      \n     - ## t/t5537-fetch-shallow.sh ##\n     -@@ t/t5537-fetch-shallow.sh: commit() {\n     - \tgit commit -m \"$1\"\n     + ## builtin/remote.c ##\n     +@@\n     + #include \"rebase.h\"\n     + #include \"refs.h\"\n     + #include \"refspec.h\"\n     ++#include \"shallow.h\"\n     + #include \"odb.h\"\n     + #include \"strvec.h\"\n     + #include \"commit-reach.h\"\n     +@@ builtin/remote.c: static void add_branch(const char *key, const char *branchname,\n     + \t\t       const char *remotename, int mirror, struct strbuf *tmp)\n     + {\n     + \tstrbuf_reset(tmp);\n     +-\tstrbuf_addch(tmp, '+');\n     +-\tif (mirror)\n     +-\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n     +-\t\t\t\tbranchname, branchname);\n     +-\telse\n     +-\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n     +-\t\t\t\tbranchname, remotename, branchname);\n     ++\tif (!strcmp(branchname, \":\")) {\n     ++\t\tstrbuf_addstr(tmp, \"+:\");\n     ++\t} else {\n     ++\t\tstrbuf_addch(tmp, '+');\n     ++\t\tif (mirror)\n     ++\t\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n     ++\t\t\t\t\tbranchname, branchname);\n     ++\t\telse\n     ++\t\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n     ++\t\t\t\t\tbranchname, remotename, branchname);\n     ++\t}\n     + \trepo_config_set_multivar(the_repository, key, tmp->buf, \"^$\", 0);\n       }\n       \n     -+check_upstream_refs () {\n     -+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream/ >actual &&\n     -+\tcat >expect &&\n     -+\ttest_cmp expect actual\n     -+}\n     +@@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix,\n     + \t\tstrbuf_reset(&buf);\n     + \t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n     + \t\tif (track.nr == 0)\n     +-\t\t\tstring_list_append(&track, \"*\");\n     +-\t\tfor (size_t i = 0; i < track.nr; i++) {\n     ++\t\t\tstring_list_append(&track,\n     ++\t\t\t\tmirror == MIRROR_NONE &&\n     ++\t\t\t\tis_repository_shallow(the_repository) ?\n     ++\t\t\t\t\":\" : \"*\");\n     ++\t\tfor (size_t i = 0; i < track.nr; i++)\n     + \t\t\tadd_branch(buf.buf, track.items[i].string,\n     + \t\t\t\t   name, mirror, &buf2);\n     +-\t\t}\n     + \t}\n     + \n     + \tif (mirror & MIRROR_PUSH) {\n     +\n     + ## refspec.c ##\n     +@@ refspec.c: static int parse_refspec(struct refspec_item *item, const char *refspec,\n     + \t\treturn 1;\n     + \t}\n     + \n     ++\tif (fetch && rhs == lhs && rhs[1] == '\\0') {\n     ++\t\titem->tracking = 1;\n     ++\t\treturn 1;\n     ++\t}\n     ++\n     + \tif (rhs) {\n     + \t\tsize_t rlen = strlen(++rhs);\n     + \t\tis_glob = (1 <= rlen && strchr(rhs, '*'));\n     +@@ refspec.c: void refspec_item_clear(struct refspec_item *item)\n     + \titem->force = 0;\n     + \titem->pattern = 0;\n     + \titem->matching = 0;\n     ++\titem->tracking = 0;\n     + \titem->exact_sha1 = 0;\n     + }\n     + \n     +@@ refspec.c: static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *\n     + \t\tstruct refspec_item *refspec = &rs->items[i];\n     + \t\tchar *expn_name;\n     + \n     +-\t\tif (refspec->negative)\n     ++\t\tif (refspec->negative || refspec->tracking)\n     + \t\t\tcontinue;\n     + \n     + \t\t/* Note the reversal of src and dst */\n     +\n     + ## refspec.h ##\n     +@@ refspec.h: struct refspec_item {\n     + \tunsigned matching : 1;\n     + \tunsigned exact_sha1 : 1;\n     + \tunsigned negative : 1;\n     ++\tunsigned tracking : 1;\n     + \n     + \tchar *src;\n     + \tchar *dst;\n     +\n     + ## remote.c ##\n     +@@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n     + \n     + int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n     + {\n     ++\tif (remote->fetch.nr == 1 && remote->fetch.items[0].tracking) {\n     ++\t\tstruct strbuf prefix = STRBUF_INIT;\n     ++\t\tconst char *branch_name;\n     ++\t\tint ret = -1;\n     ++\n     ++\t\tstrbuf_addf(&prefix, \"refs/remotes/%s/\", remote->name);\n     ++\t\tif (!refspec->src) {\n     ++\t\t\tif (refspec->dst &&\n     ++\t\t\t    skip_prefix(refspec->dst, prefix.buf, &branch_name)) {\n     ++\t\t\t\trefspec->src = xstrfmt(\"refs/heads/%s\", branch_name);\n     ++\t\t\t\trefspec->force = 1;\n     ++\t\t\t\tret = 0;\n     ++\t\t\t}\n     ++\t\t} else if (skip_prefix(refspec->src, \"refs/heads/\", &branch_name)) {\n     ++\t\t\tfree(refspec->dst);\n     ++\t\t\trefspec->dst = xstrfmt(\"%s%s\", prefix.buf, branch_name);\n     ++\t\t\trefspec->force = 1;\n     ++\t\t\tret = 0;\n     ++\t\t}\n     ++\t\tstrbuf_release(&prefix);\n     ++\t\treturn ret;\n     ++\t}\n     ++\n     + \treturn refspec_find_match(&remote->fetch, refspec);\n     + }\n     + \n     +@@ remote.c: int branch_merge_matches(struct branch *branch,\n     + \treturn refname_match(branch->merge[i]->src, refname);\n     + }\n     + \n     ++struct branches_tracking_remote_cb_data {\n     ++\tstruct remote *remote;\n     ++\tstruct string_list *tracked;\n     ++};\n     ++\n     ++static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n     ++{\n     ++\tstruct branches_tracking_remote_cb_data *data = cb_data;\n     ++\tstruct branch *branch;\n     ++\n     ++\tbranch = branch_get(ref->name);\n     ++\tif (!branch_has_merge_config(branch) ||\n     ++\t    strcmp(branch->remote_name, data->remote->name))\n     ++\t\treturn 0;\n      +\n     -+check_upstream_head () {\n     -+\tgit symbolic-ref refs/remotes/upstream/HEAD >actual &&\n     -+\techo \"refs/remotes/upstream/$1\" >expect &&\n     -+\ttest_cmp expect actual\n     ++\tfor (int i = 0; i < branch->merge_nr; i++)\n     ++\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n     ++\n     ++\treturn 0;\n      +}\n      +\n     -+check_same_tip () {\n     -+\tgit log --oneline -1 \"$1\" >expect &&\n     -+\tgit -C \"$2\" log --oneline -1 \"$3\" >actual &&\n     -+\ttest_cmp expect actual\n     ++void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n     ++{\n     ++\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n     ++\n     ++\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n     ++\t\t\t\t  add_if_tracking_remote, &data);\n      +}\n      +\n     - test_expect_success 'setup' '\n     - \tcommit 1 &&\n     - \tcommit 2 &&\n     -@@ t/t5537-fetch-shallow.sh: test_expect_success 'fetch --deepen does not truncate' '\n     - \ttest_cmp expect actual\n     + __attribute__((format (printf,2,3)))\n     + static char *error_buf(struct strbuf *err, const char *fmt, ...)\n     + {\n     +@@ remote.c: int get_fetch_map(const struct ref *remote_refs,\n     + {\n     + \tstruct ref *ref_map, **rmp;\n     + \n     +-\tif (refspec->negative)\n     ++\tif (refspec->negative || refspec->tracking)\n     + \t\treturn 0;\n     + \n     + \tif (refspec->pattern) {\n     +\n     + ## remote.h ##\n     +@@ remote.h: int branch_has_merge_config(struct branch *branch);\n     + \n     + int branch_merge_matches(struct branch *, int n, const char *);\n     + \n     ++/* fills tracked with the refname of every local branch's upstream on remote */\n     ++void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n     ++\n     + /* list of the remote in a group as configured */\n     + struct remote_group_data {\n     + \tconst char *name;\n     +\n     + ## t/meson.build ##\n     +@@ t/meson.build: integration_tests = [\n     +   't5582-fetch-negative-refspec.sh',\n     +   't5583-push-branches.sh',\n     +   't5584-http-429-retry.sh',\n     ++  't5585-fetch-tracking-refspec.sh',\n     +   't5600-clone-fail-cleanup.sh',\n     +   't5601-clone.sh',\n     +   't5602-clone-remote-exec.sh',\n     +\n     + ## t/t5505-remote.sh ##\n     +@@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only' '\n     + \ttest_grep ! \"\\[blob:none\\]\" out\n       '\n       \n     -+test_expect_success 'fetch.shallow setup' '\n     -+\tgit branch narrow-side &&\n     -+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     -+\t\t.git narrow-default &&\n     ++test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n     ++\ttest_when_finished \"rm -rf shallow-add\" &&\n      +\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     -+\t\t.git narrow-enabled &&\n     -+\t(\n     -+\t\tcd narrow-default &&\n     -+\t\tgit remote add upstream ../.git &&\n     -+\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n     -+\t\tgit branch --set-upstream-to=upstream/main main\n     -+\t) &&\n     ++\t\tone shallow-add &&\n      +\t(\n     -+\t\tcd narrow-enabled &&\n     -+\t\tgit remote add upstream ../.git &&\n     -+\t\tgit fetch --depth=1 upstream main:refs/remotes/upstream/main &&\n     -+\t\tgit branch --set-upstream-to=upstream/main main &&\n     -+\t\tgit config fetch.shallow true\n     ++\t\tcd shallow-add &&\n     ++\t\tgit remote add -t main upstream ../two &&\n     ++\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n     ++\t\t\tremote.upstream.fetch\n      +\t)\n      +'\n      +\n     -+test_expect_success 'a refspec-less fetch expands to the configured refspec by default' '\n     ++test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n     ++\ttest_when_finished \"rm -rf full-add\" &&\n     ++\tgit clone --no-local one full-add &&\n      +\t(\n     -+\t\tcd narrow-default &&\n     -+\t\tgit fetch upstream &&\n     -+\t\tcheck_upstream_refs <<-\\EOF\n     -+\t\trefs/remotes/upstream/HEAD\n     -+\t\trefs/remotes/upstream/main\n     -+\t\trefs/remotes/upstream/narrow-side\n     -+\t\tEOF\n     ++\t\tcd full-add &&\n     ++\t\tgit remote add upstream ../two &&\n     ++\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n     ++\t\t\tremote.upstream.fetch\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true limits a refspec-less fetch to the tracked branch' '\n     ++test_expect_success 'a remote added in a shallow repository defaults to the tracking refspec' '\n     ++\ttest_when_finished \"rm -rf shallow-add\" &&\n     ++\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     ++\t\tone shallow-add &&\n      +\t(\n     -+\t\tcd narrow-enabled &&\n     ++\t\tcd shallow-add &&\n     ++\t\tgit remote add upstream ../two &&\n     ++\t\ttest_cmp_config \"+:\" remote.upstream.fetch &&\n      +\t\tgit fetch upstream &&\n     -+\t\tcheck_upstream_refs <<-\\EOF\n     ++\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     ++\t\tcat >expect <<-\\EOF &&\n      +\t\trefs/remotes/upstream/HEAD\n      +\t\trefs/remotes/upstream/main\n      +\t\tEOF\n     ++\t\ttest_cmp expect actual &&\n     ++\t\tgit branch --set-upstream-to=upstream &&\n     ++\t\ttest_cmp_config upstream branch.main.remote &&\n     ++\t\ttest_cmp_config refs/heads/main branch.main.merge\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true still creates refs/remotes/<remote>/HEAD' '\n     + test_expect_success 'check remote-tracking' '\n     + \t(\n     + \t\tcd test &&\n     +\n     + ## t/t5585-fetch-tracking-refspec.sh (new) ##\n     +@@\n     ++#!/bin/sh\n     ++\n     ++test_description='\"git fetch\" with the tracking refspec \":\" (or \"+:\")\n     ++\n     ++The special refspec fetches whichever branches at the remote our local\n     ++branches have as their @{upstream}, plus the default branch of that\n     ++remote, which is always included so that it is available even before\n     ++anything is set up to track it. git remote add uses it by default for\n     ++a new remote in an already shallow repository, so that adding a\n     ++second remote there does not turn the next plain fetch or pull into\n     ++one that negotiates history for every branch the remote has.\n     ++'\n     ++\n     ++GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n     ++export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n     ++\n     ++. ./test-lib.sh\n     ++\n     ++test_expect_success 'setup' '\n     ++\ttest_commit main-1 &&\n     ++\ttest_commit main-2 &&\n     ++\tgit checkout -b side main-1 &&\n     ++\ttest_commit side-1 &&\n     ++\tgit checkout -b next main-1 &&\n     ++\ttest_commit next-1 &&\n     ++\tgit checkout main\n     ++'\n     ++\n     ++test_expect_success 'clone shallow and single-branch, then add a second remote' '\n     ++\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n      +\t(\n     -+\t\tcd narrow-enabled &&\n     -+\t\tgit symbolic-ref -d refs/remotes/upstream/HEAD &&\n     -+\t\tgit fetch upstream &&\n     -+\t\tcheck_upstream_head main\n     ++\t\tcd client &&\n     ++\t\tgit remote add upstream .. &&\n     ++\t\ttest_cmp_config \"+:\" remote.upstream.fetch\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true with followRemoteHEAD=always corrects a stale HEAD' '\n     -+\ttest_when_finished \\\n     -+\t\t\"git -C narrow-enabled update-ref -d refs/remotes/upstream/stale-branch\" &&\n     ++test_expect_success 'fetching the new remote does not need every branch it has' '\n      +\t(\n     -+\t\tcd narrow-enabled &&\n     -+\t\tgit update-ref refs/remotes/upstream/stale-branch refs/remotes/upstream/main &&\n     -+\t\tgit symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/stale-branch &&\n     -+\t\tgit -c fetch.followRemoteHEAD=always fetch upstream &&\n     -+\t\tcheck_upstream_head main\n     ++\t\tcd client &&\n     ++\t\tgit fetch upstream &&\n     ++\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     ++\t\tcat >expect <<-\\EOF &&\n     ++\t\trefs/remotes/upstream/HEAD\n     ++\t\trefs/remotes/upstream/main\n     ++\t\tEOF\n     ++\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true still updates the tracked branch' '\n     -+\tcommit 5 &&\n     -+\tgit -C narrow-enabled fetch upstream &&\n     -+\tcheck_same_tip main narrow-enabled refs/remotes/upstream/main\n     ++test_expect_success 'set-upstream-to now resolves right after that first fetch' '\n     ++\t(\n     ++\t\tcd client &&\n     ++\t\tgit branch --set-upstream-to=upstream &&\n     ++\t\ttest_cmp_config upstream branch.main.remote &&\n     ++\t\ttest_cmp_config refs/heads/main branch.main.merge\n     ++\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true keeps git pull narrowed too' '\n     -+\ttest_when_finished \"git branch -D narrow-side\" &&\n     -+\tcommit 6 &&\n     ++test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n     ++\t(\n     ++\t\tcd client &&\n     ++\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n     ++\t\tgit branch side-topic upstream/side\n     ++\t) &&\n     ++\tgit checkout side &&\n     ++\ttest_commit side-2 &&\n     ++\tgit checkout main &&\n      +\t(\n     -+\t\tcd narrow-enabled &&\n     -+\t\tgit pull &&\n     -+\t\tcheck_upstream_refs <<-\\EOF\n     ++\t\tcd client &&\n     ++\t\tgit fetch upstream &&\n     ++\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     ++\t\tcat >expect <<-\\EOF &&\n      +\t\trefs/remotes/upstream/HEAD\n      +\t\trefs/remotes/upstream/main\n     ++\t\trefs/remotes/upstream/side\n      +\t\tEOF\n     -+\t) &&\n     -+\tcheck_same_tip main narrow-enabled HEAD\n     ++\t\ttest_cmp expect actual &&\n     ++\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n     ++\t\tgit -C .. rev-parse side >expect-oid &&\n     ++\t\ttest_cmp expect-oid actual-oid\n     ++\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true has no effect on a non-shallow repository' '\n     -+\tgit clone --no-local --branch main --single-branch .git narrow-full &&\n     -+\t(\n     -+\t\tcd narrow-full &&\n     -+\t\tgit rev-parse --is-shallow-repository >actual &&\n     -+\t\techo false >expect &&\n     -+\t\ttest_cmp expect actual &&\n     -+\t\tgit remote add upstream ../.git &&\n     -+\t\tgit fetch upstream &&\n     -+\t\tgit branch --set-upstream-to=upstream/main main &&\n     -+\t\tgit config fetch.shallow true\n     -+\t) &&\n     -+\ttest_when_finished \"git branch -D narrow-full-side\" &&\n     -+\tgit branch narrow-full-side &&\n     ++test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n      +\t(\n     -+\t\tcd narrow-full &&\n     ++\t\tcd client &&\n     ++\t\tgit branch side-topic-2 upstream/side &&\n      +\t\tgit fetch upstream &&\n     -+\t\tcheck_upstream_refs <<-\\EOF\n     ++\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     ++\t\tcat >expect <<-\\EOF &&\n      +\t\trefs/remotes/upstream/HEAD\n      +\t\trefs/remotes/upstream/main\n     -+\t\trefs/remotes/upstream/narrow-full-side\n     ++\t\trefs/remotes/upstream/side\n      +\t\tEOF\n     ++\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetch.shallow=true only narrows a fetch of the tracked remote' '\n     -+\ttest_when_finished \"git branch -D other-side\" &&\n     -+\tgit branch other-side &&\n     -+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     -+\t\t.git narrow-other-remote &&\n     ++test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n      +\t(\n     -+\t\tcd narrow-other-remote &&\n     -+\t\tgit remote add upstream ../.git &&\n     -+\t\tgit config fetch.shallow true &&\n     ++\t\tcd client &&\n     ++\t\tgit remote add other .. &&\n     ++\t\tgit fetch other next:refs/remotes/other/next &&\n     ++\t\tgit branch next-topic other/next &&\n      +\t\tgit fetch upstream &&\n     -+\t\tcheck_upstream_refs <<-\\EOF\n     ++\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     ++\t\tcat >expect <<-\\EOF &&\n      +\t\trefs/remotes/upstream/HEAD\n      +\t\trefs/remotes/upstream/main\n     -+\t\trefs/remotes/upstream/other-side\n     ++\t\trefs/remotes/upstream/side\n      +\t\tEOF\n     ++\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     - . \"$TEST_DIRECTORY\"/lib-httpd.sh\n     - start_httpd\n     - \n     ++test_expect_success 'git remote show does not choke on the tracking refspec' '\n     ++\t(\n     ++\t\tcd client &&\n     ++\t\tgit remote show upstream\n     ++\t)\n     ++'\n     ++\n     ++test_done\n\n\n Documentation/git-remote.adoc       |   7 ++\n Documentation/pull-fetch-param.adoc |  10 +++\n builtin/fetch.c                     | 117 +++++++++++++++++++++-----\n builtin/remote.c                    |  27 +++---\n refspec.c                           |   8 +-\n refspec.h                           |   1 +\n remote.c                            |  54 +++++++++++-\n remote.h                            |   3 +\n t/meson.build                       |   1 +\n t/t5505-remote.sh                   |  44 ++++++++++\n t/t5585-fetch-tracking-refspec.sh   | 124 ++++++++++++++++++++++++++++\n 11 files changed, 362 insertions(+), 34 deletions(-)\n create mode 100755 t/t5585-fetch-tracking-refspec.sh\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..7856ccf54f 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,13 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t` nor `--mirror` is given, the remote is set up\n+with the special tracking refspec (see the _<refspec>_ entry in\n+linkgit:git-fetch[1]) instead of the default glob refspec, so that\n+`git fetch <name>` does not need to negotiate history for every branch\n+the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\ndiff --git a/Documentation/pull-fetch-param.adoc b/Documentation/pull-fetch-param.adoc\nindex d903dc8900..e1a42152d9 100644\n--- a/Documentation/pull-fetch-param.adoc\n+++ b/Documentation/pull-fetch-param.adoc\n@@ -48,6 +48,16 @@ Negative refspecs can themselves be pattern refspecs. However, they may only\n contain a _<src>_ and do not specify a _<dst>_. Fully spelled out hex object\n names are also not supported.\n +\n+The special refspec `:` (or `+:` to allow non-fast-forward updates)\n+directs Git to fetch whichever branches at the remote our local branches\n+have as their `@{upstream}`, plus the remote's default branch, which is\n+always included so that it is available even before anything is set up\n+to track it. Unlike the same syntax when pushing, the local branch name\n+plays no role here beyond identifying which remote branch to fetch: the\n+result is written to `refs/remotes/<remote>/<branch>` using the remote's\n+own branch name, and several local branches built on the same remote\n+branch only cause it to be fetched once.\n++\n `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;\n it requests fetching everything up to the given tag.\n +\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 533fdfe7d8..7a3387d733 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -500,10 +500,55 @@ static void filter_prefetch_refspec(struct refspec *rs)\n \t}\n }\n \n+static void fetch_tracked_branches(struct remote *remote,\n+\t\t\t\t   const struct ref *remote_refs,\n+\t\t\t\t   struct ref ***tail,\n+\t\t\t\t   char **default_branch_out,\n+\t\t\t\t   int force)\n+{\n+\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\tstruct string_list_item *item;\n+\tstruct ref *head;\n+\tchar *default_branch = NULL;\n+\n+\tbranches_tracking_remote(remote, &tracked);\n+\n+\thead = get_remote_ref(remote_refs, \"HEAD\");\n+\tif (head && head->symref && *head->symref) {\n+\t\tdefault_branch = xstrdup(head->symref);\n+\t\tstring_list_insert(&tracked, default_branch);\n+\t}\n+\tfree_one_ref(head);\n+\n+\tfor_each_string_list_item(item, &tracked) {\n+\t\tstruct refspec_item ref = { .force = force };\n+\t\tconst char *branch_name = item->string;\n+\t\tint is_default_branch = default_branch &&\n+\t\t\t!strcmp(item->string, default_branch);\n+\n+\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n+\t\tref.src = xstrdup(is_default_branch ? \"HEAD\" : item->string);\n+\t\tref.dst = xstrfmt(\"refs/remotes/%s/%s\", remote->name, branch_name);\n+\t\tget_fetch_map(remote_refs, &ref, tail, 1);\n+\t\tfree(ref.src);\n+\t\tfree(ref.dst);\n+\t}\n+\n+\tif (default_branch && default_branch_out) {\n+\t\tconst char *branch_name = default_branch;\n+\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n+\t\t*default_branch_out = xstrdup(branch_name);\n+\t}\n+\n+\tfree(default_branch);\n+\tstring_list_clear(&tracked, 0);\n+}\n+\n static struct ref *get_ref_map(struct remote *remote,\n \t\t\t       const struct ref *remote_refs,\n \t\t\t       struct refspec *rs,\n-\t\t\t       int tags, int *autotags)\n+\t\t\t       int tags, int *autotags,\n+\t\t\t       char **bootstrap_head_branch)\n {\n \tint i;\n \tstruct ref *rm;\n@@ -570,6 +615,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t     /* Note: has_merge implies non-NULL branch->remote_name */\n \t\t     (has_merge && !strcmp(branch->remote_name, remote->name)))) {\n \t\t\tfor (i = 0; i < remote->fetch.nr; i++) {\n+\t\t\t\tif (remote->fetch.items[i].tracking) {\n+\t\t\t\t\tfetch_tracked_branches(remote, remote_refs, &tail,\n+\t\t\t\t\t\t\t\tbootstrap_head_branch,\n+\t\t\t\t\t\t\t\tremote->fetch.items[i].force);\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n \t\t\t\tget_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);\n \t\t\t\tif (remote->fetch.items[i].dst &&\n \t\t\t\t    remote->fetch.items[i].dst[0])\n@@ -1751,14 +1802,14 @@ static void warn_set_head(const char *remote, const char *head_name,\n }\n \n static int set_head(const struct ref *remote_refs, struct remote *remote,\n-\t\t\tint follow_remote_head)\n+\t\t\tint follow_remote_head, const char *known_head_branch)\n {\n \tint result = 0, create_only, baremirror, was_detached;\n \tstruct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,\n \t\t      b_local_head = STRBUF_INIT;\n \tconst char *no_warn_branch = remote->no_warn_branch;\n \tchar *head_name = NULL;\n-\tstruct ref *ref, *matches;\n+\tstruct ref *ref, *matches = NULL;\n \tstruct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;\n \tstruct refspec_item refspec = {\n \t\t.force = 0,\n@@ -1769,19 +1820,23 @@ static int set_head(const struct ref *remote_refs, struct remote *remote,\n \tstruct string_list heads = STRING_LIST_INIT_DUP;\n \tstruct ref_store *refs = get_main_ref_store(the_repository);\n \n-\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n-\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n-\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n-\tfor (ref = matches; ref; ref = ref->next) {\n-\t\tstring_list_append(&heads, strip_refshead(ref->name));\n-\t}\n+\tif (known_head_branch) {\n+\t\thead_name = xstrdup(known_head_branch);\n+\t} else {\n+\t\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n+\t\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n+\t\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n+\t\tfor (ref = matches; ref; ref = ref->next) {\n+\t\t\tstring_list_append(&heads, strip_refshead(ref->name));\n+\t\t}\n \n-\tif (!heads.nr)\n-\t\tresult = 1;\n-\telse if (heads.nr > 1)\n-\t\tresult = 1;\n-\telse\n-\t\thead_name = xstrdup(heads.items[0].string);\n+\t\tif (!heads.nr)\n+\t\t\tresult = 1;\n+\t\telse if (heads.nr > 1)\n+\t\t\tresult = 1;\n+\t\telse\n+\t\t\thead_name = xstrdup(heads.items[0].string);\n+\t}\n \n \tif (!head_name)\n \t\tgoto cleanup;\n@@ -1923,6 +1978,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head;\n+\tchar *bootstrap_head_branch = NULL;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -1958,15 +2014,29 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tint tracking_refspec = transport->remote->fetch.nr == 1 &&\n+\t\t\ttransport->remote->fetch.items[0].tracking;\n \n-\t\tif (transport->remote->fetch.nr) {\n+\t\tif (tracking_refspec) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes, \"HEAD\");\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif (transport->remote->fetch.nr &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2006,7 +2076,8 @@ static int do_fetch(struct transport *transport,\n \ttransport_ls_refs_options_release(&transport_ls_refs_options);\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n-\t\t\t      tags, &autotags);\n+\t\t\t      tags, &autotags, &bootstrap_head_branch);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \n@@ -2169,10 +2240,12 @@ static int do_fetch(struct transport *transport,\n \t\t * Way too many cases where this can go wrong so let's just\n \t\t * ignore errors and fail silently for now.\n \t\t */\n-\t\tset_head(remote_refs, transport->remote, follow_remote_head);\n+\t\tset_head(remote_refs, transport->remote, follow_remote_head,\n+\t\t\t bootstrap_head_branch);\n \t}\n \n cleanup:\n+\tfree(bootstrap_head_branch);\n \t/*\n \t * When using batched updates, we want to commit the non-rejected\n \t * updates and also handle the rejections.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..e407d67037 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -127,13 +128,17 @@ static void add_branch(const char *key, const char *branchname,\n \t\t       const char *remotename, int mirror, struct strbuf *tmp)\n {\n \tstrbuf_reset(tmp);\n-\tstrbuf_addch(tmp, '+');\n-\tif (mirror)\n-\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n-\t\t\t\tbranchname, branchname);\n-\telse\n-\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n-\t\t\t\tbranchname, remotename, branchname);\n+\tif (!strcmp(branchname, \":\")) {\n+\t\tstrbuf_addstr(tmp, \"+:\");\n+\t} else {\n+\t\tstrbuf_addch(tmp, '+');\n+\t\tif (mirror)\n+\t\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n+\t\t\t\t\tbranchname, branchname);\n+\t\telse\n+\t\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n+\t\t\t\t\tbranchname, remotename, branchname);\n+\t}\n \trepo_config_set_multivar(the_repository, key, tmp->buf, \"^$\", 0);\n }\n \n@@ -233,11 +238,13 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tstrbuf_reset(&buf);\n \t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n \t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n+\t\t\tstring_list_append(&track,\n+\t\t\t\tmirror == MIRROR_NONE &&\n+\t\t\t\tis_repository_shallow(the_repository) ?\n+\t\t\t\t\":\" : \"*\");\n+\t\tfor (size_t i = 0; i < track.nr; i++)\n \t\t\tadd_branch(buf.buf, track.items[i].string,\n \t\t\t\t   name, mirror, &buf2);\n-\t\t}\n \t}\n \n \tif (mirror & MIRROR_PUSH) {\ndiff --git a/refspec.c b/refspec.c\nindex 7cb479983b..84a19b5b5c 100644\n--- a/refspec.c\n+++ b/refspec.c\n@@ -49,6 +49,11 @@ static int parse_refspec(struct refspec_item *item, const char *refspec,\n \t\treturn 1;\n \t}\n \n+\tif (fetch && rhs == lhs && rhs[1] == '\\0') {\n+\t\titem->tracking = 1;\n+\t\treturn 1;\n+\t}\n+\n \tif (rhs) {\n \t\tsize_t rlen = strlen(++rhs);\n \t\tis_glob = (1 <= rlen && strchr(rhs, '*'));\n@@ -181,6 +186,7 @@ void refspec_item_clear(struct refspec_item *item)\n \titem->force = 0;\n \titem->pattern = 0;\n \titem->matching = 0;\n+\titem->tracking = 0;\n \titem->exact_sha1 = 0;\n }\n \n@@ -373,7 +379,7 @@ static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *\n \t\tstruct refspec_item *refspec = &rs->items[i];\n \t\tchar *expn_name;\n \n-\t\tif (refspec->negative)\n+\t\tif (refspec->negative || refspec->tracking)\n \t\t\tcontinue;\n \n \t\t/* Note the reversal of src and dst */\ndiff --git a/refspec.h b/refspec.h\nindex fadef67933..97bd6ffef5 100644\n--- a/refspec.h\n+++ b/refspec.h\n@@ -27,6 +27,7 @@ struct refspec_item {\n \tunsigned matching : 1;\n \tunsigned exact_sha1 : 1;\n \tunsigned negative : 1;\n+\tunsigned tracking : 1;\n \n \tchar *src;\n \tchar *dst;\ndiff --git a/remote.c b/remote.c\nindex fe62068463..53b410f7e7 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -988,6 +988,29 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n+\tif (remote->fetch.nr == 1 && remote->fetch.items[0].tracking) {\n+\t\tstruct strbuf prefix = STRBUF_INIT;\n+\t\tconst char *branch_name;\n+\t\tint ret = -1;\n+\n+\t\tstrbuf_addf(&prefix, \"refs/remotes/%s/\", remote->name);\n+\t\tif (!refspec->src) {\n+\t\t\tif (refspec->dst &&\n+\t\t\t    skip_prefix(refspec->dst, prefix.buf, &branch_name)) {\n+\t\t\t\trefspec->src = xstrfmt(\"refs/heads/%s\", branch_name);\n+\t\t\t\trefspec->force = 1;\n+\t\t\t\tret = 0;\n+\t\t\t}\n+\t\t} else if (skip_prefix(refspec->src, \"refs/heads/\", &branch_name)) {\n+\t\t\tfree(refspec->dst);\n+\t\t\trefspec->dst = xstrfmt(\"%s%s\", prefix.buf, branch_name);\n+\t\t\trefspec->force = 1;\n+\t\t\tret = 0;\n+\t\t}\n+\t\tstrbuf_release(&prefix);\n+\t\treturn ret;\n+\t}\n+\n \treturn refspec_find_match(&remote->fetch, refspec);\n }\n \n@@ -1874,6 +1897,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\n@@ -2138,7 +2190,7 @@ int get_fetch_map(const struct ref *remote_refs,\n {\n \tstruct ref *ref_map, **rmp;\n \n-\tif (refspec->negative)\n+\tif (refspec->negative || refspec->tracking)\n \t\treturn 0;\n \n \tif (refspec->pattern) {\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..5ab187e00a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -353,6 +353,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 3ca7b27104..4c13048d46 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -728,6 +728,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5585-fetch-tracking-refspec.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..f7c3857098 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,50 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to the tracking refspec' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+:\" remote.upstream.fetch &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit branch --set-upstream-to=upstream &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\ndiff --git a/t/t5585-fetch-tracking-refspec.sh b/t/t5585-fetch-tracking-refspec.sh\nnew file mode 100755\nindex 0000000000..ebc3c5787f\n--- /dev/null\n+++ b/t/t5585-fetch-tracking-refspec.sh\n@@ -0,0 +1,124 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with the tracking refspec \":\" (or \"+:\")\n+\n+The special refspec fetches whichever branches at the remote our local\n+branches have as their @{upstream}, plus the default branch of that\n+remote, which is always included so that it is available even before\n+anything is set up to track it. git remote add uses it by default for\n+a new remote in an already shallow repository, so that adding a\n+second remote there does not turn the next plain fetch or pull into\n+one that negotiates history for every branch the remote has.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_cmp_config \"+:\" remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'fetching the new remote does not need every branch it has' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'set-upstream-to now resolves right after that first fetch' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch --set-upstream-to=upstream &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on the tracking refspec' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n\nbase-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7\n-- \ngitgitgadget\n"},{"id":"553119","messageId":"xmqq7bkb7in5.fsf@gitster.g","threadId":"66353","inReplyTo":"pull.2412.v2.git.git.1790195720941.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-23T21:38:38Z","receivedAt":"2026-09-23T21:38:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> git remote add sets a new remote up to fetch every branch by default.\n> In an already shallow repository, that turns the next plain fetch or\n> pull into a slow or hanging one, even when only one or two branches\n> are ever used.\n>\n> Add a special fetch refspec, \"+:\", that fetches only the branches our\n> local branches are built on, plus the remote's default branch so a\n> brand new remote is usable right away, without needing to first set\n> anything up to track it. git remote add now uses it instead of the\n> usual wildcard refspec whenever the repository is already shallow.\n\nThat's way too much for a single patch.  It needs to be split into\ndigestible chunks, but I offhand do not know how many pieces are\nappropriate, so let's think aloud together and try to refine the\ndesign while we do so.\n\nThe outline of our design so far should give something like this in\nour configuration file.\n\n\t[remote \"second\"]\n\t\tfetch = :+\n\n\t[branch \"topic1\"]\n\t\tremote = second\n\t\tmerge = refs/heads/main\n\n\t[branch \"topic2\"]\n\t\tremote = second\n\t\tmerge = refs/heads/next\n\nIn the \"fetch only what we build on\" mode, we would collect local\nbranches $X where branch.$X.remote == second and then collect\nbranch.$X.merge for these local branches.  In this case, we would\ndecide to fetch 'main' and 'next' branches in the end.\n\nBut notice that this does not give us sufficient information.  There\nis no explicit clue that tells that the remote-tracking branches for\nthis remote 'second' should be stored under refs/remotes/second/\nhierarchy.  A normal remote that is defined like so:\n\n\t[remote \"origin\"]\n\t\tfetch = +refs/heads/*:refs/remotes/origin/*\n\ndoes not have such a problem, as it makes it crystal clear that\ntheir branches go under refs/remotes/origin/ hierarchy.\n\nSo using \"fetch = :+\" is *not* a good idea, as I said.  Let's scrap\nthat syntax.\n\nOne thing we could do is probably to introduce\n\n\t[remote \"second\"]\n\t\trefmap = +refs/heads/*:refs/remotes/second/*\n\ninstead to give this clue (see \"git fetch --help\" for what a refmap\nis; it looks similar to refspec but only defines how their refs are\nmapped to our namespace without specifying what to be fetched, which\nis exactly what we need here).  We do not use remote.second.fetch at\nall.\n\nIt would be an easy first step to teach that an explicit\n\n\t$ git fetch second main next\n\nwith such a remote.second.refmap should behave the same way as\n\n\t$ git fetch --refmap='+refs/heads/*:refs/remotes/second/*' second \\\n\t\tmain next\n\nin a repository without the refmote.second.refmap configuration.  As\n\"git fetch --refmap=... second main next\" should already work, it\nwould be only the matter of supplementing the command line argument\nwith configured default.\n\nThen teach \"git fetch\" to further treat\n\n\t$ git fetch --refmap='+refs/heads/*:refs/remotes/second/*' second\n\ni.e., fetch with refmap but without specifying what exactly to\nfetch, as a request to fetch their branches we build on (and nothing\nelse), using the refmap, in other words, the lack of \"what to fetch\"\nin the above command line signals \"git fetch\" to rewrite the above to\n\n\t$ git fetch --refmap='+refs/heads/*:refs/remotes/second/*' second \\\n\t\tmain next\n\ninternally.  Since we have the previous remote.X.refmap step already,\nit means that with remote.second.refmap configured properly, the\nuser can only say\n\n\t$ git fetch second\n\nand it would do the right thing in our scenario.\n\nAnother and final step would be to teach \"git remote add\" to add\n\n        [remote \"second\"]\n                refmap = +refs/heads/*:refs/remotes/second/*\n\nwhen you want to fetch only what you build on.  I am not sure what\nshould trigger the decision.  Your initial message said something\nabout shallow and sparse and an earlier review refuted one of them\n(I do not recall which offhand, but probably sparse).  It probably\nis a good idea to start with an explicit command line option to \"git\nremote add --limited-fetch\" in a single commit.\n\nAnd then add heuristics (like \"in a shallow clone, this mode is\nturned on by default, but an explicit '--no-limited-fetch' can\ncountermand it\") in another commit.\n\nSo far, we identified four distinct commits, each bite sized.\n\n - remote.X.refmap configuration acts as if --refmap=... command\n   line argument was passed.\n\n - passing refmap without saying what to fetch enumerates what their\n   branches we build on, and pretend as if the user listed these\n   branches on the command line to fetch.\n\n - \"git remote add --limited-fetch\" creates remote.X.refmap instead\n   of remote.X.fetch as necessary.\n\n - \"git remote add\" without explicit \"--[no-]limited-fetch\" uses\n   heuristics to enable it.\n\nOr something like that, perhaps?\n"},{"id":"553211","messageId":"CALnO6CA5b7mpia9tkiOENdOKcOHF4errc31wuF5n5-=rrbHxVw@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqbj9nagt2.fsf@gitster.g","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-09-24T17:10:47Z","receivedAt":"2026-09-24T17:10:59Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Wed, Sep 23, 2026 at 3:50 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"D. Ben Knoble\" <ben.knoble@gmail.com> writes:\n>\n> >>     $ git fetch second main:refs/remotes/second/main\n> >\n> > I've wanted something similar for notes, so allow me to interject from\n> > the sidelines: it would be even nicer to still have the ability to map\n> > fetches (so that \"git fetch second main\" did the right thing, creating\n>\n> The thing is, the command line\n>\n>     $ git fetch second main\n>\n> has been used for the past 20 years as a \"single-shot fetch\" syntax\n> that expresses that the user does not intend to keep interacting\n> with the same 'main' branch or even the same 'second' repository,\n> and for the \"single-shot fetch\", it is absolutely the wrong thing to\n> create a remote-tracking branch.\n\nYes, perhaps I was a bit cavalier about that particular case; but in\nyour other message you suggested allowing configurable refmaps, and\nthat's what I was getting at previously without having the vocabulary\nfor it. Thanks!\n\n> It would be even worse if we created a remote 'second' and\n> remote-tracking branch 'refs/remotes/second/main' when you ran\n>\n>     $ git fetch https://ho.st/second main\n>\n> Having said that, I suspect that the fact that you have the\n> shorthand 'second' (i.e., you have \"[remote \"second\"] url = ...\"\n> defined) may be a good enough sign that you expect to keep\n> interacting with that repository, and some people might appreciate\n> it if\n>\n>     $ git fetch second main\n>\n> created a remote-tracking branch \"refs/remotes/second/main\"\n> automatically.\n>\n> But we cannot suddenly start doing so without breaking people's\n> expectations, and without a good transition plan.  We need at least\n> an escape hatch for users to say \"No, this is a single-shot fetch;\n> do not write the object anywhere other than FETCH_HEAD as we have\n> always done\".\n\nSo anyway, I think fetch.<remote>.refmap is the right thing here, thanks!\n\n-- \nD. Ben Knoble\n"},{"id":"553218","messageId":"xmqqa4p634tx.fsf@gitster.g","threadId":"66353","inReplyTo":"CALnO6CA5b7mpia9tkiOENdOKcOHF4errc31wuF5n5-=rrbHxVw@mail.gmail.com","subject":"Re: [PATCH] fetch: add config to avoid fetching every branch in shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-24T18:02:50Z","receivedAt":"2026-09-24T18:02:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"D. Ben Knoble\" <ben.knoble@gmail.com> writes:\n\n>> Having said that, I suspect that the fact that you have the\n>> shorthand 'second' (i.e., you have \"[remote \"second\"] url = ...\"\n>> defined) may be a good enough sign that you expect to keep\n>> interacting with that repository, and some people might appreciate\n>> it if\n>>\n>>     $ git fetch second main\n>>\n>> created a remote-tracking branch \"refs/remotes/second/main\"\n>> automatically.\n>>\n>> But we cannot suddenly start doing so without breaking people's\n>> expectations, and without a good transition plan.  We need at least\n>> an escape hatch for users to say \"No, this is a single-shot fetch;\n>> do not write the object anywhere other than FETCH_HEAD as we have\n>> always done\".\n>\n> So anyway, I think fetch.<remote>.refmap is the right thing here, thanks!\n\nAhh, yes, if we introduce fetch.second.refmap that makes it pretend\nas if you gave a --refmap argument on the command line, your\n\n\t$ git fetch second main\n\nwill automatically do what you want it to do.  I didn't realize that\nit was the implication of what I was outlining while preparing the\nresponse to Harald ;-)\n\nThanks.\n"},{"id":"553273","messageId":"pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v3 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-25T10:49:58Z","receivedAt":"2026-09-25T10:50:05Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (4):\n  fetch: add remote.<name>.refmap\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   7 ++\n Documentation/fetch-options.adoc |   7 ++\n Documentation/git-remote.adoc    |  14 ++-\n builtin/fetch.c                  | 142 +++++++++++++++++++++++++------\n builtin/remote.c                 |  31 +++++--\n remote.c                         |  41 ++++++++-\n remote.h                         |   9 ++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  75 ++++++++++++++++\n t/t5510-fetch.sh                 |  17 ++++\n t/t5585-fetch-refmap.sh          | 124 +++++++++++++++++++++++++++\n 11 files changed, 433 insertions(+), 35 deletions(-)\n create mode 100755 t/t5585-fetch-refmap.sh\n\n\nbase-commit: 0f8e75abebff0877cae681a3d5ff31ac47f54220\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v3\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v2:\n\n -:  ---------- > 1:  b04c00b974 fetch: add remote.<name>.refmap\n 1:  f04f5ce219 ! 2:  4ec508a223 fetch: avoid fetching every branch of a new remote in a shallow repo\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    fetch: avoid fetching every branch of a new remote in a shallow repo\n     +    fetch: infer branches to fetch from a refmap-only remote\n      \n     -    git remote add sets a new remote up to fetch every branch by default.\n     -    In an already shallow repository, that turns the next plain fetch or\n     -    pull into a slow or hanging one, even when only one or two branches\n     -    are ever used.\n     +    Configuring remote.<name>.refmap without remote.<name>.fetch used to\n     +    make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n     +    only meaningful with command-line refspec(s)\", since a refmap only\n     +    says where to put fetched refs, not what to fetch.\n      \n     -    Add a special fetch refspec, \"+:\", that fetches only the branches our\n     -    local branches are built on, plus the remote's default branch so a\n     -    brand new remote is usable right away, without needing to first set\n     -    anything up to track it. git remote add now uses it instead of the\n     -    usual wildcard refspec whenever the repository is already shallow.\n     +    Make that case infer what to fetch: the local branches whose\n     +    @{upstream} is already on that remote, plus the remote's default\n     +    branch, which is always included so it is available even before\n     +    anything is set up to track it. This lets a remote be configured to\n     +    fetch only the branches actually in use, without listing them by\n     +    hand in remote.<name>.fetch, and without needing to touch the\n     +    command line every time.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     - ## Documentation/git-remote.adoc ##\n     -@@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at\n     - _<URL>_.  The command `git fetch <name>` can then be used to create and\n     - update remote-tracking branches `<name>/<branch>`.\n     - +\n     -+If the repository is already a shallow repository (see linkgit:git-clone[1]\n     -+`--depth`) and neither `-t` nor `--mirror` is given, the remote is set up\n     -+with the special tracking refspec (see the _<refspec>_ entry in\n     -+linkgit:git-fetch[1]) instead of the default glob refspec, so that\n     -+`git fetch <name>` does not need to negotiate history for every branch\n     -+the remote has.\n     -++\n     - With `-f` option, `git fetch <name>` is run immediately after\n     - the remote information is set up.\n     - +\n     + ## Documentation/config/remote.adoc ##\n     +@@ Documentation/config/remote.adoc: remote.<name>.fetch::\n     + \n     + remote.<name>.refmap::\n     + \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n     +-\tOnly takes effect when the fetch names what to fetch explicitly,\n     +-\teither on the command line or via `remote.<name>.fetch`. See the\n     +-\t`--refmap` entry in linkgit:git-fetch[1].\n     ++\tIf `remote.<name>.fetch` is not set either, a refspec-less fetch\n     ++\tinfers what to fetch from local branches built on this remote,\n     ++\tinstead of fetching every branch it has. See the `--refmap` entry\n     ++\tin linkgit:git-fetch[1].\n     + \n     + remote.<name>.push::\n     + \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n      \n     - ## Documentation/pull-fetch-param.adoc ##\n     -@@ Documentation/pull-fetch-param.adoc: Negative refspecs can themselves be pattern refspecs. However, they may only\n     - contain a _<src>_ and do not specify a _<dst>_. Fully spelled out hex object\n     - names are also not supported.\n     - +\n     -+The special refspec `:` (or `+:` to allow non-fast-forward updates)\n     -+directs Git to fetch whichever branches at the remote our local branches\n     -+have as their `@{upstream}`, plus the remote's default branch, which is\n     -+always included so that it is available even before anything is set up\n     -+to track it. Unlike the same syntax when pushing, the local branch name\n     -+plays no role here beyond identifying which remote branch to fetch: the\n     -+result is written to `refs/remotes/<remote>/<branch>` using the remote's\n     -+own branch name, and several local branches built on the same remote\n     -+branch only cause it to be fetched once.\n     -++\n     - `tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`;\n     - it requests fetching everything up to the given tag.\n     + ## Documentation/fetch-options.adoc ##\n     +@@ Documentation/fetch-options.adoc: endif::git-pull[]\n     + \tcommand-line arguments. See section on \"Configured Remote-tracking\n     + \tBranches\" for details.\n       +\n     +-`remote.<name>.refmap` provides the default value for this option, the\n     +-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n     ++When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n     ++there is nothing to fetch, neither on the command line nor from\n     ++`remote.<name>.fetch`, Git infers what to fetch from the local branches\n     ++whose `@{upstream}` is on that remote, plus the remote's default branch,\n     ++which is always included so that it is available even before anything\n     ++is set up to track it.\n     + \n     + `-t`::\n     + `--tags`::\n      \n       ## builtin/fetch.c ##\n      @@ builtin/fetch.c: static void filter_prefetch_refspec(struct refspec *rs)\n     - \t}\n     - }\n     - \n     -+static void fetch_tracked_branches(struct remote *remote,\n     -+\t\t\t\t   const struct ref *remote_refs,\n     -+\t\t\t\t   struct ref ***tail,\n     -+\t\t\t\t   char **default_branch_out,\n     -+\t\t\t\t   int force)\n     -+{\n     -+\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\tstruct string_list_item *item;\n     -+\tstruct ref *head;\n     -+\tchar *default_branch = NULL;\n     -+\n     -+\tbranches_tracking_remote(remote, &tracked);\n     -+\n     -+\thead = get_remote_ref(remote_refs, \"HEAD\");\n     -+\tif (head && head->symref && *head->symref) {\n     -+\t\tdefault_branch = xstrdup(head->symref);\n     -+\t\tstring_list_insert(&tracked, default_branch);\n     -+\t}\n     -+\tfree_one_ref(head);\n     -+\n     -+\tfor_each_string_list_item(item, &tracked) {\n     -+\t\tstruct refspec_item ref = { .force = force };\n     -+\t\tconst char *branch_name = item->string;\n     -+\t\tint is_default_branch = default_branch &&\n     -+\t\t\t!strcmp(item->string, default_branch);\n     -+\n     -+\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     -+\t\tref.src = xstrdup(is_default_branch ? \"HEAD\" : item->string);\n     -+\t\tref.dst = xstrfmt(\"refs/remotes/%s/%s\", remote->name, branch_name);\n     -+\t\tget_fetch_map(remote_refs, &ref, tail, 1);\n     -+\t\tfree(ref.src);\n     -+\t\tfree(ref.dst);\n     -+\t}\n     -+\n     -+\tif (default_branch && default_branch_out) {\n     -+\t\tconst char *branch_name = default_branch;\n     -+\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     -+\t\t*default_branch_out = xstrdup(branch_name);\n     -+\t}\n     -+\n     -+\tfree(default_branch);\n     -+\tstring_list_clear(&tracked, 0);\n     -+}\n     -+\n       static struct ref *get_ref_map(struct remote *remote,\n       \t\t\t       const struct ref *remote_refs,\n       \t\t\t       struct refspec *rs,\n     @@ builtin/fetch.c: static void filter_prefetch_refspec(struct refspec *rs)\n       \tint i;\n       \tstruct ref *rm;\n      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     - \t\t     /* Note: has_merge implies non-NULL branch->remote_name */\n     - \t\t     (has_merge && !strcmp(branch->remote_name, remote->name)))) {\n     - \t\t\tfor (i = 0; i < remote->fetch.nr; i++) {\n     -+\t\t\t\tif (remote->fetch.items[i].tracking) {\n     -+\t\t\t\t\tfetch_tracked_branches(remote, remote_refs, &tail,\n     -+\t\t\t\t\t\t\t\tbootstrap_head_branch,\n     -+\t\t\t\t\t\t\t\tremote->fetch.items[i].force);\n     -+\t\t\t\t\tcontinue;\n     -+\t\t\t\t}\n     - \t\t\t\tget_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);\n     - \t\t\t\tif (remote->fetch.items[i].dst &&\n     - \t\t\t\t    remote->fetch.items[i].dst[0])\n     + \tstruct ref **tail = &ref_map;\n     + \tstruct refspec *effective_refmap =\n     + \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n     ++\tstruct refspec inferred_rs;\n     ++\tint inferred_branches = 0;\n     + \n     + \t/* opportunistically-updated references: */\n     + \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \tstruct hashmap existing_refs;\n     + \tint existing_refs_populated = 0;\n     + \n     ++\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n     ++\n     + \tfilter_prefetch_refspec(rs);\n     + \tif (remote)\n     + \t\tfilter_prefetch_refspec(&remote->fetch);\n     + \n     ++\tif (!rs->nr && remote && !remote->fetch.nr &&\n     ++\t    effective_refmap && effective_refmap->nr) {\n     ++\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     ++\t\tstruct string_list_item *item;\n     ++\t\tstruct ref *head;\n     ++\t\tchar *default_branch = NULL;\n     ++\t\tchar *default_branch_dst = NULL;\n     ++\n     ++\t\tbranches_tracking_remote(remote, &tracked);\n     ++\t\tfor_each_string_list_item(item, &tracked)\n     ++\t\t\trefspec_append(&inferred_rs, item->string);\n     ++\n     ++\t\t/* Always fetch the default branch too, as \"HEAD\". */\n     ++\t\thead = get_remote_ref(remote_refs, \"HEAD\");\n     ++\t\tif (head && head->symref && *head->symref)\n     ++\t\t\tdefault_branch = xstrdup(head->symref);\n     ++\t\tfree_one_ref(head);\n     ++\n     ++\t\tfor (i = 0; default_branch && !default_branch_dst &&\n     ++\t\t\t    !string_list_has_string(&tracked, default_branch) &&\n     ++\t\t\t    i < effective_refmap->nr; i++) {\n     ++\t\t\tstruct refspec_item *map = &effective_refmap->items[i];\n     ++\n     ++\t\t\tif (map->pattern)\n     ++\t\t\t\tmatch_refname_with_pattern(map->src, default_branch,\n     ++\t\t\t\t\t\t\t    map->dst, &default_branch_dst);\n     ++\t\t\telse if (!strcmp(map->src, default_branch))\n     ++\t\t\t\tdefault_branch_dst = xstrdup(map->dst);\n     ++\t\t}\n     ++\n     ++\t\tif (default_branch_dst) {\n     ++\t\t\tstruct refspec_item head_item = { .force = 1 };\n     ++\n     ++\t\t\thead_item.src = xstrdup(\"HEAD\");\n     ++\t\t\thead_item.dst = default_branch_dst;\n     ++\t\t\tget_fetch_map(remote_refs, &head_item, &tail, 1);\n     ++\t\t\tfree(head_item.src);\n     ++\t\t}\n     ++\n     ++\t\tif (default_branch && bootstrap_head_branch) {\n     ++\t\t\tconst char *branch_name = default_branch;\n     ++\t\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     ++\t\t\t*bootstrap_head_branch = xstrdup(branch_name);\n     ++\t\t}\n     ++\n     ++\t\tfree(default_branch);\n     ++\t\tfree(default_branch_dst);\n     ++\t\tstring_list_clear(&tracked, 0);\n     ++\n     ++\t\trs = &inferred_rs;\n     ++\t\tinferred_branches = 1;\n     ++\t}\n     ++\n     + \tif (rs->nr) {\n     + \t\tstruct refspec *fetch_refspec;\n     + \n     + \t\tfor (i = 0; i < rs->nr; i++) {\n     +-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n     ++\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n     ++\t\t\t\t      inferred_branches);\n     + \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n     + \t\t\t\t*autotags = 1;\n     + \t\t}\n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \n     + \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n     + \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n     ++\t} else if (inferred_branches) {\n     ++\t\t/* Already fully handled above. */\n     + \t} else if (effective_refmap && effective_refmap->nr) {\n     + \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n     + \t} else {\n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \tif (existing_refs_populated)\n     + \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n     + \n     ++\trefspec_clear(&inferred_rs);\n     + \treturn ref_map;\n     + }\n     + \n      @@ builtin/fetch.c: static void warn_set_head(const char *remote, const char *head_name,\n       }\n       \n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n       \t} else {\n       \t\tstruct branch *branch = branch_get(NULL);\n     +-\n     +-\t\tif (transport->remote->fetch.nr) {\n      +\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n      +\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n     -+\t\tint tracking_refspec = transport->remote->fetch.nr == 1 &&\n     -+\t\t\ttransport->remote->fetch.items[0].tracking;\n     - \n     --\t\tif (transport->remote->fetch.nr) {\n     -+\t\tif (tracking_refspec) {\n     ++\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n     ++\t\t\t&transport->remote->refmap;\n     ++\t\tint inferred_branches = !transport->remote->fetch.nr &&\n     ++\t\t\teffective_refmap->nr;\n     ++\n     ++\t\tif (inferred_branches) {\n      +\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n      +\t\t\tstruct string_list_item *item;\n      +\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\t}\n      -\t\tif (branch && branch_has_merge_config(branch) &&\n      -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n     -+\t\tif (transport->remote->fetch.nr &&\n     ++\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n      +\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n      +\t\t\tdo_set_head = 1;\n      +\t\tif (tracks_this_remote) {\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t * When using batched updates, we want to commit the non-rejected\n       \t * updates and also handle the rejections.\n      \n     - ## builtin/remote.c ##\n     -@@\n     - #include \"rebase.h\"\n     - #include \"refs.h\"\n     - #include \"refspec.h\"\n     -+#include \"shallow.h\"\n     - #include \"odb.h\"\n     - #include \"strvec.h\"\n     - #include \"commit-reach.h\"\n     -@@ builtin/remote.c: static void add_branch(const char *key, const char *branchname,\n     - \t\t       const char *remotename, int mirror, struct strbuf *tmp)\n     - {\n     - \tstrbuf_reset(tmp);\n     --\tstrbuf_addch(tmp, '+');\n     --\tif (mirror)\n     --\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n     --\t\t\t\tbranchname, branchname);\n     --\telse\n     --\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n     --\t\t\t\tbranchname, remotename, branchname);\n     -+\tif (!strcmp(branchname, \":\")) {\n     -+\t\tstrbuf_addstr(tmp, \"+:\");\n     -+\t} else {\n     -+\t\tstrbuf_addch(tmp, '+');\n     -+\t\tif (mirror)\n     -+\t\t\tstrbuf_addf(tmp, \"refs/%s:refs/%s\",\n     -+\t\t\t\t\tbranchname, branchname);\n     -+\t\telse\n     -+\t\t\tstrbuf_addf(tmp, \"refs/heads/%s:refs/remotes/%s/%s\",\n     -+\t\t\t\t\tbranchname, remotename, branchname);\n     -+\t}\n     - \trepo_config_set_multivar(the_repository, key, tmp->buf, \"^$\", 0);\n     - }\n     - \n     -@@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix,\n     - \t\tstrbuf_reset(&buf);\n     - \t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n     - \t\tif (track.nr == 0)\n     --\t\t\tstring_list_append(&track, \"*\");\n     --\t\tfor (size_t i = 0; i < track.nr; i++) {\n     -+\t\t\tstring_list_append(&track,\n     -+\t\t\t\tmirror == MIRROR_NONE &&\n     -+\t\t\t\tis_repository_shallow(the_repository) ?\n     -+\t\t\t\t\":\" : \"*\");\n     -+\t\tfor (size_t i = 0; i < track.nr; i++)\n     - \t\t\tadd_branch(buf.buf, track.items[i].string,\n     - \t\t\t\t   name, mirror, &buf2);\n     --\t\t}\n     - \t}\n     - \n     - \tif (mirror & MIRROR_PUSH) {\n     -\n     - ## refspec.c ##\n     -@@ refspec.c: static int parse_refspec(struct refspec_item *item, const char *refspec,\n     - \t\treturn 1;\n     - \t}\n     - \n     -+\tif (fetch && rhs == lhs && rhs[1] == '\\0') {\n     -+\t\titem->tracking = 1;\n     -+\t\treturn 1;\n     -+\t}\n     -+\n     - \tif (rhs) {\n     - \t\tsize_t rlen = strlen(++rhs);\n     - \t\tis_glob = (1 <= rlen && strchr(rhs, '*'));\n     -@@ refspec.c: void refspec_item_clear(struct refspec_item *item)\n     - \titem->force = 0;\n     - \titem->pattern = 0;\n     - \titem->matching = 0;\n     -+\titem->tracking = 0;\n     - \titem->exact_sha1 = 0;\n     - }\n     - \n     -@@ refspec.c: static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *\n     - \t\tstruct refspec_item *refspec = &rs->items[i];\n     - \t\tchar *expn_name;\n     - \n     --\t\tif (refspec->negative)\n     -+\t\tif (refspec->negative || refspec->tracking)\n     - \t\t\tcontinue;\n     - \n     - \t\t/* Note the reversal of src and dst */\n     -\n     - ## refspec.h ##\n     -@@ refspec.h: struct refspec_item {\n     - \tunsigned matching : 1;\n     - \tunsigned exact_sha1 : 1;\n     - \tunsigned negative : 1;\n     -+\tunsigned tracking : 1;\n     - \n     - \tchar *src;\n     - \tchar *dst;\n     -\n       ## remote.c ##\n     -@@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n     - \n     - int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n     - {\n     -+\tif (remote->fetch.nr == 1 && remote->fetch.items[0].tracking) {\n     -+\t\tstruct strbuf prefix = STRBUF_INIT;\n     -+\t\tconst char *branch_name;\n     -+\t\tint ret = -1;\n     -+\n     -+\t\tstrbuf_addf(&prefix, \"refs/remotes/%s/\", remote->name);\n     -+\t\tif (!refspec->src) {\n     -+\t\t\tif (refspec->dst &&\n     -+\t\t\t    skip_prefix(refspec->dst, prefix.buf, &branch_name)) {\n     -+\t\t\t\trefspec->src = xstrfmt(\"refs/heads/%s\", branch_name);\n     -+\t\t\t\trefspec->force = 1;\n     -+\t\t\t\tret = 0;\n     -+\t\t\t}\n     -+\t\t} else if (skip_prefix(refspec->src, \"refs/heads/\", &branch_name)) {\n     -+\t\t\tfree(refspec->dst);\n     -+\t\t\trefspec->dst = xstrfmt(\"%s%s\", prefix.buf, branch_name);\n     -+\t\t\trefspec->force = 1;\n     -+\t\t\tret = 0;\n     -+\t\t}\n     -+\t\tstrbuf_release(&prefix);\n     -+\t\treturn ret;\n     -+\t}\n     -+\n     - \treturn refspec_find_match(&remote->fetch, refspec);\n     - }\n     - \n      @@ remote.c: int branch_merge_matches(struct branch *branch,\n       \treturn refname_match(branch->merge[i]->src, refname);\n       }\n     @@ remote.c: int branch_merge_matches(struct branch *branch,\n       __attribute__((format (printf,2,3)))\n       static char *error_buf(struct strbuf *err, const char *fmt, ...)\n       {\n     -@@ remote.c: int get_fetch_map(const struct ref *remote_refs,\n     - {\n     - \tstruct ref *ref_map, **rmp;\n     - \n     --\tif (refspec->negative)\n     -+\tif (refspec->negative || refspec->tracking)\n     - \t\treturn 0;\n     - \n     - \tif (refspec->pattern) {\n      \n       ## remote.h ##\n      @@ remote.h: int branch_has_merge_config(struct branch *branch);\n     @@ t/meson.build: integration_tests = [\n         't5582-fetch-negative-refspec.sh',\n         't5583-push-branches.sh',\n         't5584-http-429-retry.sh',\n     -+  't5585-fetch-tracking-refspec.sh',\n     ++  't5585-fetch-refmap.sh',\n         't5600-clone-fail-cleanup.sh',\n         't5601-clone.sh',\n         't5602-clone-remote-exec.sh',\n      \n     - ## t/t5505-remote.sh ##\n     -@@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only' '\n     - \ttest_grep ! \"\\[blob:none\\]\" out\n     - '\n     - \n     -+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n     -+\ttest_when_finished \"rm -rf shallow-add\" &&\n     -+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     -+\t\tone shallow-add &&\n     -+\t(\n     -+\t\tcd shallow-add &&\n     -+\t\tgit remote add -t main upstream ../two &&\n     -+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n     -+\t\t\tremote.upstream.fetch\n     -+\t)\n     -+'\n     -+\n     -+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n     -+\ttest_when_finished \"rm -rf full-add\" &&\n     -+\tgit clone --no-local one full-add &&\n     -+\t(\n     -+\t\tcd full-add &&\n     -+\t\tgit remote add upstream ../two &&\n     -+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n     -+\t\t\tremote.upstream.fetch\n     -+\t)\n     -+'\n     -+\n     -+test_expect_success 'a remote added in a shallow repository defaults to the tracking refspec' '\n     -+\ttest_when_finished \"rm -rf shallow-add\" &&\n     -+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n     -+\t\tone shallow-add &&\n     -+\t(\n     -+\t\tcd shallow-add &&\n     -+\t\tgit remote add upstream ../two &&\n     -+\t\ttest_cmp_config \"+:\" remote.upstream.fetch &&\n     -+\t\tgit fetch upstream &&\n     -+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     -+\t\tcat >expect <<-\\EOF &&\n     -+\t\trefs/remotes/upstream/HEAD\n     -+\t\trefs/remotes/upstream/main\n     -+\t\tEOF\n     -+\t\ttest_cmp expect actual &&\n     -+\t\tgit branch --set-upstream-to=upstream &&\n     -+\t\ttest_cmp_config upstream branch.main.remote &&\n     -+\t\ttest_cmp_config refs/heads/main branch.main.merge\n     -+\t)\n     -+'\n     -+\n     - test_expect_success 'check remote-tracking' '\n     - \t(\n     - \t\tcd test &&\n     -\n     - ## t/t5585-fetch-tracking-refspec.sh (new) ##\n     + ## t/t5585-fetch-refmap.sh (new) ##\n      @@\n      +#!/bin/sh\n      +\n     -+test_description='\"git fetch\" with the tracking refspec \":\" (or \"+:\")\n     ++test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n      +\n     -+The special refspec fetches whichever branches at the remote our local\n     -+branches have as their @{upstream}, plus the default branch of that\n     -+remote, which is always included so that it is available even before\n     -+anything is set up to track it. git remote add uses it by default for\n     -+a new remote in an already shallow repository, so that adding a\n     -+second remote there does not turn the next plain fetch or pull into\n     -+one that negotiates history for every branch the remote has.\n     ++When a remote has a refmap configured but no fetch refspec, a\n     ++refspec-less fetch infers what to fetch from the local branches whose\n     ++@{upstream} is on that remote, plus the default branch of that remote,\n     ++which is always included so that it is available even before anything\n     ++is set up to track it.\n      +'\n      +\n      +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n     @@ t/t5585-fetch-tracking-refspec.sh (new)\n      +\t(\n      +\t\tcd client &&\n      +\t\tgit remote add upstream .. &&\n     -+\t\ttest_cmp_config \"+:\" remote.upstream.fetch\n     ++\t\ttest_might_fail git config unset remote.upstream.fetch &&\n     ++\t\tgit config remote.upstream.refmap \\\n     ++\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n      +\t)\n      +'\n      +\n     @@ t/t5585-fetch-tracking-refspec.sh (new)\n      +\t)\n      +'\n      +\n     -+test_expect_success 'git remote show does not choke on the tracking refspec' '\n     ++test_expect_success 'git remote show does not choke on a refmap-only remote' '\n      +\t(\n      +\t\tcd client &&\n      +\t\tgit remote show upstream\n -:  ---------- > 3:  604e584956 remote: add \"git remote add --limited-fetch\"\n -:  ---------- > 4:  31462e9445 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n"},{"id":"553274","messageId":"b04c00b974ce488ea1eb82556040fb54c05dad5a.1790333402.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com","subject":"[PATCH v3 1/4] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-25T10:49:59Z","receivedAt":"2026-09-25T10:50:07Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. It only takes effect\nwhen there is something explicit to fetch, on the command line or via\nremote.<name>.fetch, matching how --refmap itself already behaves.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  6 ++++++\n Documentation/fetch-options.adoc |  3 +++\n builtin/fetch.c                  |  8 +++++---\n remote.c                         | 12 +++++++++++-\n remote.h                         |  6 ++++++\n t/t5510-fetch.sh                 | 17 +++++++++++++++++\n 6 files changed, 48 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..00f9ed8d3d 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,12 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tOnly takes effect when the fetch names what to fetch explicitly,\n+\teither on the command line or via `remote.<name>.fetch`. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 035f780e58..538914bc6e 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 533fdfe7d8..7651b41139 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,14 +554,14 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n-\t} else if (refmap.nr) {\n+\t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n \t\t/* Use the defaults */\ndiff --git a/remote.c b/remote.c\nindex fe62068463..017cd9d13e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex a8d38d9176..bd853d0a84 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,23 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n"},{"id":"553276","messageId":"4ec508a223a19c3aac8cf368e3dc0314ff6de479.1790333402.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com","subject":"[PATCH v3 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-25T10:50:00Z","receivedAt":"2026-09-25T10:50:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote, plus the remote's default\nbranch, which is always included so it is available even before\nanything is set up to track it. This lets a remote be configured to\nfetch only the branches actually in use, without listing them by\nhand in remote.<name>.fetch, and without needing to touch the\ncommand line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   7 +-\n Documentation/fetch-options.adoc |   8 +-\n builtin/fetch.c                  | 134 +++++++++++++++++++++++++------\n remote.c                         |  29 +++++++\n remote.h                         |   3 +\n t/meson.build                    |   1 +\n t/t5585-fetch-refmap.sh          | 124 ++++++++++++++++++++++++++++\n 7 files changed, 277 insertions(+), 29 deletions(-)\n create mode 100755 t/t5585-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 00f9ed8d3d..5b68af3af1 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -35,9 +35,10 @@ remote.<name>.fetch::\n \n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n-\tOnly takes effect when the fetch names what to fetch explicitly,\n-\teither on the command line or via `remote.<name>.fetch`. See the\n-\t`--refmap` entry in linkgit:git-fetch[1].\n+\tIf `remote.<name>.fetch` is not set either, a refspec-less fetch\n+\tinfers what to fetch from local branches built on this remote,\n+\tinstead of fetching every branch it has. See the `--refmap` entry\n+\tin linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 538914bc6e..c973a07caf 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,12 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+there is nothing to fetch, neither on the command line nor from\n+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n+whose `@{upstream}` is on that remote, plus the remote's default branch,\n+which is always included so that it is available even before anything\n+is set up to track it.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 7651b41139..c8ce89cf30 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -503,7 +503,8 @@ static void filter_prefetch_refspec(struct refspec *rs)\n static struct ref *get_ref_map(struct remote *remote,\n \t\t\t       const struct ref *remote_refs,\n \t\t\t       struct refspec *rs,\n-\t\t\t       int tags, int *autotags)\n+\t\t\t       int tags, int *autotags,\n+\t\t\t       char **bootstrap_head_branch)\n {\n \tint i;\n \tstruct ref *rm;\n@@ -511,6 +512,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref **tail = &ref_map;\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint inferred_branches = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -518,15 +521,71 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\t\tstruct ref *head;\n+\t\tchar *default_branch = NULL;\n+\t\tchar *default_branch_dst = NULL;\n+\n+\t\tbranches_tracking_remote(remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\n+\t\t/* Always fetch the default branch too, as \"HEAD\". */\n+\t\thead = get_remote_ref(remote_refs, \"HEAD\");\n+\t\tif (head && head->symref && *head->symref)\n+\t\t\tdefault_branch = xstrdup(head->symref);\n+\t\tfree_one_ref(head);\n+\n+\t\tfor (i = 0; default_branch && !default_branch_dst &&\n+\t\t\t    !string_list_has_string(&tracked, default_branch) &&\n+\t\t\t    i < effective_refmap->nr; i++) {\n+\t\t\tstruct refspec_item *map = &effective_refmap->items[i];\n+\n+\t\t\tif (map->pattern)\n+\t\t\t\tmatch_refname_with_pattern(map->src, default_branch,\n+\t\t\t\t\t\t\t    map->dst, &default_branch_dst);\n+\t\t\telse if (!strcmp(map->src, default_branch))\n+\t\t\t\tdefault_branch_dst = xstrdup(map->dst);\n+\t\t}\n+\n+\t\tif (default_branch_dst) {\n+\t\t\tstruct refspec_item head_item = { .force = 1 };\n+\n+\t\t\thead_item.src = xstrdup(\"HEAD\");\n+\t\t\thead_item.dst = default_branch_dst;\n+\t\t\tget_fetch_map(remote_refs, &head_item, &tail, 1);\n+\t\t\tfree(head_item.src);\n+\t\t}\n+\n+\t\tif (default_branch && bootstrap_head_branch) {\n+\t\t\tconst char *branch_name = default_branch;\n+\t\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n+\t\t\t*bootstrap_head_branch = xstrdup(branch_name);\n+\t\t}\n+\n+\t\tfree(default_branch);\n+\t\tfree(default_branch_dst);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinferred_branches = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      inferred_branches);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -561,6 +620,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (inferred_branches) {\n+\t\t/* Already fully handled above. */\n \t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -656,6 +717,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1753,14 +1815,14 @@ static void warn_set_head(const char *remote, const char *head_name,\n }\n \n static int set_head(const struct ref *remote_refs, struct remote *remote,\n-\t\t\tint follow_remote_head)\n+\t\t\tint follow_remote_head, const char *known_head_branch)\n {\n \tint result = 0, create_only, baremirror, was_detached;\n \tstruct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,\n \t\t      b_local_head = STRBUF_INIT;\n \tconst char *no_warn_branch = remote->no_warn_branch;\n \tchar *head_name = NULL;\n-\tstruct ref *ref, *matches;\n+\tstruct ref *ref, *matches = NULL;\n \tstruct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;\n \tstruct refspec_item refspec = {\n \t\t.force = 0,\n@@ -1771,19 +1833,23 @@ static int set_head(const struct ref *remote_refs, struct remote *remote,\n \tstruct string_list heads = STRING_LIST_INIT_DUP;\n \tstruct ref_store *refs = get_main_ref_store(the_repository);\n \n-\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n-\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n-\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n-\tfor (ref = matches; ref; ref = ref->next) {\n-\t\tstring_list_append(&heads, strip_refshead(ref->name));\n-\t}\n+\tif (known_head_branch) {\n+\t\thead_name = xstrdup(known_head_branch);\n+\t} else {\n+\t\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n+\t\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n+\t\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n+\t\tfor (ref = matches; ref; ref = ref->next) {\n+\t\t\tstring_list_append(&heads, strip_refshead(ref->name));\n+\t\t}\n \n-\tif (!heads.nr)\n-\t\tresult = 1;\n-\telse if (heads.nr > 1)\n-\t\tresult = 1;\n-\telse\n-\t\thead_name = xstrdup(heads.items[0].string);\n+\t\tif (!heads.nr)\n+\t\t\tresult = 1;\n+\t\telse if (heads.nr > 1)\n+\t\t\tresult = 1;\n+\t\telse\n+\t\t\thead_name = xstrdup(heads.items[0].string);\n+\t}\n \n \tif (!head_name)\n \t\tgoto cleanup;\n@@ -1925,6 +1991,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head;\n+\tchar *bootstrap_head_branch = NULL;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -1960,15 +2027,31 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n-\n-\t\tif (transport->remote->fetch.nr) {\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n+\t\t\teffective_refmap->nr;\n+\n+\t\tif (inferred_branches) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes, \"HEAD\");\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2008,7 +2091,8 @@ static int do_fetch(struct transport *transport,\n \ttransport_ls_refs_options_release(&transport_ls_refs_options);\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n-\t\t\t      tags, &autotags);\n+\t\t\t      tags, &autotags, &bootstrap_head_branch);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \n@@ -2171,10 +2255,12 @@ static int do_fetch(struct transport *transport,\n \t\t * Way too many cases where this can go wrong so let's just\n \t\t * ignore errors and fail silently for now.\n \t\t */\n-\t\tset_head(remote_refs, transport->remote, follow_remote_head);\n+\t\tset_head(remote_refs, transport->remote, follow_remote_head,\n+\t\t\t bootstrap_head_branch);\n \t}\n \n cleanup:\n+\tfree(bootstrap_head_branch);\n \t/*\n \t * When using batched updates, we want to commit the non-rejected\n \t * updates and also handle the rejections.\ndiff --git a/remote.c b/remote.c\nindex 017cd9d13e..a5deb9b929 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..da6c3ef53a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 3ca7b27104..ab83d5409c 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -728,6 +728,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5585-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5585-fetch-refmap.sh b/t/t5585-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..4b1f51f6cd\n--- /dev/null\n+++ b/t/t5585-fetch-refmap.sh\n@@ -0,0 +1,124 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote, plus the default branch of that remote,\n+which is always included so that it is available even before anything\n+is set up to track it.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'fetching the new remote does not need every branch it has' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'set-upstream-to now resolves right after that first fetch' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch --set-upstream-to=upstream &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n"},{"id":"553275","messageId":"604e5849565f9a1d1697cd0983809f80f0e1319b.1790333402.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com","subject":"[PATCH v3 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-25T10:50:01Z","receivedAt":"2026-09-25T10:50:10Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, plus\nthe remote's default branch, as described in the previous commit.\nIt is rejected together with -t/--track or --mirror, since those\nalready say explicitly what to fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..74fbce6e0e 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on, plus the remote's default branch. See the `--refmap` entry\n+in linkgit:git-fetch[1] for details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n"},{"id":"553277","messageId":"31462e944563c94829a32e35577d7d2567ff8b36.1790333402.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com","subject":"[PATCH v3 4/4] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-25T10:50:02Z","receivedAt":"2026-09-25T10:50:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 59 +++++++++++++++++++++++++++++++++++\n 3 files changed, 70 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 74fbce6e0e..82b21eafa7 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on, plus the remote's default branch. See the `--refmap` entry\n-in linkgit:git-fetch[1] for details.\n+in linkgit:git-fetch[1] for details. `--no-limited-fetch` explicitly\n+disables this, overriding the shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..9cb88d8806 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,65 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit branch --set-upstream-to=upstream &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n"},{"id":"553337","messageId":"xmqq5wztt0r4.fsf@gitster.g","threadId":"66353","inReplyTo":"b04c00b974ce488ea1eb82556040fb54c05dad5a.1790333402.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 1/4] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-25T22:38:39Z","receivedAt":"2026-09-25T22:38:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Add a per-remote config variable, remote.<name>.refmap, that provides\n> the default value for --refmap the same way remote.<name>.fetch\n> already provides the default refspecs to fetch. It only takes effect\n> when there is something explicit to fetch, on the command line or via\n\nThis ...\n\n> remote.<name>.fetch, matching how --refmap itself already behaves.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n\n> +remote.<name>.refmap::\n> +\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n> +\tOnly takes effect when the fetch names what to fetch explicitly,\n> +\teither on the command line or via `remote.<name>.fetch`. See the\n> +\t`--refmap` entry in linkgit:git-fetch[1].\n\n... and this made me a bit puzzled.  It may be a philosophical\ndifference, but I've always viewed --refmap=<src>:<dst> to \"take\neffect\" whenever they are given, regardless of 0, 1, or more\nexplicit things to fetch.  It is just when you have zero explicit\nthings to fetch, 0 things are mapped via the refmap mechanism and 0\nthings are fetched.\n\nIn other words, what does not \"take effect\" when 0 things are given\nexplicitly to fetch is not the effect of refmap alone, but the\nentire 'git fetch' operation itself.\n\n    The default value of the `--refmap` option for linkgit:git-fetch[1].\n    Used to map remote refs being fetched to remote-tracking refs to\n    store.  See the `--refmap` entry in linkgit:git-fetch[1].\n\n> @@ -244,6 +244,9 @@ endif::git-pull[]\n>  \trefspecs and rely entirely on the refspecs supplied as\n>  \tcommand-line arguments. See section on \"Configured Remote-tracking\n>  \tBranches\" for details.\n> ++\n> +`remote.<name>.refmap` provides the default value for this option, the\n> +same way `remote.<name>.fetch` provides the default refspecs to fetch.\n\nThis is perfect.\n\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 533fdfe7d8..7651b41139 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>  \tstruct ref *rm;\n>  \tstruct ref *ref_map = NULL;\n>  \tstruct ref **tail = &ref_map;\n> +\tstruct refspec *effective_refmap =\n> +\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n>  \n>  \t/* opportunistically-updated references: */\n>  \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n> @@ -552,14 +554,14 @@ static struct ref *get_ref_map(struct remote *remote,\n>  \t\t * by ref_remove_duplicates() in favor of one of these\n>  \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n>  \t\t */\n> -\t\tif (refmap.nr)\n> -\t\t\tfetch_refspec = &refmap;\n> +\t\tif (effective_refmap && effective_refmap->nr)\n> +\t\t\tfetch_refspec = effective_refmap;\n>  \t\telse\n>  \t\t\tfetch_refspec = &remote->fetch;\n>  \n>  \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n>  \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n> -\t} else if (refmap.nr) {\n> +\t} else if (effective_refmap && effective_refmap->nr) {\n>  \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n>  \t} else {\n>  \t\t/* Use the defaults */\n\nLooking good.  One of these days, we probably should reduce our\nreliance on the file-scope static \"global variables\" but that is\nclearly outside the scope of this topic.  Perhaps once the dust\nsettles after this topic stabilizes.  I wonder if most of them can\nbe added as members to \"struct fetch_config\" and then we can pass\none instance of such struct around in the call chain, or if it\nneeds a lot more involved changes.\n"},{"id":"553339","messageId":"xmqqo6dksyj2.fsf@gitster.g","threadId":"66353","inReplyTo":"4ec508a223a19c3aac8cf368e3dc0314ff6de479.1790333402.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-25T23:26:41Z","receivedAt":"2026-09-25T23:26:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Configuring remote.<name>.refmap without remote.<name>.fetch used to\n> make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n> only meaningful with command-line refspec(s)\", since a refmap only\n> says where to put fetched refs, not what to fetch.\n>\n> Make that case infer what to fetch: the local branches whose\n> @{upstream} is already on that remote, plus the remote's default\n> branch, which is always included so it is available even before\n> anything is set up to track it. This lets a remote be configured to\n> fetch only the branches actually in use, without listing them by\n> hand in remote.<name>.fetch, and without needing to touch the\n> command line every time.\n\nYou may not necessarily be interested in what the remote's HEAD is\npointing at.\n\n - You may only be working on top of their 'maint' and their 'next'\n   without ever touching their 'master'.  It would be rude for Git\n   to decide for you to fetch their 'master', which you have no use\n   for, without being asked at all.\n\n - Or you may be very much interested in their 'master' that is\n   pointed at by their HEAD.  In such a case, you would have your\n   local branch that is forked from their 'master' *anyway*, and\n   you'll get one, without special casing their HEAD.\n\nEspecially since we are about to add the knob to trigger this\nmechanism to \"git remote add\", presumably this is to be used for\nsecond and later remotes.  The primary remote may already be\nsupplying \"refs/heads/master -> refs/remotes/origin/master\" when the\nrepository was made with \"git clone --shallow --single-branch\" or\nsomething like that.  That makes it doubly dubious to assume that\nthe branch the HEAD of this secondary remote points at is so special\nand everybody that does \"git remote add\" wants to interact with it.\n\n> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> index 538914bc6e..c973a07caf 100644\n> --- a/Documentation/fetch-options.adoc\n> +++ b/Documentation/fetch-options.adoc\n> @@ -245,8 +245,12 @@ endif::git-pull[]\n>  \tcommand-line arguments. See section on \"Configured Remote-tracking\n>  \tBranches\" for details.\n>  +\n> -`remote.<name>.refmap` provides the default value for this option, the\n> -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n> +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n> +there is nothing to fetch, neither on the command line nor from\n> +`remote.<name>.fetch`, Git infers what to fetch from the local branches\n> +whose `@{upstream}` is on that remote, plus the remote's default branch,\n> +which is always included so that it is available even before anything\n> +is set up to track it.\n\nSo, I would \"plus the remote's default branch, which is always\nincluded\".\n\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 7651b41139..c8ce89cf30 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> ...\n> +\t\tif (default_branch_dst) {\n> +\t\t\tstruct refspec_item head_item = { .force = 1 };\n> +\n> +\t\t\thead_item.src = xstrdup(\"HEAD\");\n> +\t\t\thead_item.dst = default_branch_dst;\n> +\t\t\tget_fetch_map(remote_refs, &head_item, &tail, 1);\n> +\t\t\tfree(head_item.src);\n\nThis adds a fetch map entry with rm->name = \"HEAD\" and\nrm->peer_ref->name = \"refs/remotes/second/main\".  Because \"HEAD\"\ndoes not begin with \"refs/heads/\", the report after fetch says\n\n\t* [new ref]    HEAD  -> second/main\n\ninstead the usual\n\n\t* [new branch] main  -> second/main\n\ndoesn't it?  If we do not force-include HEAD, the code would be\nsimplar and we won't have to worry about this.\n\nThanks.\n"},{"id":"553564","messageId":"d48a7004e409952cd1be104840a83168e5e72659.1790673598.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","subject":"[PATCH v4 1/4] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T09:19:55Z","receivedAt":"2026-09-29T09:20:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++++\n Documentation/fetch-options.adoc |  3 +++\n builtin/fetch.c                  |  8 +++++---\n remote.c                         | 12 +++++++++++-\n remote.h                         |  6 ++++++\n t/t5510-fetch.sh                 | 17 +++++++++++++++++\n 6 files changed, 47 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 035f780e58..538914bc6e 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 533fdfe7d8..7651b41139 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,14 +554,14 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n-\t} else if (refmap.nr) {\n+\t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n \t\t/* Use the defaults */\ndiff --git a/remote.c b/remote.c\nindex fe62068463..017cd9d13e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex a8d38d9176..bd853d0a84 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,23 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n"},{"id":"553566","messageId":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v4 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T09:19:54Z","receivedAt":"2026-09-29T09:20:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (4):\n  fetch: add remote.<name>.refmap\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  60 +++++++++++++---\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  41 ++++++++++-\n remote.h                         |   9 +++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  17 +++++\n t/t5585-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 363 insertions(+), 18 deletions(-)\n create mode 100755 t/t5585-fetch-refmap.sh\n\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v4\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v3:\n\n 1:  b04c00b974 ! 1:  d48a7004e4 fetch: add remote.<name>.refmap\n     @@ Commit message\n      \n          Add a per-remote config variable, remote.<name>.refmap, that provides\n          the default value for --refmap the same way remote.<name>.fetch\n     -    already provides the default refspecs to fetch. It only takes effect\n     -    when there is something explicit to fetch, on the command line or via\n     -    remote.<name>.fetch, matching how --refmap itself already behaves.\n     +    already provides the default refspecs to fetch. Like --refmap itself,\n     +    it only maps refs that are actually being fetched, so it has nothing\n     +    to do when there is nothing explicit to fetch, on the command line or\n     +    via remote.<name>.fetch.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/config/remote.adoc: remote.<name>.fetch::\n       \n      +remote.<name>.refmap::\n      +\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n     -+\tOnly takes effect when the fetch names what to fetch explicitly,\n     -+\teither on the command line or via `remote.<name>.fetch`. See the\n     -+\t`--refmap` entry in linkgit:git-fetch[1].\n     ++\tUsed to map remote refs being fetched to remote-tracking refs to\n     ++\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n      +\n       remote.<name>.push::\n       \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n 2:  4ec508a223 ! 2:  45b26e2bb2 fetch: infer branches to fetch from a refmap-only remote\n     @@ Commit message\n          says where to put fetched refs, not what to fetch.\n      \n          Make that case infer what to fetch: the local branches whose\n     -    @{upstream} is already on that remote, plus the remote's default\n     -    branch, which is always included so it is available even before\n     -    anything is set up to track it. This lets a remote be configured to\n     -    fetch only the branches actually in use, without listing them by\n     -    hand in remote.<name>.fetch, and without needing to touch the\n     -    command line every time.\n     +    @{upstream} is already on that remote. This lets a remote be\n     +    configured to fetch only the branches actually in use, without\n     +    listing them by hand in remote.<name>.fetch, and without needing to\n     +    touch the command line every time.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/config/remote.adoc ##\n      @@ Documentation/config/remote.adoc: remote.<name>.fetch::\n     - \n       remote.<name>.refmap::\n       \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n     --\tOnly takes effect when the fetch names what to fetch explicitly,\n     --\teither on the command line or via `remote.<name>.fetch`. See the\n     --\t`--refmap` entry in linkgit:git-fetch[1].\n     -+\tIf `remote.<name>.fetch` is not set either, a refspec-less fetch\n     -+\tinfers what to fetch from local branches built on this remote,\n     -+\tinstead of fetching every branch it has. See the `--refmap` entry\n     -+\tin linkgit:git-fetch[1].\n     + \tUsed to map remote refs being fetched to remote-tracking refs to\n     +-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n     ++\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n     ++\tfetch infers what to fetch from local branches built on this\n     ++\tremote, instead of fetching every branch it has. See the\n     ++\t`--refmap` entry in linkgit:git-fetch[1].\n       \n       remote.<name>.push::\n       \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n     @@ Documentation/fetch-options.adoc: endif::git-pull[]\n      +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n      +there is nothing to fetch, neither on the command line nor from\n      +`remote.<name>.fetch`, Git infers what to fetch from the local branches\n     -+whose `@{upstream}` is on that remote, plus the remote's default branch,\n     -+which is always included so that it is available even before anything\n     -+is set up to track it.\n     ++whose `@{upstream}` is on that remote.\n       \n       `-t`::\n       `--tags`::\n      \n       ## builtin/fetch.c ##\n     -@@ builtin/fetch.c: static void filter_prefetch_refspec(struct refspec *rs)\n     - static struct ref *get_ref_map(struct remote *remote,\n     - \t\t\t       const struct ref *remote_refs,\n     - \t\t\t       struct refspec *rs,\n     --\t\t\t       int tags, int *autotags)\n     -+\t\t\t       int tags, int *autotags,\n     -+\t\t\t       char **bootstrap_head_branch)\n     - {\n     - \tint i;\n     - \tstruct ref *rm;\n      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \tstruct ref **tail = &ref_map;\n       \tstruct refspec *effective_refmap =\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n      +\t    effective_refmap && effective_refmap->nr) {\n      +\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n      +\t\tstruct string_list_item *item;\n     -+\t\tstruct ref *head;\n     -+\t\tchar *default_branch = NULL;\n     -+\t\tchar *default_branch_dst = NULL;\n      +\n      +\t\tbranches_tracking_remote(remote, &tracked);\n      +\t\tfor_each_string_list_item(item, &tracked)\n      +\t\t\trefspec_append(&inferred_rs, item->string);\n     -+\n     -+\t\t/* Always fetch the default branch too, as \"HEAD\". */\n     -+\t\thead = get_remote_ref(remote_refs, \"HEAD\");\n     -+\t\tif (head && head->symref && *head->symref)\n     -+\t\t\tdefault_branch = xstrdup(head->symref);\n     -+\t\tfree_one_ref(head);\n     -+\n     -+\t\tfor (i = 0; default_branch && !default_branch_dst &&\n     -+\t\t\t    !string_list_has_string(&tracked, default_branch) &&\n     -+\t\t\t    i < effective_refmap->nr; i++) {\n     -+\t\t\tstruct refspec_item *map = &effective_refmap->items[i];\n     -+\n     -+\t\t\tif (map->pattern)\n     -+\t\t\t\tmatch_refname_with_pattern(map->src, default_branch,\n     -+\t\t\t\t\t\t\t    map->dst, &default_branch_dst);\n     -+\t\t\telse if (!strcmp(map->src, default_branch))\n     -+\t\t\t\tdefault_branch_dst = xstrdup(map->dst);\n     -+\t\t}\n     -+\n     -+\t\tif (default_branch_dst) {\n     -+\t\t\tstruct refspec_item head_item = { .force = 1 };\n     -+\n     -+\t\t\thead_item.src = xstrdup(\"HEAD\");\n     -+\t\t\thead_item.dst = default_branch_dst;\n     -+\t\t\tget_fetch_map(remote_refs, &head_item, &tail, 1);\n     -+\t\t\tfree(head_item.src);\n     -+\t\t}\n     -+\n     -+\t\tif (default_branch && bootstrap_head_branch) {\n     -+\t\t\tconst char *branch_name = default_branch;\n     -+\t\t\tskip_prefix(branch_name, \"refs/heads/\", &branch_name);\n     -+\t\t\t*bootstrap_head_branch = xstrdup(branch_name);\n     -+\t\t}\n     -+\n     -+\t\tfree(default_branch);\n     -+\t\tfree(default_branch_dst);\n      +\t\tstring_list_clear(&tracked, 0);\n      +\n      +\t\trs = &inferred_rs;\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \treturn ref_map;\n       }\n       \n     -@@ builtin/fetch.c: static void warn_set_head(const char *remote, const char *head_name,\n     - }\n     - \n     - static int set_head(const struct ref *remote_refs, struct remote *remote,\n     --\t\t\tint follow_remote_head)\n     -+\t\t\tint follow_remote_head, const char *known_head_branch)\n     - {\n     - \tint result = 0, create_only, baremirror, was_detached;\n     - \tstruct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,\n     - \t\t      b_local_head = STRBUF_INIT;\n     - \tconst char *no_warn_branch = remote->no_warn_branch;\n     - \tchar *head_name = NULL;\n     --\tstruct ref *ref, *matches;\n     -+\tstruct ref *ref, *matches = NULL;\n     - \tstruct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;\n     - \tstruct refspec_item refspec = {\n     - \t\t.force = 0,\n     -@@ builtin/fetch.c: static int set_head(const struct ref *remote_refs, struct remote *remote,\n     - \tstruct string_list heads = STRING_LIST_INIT_DUP;\n     - \tstruct ref_store *refs = get_main_ref_store(the_repository);\n     - \n     --\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n     --\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n     --\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n     --\tfor (ref = matches; ref; ref = ref->next) {\n     --\t\tstring_list_append(&heads, strip_refshead(ref->name));\n     --\t}\n     -+\tif (known_head_branch) {\n     -+\t\thead_name = xstrdup(known_head_branch);\n     -+\t} else {\n     -+\t\tget_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);\n     -+\t\tmatches = guess_remote_head(find_ref_by_name(remote_refs, \"HEAD\"),\n     -+\t\t\t\t\t    fetch_map, REMOTE_GUESS_HEAD_ALL);\n     -+\t\tfor (ref = matches; ref; ref = ref->next) {\n     -+\t\t\tstring_list_append(&heads, strip_refshead(ref->name));\n     -+\t\t}\n     - \n     --\tif (!heads.nr)\n     --\t\tresult = 1;\n     --\telse if (heads.nr > 1)\n     --\t\tresult = 1;\n     --\telse\n     --\t\thead_name = xstrdup(heads.items[0].string);\n     -+\t\tif (!heads.nr)\n     -+\t\t\tresult = 1;\n     -+\t\telse if (heads.nr > 1)\n     -+\t\t\tresult = 1;\n     -+\t\telse\n     -+\t\t\thead_name = xstrdup(heads.items[0].string);\n     -+\t}\n     - \n     - \tif (!head_name)\n     - \t\tgoto cleanup;\n     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \tstruct strmap rejected_refs = STRMAP_INIT;\n     - \tint summary_width = 0;\n     - \tint follow_remote_head;\n     -+\tchar *bootstrap_head_branch = NULL;\n     - \n     - \tif (tags == TAGS_DEFAULT) {\n     - \t\tif (transport->remote->fetch_tags == 2)\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n       \t} else {\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      +\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n      +\t\t\t\t\t    item->string);\n      +\t\t\tstring_list_clear(&tracked, 0);\n     -+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes, \"HEAD\");\n      +\t\t} else if (transport->remote->fetch.nr) {\n       \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n       \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n       \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \ttransport_ls_refs_options_release(&transport_ls_refs_options);\n       \n       \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n     --\t\t\t      tags, &autotags);\n     -+\t\t\t      tags, &autotags, &bootstrap_head_branch);\n     + \t\t\t      tags, &autotags);\n      +\n       \tif (!update_head_ok)\n       \t\tcheck_not_current_branch(ref_map);\n       \n     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \t\t * Way too many cases where this can go wrong so let's just\n     - \t\t * ignore errors and fail silently for now.\n     - \t\t */\n     --\t\tset_head(remote_refs, transport->remote, follow_remote_head);\n     -+\t\tset_head(remote_refs, transport->remote, follow_remote_head,\n     -+\t\t\t bootstrap_head_branch);\n     - \t}\n     - \n     - cleanup:\n     -+\tfree(bootstrap_head_branch);\n     - \t/*\n     - \t * When using batched updates, we want to commit the non-rejected\n     - \t * updates and also handle the rejections.\n      \n       ## remote.c ##\n      @@ remote.c: int branch_merge_matches(struct branch *branch,\n     @@ t/t5585-fetch-refmap.sh (new)\n      +\n      +When a remote has a refmap configured but no fetch refspec, a\n      +refspec-less fetch infers what to fetch from the local branches whose\n     -+@{upstream} is on that remote, plus the default branch of that remote,\n     -+which is always included so that it is available even before anything\n     -+is set up to track it.\n     ++@{upstream} is on that remote.\n      +'\n      +\n      +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n     @@ t/t5585-fetch-refmap.sh (new)\n      +\t)\n      +'\n      +\n     -+test_expect_success 'fetching the new remote does not need every branch it has' '\n     ++test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n      +\t(\n      +\t\tcd client &&\n      +\t\tgit fetch upstream &&\n      +\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n     -+\t\tcat >expect <<-\\EOF &&\n     -+\t\trefs/remotes/upstream/HEAD\n     -+\t\trefs/remotes/upstream/main\n     -+\t\tEOF\n     -+\t\ttest_cmp expect actual\n     ++\t\ttest_must_be_empty actual\n      +\t)\n      +'\n      +\n     -+test_expect_success 'set-upstream-to now resolves right after that first fetch' '\n     ++test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n      +\t(\n      +\t\tcd client &&\n     -+\t\tgit branch --set-upstream-to=upstream &&\n     ++\t\tgit fetch upstream main &&\n     ++\t\tgit branch --set-upstream-to=upstream/main &&\n      +\t\ttest_cmp_config upstream branch.main.remote &&\n      +\t\ttest_cmp_config refs/heads/main branch.main.merge\n      +\t)\n 3:  604e584956 ! 3:  e2f072c254 remote: add \"git remote add --limited-fetch\"\n     @@ Documentation/git-remote.adoc: the `refs/remotes/<name>/` namespace, a refspec t\n      +With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n      +that tracks all branches, `remote.<name>.refmap` is set up so that a\n      +refspec-less `git fetch <name>` only fetches branches our local branches\n     -+are built on, plus the remote's default branch. See the `--refmap` entry\n     -+in linkgit:git-fetch[1] for details.\n     ++are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n     ++details.\n      ++\n       With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n       up to point at remote's _<master>_ branch. See also the set-head command.\n 4:  31462e9445 ! 4:  43b9711a2c remote: default to --limited-fetch in a shallow repository\n     @@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at\n      @@ Documentation/git-remote.adoc: With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n       that tracks all branches, `remote.<name>.refmap` is set up so that a\n       refspec-less `git fetch <name>` only fetches branches our local branches\n     - are built on, plus the remote's default branch. See the `--refmap` entry\n     --in linkgit:git-fetch[1] for details.\n     -+in linkgit:git-fetch[1] for details. `--no-limited-fetch` explicitly\n     -+disables this, overriding the shallow-repository default described above.\n     + are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n     +-details.\n     ++details. `--no-limited-fetch` explicitly disables this, overriding the\n     ++shallow-repository default described above.\n       +\n       With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n       up to point at remote's _<master>_ branch. See also the set-head command.\n     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only\n      +\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n      +\t\t\tremote.upstream.refmap &&\n      +\t\ttest_must_fail git config get remote.upstream.fetch &&\n     ++\t\tgit fetch upstream main &&\n     ++\t\tgit branch --set-upstream-to=upstream/main &&\n     ++\t\ttest_cmp_config upstream branch.main.remote &&\n     ++\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n      +\t\tgit fetch upstream &&\n      +\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n      +\t\tcat >expect <<-\\EOF &&\n      +\t\trefs/remotes/upstream/HEAD\n      +\t\trefs/remotes/upstream/main\n      +\t\tEOF\n     -+\t\ttest_cmp expect actual &&\n     -+\t\tgit branch --set-upstream-to=upstream &&\n     -+\t\ttest_cmp_config upstream branch.main.remote &&\n     -+\t\ttest_cmp_config refs/heads/main branch.main.merge\n     ++\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n\n-- \ngitgitgadget\n"},{"id":"553563","messageId":"45b26e2bb292f31eb36d88e2fcf8801eb8313374.1790673598.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","subject":"[PATCH v4 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T09:19:56Z","receivedAt":"2026-09-29T09:20:05Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  52 ++++++++++++--\n remote.c                         |  29 ++++++++\n remote.h                         |   3 +\n t/meson.build                    |   1 +\n t/t5585-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 205 insertions(+), 10 deletions(-)\n create mode 100755 t/t5585-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 538914bc6e..315daf97d7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+there is nothing to fetch, neither on the command line nor from\n+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n+whose `@{upstream}` is on that remote.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 7651b41139..7ef2776f17 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -511,6 +511,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref **tail = &ref_map;\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint inferred_branches = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -518,15 +520,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinferred_branches = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      inferred_branches);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -561,6 +580,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (inferred_branches) {\n+\t\t/* Already fully handled above. */\n \t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -656,6 +677,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n-\n-\t\tif (transport->remote->fetch.nr) {\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n+\t\t\teffective_refmap->nr;\n+\n+\t\tif (inferred_branches) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n \t\t\t      tags, &autotags);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \ndiff --git a/remote.c b/remote.c\nindex 017cd9d13e..a5deb9b929 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..da6c3ef53a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 3ca7b27104..ab83d5409c 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -728,6 +728,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5585-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5585-fetch-refmap.sh b/t/t5585-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5585-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n"},{"id":"553567","messageId":"e2f072c254d00473c11c1ac83cc0631acffbaecb.1790673598.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","subject":"[PATCH v4 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T09:19:57Z","receivedAt":"2026-09-29T09:20:05Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, plus\nthe remote's default branch, as described in the previous commit.\nIt is rejected together with -t/--track or --mirror, since those\nalready say explicitly what to fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n"},{"id":"553565","messageId":"43b9711a2cd100e4987393acd588de2823b0acb3.1790673598.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","subject":"[PATCH v4 4/4] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-09-29T09:19:58Z","receivedAt":"2026-09-29T09:20:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n"},{"id":"553568","messageId":"CAHwyqnX2O1e+f6qss5+AL3f-Q-PHQmZjbayiph2cQiQGDjSAkA@mail.gmail.com","threadId":"66353","inReplyTo":"45b26e2bb292f31eb36d88e2fcf8801eb8313374.1790673598.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-09-29T09:27:29Z","receivedAt":"2026-09-29T09:28:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> +test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n> +       (\n> +               cd client &&\n> +               git fetch upstream &&\n> +               git for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n> +               test_must_be_empty actual\n> +       )\n> +'\n\nI still don't think it makes sense that bare 'git fetch upstream'\ndoesn't fetch the HEAD branch to then allow me to do\n\n    git branch --set-upstream-to=upstream\n\nBut maybe that's for another topic.\n\n\nHarald\n"},{"id":"553634","messageId":"xmqqqzibg884.fsf@gitster.g","threadId":"66353","inReplyTo":"pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-29T19:36:59Z","receivedAt":"2026-09-29T19:37:02Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Avoid fetching every branch of a new remote in a shallow repo.\n>\n> Changes in v4:\n>\n>  * Removed the automatic default-branch fetch. A fresh remote fetches\n>    nothing until you track a branch explicitly.\n>  * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n>  * Reworded remote..refmap docs and commit message.\n\nWill replace.\n\nAs my eyes have been contaminated by and biased for this topic, I'll\nspend my time on other topics first to let my eyes and brain\n\"forget\" about it before I revisit these patches.  I'd really\nwelcome reviews with fresh eyes to happen in the meantime.\n\nThanks.\n"},{"id":"553639","messageId":"xmqqzewzerrh.fsf@gitster.g","threadId":"66353","inReplyTo":"45b26e2bb292f31eb36d88e2fcf8801eb8313374.1790673598.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-29T20:17:54Z","receivedAt":"2026-09-29T20:17:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n>  t/t5585-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n\nPlease renumber.  This number is taken by another topic already in\nflight, I think.\n"},{"id":"553890","messageId":"pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v5 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-02T07:13:14Z","receivedAt":"2026-10-02T07:13:22Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (4):\n  fetch: add remote.<name>.refmap\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  60 +++++++++++++---\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  41 ++++++++++-\n remote.h                         |   9 +++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  17 +++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 363 insertions(+), 18 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v5\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v4:\n\n 1:  d48a7004e4 = 1:  d48a7004e4 fetch: add remote.<name>.refmap\n 2:  45b26e2bb2 ! 2:  fecdbc43b8 fetch: infer branches to fetch from a refmap-only remote\n     @@ t/meson.build: integration_tests = [\n         't5582-fetch-negative-refspec.sh',\n         't5583-push-branches.sh',\n         't5584-http-429-retry.sh',\n     -+  't5585-fetch-refmap.sh',\n     ++  't5586-fetch-refmap.sh',\n         't5600-clone-fail-cleanup.sh',\n         't5601-clone.sh',\n         't5602-clone-remote-exec.sh',\n      \n     - ## t/t5585-fetch-refmap.sh (new) ##\n     + ## t/t5586-fetch-refmap.sh (new) ##\n      @@\n      +#!/bin/sh\n      +\n 3:  e2f072c254 = 3:  b45f3cf0ab remote: add \"git remote add --limited-fetch\"\n 4:  43b9711a2c = 4:  8c2a144eb7 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n"},{"id":"553891","messageId":"d48a7004e409952cd1be104840a83168e5e72659.1790925198.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com","subject":"[PATCH v5 1/4] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-02T07:13:15Z","receivedAt":"2026-10-02T07:13:25Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++++\n Documentation/fetch-options.adoc |  3 +++\n builtin/fetch.c                  |  8 +++++---\n remote.c                         | 12 +++++++++++-\n remote.h                         |  6 ++++++\n t/t5510-fetch.sh                 | 17 +++++++++++++++++\n 6 files changed, 47 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 035f780e58..538914bc6e 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 533fdfe7d8..7651b41139 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,14 +554,14 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n-\t} else if (refmap.nr) {\n+\t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n \t\t/* Use the defaults */\ndiff --git a/remote.c b/remote.c\nindex fe62068463..017cd9d13e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex a8d38d9176..bd853d0a84 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,23 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n"},{"id":"553892","messageId":"fecdbc43b8fcf1563756234f28928c737562c583.1790925198.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com","subject":"[PATCH v5 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-02T07:13:16Z","receivedAt":"2026-10-02T07:13:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  52 ++++++++++++--\n remote.c                         |  29 ++++++++\n remote.h                         |   3 +\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 205 insertions(+), 10 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 538914bc6e..315daf97d7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+there is nothing to fetch, neither on the command line nor from\n+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n+whose `@{upstream}` is on that remote.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 7651b41139..7ef2776f17 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -511,6 +511,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref **tail = &ref_map;\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint inferred_branches = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -518,15 +520,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinferred_branches = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      inferred_branches);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -561,6 +580,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (inferred_branches) {\n+\t\t/* Already fully handled above. */\n \t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -656,6 +677,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n-\n-\t\tif (transport->remote->fetch.nr) {\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n+\t\t\teffective_refmap->nr;\n+\n+\t\tif (inferred_branches) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n \t\t\t      tags, &autotags);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \ndiff --git a/remote.c b/remote.c\nindex 017cd9d13e..a5deb9b929 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..da6c3ef53a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 3ca7b27104..a1c7465c2b 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -728,6 +728,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n"},{"id":"553893","messageId":"b45f3cf0ab1e2dccd87cc4615e0ba15e6cc996b6.1790925198.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com","subject":"[PATCH v5 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-02T07:13:17Z","receivedAt":"2026-10-02T07:13:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, plus\nthe remote's default branch, as described in the previous commit.\nIt is rejected together with -t/--track or --mirror, since those\nalready say explicitly what to fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n"},{"id":"553894","messageId":"8c2a144eb7d9010ce1239d92aadd1288108921c3.1790925198.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com","subject":"[PATCH v5 4/4] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-02T07:13:18Z","receivedAt":"2026-10-02T07:13:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n"},{"id":"553981","messageId":"xmqqzewwxe1y.fsf@gitster.g","threadId":"66353","inReplyTo":"b45f3cf0ab1e2dccd87cc4615e0ba15e6cc996b6.1790925198.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v5 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-02T16:28:09Z","receivedAt":"2026-10-02T16:28:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A remote added the ordinary way tracks every branch it has, via a\n> wildcard remote.<name>.fetch refspec. That is wasteful for a remote\n> whose history is only worth following for the branches actually in\n> use locally, and it can make \"git fetch\" negotiate history for\n> branches nobody asked for.\n>\n> Give \"git remote add\" a --limited-fetch option that sets up\n> remote.<name>.refmap instead of remote.<name>.fetch, so that\n> \"git fetch <name>\" only fetches the branches already tracked, plus\n> the remote's default branch, as described in the previous commit.\n> It is rejected together with -t/--track or --mirror, since those\n> already say explicitly what to fetch.\n\nYou forgot to remove \", plus the ... in the previous commit\",\nprobably, when you updated the series to stop doing so in the step\n[2/4].\n"},{"id":"554095","messageId":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v6 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-04T08:31:20Z","receivedAt":"2026-10-04T08:31:27Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (4):\n  fetch: add remote.<name>.refmap\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  60 +++++++++++++---\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  41 ++++++++++-\n remote.h                         |   9 +++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  17 +++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 363 insertions(+), 18 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: c46c1e37724f0478939de636ab8ea5a89086d532\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v6\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v5:\n\n 1:  d48a7004e4 = 1:  5c31a51bb7 fetch: add remote.<name>.refmap\n 2:  fecdbc43b8 = 2:  74c177e3e2 fetch: infer branches to fetch from a refmap-only remote\n 3:  b45f3cf0ab ! 3:  3abcc8915b remote: add \"git remote add --limited-fetch\"\n     @@ Commit message\n      \n          Give \"git remote add\" a --limited-fetch option that sets up\n          remote.<name>.refmap instead of remote.<name>.fetch, so that\n     -    \"git fetch <name>\" only fetches the branches already tracked, plus\n     -    the remote's default branch, as described in the previous commit.\n     -    It is rejected together with -t/--track or --mirror, since those\n     -    already say explicitly what to fetch.\n     +    \"git fetch <name>\" only fetches the branches already tracked, as\n     +    described in the previous commit. It is rejected together with\n     +    -t/--track or --mirror, since those already say explicitly what to\n     +    fetch.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n 4:  8c2a144eb7 = 4:  84d192445c remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n"},{"id":"554096","messageId":"5c31a51bb7ad9d8e7e88522e3e4903a07d830568.1791102684.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","subject":"[PATCH v6 1/4] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-04T08:31:21Z","receivedAt":"2026-10-04T08:31:29Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++++\n Documentation/fetch-options.adoc |  3 +++\n builtin/fetch.c                  |  8 +++++---\n remote.c                         | 12 +++++++++++-\n remote.h                         |  6 ++++++\n t/t5510-fetch.sh                 | 17 +++++++++++++++++\n 6 files changed, 47 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 035f780e58..538914bc6e 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..04d0a78ecf 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,14 +554,14 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n-\t} else if (refmap.nr) {\n+\t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n \t\t/* Use the defaults */\ndiff --git a/remote.c b/remote.c\nindex fe62068463..017cd9d13e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..36fa9319e1 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,23 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n"},{"id":"554097","messageId":"74c177e3e22d140c7cd29b8e8a2a550944eb5cd2.1791102684.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","subject":"[PATCH v6 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-04T08:31:22Z","receivedAt":"2026-10-04T08:31:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  52 ++++++++++++--\n remote.c                         |  29 ++++++++\n remote.h                         |   3 +\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 205 insertions(+), 10 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 538914bc6e..315daf97d7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+there is nothing to fetch, neither on the command line nor from\n+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n+whose `@{upstream}` is on that remote.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 04d0a78ecf..e928f5d7e0 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -511,6 +511,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref **tail = &ref_map;\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint inferred_branches = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -518,15 +520,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinferred_branches = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      inferred_branches);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -561,6 +580,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (inferred_branches) {\n+\t\t/* Already fully handled above. */\n \t} else if (effective_refmap && effective_refmap->nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -656,6 +677,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n-\n-\t\tif (transport->remote->fetch.nr) {\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n+\t\t\teffective_refmap->nr;\n+\n+\t\tif (inferred_branches) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n \t\t\t      tags, &autotags);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \ndiff --git a/remote.c b/remote.c\nindex 017cd9d13e..a5deb9b929 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..da6c3ef53a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..e287a6b947 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -730,6 +730,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n"},{"id":"554099","messageId":"3abcc8915b4aa199d95a4eeb39a57de2fc491d3d.1791102684.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","subject":"[PATCH v6 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-04T08:31:23Z","receivedAt":"2026-10-04T08:31:32Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n"},{"id":"554098","messageId":"84d192445c2636b618e86e632ee8439574aa0058.1791102684.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","subject":"[PATCH v6 4/4] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-04T08:31:24Z","receivedAt":"2026-10-04T08:31:33Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n"},{"id":"554119","messageId":"xmqqv77hs7ut.fsf@gitster.g","threadId":"66353","inReplyTo":"pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com","subject":"Re: [PATCH v6 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-04T17:17:46Z","receivedAt":"2026-10-04T17:17:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Avoid fetching every branch of a new remote in a shallow repo.\n>\n> Changes in v6:\n>\n>  * Remove leftover reference to deleted default-branch logic in commit\n>    message.\n\nThanks.  I think this is becoming much better, but I see one glitch\nand one design question, for which I do not yet know the right\nanswer.\n\nBefore going there, since one of the test scripts added by this\nseries is called 'fetch refmap', we should have a test or two to\ncheck its more basic use.\n\nWhen the user configures remote.origin.refmap, the command should\nbehave as if --refmap were given on the command line, even when the\nrepository does not yet have a local branch that builds on anything\nfrom the remote.  Attached is my attempt to do so.  It does multiple\nthings in a single block, which we may want to split up, but I am\nsending it here to illustrate what we might want to test and, more\nimportantly, to present a scenario that exposes both the design\nquestion and the glitch.\n\nThe early part of the scenario goes like this:\n\n * We create a new repository and add \"..\" as a remote.\n * We remove remote.origin.fetch and set up remote.origin.refmap.\n * When we run \"git fetch origin\", nothing is fetched because\n   nothing yet builds on what we would fetch from them.\n\nIf you try to run this with [1/4] alone, however, it errors out with\n\"fatal: --refmap option is only meaningful with command-line\nrefspec\", which is suboptimal when triggered by a configuration\nvariable.  Even though our design says that remote.*.refmap makes\nthe command behave as if the user gave '--refmap' on the command\nline, applying that rule here is a bit too strict.\n\nFortunately, this is rectified later in the series when we begin\ntracking which of our local branches build on what we get from them.\nEven when the number of branches to fetch is zero, meaning we should\npretend no command-line refspec was given with --refmap, we no\nlonger get the same error, which is good.\n\nThe second part of the scenario explicitly specifies what to fetch\non the command line and verifies that we fetch exactly that.\n\nThen there is the last part, where the desired behavior is unclear.\nWhat should happen if the remote.origin.* configuration defines both\nfetch and refmap?  How would we explain our choice to the users?  I\ndo not have a good answer to this design question.\n\nAs for the glitch, the last part of the test below dies with the\n\"fatal: --refmap option is only meaningful...\" message when run with\nthe current patchset.  We might decide to error out if both are set.\nAlternatively, we could ignore .refmap and use .fetch, or ignore\n.fetch and use .refmap.  Whatever we decide, the \"fatal: --refmap\noption is only meaningful...\" error is not the right message to show\nin this situation.\n\nThoughts?\n\n\n t/t5586-fetch-refmap.sh | 29 +++++++++++++++++++++++++++++\n 1 file changed, 29 insertions(+)\n\ndiff --git c/t/t5586-fetch-refmap.sh w/t/t5586-fetch-refmap.sh\nindex b81fc48cbe..30a8d79c16 100755\n--- c/t/t5586-fetch-refmap.sh\n+++ w/t/t5586-fetch-refmap.sh\n@@ -22,6 +22,35 @@ test_expect_success 'setup' '\n \tgit checkout main\n '\n \n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline\" &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../ &&\n+\n+\t\t# without fetch refspec, but with fetch refmap\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# nothing tracked, nothing fetched, no error\n+\t\tgit fetch origin 2>error &&\n+\t\ttest_grep ! \"fatal: --refmap option is only meaningful\" error &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\t\ttest_line_count = 0 actual &&\n+\n+\t\t# nothing tracked, explicit ref on the command line\n+\t\tgit fetch origin main &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\t\techo refs/remotes/origin/main >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# what should happen when we have both refmap and refspec?\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin\n+\t)\n+'\n+\n test_expect_success 'clone shallow and single-branch, then add a second remote' '\n \tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n \t(\n"},{"id":"554128","messageId":"CAHwyqnXz+acRBytu9tWL+RzsKnzTZyZCnzzQQhEFjvWT5cgoww@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqv77hs7ut.fsf@gitster.g","subject":"Re: [PATCH v6 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-04T19:51:01Z","receivedAt":"2026-10-04T19:51:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> If you try to run this with [1/4] alone, however, it errors out with\n> \"fatal: --refmap option is only meaningful with command-line\n> refspec\", which is suboptimal when triggered by a configuration\n> variable.  Even though our design says that remote.*.refmap makes\n> the command behave as if the user gave '--refmap' on the command\n> line, applying that rule here is a bit too strict.\n\nThanks for pointing that out. How did you find that?\n\n> Then there is the last part, where the desired behavior is unclear.\n> What should happen if the remote.origin.* configuration defines both\n> fetch and refmap?  How would we explain our choice to the users?  I\n> do not have a good answer to this design question.\n\nI think that when both `remote.<name>.fetch` and\n`remote.<name>.refmap` are configured, remote.<name>.fetch can win.\n\n> +test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n> +       test_when_finished \"rm -fr fetch-refmap-baseline\" &&\n> +       git init fetch-refmap-baseline &&\n> +       (\n> +               cd fetch-refmap-baseline &&\n> +               git remote add origin ../ &&\n> +\n> +               # without fetch refspec, but with fetch refmap\n> +               git config --unset-all remote.origin.fetch &&\n> +               git config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\n> +               # nothing tracked, nothing fetched, no error\n> +               git fetch origin 2>error &&\n> +               test_grep ! \"fatal: --refmap option is only meaningful\" error &&\n> +               git for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n> +               test_line_count = 0 actual &&\n> +\n> +               # nothing tracked, explicit ref on the command line\n> +               git fetch origin main &&\n> +               git for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n> +               echo refs/remotes/origin/main >expect &&\n> +               test_cmp expect actual &&\n> +\n> +               # what should happen when we have both refmap and refspec?\n> +               git config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +               git fetch origin\n> +       )\n\nThanks for providing this!\n\n\n\nHarald\n"},{"id":"554167","messageId":"xmqqv77gnxyg.fsf@gitster.g","threadId":"66353","inReplyTo":"CAHwyqnXz+acRBytu9tWL+RzsKnzTZyZCnzzQQhEFjvWT5cgoww@mail.gmail.com","subject":"Re: [PATCH v6 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-05T12:17:27Z","receivedAt":"2026-10-05T12:17:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n>> If you try to run this with [1/4] alone, however, it errors out with\n>> \"fatal: --refmap option is only meaningful with command-line\n>> refspec\", which is suboptimal when triggered by a configuration\n>> variable.  Even though our design says that remote.*.refmap makes\n>> the command behave as if the user gave '--refmap' on the command\n>> line, applying that rule here is a bit too strict.\n>\n> Thanks for pointing that out. How did you find that?\n\nBy thinking the process through while writing these very basic\ntests.\n\nI think this topic textually interacts with Collin's followRemoteHAD\nwork.  I tried to be careful while resolving the conflicts when I\nmerged both to 'seen', but please double check the result when I\npush it out.\n"},{"id":"554207","messageId":"CAHwyqnXSmQ_E4u6S_dTVHe82E6ErqQAHpOUDcUZBGyLRbne-Pg@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqv77gnxyg.fsf@gitster.g","subject":"Re: [PATCH v6 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-05T18:09:30Z","receivedAt":"2026-10-05T18:09:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> I think this topic textually interacts with Collin's followRemoteHAD\n> work.  I tried to be careful while resolving the conflicts when I\n> merged both to 'seen', but please double check the result when I\n> push it out.\n\nI range-diffed against 'seen' and everything looks normal.\n\nI have a new version ready to send out, do you want me to hold off?\n\n\nHarald\n\n"},{"id":"554426","messageId":"pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v7 0/4] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-07T21:56:00Z","receivedAt":"2026-10-07T21:56:00Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (4):\n  fetch: add remote.<name>.refmap\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  58 ++++++++++++---\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  41 ++++++++++-\n remote.h                         |   9 +++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  63 ++++++++++++++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 408 insertions(+), 17 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v7\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v6:\n\n 1:  5c31a51bb7 ! 1:  b5db64d56f fetch: add remote.<name>.refmap\n     @@ Commit message\n          to do when there is nothing explicit to fetch, on the command line or\n          via remote.<name>.fetch.\n      \n     +    A refspec-less fetch with a refmap configured but nothing else to say\n     +    what to fetch falls back to the same defaults as a remote with no\n     +    refspec or refmap at all. Only an explicit --refmap given on the\n     +    command line with no command-line refspec to go with it is still\n     +    rejected, since that combination is a plain mistake to type, unlike a\n     +    remote.<name>.refmap configured on its own. A remote.<name>.fetch\n     +    that is also configured is used as before.\n     +\n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/config/remote.adoc ##\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \t\telse\n       \t\t\tfetch_refspec = &remote->fetch;\n       \n     - \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n     - \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n     --\t} else if (refmap.nr) {\n     -+\t} else if (effective_refmap && effective_refmap->nr) {\n     - \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n     - \t} else {\n     - \t\t/* Use the defaults */\n      \n       ## remote.c ##\n      @@ remote.c: static struct remote *make_remote(struct remote_state *remote_state,\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t\tgit rev-parse --verify refs/remotes/other/main\n      +\t)\n      +'\n     ++\n     ++check_fetched_refs () {\n     ++\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n     ++\tcat >expect &&\n     ++\ttest_cmp expect actual\n     ++}\n     ++\n     ++test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n     ++\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n     ++\tgit init -b main fetch-refmap-upstream &&\n     ++\ttest_commit -C fetch-refmap-upstream base &&\n     ++\tgit -C fetch-refmap-upstream branch other &&\n     ++\tgit init fetch-refmap-baseline &&\n     ++\t(\n     ++\t\tcd fetch-refmap-baseline &&\n     ++\t\tgit remote add origin ../fetch-refmap-upstream &&\n     ++\n     ++\t\t# Without fetch refspec, but with fetch refmap.\n     ++\t\tgit config --unset-all remote.origin.fetch &&\n     ++\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\n     ++\t\t# Nothing tracked, nothing fetched, no error.\n     ++\t\tgit fetch origin &&\n     ++\t\tcheck_fetched_refs <<-\\EOF &&\n     ++\t\tEOF\n     ++\n     ++\t\t# Nothing tracked, explicit ref on the command line.\n     ++\t\tgit fetch origin main &&\n     ++\t\tcheck_fetched_refs <<-\\EOF &&\n     ++\t\trefs/remotes/origin/main\n     ++\t\tEOF\n     ++\n     ++\t\t# With both refmap and fetch configured, remote.<name>.fetch\n     ++\t\t# wins: a refspec-less fetch follows it as usual, and\n     ++\t\t# remote.<name>.refmap plays no part in deciding what to\n     ++\t\t# fetch, only in remapping something that is already being\n     ++\t\t# fetched by name.\n     ++\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit fetch origin &&\n     ++\t\tcheck_fetched_refs <<-\\EOF\n     ++\t\trefs/remotes/origin/HEAD\n     ++\t\trefs/remotes/origin/main\n     ++\t\trefs/remotes/origin/other\n     ++\t\tEOF\n     ++\t)\n     ++'\n      +\n       test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n       \tgit branch -f side &&\n 2:  74c177e3e2 ! 2:  fd6864daaf fetch: infer branches to fetch from a refmap-only remote\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n      +\t} else if (inferred_branches) {\n      +\t\t/* Already fully handled above. */\n     - \t} else if (effective_refmap && effective_refmap->nr) {\n     + \t} else if (refmap.nr) {\n       \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n       \t} else {\n      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n 3:  3abcc8915b = 3:  a2208875b6 remote: add \"git remote add --limited-fetch\"\n 4:  84d192445c = 4:  c8fd073de3 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n\n"},{"id":"554427","messageId":"b5db64d56fa328ae95c420f20ecb1ebe6a39b987.1791410164.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com","subject":"[PATCH v7 1/4] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-07T21:56:01Z","receivedAt":"2026-10-07T21:56:01Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nA refspec-less fetch with a refmap configured but nothing else to say\nwhat to fetch falls back to the same defaults as a remote with no\nrefspec or refmap at all. Only an explicit --refmap given on the\ncommand line with no command-line refspec to go with it is still\nrejected, since that combination is a plain mistake to type, unlike a\nremote.<name>.refmap configured on its own. A remote.<name>.fetch\nthat is also configured is used as before.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++\n Documentation/fetch-options.adoc |  3 ++\n builtin/fetch.c                  |  6 ++-\n remote.c                         | 12 +++++-\n remote.h                         |  6 +++\n t/t5510-fetch.sh                 | 63 ++++++++++++++++++++++++++++++++\n 6 files changed, 92 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..b0ad8c0b5f 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,8 +554,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \ndiff --git a/remote.c b/remote.c\nindex 71170f36a9..99a086ea5a 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..cfd09db155 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.refmap plays no part in deciding what to\n+\t\t# fetch, only in remapping something that is already being\n+\t\t# fetched by name.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"554428","messageId":"fd6864daaf47dc3cfbd3cc7dadb5f0bd76d4eb79.1791410164.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com","subject":"[PATCH v7 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-07T21:56:02Z","receivedAt":"2026-10-07T21:56:02Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  52 ++++++++++++--\n remote.c                         |  29 ++++++++\n remote.h                         |   3 +\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 205 insertions(+), 10 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..75899d91cc 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+there is nothing to fetch, neither on the command line nor from\n+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n+whose `@{upstream}` is on that remote.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b0ad8c0b5f..e8c481ed3f 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -511,6 +511,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref **tail = &ref_map;\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint inferred_branches = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -518,15 +520,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinferred_branches = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      inferred_branches);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -561,6 +580,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (inferred_branches) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -656,6 +677,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport,\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n \t\tstruct branch *branch = branch_get(NULL);\n-\n-\t\tif (transport->remote->fetch.nr) {\n+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n+\t\t\teffective_refmap->nr;\n+\n+\t\tif (inferred_branches) {\n+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\t\tstruct string_list_item *item;\n+\n+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n+\t\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t\t    item->string);\n+\t\t\tstring_list_clear(&tracked, 0);\n+\t\t} else if (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\t\tif (tracks_this_remote) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n@@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n \n \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n \t\t\t      tags, &autotags);\n+\n \tif (!update_head_ok)\n \t\tcheck_not_current_branch(ref_map);\n \ndiff --git a/remote.c b/remote.c\nindex 99a086ea5a..c26312beea 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\tstruct branch *branch;\n+\n+\tbranch = branch_get(ref->name);\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, data->remote->name))\n+\t\treturn 0;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n+\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..da6c3ef53a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..e287a6b947 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -730,6 +730,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"554429","messageId":"a2208875b66546330ab1d0ae3632b3a01ecc5d5e.1791410164.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com","subject":"[PATCH v7 3/4] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-07T21:56:03Z","receivedAt":"2026-10-07T21:56:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"554430","messageId":"c8fd073de34b6c63d0e9e9f41220fde36f5cd40e.1791410164.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com","subject":"[PATCH v7 4/4] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-07T21:56:04Z","receivedAt":"2026-10-07T21:56:04Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"554514","messageId":"xmqqo6d4163l.fsf@gitster.g","threadId":"66353","inReplyTo":"fd6864daaf47dc3cfbd3cc7dadb5f0bd76d4eb79.1791410164.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v7 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-08T16:59:10Z","receivedAt":"2026-10-08T16:59:10Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Configuring remote.<name>.refmap without remote.<name>.fetch used to\n> make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n> only meaningful with command-line refspec(s)\", since a refmap only\n> says where to put fetched refs, not what to fetch.\n>\n> Make that case infer what to fetch: the local branches whose\n> @{upstream} is already on that remote. This lets a remote be\n> configured to fetch only the branches actually in use, without\n> listing them by hand in remote.<name>.fetch, and without needing to\n> touch the command line every time.\n\nNicely explained what we want out of this new feature.\n\n>  remote.<name>.refmap::\n>  \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n>  \tUsed to map remote refs being fetched to remote-tracking refs to\n> -\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n> +\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n> +\tfetch infers what to fetch from local branches built on this\n> +\tremote, instead of fetching every branch it has. See the\n> +\t`--refmap` entry in linkgit:git-fetch[1].\n\nOK.\n\n> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> index c2101a7b39..75899d91cc 100644\n> --- a/Documentation/fetch-options.adoc\n> +++ b/Documentation/fetch-options.adoc\n> @@ -245,8 +245,10 @@ endif::git-pull[]\n>  \tcommand-line arguments. See section on \"Configured Remote-tracking\n>  \tBranches\" for details.\n>  +\n> -`remote.<name>.refmap` provides the default value for this option, the\n> -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n> +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n> +there is nothing to fetch, neither on the command line nor from\n> +`remote.<name>.fetch`, Git infers what to fetch from the local branches\n> +whose `@{upstream}` is on that remote.\n\nTo say \"infers what to fetch\" without explicitly saying how the\ninference is made is not sufficient in a technical manual.  Even\na reading like \"up to three branches that our local branches\nhave as '@{u}'\" is possible, if not very probable.\n\nPerhaps something like this instead:\n\n    ... but nothing to fetch is specified on the command line,\n    branches from the remote that are used as '@{upstream}' of\n    our local branches are fetched.\n\n\n[...]\n\n> @@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport,\n>  \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n>  \t} else {\n>  \t\tstruct branch *branch = branch_get(NULL);\n> -\n> -\t\tif (transport->remote->fetch.nr) {\n> +\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n> +\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n> +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n> +\t\t\t&transport->remote->refmap;\n> +\t\tint inferred_branches = !transport->remote->fetch.nr &&\n> +\t\t\teffective_refmap->nr;\n> +\n> +\t\tif (inferred_branches) {\n> +\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n> +\t\t\tstruct string_list_item *item;\n> +\n> +\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n> +\t\t\tfor_each_string_list_item(item, &tracked)\n> +\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> +\t\t\t\t\t    item->string);\n> +\t\t\tstring_list_clear(&tracked, 0);\n> +\t\t} else if (transport->remote->fetch.nr) {\n>  \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n>  \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n> -\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n> -\t\t\t\tdo_set_head = 1;\n>  \t\t}\n> -\t\tif (branch && branch_has_merge_config(branch) &&\n> -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n> +\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n> +\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n> +\t\t\tdo_set_head = 1;\n> +\t\tif (tracks_this_remote) {\n>  \t\t\tint i;\n>  \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n>  \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n\n\nThe unified diff is a bit messy to compare the before-and-after\nbehaviour, so let's see what the preimage said first.\n\n>  \t\tstruct branch *branch = branch_get(NULL);\n> -\n> -\t\tif (transport->remote->fetch.nr) {\n>  \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n>  \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n> -\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n> -\t\t\t\tdo_set_head = 1;\n>  \t\t}\n> -\t\tif (branch && branch_has_merge_config(branch) &&\n> -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n>  \t\t\tint i;\n>  \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n>  \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n\nSo, when !rs->nr (i.e., nothing given on the command line to be fetched)\nand there is no fetch refspec, we checked the current branch and if\nit has merge config to merge from branches at the remote, we\nautomatically fetched them.  This is the world order before this\n\"infer with @{u} and refmap\" work, and we should behave the same way\nwhen remote.*.refmap is not set.\n\nLet's see what the postimage says.\n\n>  \t\tstruct branch *branch = branch_get(NULL);\n> +\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n> +\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n\nThis is the \"does the current branch pull branches from the remote\nwe are working with right now?\" condition we saw in the original.\n\n> +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n> +\t\t\t&transport->remote->refmap;\n\nIt is a bit annoying that refmap is a file scope static variable but\nhere we say \"The value of the --refmap option from the command line,\nor the value remote.*.refmap otherwise\".\n\n> +\t\tint inferred_branches = !transport->remote->fetch.nr &&\n> +\t\t\teffective_refmap->nr;\n\nThis variable tells the code that it must infer branches, but named\nas if it were a list of branches that were inferred.  \"When remote.*.fetch\ndoes not exist and we have the refmap to use for inferring\".\n\n> +\t\tif (inferred_branches) {\n> +\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n> +\t\t\tstruct string_list_item *item;\n> +\n> +\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n> +\t\t\tfor_each_string_list_item(item, &tracked)\n> +\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> +\t\t\t\t\t    item->string);\n> +\t\t\tstring_list_clear(&tracked, 0);\n> +\t\t} else if (transport->remote->fetch.nr) {\n>  \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n>  \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n>  \t\t}\n\nIt would have been much easier to follow if the existing code came\nfirst to make it clear that the new code is an add-on.  After all,\nwhen transport->remote->fetch.nr is true, inferred_branches is never\ntrue.\n\n> +\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n> +\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n> +\t\t\tdo_set_head = 1;\n> +\t\tif (tracks_this_remote) {\n>  \t\t\tint i;\n>  \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n>  \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n\nHow does tracks_this_remote and inferred_branches interact?  Doesn't\nthe old code that grabs necessary remote-tracking branches for the\ncurrent branch add the same branch from the remote?  Doesn't @{u}\nfor the current branch added twice on the list of branches to fetch?\n\n> @@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n>  \n>  \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n>  \t\t\t      tags, &autotags);\n> +\n>  \tif (!update_head_ok)\n>  \t\tcheck_not_current_branch(ref_map);\n\nUseless patch noise.\n\n> diff --git a/remote.c b/remote.c\n> index 99a086ea5a..c26312beea 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n>  \treturn refname_match(branch->merge[i]->src, refname);\n>  }\n>  \n> +struct branches_tracking_remote_cb_data {\n> +\tstruct remote *remote;\n> +\tstruct string_list *tracked;\n> +};\n> +\n> +static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n> +{\n> +\tstruct branches_tracking_remote_cb_data *data = cb_data;\n> +\tstruct branch *branch;\n> +\n> +\tbranch = branch_get(ref->name);\n\nI know branch_get() is defined here and allows implicit use of\nthe_repository, but can't we pass \"struct repository *\" around in\ncb_data so that we can use repo_branch_get() here?\n\n> +\tif (!branch_has_merge_config(branch) ||\n> +\t    strcmp(branch->remote_name, data->remote->name))\n> +\t\treturn 0;\n> +\n> +\tfor (int i = 0; i < branch->merge_nr; i++)\n> +\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n> +\n> +\treturn 0;\n> +}\n\nThis is more or less identical to the \"if current branch integrates\nwith branches from the remote, then fetch them\" code we saw earlier\nin the builtin/fetch.c:do_fetch() above.  I notice that its return\nvalue is meaningless, as it always returns 0.\n\nStepping back a bit, because your new logic would become superset of\nwhat we already have to support the current branch when refmap is\nused, would it make sense to restructure the code change to\ndo_fetch() more like this:\n\n\tif (rs->nr) {\n\t\t... use command line refspec ...\n\t} else if (transport->remote->fetch.nr) {\n\t\t... use remote.*.fetch refspec ...\n\t} else if (effective_refmap->nr) {\n\t\t... your new logic ...\n\t} else {\n\t\tstruct string_list list = STRING_LIST_INIT;\n\t\tcollect_upstream_from_remote(&list, remote, NULL);\n\t\tfor_each_string_list_item(item, &list)\n\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n\t\t\t\t\titem->string);\n\t}\n\t\t\t\nwhere collect_upstream_from_remote() performs the bulk of what\nadd_if_tracking_remote() does, which means add_if_tracking_remote()\nbecomes\n\n\tstatic int add_if_tracking_remote(...)\n\t{\n\t\tstruct branches_tracking_remote_cb_data *data = cb_data;\n\n\t\tcollect_upstream_from_remote(data->tracked, data->remote, ref->name);\n\t}\n\nThis will mean we will have a very small preliminary patch to\nintroduce collect_upstream_from_remote() function in remote.c and\nupdate the \"help current branch by fetching what are merged into it\"\ncode in do_fetch() to use it, which will have the above ontlined\nif/else if/ cascade except for your new refmap code.  On top, this\nstep will insert a single \"else if\" block to add your new logic to\ndo_fetch().\n\nDoesn't it make the series (and more importantly, the resulting\ncode) much easier to understand?\n\n> +void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n> +{\n> +\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n> +\n> +\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n> +\t\t\t\t  add_if_tracking_remote, &data);\n> +}\n\nThis also hardcodes the_repository, but shouldn't this function take\n\"struct repository *\" pointer (and shove it in data structure to\npass it down)?\n\nBy the way, when merged to 'seen', it seems to have some\ninteractions with other topics and makes t5505 and t5586 fail.  I\ndidn't have time to dig down to the cause.  Can you perhaps help\nfinding the cause when I push the integration result out early this\nafternoon?\n\nThanks.\n\n\n\n"},{"id":"554556","messageId":"CAHwyqnWzYvvtEWLxquOfq5yFHBTkcKCaK5z2cNa=omPtuwww4w@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqo6d4163l.fsf@gitster.g","subject":"Re: [PATCH v7 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-09T07:05:37Z","receivedAt":"2026-10-09T07:05:37Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> By the way, when merged to 'seen', it seems to have some\n> interactions with other topics and makes t5505 and t5586 fail.  I\n> didn't have time to dig down to the cause.  Can you perhaps help\n> finding the cause when I push the integration result out early this\n> afternoon?\n\nYes, there is a conflict between\n'ch/fetch-followremotehead-lazy-validation's' and my v7.\n\nBut that way it's looking now, it will go away when pushing out v8.\n\n\nHarald\n\n"},{"id":"554557","messageId":"CAHwyqnV7HufU_=T31KNa1m2PgpdVYU_EJj6doELgB-SiBQKDRA@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqo6d4163l.fsf@gitster.g","subject":"Re: [PATCH v7 2/4] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-09T08:09:59Z","receivedAt":"2026-10-09T08:09:59Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> Perhaps something like this instead:\n>\n>     ... but nothing to fetch is specified on the command line,\n>     branches from the remote that are used as '@{upstream}' of\n>     our local branches are fetched.\n\nThanks, will update.\n\n> The unified diff is a bit messy to compare the before-and-after\n> behaviour, so let's see what the preimage said first.\n>\n> >               struct branch *branch = branch_get(NULL);\n> > -\n> > -             if (transport->remote->fetch.nr) {\n> >                       refspec_ref_prefixes(&transport->remote->fetch,\n> >                                            &transport_ls_refs_options.ref_prefixes);\n> > -                     if (follow_remote_head != FOLLOW_REMOTE_NEVER)\n> > -                             do_set_head = 1;\n> >               }\n> > -             if (branch && branch_has_merge_config(branch) &&\n> > -                 !strcmp(branch->remote_name, transport->remote->name)) {\n> >                       int i;\n> >                       for (i = 0; i < branch->merge_nr; i++) {\n> >                               strvec_push(&transport_ls_refs_options.ref_prefixes,\n>\n> So, when !rs->nr (i.e., nothing given on the command line to be fetched)\n> and there is no fetch refspec, we checked the current branch and if\n> it has merge config to merge from branches at the remote, we\n> automatically fetched them.  This is the world order before this\n> \"infer with @{u} and refmap\" work, and we should behave the same way\n> when remote.*.refmap is not set.\n>\n> Let's see what the postimage says.\n>\n> >               struct branch *branch = branch_get(NULL);\n> > +             int tracks_this_remote = branch && branch_has_merge_config(branch) &&\n> > +                     !strcmp(branch->remote_name, transport->remote->name);\n\nYes, this can be improved.\n\n> This variable tells the code that it must infer branches, but named\n> as if it were a list of branches that were inferred.  \"When remote.*.fetch\n> does not exist and we have the refmap to use for inferring\".\n>\n> > +             if (inferred_branches) {\n> > +                     struct string_list tracked = STRING_LIST_INIT_DUP;\n> > +                     struct string_list_item *item;\n> > +\n> > +                     branches_tracking_remote(transport->remote, &tracked);\n> > +                     for_each_string_list_item(item, &tracked)\n> > +                             strvec_push(&transport_ls_refs_options.ref_prefixes,\n> > +                                         item->string);\n> > +                     string_list_clear(&tracked, 0);\n> > +             } else if (transport->remote->fetch.nr) {\n> >                       refspec_ref_prefixes(&transport->remote->fetch,\n> >                                            &transport_ls_refs_options.ref_prefixes);\n> >               }\n\nI'll rename it to 'infer_from_refmap'.\n\n> It would have been much easier to follow if the existing code came\n> first to make it clear that the new code is an add-on.  After all,\n> when transport->remote->fetch.nr is true, inferred_branches is never\n> true.\n>\n> > +             if ((transport->remote->fetch.nr || inferred_branches) &&\n> > +                 follow_remote_head != FOLLOW_REMOTE_NEVER)\n> > +                     do_set_head = 1;\n> > +             if (tracks_this_remote) {\n> >                       int i;\n> >                       for (i = 0; i < branch->merge_nr; i++) {\n> >                               strvec_push(&transport_ls_refs_options.ref_prefixes,\n>\n> How does tracks_this_remote and inferred_branches interact?  Doesn't\n> the old code that grabs necessary remote-tracking branches for the\n> current branch add the same branch from the remote?  Doesn't @{u}\n> for the current branch added twice on the list of branches to fetch?\n\nGood points.\n\n> > @@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport,\n> >\n> >       ref_map = get_ref_map(transport->remote, remote_refs, rs,\n> >                             tags, &autotags);\n> > +\n> >       if (!update_head_ok)\n> >               check_not_current_branch(ref_map);\n>\n> Useless patch noise.\n\nFixed.\n\n> > diff --git a/remote.c b/remote.c\n> > index 99a086ea5a..c26312beea 100644\n> > --- a/remote.c\n> > +++ b/remote.c\n> > @@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch,\n> >       return refname_match(branch->merge[i]->src, refname);\n> >  }\n> >\n> > +struct branches_tracking_remote_cb_data {\n> > +     struct remote *remote;\n> > +     struct string_list *tracked;\n> > +};\n> > +\n> > +static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n> > +{\n> > +     struct branches_tracking_remote_cb_data *data = cb_data;\n> > +     struct branch *branch;\n> > +\n> > +     branch = branch_get(ref->name);\n>\n> I know branch_get() is defined here and allows implicit use of\n> the_repository, but can't we pass \"struct repository *\" around in\n> cb_data so that we can use repo_branch_get() here?\n>\n> > +     if (!branch_has_merge_config(branch) ||\n> > +         strcmp(branch->remote_name, data->remote->name))\n> > +             return 0;\n> > +\n> > +     for (int i = 0; i < branch->merge_nr; i++)\n> > +             string_list_insert(data->tracked, branch->merge[i]->src);\n> > +\n> > +     return 0;\n> > +}\n>\n> This is more or less identical to the \"if current branch integrates\n> with branches from the remote, then fetch them\" code we saw earlier\n> in the builtin/fetch.c:do_fetch() above.  I notice that its return\n> value is meaningless, as it always returns 0.\n\nI'll look into unifying.\n\nThe 0 return value is there so it can be used with\n'refs_for_each_branch_ref' without stopping the iteration.\n\n>         static int add_if_tracking_remote(...)\n>         {\n>                 struct branches_tracking_remote_cb_data *data = cb_data;\n>\n>                 collect_upstream_from_remote(data->tracked, data->remote, ref->name);\n>         }\n>\n> This will mean we will have a very small preliminary patch to\n> introduce collect_upstream_from_remote() function in remote.c and\n> update the \"help current branch by fetching what are merged into it\"\n> code in do_fetch() to use it, which will have the above ontlined\n> if/else if/ cascade except for your new refmap code.  On top, this\n> step will insert a single \"else if\" block to add your new logic to\n> do_fetch().\n\nYes, good idea.\n\n> > +void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n> > +{\n> > +     struct branches_tracking_remote_cb_data data = { remote, tracked };\n> > +\n> > +     refs_for_each_branch_ref(get_main_ref_store(the_repository),\n> > +                               add_if_tracking_remote, &data);\n> > +}\n>\n> This also hardcodes the_repository, but shouldn't this function take\n> \"struct repository *\" pointer (and shove it in data structure to\n> pass it down)?\n\nGood point.\n\n\nHarald\n\n"},{"id":"554677","messageId":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v8 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:09Z","receivedAt":"2026-10-10T08:02:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.refmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  84 ++++++++++++++++++----\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  51 ++++++++++++-\n remote.h                         |  19 +++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  63 ++++++++++++++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 448 insertions(+), 23 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v8\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v8\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v7:\n\n 1:  b5db64d56f ! 1:  2a48c21bc8 fetch: add remote.<name>.refmap\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \tstruct ref *rm;\n       \tstruct ref *ref_map = NULL;\n       \tstruct ref **tail = &ref_map;\n     ++\t/*\n     ++\t * The --refmap command line option, if given, takes precedence\n     ++\t * over remote.<name>.refmap.\n     ++\t */\n      +\tstruct refspec *effective_refmap =\n      +\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n       \n -:  ---------- > 2:  08fea07a8b fetch: extract collect_upstream_from_remote() helper\n 2:  fd6864daaf ! 3:  a39316d233 fetch: infer branches to fetch from a refmap-only remote\n     @@ Documentation/fetch-options.adoc: endif::git-pull[]\n      -`remote.<name>.refmap` provides the default value for this option, the\n      -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n      +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n     -+there is nothing to fetch, neither on the command line nor from\n     -+`remote.<name>.fetch`, Git infers what to fetch from the local branches\n     -+whose `@{upstream}` is on that remote.\n     ++nothing to fetch is specified on the command line, nor is there a\n     ++`remote.<name>.fetch`, branches from the remote that are used as the\n     ++`@{upstream}` of our local branches are fetched.\n       \n       `-t`::\n       `--tags`::\n      \n       ## builtin/fetch.c ##\n      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     - \tstruct ref **tail = &ref_map;\n     + \t */\n       \tstruct refspec *effective_refmap =\n       \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n      +\tstruct refspec inferred_rs;\n     -+\tint inferred_branches = 0;\n     ++\tint infer_from_refmap = 0;\n       \n       \t/* opportunistically-updated references: */\n       \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n      +\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n      +\t\tstruct string_list_item *item;\n      +\n     -+\t\tbranches_tracking_remote(remote, &tracked);\n     ++\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n      +\t\tfor_each_string_list_item(item, &tracked)\n      +\t\t\trefspec_append(&inferred_rs, item->string);\n      +\t\tstring_list_clear(&tracked, 0);\n      +\n      +\t\trs = &inferred_rs;\n     -+\t\tinferred_branches = 1;\n     ++\t\tinfer_from_refmap = 1;\n      +\t}\n      +\n       \tif (rs->nr) {\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \t\tfor (i = 0; i < rs->nr; i++) {\n      -\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n      +\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n     -+\t\t\t\t      inferred_branches);\n     ++\t\t\t\t      infer_from_refmap);\n       \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n       \t\t\t\t*autotags = 1;\n       \t\t}\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \n       \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n       \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n     -+\t} else if (inferred_branches) {\n     ++\t} else if (infer_from_refmap) {\n      +\t\t/* Already fully handled above. */\n       \t} else if (refmap.nr) {\n       \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       }\n       \n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n     + \t\t\t\t    item->string);\n     + \t\tstring_list_clear(&tracked, 0);\n       \t} else {\n     - \t\tstruct branch *branch = branch_get(NULL);\n     --\n     --\t\tif (transport->remote->fetch.nr) {\n     -+\t\tint tracks_this_remote = branch && branch_has_merge_config(branch) &&\n     -+\t\t\t!strcmp(branch->remote_name, transport->remote->name);\n     ++\t\t/*\n     ++\t\t * The --refmap command line option, if given, takes\n     ++\t\t * precedence over remote.<name>.refmap.\n     ++\t\t */\n      +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n      +\t\t\t&transport->remote->refmap;\n     -+\t\tint inferred_branches = !transport->remote->fetch.nr &&\n     -+\t\t\teffective_refmap->nr;\n     -+\n     -+\t\tif (inferred_branches) {\n     -+\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\t\tstruct string_list_item *item;\n     -+\n     -+\t\t\tbranches_tracking_remote(transport->remote, &tracked);\n     -+\t\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -+\t\t\t\t\t    item->string);\n     -+\t\t\tstring_list_clear(&tracked, 0);\n     -+\t\t} else if (transport->remote->fetch.nr) {\n     - \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     - \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     --\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     --\t\t\t\tdo_set_head = 1;\n     - \t\t}\n     --\t\tif (branch && branch_has_merge_config(branch) &&\n     --\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n     -+\t\tif ((transport->remote->fetch.nr || inferred_branches) &&\n     -+\t\t    follow_remote_head != FOLLOW_REMOTE_NEVER)\n     -+\t\t\tdo_set_head = 1;\n     -+\t\tif (tracks_this_remote) {\n     - \t\t\tint i;\n     - \t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n     - \t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \n     - \tref_map = get_ref_map(transport->remote, remote_refs, rs,\n     - \t\t\t      tags, &autotags);\n     -+\n     - \tif (!update_head_ok)\n     - \t\tcheck_not_current_branch(ref_map);\n     + \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     + \t\tstruct string_list_item *item;\n       \n     +-\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     +-\t\t\t\t\t      transport->remote, NULL);\n     ++\t\tif (effective_refmap->nr) {\n     ++\t\t\tbranches_tracking_remote(the_repository,\n     ++\t\t\t\t\t\t  transport->remote, &tracked);\n     ++\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     ++\t\t\t\tdo_set_head = 1;\n     ++\t\t} else {\n     ++\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     ++\t\t\t\t\t\t      transport->remote, NULL);\n     ++\t\t}\n     + \t\tfor_each_string_list_item(item, &tracked)\n     + \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     + \t\t\t\t    item->string);\n      \n       ## remote.c ##\n     -@@ remote.c: int branch_merge_matches(struct branch *branch,\n     - \treturn refname_match(branch->merge[i]->src, refname);\n     +@@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n     + \t\tstring_list_insert(tracked, branch->merge[i]->src);\n       }\n       \n      +struct branches_tracking_remote_cb_data {\n     ++\tstruct repository *repo;\n      +\tstruct remote *remote;\n      +\tstruct string_list *tracked;\n      +};\n     @@ remote.c: int branch_merge_matches(struct branch *branch,\n      +static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n      +{\n      +\tstruct branches_tracking_remote_cb_data *data = cb_data;\n     -+\tstruct branch *branch;\n     -+\n     -+\tbranch = branch_get(ref->name);\n     -+\tif (!branch_has_merge_config(branch) ||\n     -+\t    strcmp(branch->remote_name, data->remote->name))\n     -+\t\treturn 0;\n     -+\n     -+\tfor (int i = 0; i < branch->merge_nr; i++)\n     -+\t\tstring_list_insert(data->tracked, branch->merge[i]->src);\n      +\n     ++\tcollect_upstream_from_remote(data->repo, data->tracked, data->remote,\n     ++\t\t\t\t      ref->name);\n      +\treturn 0;\n      +}\n      +\n     -+void branches_tracking_remote(struct remote *remote, struct string_list *tracked)\n     ++void branches_tracking_remote(struct repository *repo, struct remote *remote,\n     ++\t\t\t       struct string_list *tracked)\n      +{\n     -+\tstruct branches_tracking_remote_cb_data data = { remote, tracked };\n     ++\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n      +\n     -+\trefs_for_each_branch_ref(get_main_ref_store(the_repository),\n     ++\trefs_for_each_branch_ref(get_main_ref_store(repo),\n      +\t\t\t\t  add_if_tracking_remote, &data);\n      +}\n      +\n     @@ remote.c: int branch_merge_matches(struct branch *branch,\n       {\n      \n       ## remote.h ##\n     -@@ remote.h: int branch_has_merge_config(struct branch *branch);\n     - \n     - int branch_merge_matches(struct branch *, int n, const char *);\n     +@@ remote.h: void collect_upstream_from_remote(struct repository *repo,\n     + \t\t\t\t   struct remote *remote,\n     + \t\t\t\t   const char *refname);\n       \n      +/* fills tracked with the refname of every local branch's upstream on remote */\n     -+void branches_tracking_remote(struct remote *remote, struct string_list *tracked);\n     ++void branches_tracking_remote(struct repository *repo, struct remote *remote,\n     ++\t\t\t       struct string_list *tracked);\n      +\n       /* list of the remote in a group as configured */\n       struct remote_group_data {\n 3:  a2208875b6 = 4:  2b974bbe0a remote: add \"git remote add --limited-fetch\"\n 4:  c8fd073de3 = 5:  db27c29086 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n\n"},{"id":"554678","messageId":"2a48c21bc834367af595f4480596e100502623c9.1791619334.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","subject":"[PATCH v8 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:10Z","receivedAt":"2026-10-10T08:02:10Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nA refspec-less fetch with a refmap configured but nothing else to say\nwhat to fetch falls back to the same defaults as a remote with no\nrefspec or refmap at all. Only an explicit --refmap given on the\ncommand line with no command-line refspec to go with it is still\nrejected, since that combination is a plain mistake to type, unlike a\nremote.<name>.refmap configured on its own. A remote.<name>.fetch\nthat is also configured is used as before.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++\n Documentation/fetch-options.adoc |  3 ++\n builtin/fetch.c                  | 10 ++++-\n remote.c                         | 12 +++++-\n remote.h                         |  6 +++\n t/t5510-fetch.sh                 | 63 ++++++++++++++++++++++++++++++++\n 6 files changed, 96 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..c1c65c7528 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.refmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,8 +558,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \ndiff --git a/remote.c b/remote.c\nindex 71170f36a9..99a086ea5a 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..cfd09db155 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.refmap plays no part in deciding what to\n+\t\t# fetch, only in remapping something that is already being\n+\t\t# fetched by name.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"554679","messageId":"08fea07a8be8a91ac54db44f9c035ecb49c86c9f.1791619334.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","subject":"[PATCH v8 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:11Z","receivedAt":"2026-10-10T08:02:11Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we fall back to guessing\nwhat to fetch: the remote's configured fetch refspec if there is one,\nand separately, whether the current branch's upstream lives on this\nremote. The latter is independent of the former on purpose: a narrow\nrefspec (for example from \"git remote add -t\") can leave the current\nbranch's own upstream uncovered, and we still need to be able to fetch\nit.\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 43 ++++++++++++++++++++++++++++---------------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 52 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex c1c65c7528..4753656288 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1962,23 +1962,36 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n+\t} else if (transport->remote->fetch.nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t      transport->remote, NULL);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n \n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n-\t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t      transport->remote, NULL);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 99a086ea5a..5e980625b8 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..7c86c529b6 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"554680","messageId":"a39316d233ee656c6e003927e9e3e526ada0553e.1791619334.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","subject":"[PATCH v8 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:12Z","receivedAt":"2026-10-10T08:02:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  41 ++++++++++-\n remote.c                         |  24 +++++++\n remote.h                         |   4 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 194 insertions(+), 6 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..90b9e13ac2 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, branches from the remote that are used as the\n+`@{upstream}` of our local branches are fetched.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 4753656288..de3298c0b1 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -515,6 +515,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -522,15 +524,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -565,6 +584,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -660,6 +681,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1983,11 +2005,24 @@ static int do_fetch(struct transport *transport,\n \t\t\t\t    item->string);\n \t\tstring_list_clear(&tracked, 0);\n \t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.refmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n \t\tstruct string_list_item *item;\n \n-\t\tcollect_upstream_from_remote(the_repository, &tracked,\n-\t\t\t\t\t      transport->remote, NULL);\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository,\n+\t\t\t\t\t\t  transport->remote, &tracked);\n+\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\t\tdo_set_head = 1;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t\t      transport->remote, NULL);\n+\t\t}\n \t\tfor_each_string_list_item(item, &tracked)\n \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t\t    item->string);\ndiff --git a/remote.c b/remote.c\nindex 5e980625b8..703c71ba45 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,30 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstring_list_insert(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->tracked, data->remote,\n+\t\t\t\t      ref->name);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 7c86c529b6..62265346d3 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   struct remote *remote,\n \t\t\t\t   const char *refname);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..e287a6b947 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -730,6 +730,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"554681","messageId":"2b974bbe0a1ff1f70944a2db9d996c0c283bd3fa.1791619334.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","subject":"[PATCH v8 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:13Z","receivedAt":"2026-10-10T08:02:13Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"554682","messageId":"db27c290866c893591a3c69b460cf245c0dacf5d.1791619334.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com","subject":"[PATCH v8 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-10T08:02:14Z","receivedAt":"2026-10-10T08:02:14Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"554694","messageId":"xmqqik39iipg.fsf@gitster.g","threadId":"66353","inReplyTo":"08fea07a8be8a91ac54db44f9c035ecb49c86c9f.1791619334.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v8 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-10T17:11:39Z","receivedAt":"2026-10-10T17:11:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> @@ -1962,23 +1962,36 @@ static int do_fetch(struct transport *transport,\n>  \n>  \tif (rs->nr) {\n>  \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n>  \t} else {\n> -\t\tstruct branch *branch = branch_get(NULL);\n>  \n> -\t\tif (transport->remote->fetch.nr) {\n> -\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n> -\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n> -\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n> -\t\t\t\tdo_set_head = 1;\n> -\t\t}\n> -\t\tif (branch && branch_has_merge_config(branch) &&\n> -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n> -\t\t\tint i;\n> -\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n> -\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> -\t\t\t\t\t    branch->merge[i]->src);\n> -\t\t\t}\n> -\t\t}\n>  \n>  \t\t/*\n>  \t\t * If there are no refs specified to fetch, then we just\n\nWe used to say \"if there is remote.*.fetch, add them.  whether there\nis remote.*.fetch or not, if the current branch builds on their\nbranch(es), add them too\".\n\n> @@ -1962,23 +1962,36 @@ static int do_fetch(struct transport *transport,\n>  \n>  \tif (rs->nr) {\n>  \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n> +\t} else if (transport->remote->fetch.nr) {\n> +\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n> +\t\tstruct string_list_item *item;\n> +\n> +\t\trefspec_ref_prefixes(&transport->remote->fetch,\n> +\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n> +\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n> +\t\t\tdo_set_head = 1;\n> +\n> +\t\t/*\n> +\t\t * The configured refspec may not cover the current\n> +\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n> +\t\t * make sure we can still fetch it regardless.\n> +\t\t */\n> +\t\tcollect_upstream_from_remote(the_repository, &tracked,\n> +\t\t\t\t\t      transport->remote, NULL);\n> +\t\tfor_each_string_list_item(item, &tracked)\n> +\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> +\t\t\t\t    item->string);\n> +\t\tstring_list_clear(&tracked, 0);\n>  \t} else {\n> +\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n> +\t\tstruct string_list_item *item;\n>  \n> +\t\tcollect_upstream_from_remote(the_repository, &tracked,\n> +\t\t\t\t\t      transport->remote, NULL);\n> +\t\tfor_each_string_list_item(item, &tracked)\n> +\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n> +\t\t\t\t    item->string);\n> +\t\tstring_list_clear(&tracked, 0);\n\nThis reorganizes it to say \"if there is remote.*.fetch, do the same\nas before.  If there is not remote.*.fetch, add their branch(es) the\ncurrent branch wants\".\n\nThis \"duplicate what happens to the current branch\" belongs to the\nnext step where you want to add more logic to the latter part (i.e.,\n\"no remote.*.fetch\" case), and not to this step.\n\nIOW I would have expected to see\n\n\t} else {\n\t\tif (transport->remote->fetch.nr) {\n\t\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n\t\t\tstruct string_list_item *item;\n\n\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n\t\t\t\tdo_set_head = 1;\n\t\t}\n\n\t\t/*\n\t\t * The configured refspec may not cover the current\n\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n\t\t * make sure we can still fetch it regardless.\n\t\t */\n\t\tcollect_upstream_from_remote(the_repository, &tracked,\n\t\t\t\t\t      transport->remote, NULL);\n\t\tfor_each_string_list_item(item, &tracked)\n\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n\t\t\t\t    item->string);\n\t\tstring_list_clear(&tracked, 0);\n\t}\n\nthat touches \"help the current branch when rs->nr == 0\" part and\nnothing else.\n\nOf course, because the next step [3/5] wants to split the above\nelse{} block to do different things between the case where\nremote.*.fetch is and is not empty, at that point the extra\ncode may be introduced there, ending up with the shape of if/else if\ncascade as we see in your patch.\n\n> diff --git a/remote.c b/remote.c\n> index 99a086ea5a..5e980625b8 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -1884,6 +1884,21 @@ int branch_merge_matches(struct branch *branch,\n>  \treturn refname_match(branch->merge[i]->src, refname);\n>  }\n>  \n> +void collect_upstream_from_remote(struct repository *repo,\n> +\t\t\t\t   struct string_list *tracked,\n> +\t\t\t\t   struct remote *remote,\n> +\t\t\t\t   const char *refname)\n> +{\n> +\tstruct branch *branch = repo_branch_get(repo, refname);\n> +\n> +\tif (!branch_has_merge_config(branch) ||\n> +\t    strcmp(branch->remote_name, remote->name))\n> +\t\treturn;\n> +\tfor (int i = 0; i < branch->merge_nr; i++)\n> +\t\tstring_list_insert(tracked, branch->merge[i]->src);\n> +}\n\nThe original tries to avoid branch == NULL causing a segfault, but\nthe above does not.  Intended or overlooked?\n\nOther than that, this looks like a straight-forward refactoring.\n\n"},{"id":"554695","messageId":"xmqqecdxiihc.fsf@gitster.g","threadId":"66353","inReplyTo":"2b974bbe0a1ff1f70944a2db9d996c0c283bd3fa.1791619334.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v8 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-10T17:16:31Z","receivedAt":"2026-10-10T17:16:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A remote added the ordinary way tracks every branch it has, via a\n> wildcard remote.<name>.fetch refspec. That is wasteful for a remote\n> whose history is only worth following for the branches actually in\n> use locally, and it can make \"git fetch\" negotiate history for\n> branches nobody asked for.\n>\n> Give \"git remote add\" a --limited-fetch option that sets up\n> remote.<name>.refmap instead of remote.<name>.fetch, so that\n> \"git fetch <name>\" only fetches the branches already tracked, as\n> described in the previous commit. It is rejected together with\n> -t/--track or --mirror, since those already say explicitly what to\n> fetch.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  Documentation/git-remote.adoc |  8 +++++++-\n>  builtin/remote.c              | 28 ++++++++++++++++++++++------\n>  t/t5505-remote.sh             | 16 ++++++++++++++++\n>  3 files changed, 45 insertions(+), 7 deletions(-)\n\nThis step looks quite straight-forward and nicely done.  I didn't\nlook at the tests, though.\n\n"},{"id":"554698","messageId":"CAHwyqnW2FqKBUK=s9ZL_kwwZDf2WE87qT9YN-syLs2O5FXpMtA@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqik39iipg.fsf@gitster.g","subject":"Re: [PATCH v8 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-10T18:36:26Z","receivedAt":"2026-10-10T18:36:26Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> IOW I would have expected to see\n>\n>         } else {\n>                 if (transport->remote->fetch.nr) {\n>                         struct string_list tracked = STRING_LIST_INIT_DUP;\n>                         struct string_list_item *item;\n>\n>                         refspec_ref_prefixes(&transport->remote->fetch,\n>                                              &transport_ls_refs_options.ref_prefixes);\n>                         if (follow_remote_head != FOLLOW_REMOTE_NEVER)\n>                                 do_set_head = 1;\n>                 }\n>\n>                 /*\n>                  * The configured refspec may not cover the current\n>                  * branch's upstream (e.g. a narrowed -t refspec), so\n>                  * make sure we can still fetch it regardless.\n>                  */\n>                 collect_upstream_from_remote(the_repository, &tracked,\n>                                               transport->remote, NULL);\n>                 for_each_string_list_item(item, &tracked)\n>                         strvec_push(&transport_ls_refs_options.ref_prefixes,\n>                                     item->string);\n>                 string_list_clear(&tracked, 0);\n>         }\n>\n> that touches \"help the current branch when rs->nr == 0\" part and\n> nothing else.\n>\n> Of course, because the next step [3/5] wants to split the above\n> else{} block to do different things between the case where\n> remote.*.fetch is and is not empty, at that point the extra\n> code may be introduced there, ending up with the shape of if/else if\n> cascade as we see in your patch.\n\nThis is a good point about how commits tell their story, and I'll look into it.\n\nThanks for the snippet!\n\n> > diff --git a/remote.c b/remote.c\n> > index 99a086ea5a..5e980625b8 100644\n> > --- a/remote.c\n> > +++ b/remote.c\n> > @@ -1884,6 +1884,21 @@ int branch_merge_matches(struct branch *branch,\n> >       return refname_match(branch->merge[i]->src, refname);\n> >  }\n> >\n> > +void collect_upstream_from_remote(struct repository *repo,\n> > +                                struct string_list *tracked,\n> > +                                struct remote *remote,\n> > +                                const char *refname)\n> > +{\n> > +     struct branch *branch = repo_branch_get(repo, refname);\n> > +\n> > +     if (!branch_has_merge_config(branch) ||\n> > +         strcmp(branch->remote_name, remote->name))\n> > +             return;\n> > +     for (int i = 0; i < branch->merge_nr; i++)\n> > +             string_list_insert(tracked, branch->merge[i]->src);\n> > +}\n>\n> The original tries to avoid branch == NULL causing a segfault, but\n> the above does not.  Intended or overlooked?\n\nOverlooked, but I don't think the segfault would actually happen (in a\nway that could be reproduced by a test)?\n\n\nHarald\n\n"},{"id":"554955","messageId":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v9 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:07Z","receivedAt":"2026-10-14T07:04:07Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.refmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  84 ++++++++++++++++++----\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  51 ++++++++++++-\n remote.h                         |  19 +++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  63 ++++++++++++++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 448 insertions(+), 23 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v9\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v9\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v8:\n\n 1:  2a48c21bc8 = 1:  2a48c21bc8 fetch: add remote.<name>.refmap\n 2:  08fea07a8b ! 2:  50382bce7e fetch: extract collect_upstream_from_remote() helper\n     @@ Commit message\n      \n       ## builtin/fetch.c ##\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \n       \tif (rs->nr) {\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n     -+\t} else if (transport->remote->fetch.nr) {\n     -+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\tstruct string_list_item *item;\n     -+\n     -+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     -+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     -+\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     -+\t\t\tdo_set_head = 1;\n     -+\n     -+\t\t/*\n     -+\t\t * The configured refspec may not cover the current\n     -+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     -+\t\t * make sure we can still fetch it regardless.\n     -+\t\t */\n     -+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     -+\t\t\t\t\t      transport->remote, NULL);\n     -+\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -+\t\t\t\t    item->string);\n     -+\t\tstring_list_clear(&tracked, 0);\n       \t} else {\n      -\t\tstruct branch *branch = branch_get(NULL);\n      +\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n      +\t\tstruct string_list_item *item;\n       \n     --\t\tif (transport->remote->fetch.nr) {\n     --\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     --\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     --\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     --\t\t\t\tdo_set_head = 1;\n     --\t\t}\n     + \t\tif (transport->remote->fetch.nr) {\n     + \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     + \t\t\t\tdo_set_head = 1;\n     + \t\t}\n      -\t\tif (branch && branch_has_merge_config(branch) &&\n      -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n      -\t\t\tint i;\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      -\t\t\t\t\t    branch->merge[i]->src);\n      -\t\t\t}\n      -\t\t}\n     ++\n     ++\t\t/*\n     ++\t\t * The configured refspec may not cover the current\n     ++\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     ++\t\t * make sure we can still fetch it regardless.\n     ++\t\t */\n      +\t\tcollect_upstream_from_remote(the_repository, &tracked,\n      +\t\t\t\t\t      transport->remote, NULL);\n      +\t\tfor_each_string_list_item(item, &tracked)\n 3:  a39316d233 ! 3:  9ad8ad3eb4 fetch: infer branches to fetch from a refmap-only remote\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       }\n       \n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \n     + \tif (rs->nr) {\n     + \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n     +-\t} else {\n     ++\t} else if (transport->remote->fetch.nr) {\n     + \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     + \t\tstruct string_list_item *item;\n     + \n     +-\t\tif (transport->remote->fetch.nr) {\n     +-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     +-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     +-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     +-\t\t\t\tdo_set_head = 1;\n     +-\t\t}\n     ++\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     ++\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     ++\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     ++\t\t\tdo_set_head = 1;\n     + \n     + \t\t/*\n     + \t\t * The configured refspec may not cover the current\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n       \t\t\t\t    item->string);\n       \t\tstring_list_clear(&tracked, 0);\n     - \t} else {\n     ++\t} else {\n      +\t\t/*\n      +\t\t * The --refmap command line option, if given, takes\n      +\t\t * precedence over remote.<name>.refmap.\n      +\t\t */\n      +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n      +\t\t\t&transport->remote->refmap;\n     - \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     - \t\tstruct string_list_item *item;\n     - \n     --\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     --\t\t\t\t\t      transport->remote, NULL);\n     ++\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     ++\t\tstruct string_list_item *item;\n     ++\n      +\t\tif (effective_refmap->nr) {\n      +\t\t\tbranches_tracking_remote(the_repository,\n      +\t\t\t\t\t\t  transport->remote, &tracked);\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      +\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n      +\t\t\t\t\t\t      transport->remote, NULL);\n      +\t\t}\n     - \t\tfor_each_string_list_item(item, &tracked)\n     - \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     - \t\t\t\t    item->string);\n     ++\t\tfor_each_string_list_item(item, &tracked)\n     ++\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     ++\t\t\t\t    item->string);\n     ++\t\tstring_list_clear(&tracked, 0);\n     + \n     + \t\t/*\n     + \t\t * If there are no refs specified to fetch, then we just\n      \n       ## remote.c ##\n      @@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n 4:  2b974bbe0a = 4:  75169f668e remote: add \"git remote add --limited-fetch\"\n 5:  db27c29086 = 5:  9ee090d3d0 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n\n"},{"id":"554956","messageId":"2a48c21bc834367af595f4480596e100502623c9.1791961452.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","subject":"[PATCH v9 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:08Z","receivedAt":"2026-10-14T07:04:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nA refspec-less fetch with a refmap configured but nothing else to say\nwhat to fetch falls back to the same defaults as a remote with no\nrefspec or refmap at all. Only an explicit --refmap given on the\ncommand line with no command-line refspec to go with it is still\nrejected, since that combination is a plain mistake to type, unlike a\nremote.<name>.refmap configured on its own. A remote.<name>.fetch\nthat is also configured is used as before.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++\n Documentation/fetch-options.adoc |  3 ++\n builtin/fetch.c                  | 10 ++++-\n remote.c                         | 12 +++++-\n remote.h                         |  6 +++\n t/t5510-fetch.sh                 | 63 ++++++++++++++++++++++++++++++++\n 6 files changed, 96 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..c1c65c7528 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -509,6 +509,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.refmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -552,8 +558,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \ndiff --git a/remote.c b/remote.c\nindex 71170f36a9..99a086ea5a 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -988,7 +996,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..cfd09db155 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.refmap plays no part in deciding what to\n+\t\t# fetch, only in remapping something that is already being\n+\t\t# fetched by name.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"554957","messageId":"50382bce7e13f318aaac4ab9c1b2a0f79ca3be68.1791961452.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","subject":"[PATCH v9 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:09Z","receivedAt":"2026-10-14T07:04:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we fall back to guessing\nwhat to fetch: the remote's configured fetch refspec if there is one,\nand separately, whether the current branch's upstream lives on this\nremote. The latter is independent of the former on purpose: a narrow\nrefspec (for example from \"git remote add -t\") can leave the current\nbranch's own upstream uncovered, and we still need to be able to fetch\nit.\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 23 ++++++++++++++---------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 38 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex c1c65c7528..2eb58195a2 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1963,7 +1963,8 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n \n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n@@ -1971,14 +1972,18 @@ static int do_fetch(struct transport *transport,\n \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n \t\t\t\tdo_set_head = 1;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t      transport->remote, NULL);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 99a086ea5a..5e980625b8 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1884,6 +1884,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..7c86c529b6 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"554958","messageId":"9ad8ad3eb40786c1340b53c7c74c6b61f9fcf210.1791961452.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","subject":"[PATCH v9 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:10Z","receivedAt":"2026-10-14T07:04:10Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  59 ++++++++++++---\n remote.c                         |  24 +++++++\n remote.h                         |   4 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 207 insertions(+), 11 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..90b9e13ac2 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, branches from the remote that are used as the\n+`@{upstream}` of our local branches are fetched.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 2eb58195a2..de3298c0b1 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -515,6 +515,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -522,15 +524,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -565,6 +584,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -660,6 +681,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1962,16 +1984,14 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n-\t} else {\n+\t} else if (transport->remote->fetch.nr) {\n \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n \t\tstruct string_list_item *item;\n \n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n-\t\t\t\tdo_set_head = 1;\n-\t\t}\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\tdo_set_head = 1;\n \n \t\t/*\n \t\t * The configured refspec may not cover the current\n@@ -1984,6 +2004,29 @@ static int do_fetch(struct transport *transport,\n \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t\t    item->string);\n \t\tstring_list_clear(&tracked, 0);\n+\t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.refmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository,\n+\t\t\t\t\t\t  transport->remote, &tracked);\n+\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n+\t\t\t\tdo_set_head = 1;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t\t      transport->remote, NULL);\n+\t\t}\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 5e980625b8..703c71ba45 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,30 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstring_list_insert(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->tracked, data->remote,\n+\t\t\t\t      ref->name);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 7c86c529b6..62265346d3 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   struct remote *remote,\n \t\t\t\t   const char *refname);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..e287a6b947 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -730,6 +730,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"554959","messageId":"75169f668e4e528c337f4f5a8e476f4076199afd.1791961452.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","subject":"[PATCH v9 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:11Z","receivedAt":"2026-10-14T07:04:11Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex eaae30aa88..4255f8b3e6 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -70,6 +70,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex de989ea3ba..f036dd5d6f 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"554960","messageId":"9ee090d3d0e2b64730864ae49896d1f72eca310e.1791961452.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com","subject":"[PATCH v9 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-14T07:04:12Z","receivedAt":"2026-10-14T07:04:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 4255f8b3e6..80f8a4a183 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -52,6 +52,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -74,7 +79,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f036dd5d6f..0cf1126976 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"555541","messageId":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:53Z","receivedAt":"2026-10-20T13:42:53Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v10:\n\n * Resolve merge conflict after master integrated\n   ch/fetch-followremotehead-lazy-validation.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.refmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |   5 ++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  92 ++++++++++++++++++++----\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  51 ++++++++++++-\n remote.h                         |  19 +++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 |  63 ++++++++++++++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 456 insertions(+), 23 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v10\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v10\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v9:\n\n 1:  2a48c21bc8 = 1:  ba7f1354cd fetch: add remote.<name>.refmap\n 2:  50382bce7e ! 2:  aa20604144 fetch: extract collect_upstream_from_remote() helper\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\tif (transport->remote->fetch.nr) {\n       \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     - \t\t\t\tdo_set_head = 1;\n     + \t\t\telse\n     + \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n       \t\t}\n      -\t\tif (branch && branch_has_merge_config(branch) &&\n      -\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n 3:  9ad8ad3eb4 ! 3:  9fb7e77a86 fetch: infer branches to fetch from a refmap-only remote\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \n       \tif (rs->nr) {\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n     --\t} else {\n      +\t} else if (transport->remote->fetch.nr) {\n     - \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     - \t\tstruct string_list_item *item;\n     - \n     --\t\tif (transport->remote->fetch.nr) {\n     --\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     --\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     --\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     --\t\t\t\tdo_set_head = 1;\n     --\t\t}\n     ++\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     ++\t\tstruct string_list_item *item;\n     ++\n      +\t\trefspec_ref_prefixes(&transport->remote->fetch,\n      +\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     -+\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     -+\t\t\tdo_set_head = 1;\n     - \n     - \t\t/*\n     - \t\t * The configured refspec may not cover the current\n     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     - \t\t\t\t    item->string);\n     - \t\tstring_list_clear(&tracked, 0);\n     -+\t} else {\n     ++\t\t/*\n     ++\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n     ++\t\t * for the analogous, still-unresolved case.\n     ++\t\t */\n     ++\t\tif (transport->remote->follow_remote_head)\n     ++\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n     ++\t\telse if (config->follow_remote_head_seen)\n     ++\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n     ++\t\telse\n     ++\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n     ++\n     ++\t\t/*\n     ++\t\t * The configured refspec may not cover the current\n     ++\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     ++\t\t * make sure we can still fetch it regardless.\n     ++\t\t */\n     ++\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     ++\t\t\t\t\t      transport->remote, NULL);\n     ++\t\tfor_each_string_list_item(item, &tracked)\n     ++\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     ++\t\t\t\t    item->string);\n     ++\t\tstring_list_clear(&tracked, 0);\n     + \t} else {\n      +\t\t/*\n      +\t\t * The --refmap command line option, if given, takes\n      +\t\t * precedence over remote.<name>.refmap.\n      +\t\t */\n      +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n      +\t\t\t&transport->remote->refmap;\n     -+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\tstruct string_list_item *item;\n     -+\n     + \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     + \t\tstruct string_list_item *item;\n     + \n     +-\t\tif (transport->remote->fetch.nr) {\n     +-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     +-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n      +\t\tif (effective_refmap->nr) {\n      +\t\t\tbranches_tracking_remote(the_repository,\n      +\t\t\t\t\t\t  transport->remote, &tracked);\n     -+\t\t\tif (follow_remote_head != FOLLOW_REMOTE_NEVER)\n     -+\t\t\t\tdo_set_head = 1;\n     + \t\t\t/*\n     + \t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n     + \t\t\t * for the analogous, still-unresolved case.\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n     + \t\t\telse\n     + \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n      +\t\t} else {\n      +\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n      +\t\t\t\t\t\t      transport->remote, NULL);\n     -+\t\t}\n     -+\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -+\t\t\t\t    item->string);\n     -+\t\tstring_list_clear(&tracked, 0);\n     - \n     - \t\t/*\n     - \t\t * If there are no refs specified to fetch, then we just\n     + \t\t}\n     +-\n     +-\t\t/*\n     +-\t\t * The configured refspec may not cover the current\n     +-\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     +-\t\t * make sure we can still fetch it regardless.\n     +-\t\t */\n     +-\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     +-\t\t\t\t\t      transport->remote, NULL);\n     + \t\tfor_each_string_list_item(item, &tracked)\n     + \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     + \t\t\t\t    item->string);\n      \n       ## remote.c ##\n      @@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n 4:  75169f668e = 4:  80028d4f86 remote: add \"git remote add --limited-fetch\"\n 5:  9ee090d3d0 = 5:  552bf08971 remote: default to --limited-fetch in a shallow repository\n\n-- \ngitgitgadget\n\n"},{"id":"555542","messageId":"ba7f1354cd5492ef964ad4d1d398b5ecfc4e0086.1792503778.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"[PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:54Z","receivedAt":"2026-10-20T13:42:54Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nA refspec-less fetch with a refmap configured but nothing else to say\nwhat to fetch falls back to the same defaults as a remote with no\nrefspec or refmap at all. Only an explicit --refmap given on the\ncommand line with no command-line refspec to go with it is still\nrejected, since that combination is a plain mistake to type, unlike a\nremote.<name>.refmap configured on its own. A remote.<name>.fetch\nthat is also configured is used as before.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |  5 +++\n Documentation/fetch-options.adoc |  3 ++\n builtin/fetch.c                  | 10 ++++-\n remote.c                         | 12 +++++-\n remote.h                         |  6 +++\n t/t5510-fetch.sh                 | 63 ++++++++++++++++++++++++++++++++\n 6 files changed, 96 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 1f76ae6242..ffcb5ba29a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.refmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -561,8 +567,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \ndiff --git a/remote.c b/remote.c\nindex 49ee2ea318..c9252a90f4 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -995,7 +1003,9 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tif (remote->fetch.nr)\n+\t\treturn refspec_find_match(&remote->fetch, refspec);\n+\treturn refspec_find_match(&remote->refmap, refspec);\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 17c0be499c..ec5cd9dca6 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.refmap plays no part in deciding what to\n+\t\t# fetch, only in remapping something that is already being\n+\t\t# fetched by name.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"555543","messageId":"aa20604144efdcc5ada00d65d538fdb42c2e31d3.1792503778.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"[PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:55Z","receivedAt":"2026-10-20T13:42:55Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we fall back to guessing\nwhat to fetch: the remote's configured fetch refspec if there is one,\nand separately, whether the current branch's upstream lives on this\nremote. The latter is independent of the former on purpose: a narrow\nrefspec (for example from \"git remote add -t\") can leave the current\nbranch's own upstream uncovered, and we still need to be able to fetch\nit.\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 23 ++++++++++++++---------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 38 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex ffcb5ba29a..136a59154a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1979,7 +1979,8 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n \n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n@@ -1995,14 +1996,18 @@ static int do_fetch(struct transport *transport,\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t      transport->remote, NULL);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex c9252a90f4..965e42ff52 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1891,6 +1891,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstring_list_insert(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..7c86c529b6 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   struct string_list *tracked,\n+\t\t\t\t   struct remote *remote,\n+\t\t\t\t   const char *refname);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"555544","messageId":"9fb7e77a862640007339df639d03e61db37b6f70.1792503778.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"[PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:56Z","receivedAt":"2026-10-20T13:42:56Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nConfiguring remote.<name>.refmap without remote.<name>.fetch used to\nmake a refspec-less \"git fetch <name>\" fail with \"--refmap option is\nonly meaningful with command-line refspec(s)\", since a refmap only\nsays where to put fetched refs, not what to fetch.\n\nMake that case infer what to fetch: the local branches whose\n@{upstream} is already on that remote. This lets a remote be\nconfigured to fetch only the branches actually in use, without\nlisting them by hand in remote.<name>.fetch, and without needing to\ntouch the command line every time.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |   6 +-\n builtin/fetch.c                  |  75 +++++++++++++++----\n remote.c                         |  24 +++++++\n remote.h                         |   4 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 219 insertions(+), 15 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..7c52e443a5 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of fetching every branch it has. See the\n+\t`--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..90b9e13ac2 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,10 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, branches from the remote that are used as the\n+`@{upstream}` of our local branches are fetched.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 136a59154a..0fc1fb66d5 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -531,15 +533,32 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\trefspec_append(&inferred_rs, item->string);\n+\t\tstring_list_clear(&tracked, 0);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -574,6 +593,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -669,6 +690,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1978,13 +2000,47 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n+\t} else if (transport->remote->fetch.nr) {\n+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\t/*\n+\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n+\t\t * for the analogous, still-unresolved case.\n+\t\t */\n+\t\tif (transport->remote->follow_remote_head)\n+\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n+\t\telse if (config->follow_remote_head_seen)\n+\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n+\t\telse\n+\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t      transport->remote, NULL);\n+\t\tfor_each_string_list_item(item, &tracked)\n+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n+\t\t\t\t    item->string);\n+\t\tstring_list_clear(&tracked, 0);\n \t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.refmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n \t\tstruct string_list_item *item;\n \n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository,\n+\t\t\t\t\t\t  transport->remote, &tracked);\n \t\t\t/*\n \t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n \t\t\t * for the analogous, still-unresolved case.\n@@ -1995,15 +2051,10 @@ static int do_fetch(struct transport *transport,\n \t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n+\t\t\t\t\t\t      transport->remote, NULL);\n \t\t}\n-\n-\t\t/*\n-\t\t * The configured refspec may not cover the current\n-\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n-\t\t * make sure we can still fetch it regardless.\n-\t\t */\n-\t\tcollect_upstream_from_remote(the_repository, &tracked,\n-\t\t\t\t\t      transport->remote, NULL);\n \t\tfor_each_string_list_item(item, &tracked)\n \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t\t    item->string);\ndiff --git a/remote.c b/remote.c\nindex 965e42ff52..6467496a0f 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1906,6 +1906,30 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstring_list_insert(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tstruct remote *remote;\n+\tstruct string_list *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->tracked, data->remote,\n+\t\t\t\t      ref->name);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 7c86c529b6..62265346d3 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   struct remote *remote,\n \t\t\t\t   const char *refname);\n \n+/* fills tracked with the refname of every local branch's upstream on remote */\n+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n+\t\t\t       struct string_list *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 11b3752f00..0953a4112f 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -736,6 +736,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"555545","messageId":"80028d4f8638f766a73b84a5631d21101a413490.1792503778.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"[PATCH v10 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:57Z","receivedAt":"2026-10-20T13:42:57Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 315100409d..f8764394e7 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex fb7e5b114f..a7c13946d1 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"555546","messageId":"552bf08971be5204311c96bf232b5da49e852db1.1792503778.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"[PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T13:42:58Z","receivedAt":"2026-10-20T13:42:58Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow, single-branch clone used to\nstill fetch every branch that remote has, since \"git remote add\"\nalways set up a wildcard remote.<name>.fetch refspec regardless of\nhow shallow the repository already was. That defeats the purpose of\nhaving cloned shallow and single-branch in the first place, and can\nmake a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex f8764394e7..f43d707c91 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex a7c13946d1..8c4497cccd 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"556128","messageId":"xmqqcxt02eoa.fsf@gitster.g","threadId":"66353","inReplyTo":"pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-23T23:13:57Z","receivedAt":"2026-10-23T23:13:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Avoid fetching every branch of a new remote in a shallow repo.\n>\n> Changes in v10:\n>\n>  * Resolve merge conflict after master integrated\n>    ch/fetch-followremotehead-lazy-validation.\n\n> Harald Nordgren (5):\n>   fetch: add remote.<name>.refmap\n>   fetch: extract collect_upstream_from_remote() helper\n>   fetch: infer branches to fetch from a refmap-only remote\n>   remote: add \"git remote add --limited-fetch\"\n>   remote: default to --limited-fetch in a shallow repository\n>\n>  Documentation/config/remote.adoc |   8 +++\n>  Documentation/fetch-options.adoc |   5 ++\n>  Documentation/git-remote.adoc    |  14 +++-\n>  builtin/fetch.c                  |  92 ++++++++++++++++++++----\n>  builtin/remote.c                 |  31 ++++++--\n>  remote.c                         |  51 ++++++++++++-\n>  remote.h                         |  19 +++++\n>  t/meson.build                    |   1 +\n>  t/t5505-remote.sh                |  76 ++++++++++++++++++++\n>  t/t5510-fetch.sh                 |  63 ++++++++++++++++\n>  t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n>  11 files changed, 456 insertions(+), 23 deletions(-)\n>  create mode 100755 t/t5586-fetch-refmap.sh\n\nThis topic seeems fairly well done, without seeing any additional\ncomments in the last two rounds, but I am highly disturbed that I\nwas practically the only person who reviewd its later iterations.\nBecause I am not a target audience of the feature, I would not be\nsurprised if I am missing something large.\n\nI would very much appreciate if somebody else can give the series\none more read and Ack them, so that we can move it forward.\n\nThanks.\n\n"},{"id":"556161","messageId":"CALnO6CAhUNRMf=KZmY6YbC-5g3h8DE6U40HehGwbcRTPhDasrg@mail.gmail.com","threadId":"66353","inReplyTo":"ba7f1354cd5492ef964ad4d1d398b5ecfc4e0086.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:08:15Z","receivedAt":"2026-10-24T15:08:15Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Add a per-remote config variable, remote.<name>.refmap, that provides\n> the default value for --refmap the same way remote.<name>.fetch\n> already provides the default refspecs to fetch. Like --refmap itself,\n> it only maps refs that are actually being fetched, so it has nothing\n> to do when there is nothing explicit to fetch, on the command line or\n> via remote.<name>.fetch.\n>\n> A refspec-less fetch with a refmap configured but nothing else to say\n> what to fetch falls back to the same defaults as a remote with no\n> refspec or refmap at all. Only an explicit --refmap given on the\n> command line with no command-line refspec to go with it is still\n> rejected, since that combination is a plain mistake to type, unlike a\n> remote.<name>.refmap configured on its own. A remote.<name>.fetch\n> that is also configured is used as before.\n\nA bit wordy in the 2nd paragraph (and LLM-tasting), but… :shrug:\n\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n\n>  int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n>  {\n> -       return refspec_find_match(&remote->fetch, refspec);\n> +       if (remote->fetch.nr)\n> +               return refspec_find_match(&remote->fetch, refspec);\n> +       return refspec_find_match(&remote->refmap, refspec);\n>  }\n\nI think this appeared in v2 a bit open-coded, then was refactored in\nv3, but I haven't seen any comment on it? I might just be missing\nsome.\n\nAnyway, let me think aloud about this. At first glance, we can't\nfallback to refmap if fetch *fails* to find refspec. Should we or not?\nI'm not sure.\n\nremote.h says this procedure does \"For the given remote, reads the\nrefspec's src and sets the other fields.\" Ok, and glancing at\nrefspec_find_match the idea is that \"refspec\" input is a query (with a\nsrc or dst) and we find the matching entry and fill out the input's\nfields.\n\nSo… I assume the usual idea would be that refmap is a \"superset\" of\nthe fetch refspec, since I might want to map refs that I don't\nnormally fetch. (Or it might be disjoint, if none of the mappings\napply to my usual fetches.) But that means that remote_find_tracking()\nwon't be able to find entries for the mapped refs that aren't part of\nmy \"normal\" fetches? Are there downstream consequences of that? I'm\nnot sure, I haven't looked at how else remote_find_tracking is used.\n\nCould that affect, for example, --delete-merged?\n\n-- \nD. Ben Knoble\n\n"},{"id":"556162","messageId":"CALnO6CAgS3=ne1RkT-DioLmY6SqhnOZ8NH5kxxCZA_1QFx-dBg@mail.gmail.com","threadId":"66353","inReplyTo":"aa20604144efdcc5ada00d65d538fdb42c2e31d3.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:08:45Z","receivedAt":"2026-10-24T15:08:45Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> When a fetch has no refspec to work from, we fall back to guessing\n> what to fetch: the remote's configured fetch refspec if there is one,\n> and separately, whether the current branch's upstream lives on this\n> remote.\n\nIt took me a couple tries to parse this because the 2 halves (\"[one],\nand separately, [two]\") are 2 different kinds of objects! I think what\nyou are trying to tell me is\n\n    …we fall back to guessing what to fetch: the remote's …\nseparately, the current branch's upstream if it lives on the fetched\nremote.\n\nThat way [two] is also a \"thing to fetch\" in the same class of objects\nas [one]? Or have I misunderstood?\n\n> The latter is independent of the former on purpose: a narrow\n> refspec (for example from \"git remote add -t\") can leave the current\n> branch's own upstream uncovered, and we still need to be able to fetch\n> it.\n\nI don't really understand the meaning here. I can guess that you mean\nsomething like \"A remote with a narrow refspec, like what you get from\n'git remote add -t', can omit a branch's upstream from the remote's\nfetch refspec, and we still want to fetch that upstream.\" But it's\ntaken me a while to put that together (after puzzling out the above)\nafter first not knowing what \"uncovered\" meant.\n\nI think the whole thing would be clearer with an example:\n\n\"\"\"\nImagine a remote with a narrow refspec:\n\n    git remote add -t main colleague <remote>\n\nand a branch whose upstream points at that remote, but isn't part of\nthe remote's refspec:\n\n    git fetch colleague work:refs/remotes/colleague/work\n    git switch -c work colleague/work\n\nWhen we \"git fetch colleague\" without a refspec, we decide what to\nfetch by combining the remote's refspec (main -> remote-tracking\nbranch, in this case) and the current branch's upstream, if it was\nbased on that remote (work -> remote-tracking branch, in this case).\n\"\"\"\n\nNote: we aren't \"guessing\" what to fetch!\n\n> That \"does this branch build on this remote\" check was inlined for\n> just the current branch. Move it into its own function,\n> collect_upstream_from_remote(), so the same logic can be reused for\n> branches other than just the current one.\n\nAnd then this maybe comes next. Personally I appreciate the extra\nexplanation, but if it exists elsewhere we could also refer to the\nimplementing commit?\n\nI've also made some assumptions about how that \"current branch\" check\nworks, since I'm not sure how to make sense of it without this\nmanually created remote-tracking branch?\n\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  builtin/fetch.c | 23 ++++++++++++++---------\n>  remote.c        | 15 +++++++++++++++\n>  remote.h        |  9 +++++++++\n>  3 files changed, 38 insertions(+), 9 deletions(-)\n>\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index ffcb5ba29a..136a59154a 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -1979,7 +1979,8 @@ static int do_fetch(struct transport *transport,\n>         if (rs->nr) {\n>                 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n>         } else {\n> -               struct branch *branch = branch_get(NULL);\n> +               struct string_list tracked = STRING_LIST_INIT_DUP;\n> +               struct string_list_item *item;\n>\n>                 if (transport->remote->fetch.nr) {\n>                         refspec_ref_prefixes(&transport->remote->fetch,\n> @@ -1995,14 +1996,18 @@ static int do_fetch(struct transport *transport,\n>                         else\n>                                 follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n>                 }\n> -               if (branch && branch_has_merge_config(branch) &&\n> -                   !strcmp(branch->remote_name, transport->remote->name)) {\n> -                       int i;\n> -                       for (i = 0; i < branch->merge_nr; i++) {\n> -                               strvec_push(&transport_ls_refs_options.ref_prefixes,\n> -                                           branch->merge[i]->src);\n> -                       }\n> -               }\n> +\n> +               /*\n> +                * The configured refspec may not cover the current\n> +                * branch's upstream (e.g. a narrowed -t refspec), so\n> +                * make sure we can still fetch it regardless.\n> +                */\n> +               collect_upstream_from_remote(the_repository, &tracked,\n> +                                             transport->remote, NULL);\n> +               for_each_string_list_item(item, &tracked)\n> +                       strvec_push(&transport_ls_refs_options.ref_prefixes,\n> +                                   item->string);\n> +               string_list_clear(&tracked, 0);\n>\n>                 /*\n>                  * If there are no refs specified to fetch, then we just\n> diff --git a/remote.c b/remote.c\n> index c9252a90f4..965e42ff52 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -1891,6 +1891,21 @@ int branch_merge_matches(struct branch *branch,\n>         return refname_match(branch->merge[i]->src, refname);\n>  }\n>\n> +void collect_upstream_from_remote(struct repository *repo,\n> +                                  struct string_list *tracked,\n> +                                  struct remote *remote,\n> +                                  const char *refname)\n\nI'm not sure if we have guidance about where in the parameter list\n\"out\" parameters should go, but I would expect them to be last? Idk.\n\nShould the \"inputs\" be const to better communicate that we aren't\ngoing to modify them? I'm not really sure of the const semantics of C\nanyway, and I know there have been topics in the recent past around\nconst-modifier lints and so on.\n\n> +{\n> +       struct branch *branch = repo_branch_get(repo, refname);\n> +\n> +       if (!branch_has_merge_config(branch) ||\n> +           strcmp(branch->remote_name, remote->name))\n> +               return;\n> +\n> +       for (int i = 0; i < branch->merge_nr; i++)\n> +               string_list_insert(tracked, branch->merge[i]->src);\n> +}\n\nWe do trade some performance now, right? We first collect a\nstring_list, then push them into a strvec, which has some overhead\nrelative to skipping the intermediate step. Plus, string_lists sort,\nwhich the original didn't do. And the caller uses the DUP version of\nstring_list (and IIUC strvec_push strdup's as well), so we end up\ncopying twice?\n\nIf we used a strvec instead, we could use strvec_pushv, perhaps, being\na bit more efficient? (It would be nice if we had an ownership-taking\nstrvec_appendv or something?)\n\nBut stepping back, if the caller just provides a strvec instead of a\nstring_list, we can push to it directly, since that's what happens\nanyway! We don't do any filtering or anything in the new caller.\n\n>  __attribute__((format (printf,2,3)))\n>  static char *error_buf(struct strbuf *err, const char *fmt, ...)\n>  {\n> diff --git a/remote.h b/remote.h\n> index ac485a584d..7c86c529b6 100644\n> --- a/remote.h\n> +++ b/remote.h\n> @@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n>\n>  int branch_merge_matches(struct branch *, int n, const char *);\n>\n> +/*\n> + * If refname's branch builds on remote, add its upstream on remote to\n> + * tracked. A NULL refname means the current branch.\n> + */\n> +void collect_upstream_from_remote(struct repository *repo,\n> +                                  struct string_list *tracked,\n> +                                  struct remote *remote,\n> +                                  const char *refname);\n> +\n>  /* list of the remote in a group as configured */\n>  struct remote_group_data {\n>         const char *name;\n> --\n> gitgitgadget\n>\n\n\n-- \nD. Ben Knoble\n\n"},{"id":"556163","messageId":"CALnO6CCJzzX1qKeX4AR-GViM4LJjKrfdj6iafVXUQZM-c2Su4A@mail.gmail.com","threadId":"66353","inReplyTo":"9fb7e77a862640007339df639d03e61db37b6f70.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:09:16Z","receivedAt":"2026-10-24T15:09:16Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Configuring remote.<name>.refmap without remote.<name>.fetch used to\n> make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n> only meaningful with command-line refspec(s)\", since a refmap only\n> says where to put fetched refs, not what to fetch.\n\nThis makes this commit sound like a fixup of [1/5], but I think it's\nactually one of the main goals of the series! Can we flip the\njustification around to say something like \"we want to allow refmaps\nto do XYZ […] Incidentally, we also make sense of the case when refmap\nis configured without fetch\" ? That would make the purpose clearer to\nme, at least.\n\n> Make that case infer what to fetch: the local branches whose\n> @{upstream} is already on that remote. This lets a remote be\n> configured to fetch only the branches actually in use, without\n> listing them by hand in remote.<name>.fetch, and without needing to\n> touch the command line every time.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  Documentation/config/remote.adoc |   5 +-\n>  Documentation/fetch-options.adoc |   6 +-\n>  builtin/fetch.c                  |  75 +++++++++++++++----\n>  remote.c                         |  24 +++++++\n>  remote.h                         |   4 ++\n>  t/meson.build                    |   1 +\n>  t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n>  7 files changed, 219 insertions(+), 15 deletions(-)\n>  create mode 100755 t/t5586-fetch-refmap.sh\n>\n> diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\n> index 103eada406..7c52e443a5 100644\n> --- a/Documentation/config/remote.adoc\n> +++ b/Documentation/config/remote.adoc\n> @@ -36,7 +36,10 @@ remote.<name>.fetch::\n>  remote.<name>.refmap::\n>         The default value of the `--refmap` option for linkgit:git-fetch[1].\n>         Used to map remote refs being fetched to remote-tracking refs to\n> -       store. See the `--refmap` entry in linkgit:git-fetch[1].\n> +       store. If `remote.<name>.fetch` is not set either, a refspec-less\n> +       fetch infers what to fetch from local branches built on this\n> +       remote, instead of fetching every branch it has. See the\n> +       `--refmap` entry in linkgit:git-fetch[1].\n>\n>  remote.<name>.push::\n>         The default set of \"refspec\" for linkgit:git-push[1]. See\n> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> index c2101a7b39..90b9e13ac2 100644\n> --- a/Documentation/fetch-options.adoc\n> +++ b/Documentation/fetch-options.adoc\n> @@ -245,8 +245,10 @@ endif::git-pull[]\n>         command-line arguments. See section on \"Configured Remote-tracking\n>         Branches\" for details.\n>  +\n> -`remote.<name>.refmap` provides the default value for this option, the\n> -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n> +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n> +nothing to fetch is specified on the command line, nor is there a\n> +`remote.<name>.fetch`, branches from the remote that are used as the\n\n\"remote-tracking branches\" ??\n\n> +`@{upstream}` of our local branches are fetched.\n\nThe clause here is complicated ':D I found \"infer what to fetch from\nlocal branches built on this remote\" to be a bit clearer, but we\ndefinitely still need to get to the point, which is that\nremote-tracking branches from this remote that are also configured as\nupstreams of local branches are fetched.\n\nNow, I wonder a bit: the doc says \"when a refmap is active\" we do that\n\"fetch remote-tracking branches that are upstream of local branches\"\n[all scoped to the current remote]---but it doesn't really say how the\nrefmap affects that? I mean, I imagine it's used for mapping (duh),\nbut I don't know if it also narrows the scope, or if the scope is\ndeliberately all remote-tracking branches that meet the requirements,\nor…?\n\nIOW, it seems to beg the question \"why does the presence of a refmap\ncontrol this if it has no effect on the set of selected branches,\" and\nI think the answer is partly \"because we designed it that way\" and\npossibly found in the mails leading up to this point, but we might\nwant to try to connect the dots for folks? Idk.\n\n>\n>  `-t`::\n>  `--tags`::\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 136a59154a..0fc1fb66d5 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>          */\n>         struct refspec *effective_refmap =\n>                 refmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n> +       struct refspec inferred_rs;\n> +       int infer_from_refmap = 0;\n>\n>         /* opportunistically-updated references: */\n>         struct ref *orefs = NULL, **oref_tail = &orefs;\n> @@ -531,15 +533,32 @@ static struct ref *get_ref_map(struct remote *remote,\n>         struct hashmap existing_refs;\n>         int existing_refs_populated = 0;\n>\n> +       refspec_init_fetch(&inferred_rs, the_hash_algo);\n> +\n>         filter_prefetch_refspec(rs);\n>         if (remote)\n>                 filter_prefetch_refspec(&remote->fetch);\n>\n> +       if (!rs->nr && remote && !remote->fetch.nr &&\n> +           effective_refmap && effective_refmap->nr) {\n> +               struct string_list tracked = STRING_LIST_INIT_DUP;\n> +               struct string_list_item *item;\n> +\n> +               branches_tracking_remote(the_repository, remote, &tracked);\n> +               for_each_string_list_item(item, &tracked)\n> +                       refspec_append(&inferred_rs, item->string);\n> +               string_list_clear(&tracked, 0);\n\nInteresting choice of string_list again ;) do we need the sorting\nbehavior? (In fact, since it could come from an \"ordered\" config,\nperhaps we should use that order?) A strvec seems more appropriate\nthen…\n\n> +\n> +               rs = &inferred_rs;\n> +               infer_from_refmap = 1;\n> +       }\n> +\n>         if (rs->nr) {\n>                 struct refspec *fetch_refspec;\n>\n>                 for (i = 0; i < rs->nr; i++) {\n> -                       get_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n> +                       get_fetch_map(remote_refs, &rs->items[i], &tail,\n> +                                     infer_from_refmap);\n>                         if (rs->items[i].dst && rs->items[i].dst[0])\n>                                 *autotags = 1;\n>                 }\n> @@ -574,6 +593,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>\n>                 for (i = 0; i < fetch_refspec->nr; i++)\n>                         get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n> +       } else if (infer_from_refmap) {\n> +               /* Already fully handled above. */\n>         } else if (refmap.nr) {\n>                 die(\"--refmap option is only meaningful with command-line refspec(s)\");\n>         } else {\n> @@ -669,6 +690,7 @@ static struct ref *get_ref_map(struct remote *remote,\n>         if (existing_refs_populated)\n>                 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n>\n> +       refspec_clear(&inferred_rs);\n>         return ref_map;\n>  }\n>\n> @@ -1978,13 +2000,47 @@ static int do_fetch(struct transport *transport,\n>\n>         if (rs->nr) {\n>                 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n> +       } else if (transport->remote->fetch.nr) {\n> +               struct string_list tracked = STRING_LIST_INIT_DUP;\n> +               struct string_list_item *item;\n> +\n> +               refspec_ref_prefixes(&transport->remote->fetch,\n> +                                    &transport_ls_refs_options.ref_prefixes);\n> +               /*\n> +                * See remote.c's handling of remote.<name>.followRemoteHEAD\n> +                * for the analogous, still-unresolved case.\n\nIs this a \"we haven't resolved what to do\" case, or a \"some remote (or\nsomething) hasn't resolved\" case? I'm not sure what I should be\nlooking at here.\n\nOh, I see. It's a duplicate of comment and code from the\nelse{effective_refmap->nr} block below. Hrm… probably it would be nice\nnot to duplicate that? I can't spot any differences between the code\nblocks.\n\n> +                */\n> +               if (transport->remote->follow_remote_head)\n> +                       follow_remote_head = transport->remote->follow_remote_head;\n> +               else if (config->follow_remote_head_seen)\n> +                       follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n> +               else\n> +                       follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n> +\n> +               /*\n> +                * The configured refspec may not cover the current\n> +                * branch's upstream (e.g. a narrowed -t refspec), so\n> +                * make sure we can still fetch it regardless.\n> +                */\n> +               collect_upstream_from_remote(the_repository, &tracked,\n> +                                             transport->remote, NULL);\n> +               for_each_string_list_item(item, &tracked)\n> +                       strvec_push(&transport_ls_refs_options.ref_prefixes,\n> +                                   item->string);\n> +               string_list_clear(&tracked, 0);\n>         } else {\n> +               /*\n> +                * The --refmap command line option, if given, takes\n> +                * precedence over remote.<name>.refmap.\n> +                */\n> +               struct refspec *effective_refmap = refmap.nr ? &refmap :\n> +                       &transport->remote->refmap;\n>                 struct string_list tracked = STRING_LIST_INIT_DUP;\n>                 struct string_list_item *item;\n>\n> -               if (transport->remote->fetch.nr) {\n> -                       refspec_ref_prefixes(&transport->remote->fetch,\n> -                                            &transport_ls_refs_options.ref_prefixes);\n> +               if (effective_refmap->nr) {\n> +                       branches_tracking_remote(the_repository,\n> +                                                 transport->remote, &tracked);\n>                         /*\n>                          * See remote.c's handling of remote.<name>.followRemoteHEAD\n>                          * for the analogous, still-unresolved case.\n> @@ -1995,15 +2051,10 @@ static int do_fetch(struct transport *transport,\n>                                 follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n>                         else\n>                                 follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n> +               } else {\n> +                       collect_upstream_from_remote(the_repository, &tracked,\n> +                                                     transport->remote, NULL);\n>                 }\n> -\n> -               /*\n> -                * The configured refspec may not cover the current\n> -                * branch's upstream (e.g. a narrowed -t refspec), so\n> -                * make sure we can still fetch it regardless.\n> -                */\n> -               collect_upstream_from_remote(the_repository, &tracked,\n> -                                             transport->remote, NULL);\n>                 for_each_string_list_item(item, &tracked)\n>                         strvec_push(&transport_ls_refs_options.ref_prefixes,\n>                                     item->string);\n> diff --git a/remote.c b/remote.c\n> index 965e42ff52..6467496a0f 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -1906,6 +1906,30 @@ void collect_upstream_from_remote(struct repository *repo,\n>                 string_list_insert(tracked, branch->merge[i]->src);\n>  }\n>\n> +struct branches_tracking_remote_cb_data {\n> +       struct repository *repo;\n> +       struct remote *remote;\n> +       struct string_list *tracked;\n> +};\n> +\n> +static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n> +{\n> +       struct branches_tracking_remote_cb_data *data = cb_data;\n> +\n> +       collect_upstream_from_remote(data->repo, data->tracked, data->remote,\n> +                                     ref->name);\n> +       return 0;\n> +}\n> +\n> +void branches_tracking_remote(struct repository *repo, struct remote *remote,\n> +                              struct string_list *tracked)\n> +{\n> +       struct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n> +\n> +       refs_for_each_branch_ref(get_main_ref_store(repo),\n> +                                 add_if_tracking_remote, &data);\n> +}\n> +\n>  __attribute__((format (printf,2,3)))\n>  static char *error_buf(struct strbuf *err, const char *fmt, ...)\n>  {\n> diff --git a/remote.h b/remote.h\n> index 7c86c529b6..62265346d3 100644\n> --- a/remote.h\n> +++ b/remote.h\n> @@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,\n>                                    struct remote *remote,\n>                                    const char *refname);\n>\n> +/* fills tracked with the refname of every local branch's upstream on remote */\n> +void branches_tracking_remote(struct repository *repo, struct remote *remote,\n> +                              struct string_list *tracked);\n> +\n>  /* list of the remote in a group as configured */\n>  struct remote_group_data {\n>         const char *name;\n\nI stopped reading here, but it made a bit of sense up to this point. I\nwonder if the remote.h comment on the new function could be more\nclear, but :shrug:\n\n-- \nD. Ben Knoble\n\n"},{"id":"556164","messageId":"CALnO6CAqhj91e6f=4_1fQRKDdkFx8t8RMyj-60Lt7a6vBV4uMw@mail.gmail.com","threadId":"66353","inReplyTo":"80028d4f8638f766a73b84a5631d21101a413490.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:11:41Z","receivedAt":"2026-10-24T15:11:41Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A remote added the ordinary way tracks every branch it has, via a\n> wildcard remote.<name>.fetch refspec. That is wasteful for a remote\n> whose history is only worth following for the branches actually in\n> use locally, and it can make \"git fetch\" negotiate history for\n> branches nobody asked for.\n>\n> Give \"git remote add\" a --limited-fetch option that sets up\n> remote.<name>.refmap instead of remote.<name>.fetch, so that\n> \"git fetch <name>\" only fetches the branches already tracked, as\n> described in the previous commit. It is rejected together with\n> -t/--track or --mirror, since those already say explicitly what to\n> fetch.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n\n> diff --git a/builtin/remote.c b/builtin/remote.c\n> index fb7e5b114f..a7c13946d1 100644\n> --- a/builtin/remote.c\n> +++ b/builtin/remote.c\n> @@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n>  {\n>         int fetch = 0, fetch_tags = TAGS_DEFAULT;\n>         unsigned mirror = MIRROR_NONE;\n> +       int limited_fetch = -1; /* unspecified */\n>         struct string_list track = STRING_LIST_INIT_NODUP;\n>         const char *master = NULL;\n>         struct remote *remote;\n> @@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n>                 OPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n>                         N_(\"set up remote as a mirror to push to or fetch from\"),\n>                         PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n> +               OPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n> +                       N_(\"fetch only the branches we build on, instead of every branch\")),\n>                 OPT_END()\n>         };\n>\n> @@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n>                 die(_(\"specifying a master branch makes no sense with --mirror\"));\n>         if (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n>                 die(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n> +       if (limited_fetch == 1 && track.nr)\n> +               die(_(\"--limited-fetch does not make sense with -t/--track\"));\n> +       if (limited_fetch == 1 && mirror)\n> +               die(_(\"--limited-fetch does not make sense with --mirror\"));\n\nWe need \"== 1\" because default to \"-1\" for unspecified, which is a bit\natypical of boolean flags unless we need to know about that case.\n\nReading ahead, 5/5 wants to know if limited-fetch was unspecified so\nwe can act on it. Ok.\n\n>\n>         name = argv[0];\n>         url = argv[1];\n> @@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n>         repo_config_set(the_repository, buf.buf, url);\n>\n>         if (!mirror || mirror & MIRROR_FETCH) {\n> +               int use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n> +                       limited_fetch == 1;\n> +\n\n:\\ I don't have a better suggestion for formatting this line, but it's… odd.\n\n>                 strbuf_reset(&buf);\n> -               strbuf_addf(&buf, \"remote.%s.fetch\", name);\n> -               if (track.nr == 0)\n> -                       string_list_append(&track, \"*\");\n> -               for (size_t i = 0; i < track.nr; i++) {\n> -                       add_branch(buf.buf, track.items[i].string,\n> -                                  name, mirror, &buf2);\n> +               if (use_limited_fetch) {\n> +                       strbuf_addf(&buf, \"remote.%s.refmap\", name);\n> +                       strbuf_reset(&buf2);\n> +                       strbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n> +                       repo_config_set(the_repository, buf.buf, buf2.buf);\n> +               } else {\n> +                       strbuf_addf(&buf, \"remote.%s.fetch\", name);\n> +                       if (track.nr == 0)\n> +                               string_list_append(&track, \"*\");\n> +                       for (size_t i = 0; i < track.nr; i++)\n> +                               add_branch(buf.buf, track.items[i].string,\n> +                                          name, mirror, &buf2);\n>                 }\n>         }\n>\n> diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\n> index 5fbfcb0848..0168d5abfe 100755\n> --- a/t/t5505-remote.sh\n> +++ b/t/t5505-remote.sh\n> @@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n>         test_grep ! \"\\[blob:none\\]\" out\n>  '\n>\n> +test_expect_success '--limited-fetch works in a full repository too' '\n> +       test_when_finished \"rm -rf full-add\" &&\n> +       git clone --no-local one full-add &&\n> +       (\n> +               cd full-add &&\n> +               git remote add --limited-fetch upstream ../two &&\n> +               test_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n> +                       remote.upstream.refmap &&\n> +               test_must_fail git config get remote.upstream.fetch\n> +       )\n> +'\n> +\n> +test_expect_success '--limited-fetch conflicts with -t' '\n> +       test_must_fail git remote add --limited-fetch -t main upstream ../two\n> +'\n> +\n>  test_expect_success 'check remote-tracking' '\n>         (\n>                 cd test &&\n> --\n> gitgitgadget\n\nLooks reasonable.\n\n-- \nD. Ben Knoble\n\n"},{"id":"556165","messageId":"CALnO6CBA3oQMm--3VxhbUdDmXfAGjZo9H5bFxWOGUCY=GADk3w@mail.gmail.com","threadId":"66353","inReplyTo":"552bf08971be5204311c96bf232b5da49e852db1.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:13:05Z","receivedAt":"2026-10-24T15:13:05Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> Adding a second remote to a shallow, single-branch clone used to\n> still fetch every branch that remote has, since \"git remote add\"\n> always set up a wildcard remote.<name>.fetch refspec regardless of\n> how shallow the repository already was. That defeats the purpose of\n> having cloned shallow and single-branch in the first place, and can\n> make a plain \"git fetch\" on that remote hang or take a very long\n> time on a repository with many branches.\n\nFirst we focus on \"single-branch shallow clones\", but…\n\n> Turn --limited-fetch on by default when the repository is already\n> shallow and neither -t/--track nor --mirror was given, so that\n> adding a remote there does not by itself commit to following every\n> branch it has. --no-limited-fetch keeps the previous behavior for\n> whoever wants it.\n\n…now we only care about shallow. I think that's reasonable, since we\ndon't have a way to track \"was cloned with a single-branch\" (and it's\nnot really the key criterion anyway), but the wording begged the\nquestion ;)\n\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  Documentation/git-remote.adoc |  8 ++++-\n>  builtin/remote.c              |  5 ++-\n>  t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n>  3 files changed, 71 insertions(+), 2 deletions(-)\n>\n> diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\n> index f8764394e7..f43d707c91 100644\n> --- a/Documentation/git-remote.adoc\n> +++ b/Documentation/git-remote.adoc\n> @@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n>  _<URL>_.  The command `git fetch <name>` can then be used to create and\n>  update remote-tracking branches `<name>/<branch>`.\n>  +\n> +If the repository is already a shallow repository (see linkgit:git-clone[1]\n> +`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n> +`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n> +not need to negotiate history for every branch the remote has.\n> ++\n>  With `-f` option, `git fetch <name>` is run immediately after\n>  the remote information is set up.\n>  +\n> @@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n>  that tracks all branches, `remote.<name>.refmap` is set up so that a\n>  refspec-less `git fetch <name>` only fetches branches our local branches\n>  are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n> -details.\n> +details. `--no-limited-fetch` explicitly disables this, overriding the\n> +shallow-repository default described above.\n>  +\n>  With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n>  up to point at remote's _<master>_ branch. See also the set-head command.\n> diff --git a/builtin/remote.c b/builtin/remote.c\n> index a7c13946d1..8c4497cccd 100644\n> --- a/builtin/remote.c\n> +++ b/builtin/remote.c\n> @@ -16,6 +16,7 @@\n>  #include \"rebase.h\"\n>  #include \"refs.h\"\n>  #include \"refspec.h\"\n> +#include \"shallow.h\"\n>  #include \"odb.h\"\n>  #include \"strvec.h\"\n>  #include \"commit-reach.h\"\n> @@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n>\n>         if (!mirror || mirror & MIRROR_FETCH) {\n>                 int use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n> -                       limited_fetch == 1;\n> +                       (limited_fetch == 1 ||\n> +                        (limited_fetch == -1 &&\n> +                         is_repository_shallow(the_repository)));\n>\n>                 strbuf_reset(&buf);\n>                 if (use_limited_fetch) {\n\nI didn't read the tests, but the code looks short and sweet ;)\n\n"},{"id":"556166","messageId":"CALnO6CD9sB8JCE42kWa5MO2sD=WzstZ8GoHW2PSpjdpRDF2EjQ@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqcxt02eoa.fsf@gitster.g","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:15:51Z","receivedAt":"2026-10-24T15:15:51Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Fri, Oct 23, 2026 at 7:13 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > Avoid fetching every branch of a new remote in a shallow repo.\n> >\n> > Changes in v10:\n> >\n> >  * Resolve merge conflict after master integrated\n> >    ch/fetch-followremotehead-lazy-validation.\n>\n> > Harald Nordgren (5):\n> >   fetch: add remote.<name>.refmap\n> >   fetch: extract collect_upstream_from_remote() helper\n> >   fetch: infer branches to fetch from a refmap-only remote\n> >   remote: add \"git remote add --limited-fetch\"\n> >   remote: default to --limited-fetch in a shallow repository\n> >\n> >  Documentation/config/remote.adoc |   8 +++\n> >  Documentation/fetch-options.adoc |   5 ++\n> >  Documentation/git-remote.adoc    |  14 +++-\n> >  builtin/fetch.c                  |  92 ++++++++++++++++++++----\n> >  builtin/remote.c                 |  31 ++++++--\n> >  remote.c                         |  51 ++++++++++++-\n> >  remote.h                         |  19 +++++\n> >  t/meson.build                    |   1 +\n> >  t/t5505-remote.sh                |  76 ++++++++++++++++++++\n> >  t/t5510-fetch.sh                 |  63 ++++++++++++++++\n> >  t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n> >  11 files changed, 456 insertions(+), 23 deletions(-)\n> >  create mode 100755 t/t5586-fetch-refmap.sh\n>\n> This topic seeems fairly well done, without seeing any additional\n> comments in the last two rounds, but I am highly disturbed that I\n> was practically the only person who reviewd its later iterations.\n> Because I am not a target audience of the feature, I would not be\n> surprised if I am missing something large.\n>\n> I would very much appreciate if somebody else can give the series\n> one more read and Ack them, so that we can move it forward.\n>\n> Thanks.\n\nI gave it a read; it's possible the things I wrote were already seen\nby you and not worth commenting on, but I'm not sure.\n\nAs for the reviewing… well, it was nice to review when it felt like\nthings had settled somewhat :)\n\n-- \nD. Ben Knoble\n\n"},{"id":"556178","messageId":"CAHwyqnXWxiF6NLh3e4yGq6_y+=A+-_PwKF7D9DJJDxbk_0vF-g@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CAhUNRMf=KZmY6YbC-5g3h8DE6U40HehGwbcRTPhDasrg@mail.gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-24T17:18:57Z","receivedAt":"2026-10-24T17:18:57Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> remote.h says this procedure does \"For the given remote, reads the\n> refspec's src and sets the other fields.\" Ok, and glancing at\n> refspec_find_match the idea is that \"refspec\" input is a query (with a\n> src or dst) and we find the matching entry and fill out the input's\n> fields.\n>\n> So… I assume the usual idea would be that refmap is a \"superset\" of\n> the fetch refspec, since I might want to map refs that I don't\n> normally fetch. (Or it might be disjoint, if none of the mappings\n> apply to my usual fetches.) But that means that remote_find_tracking()\n> won't be able to find entries for the mapped refs that aren't part of\n> my \"normal\" fetches? Are there downstream consequences of that? I'm\n> not sure, I haven't looked at how else remote_find_tracking is used.\n>\n> Could that affect, for example, --delete-merged?\n\nGood point, I will change it like this:\n\n```\n 1:  ba7f1354cd ! 16:  4603c84d68 fetch: add remote.<name>.refmap\n    @@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n      int remote_find_tracking(struct remote *remote, struct\nrefspec_item *refspec)\n      {\n     -  return refspec_find_match(&remote->fetch, refspec);\n    -+  if (remote->fetch.nr)\n    -+          return refspec_find_match(&remote->fetch, refspec);\n    -+  return refspec_find_match(&remote->refmap, refspec);\n    ++  if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))\n    ++          return refspec_find_match(&remote->refmap, refspec);\n    ++  return 0;\n      }\n\n      static struct ref *alloc_ref_with_prefix(const char *prefix,\nsize_t prefixlen,\n```\n\n\nHarald\n\n"},{"id":"556211","messageId":"CALnO6CBAEB8=FfZfde0OWvfLJikVkXQ8itP9wZpGHxfxK+y_LQ@mail.gmail.com","threadId":"66353","inReplyTo":"CAHwyqnXWxiF6NLh3e4yGq6_y+=A+-_PwKF7D9DJJDxbk_0vF-g@mail.gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-25T14:14:46Z","receivedAt":"2026-10-25T14:14:46Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Sat, Oct 24, 2026 at 1:19 PM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> > remote.h says this procedure does \"For the given remote, reads the\n> > refspec's src and sets the other fields.\" Ok, and glancing at\n> > refspec_find_match the idea is that \"refspec\" input is a query (with a\n> > src or dst) and we find the matching entry and fill out the input's\n> > fields.\n> >\n> > So… I assume the usual idea would be that refmap is a \"superset\" of\n> > the fetch refspec, since I might want to map refs that I don't\n> > normally fetch. (Or it might be disjoint, if none of the mappings\n> > apply to my usual fetches.) But that means that remote_find_tracking()\n> > won't be able to find entries for the mapped refs that aren't part of\n> > my \"normal\" fetches? Are there downstream consequences of that? I'm\n> > not sure, I haven't looked at how else remote_find_tracking is used.\n> >\n> > Could that affect, for example, --delete-merged?\n>\n> Good point, I will change it like this:\n>\n> ```\n>  1:  ba7f1354cd ! 16:  4603c84d68 fetch: add remote.<name>.refmap\n>     @@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n>       int remote_find_tracking(struct remote *remote, struct\n> refspec_item *refspec)\n>       {\n>      -  return refspec_find_match(&remote->fetch, refspec);\n>     -+  if (remote->fetch.nr)\n>     -+          return refspec_find_match(&remote->fetch, refspec);\n>     -+  return refspec_find_match(&remote->refmap, refspec);\n>     ++  if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))\n>     ++          return refspec_find_match(&remote->refmap, refspec);\n>     ++  return 0;\n>       }\n>\n>       static struct ref *alloc_ref_with_prefix(const char *prefix,\n> size_t prefixlen,\n> ```\n\nThat does look like it would fall back on the refmap, but to be honest\nI'm not sure what is \"correct\" here :) I was hoping for more\ndiscussion of the questions I raised to see some thought about what we\nshould or shouldn't do. I suppose falling back might be important for\nthe later step with --limited-fetch, though?\n\n-- \nD. Ben Knoble\n\n"},{"id":"556214","messageId":"CAHwyqnWuj_1x9B4H+Kto2rZn6YeaM+X-vUb-uUb2jr134gcHjA@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CBAEB8=FfZfde0OWvfLJikVkXQ8itP9wZpGHxfxK+y_LQ@mail.gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-25T16:47:16Z","receivedAt":"2026-10-25T16:47:16Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Sun, Oct 25, 2026 at 4:14 PM D. Ben Knoble <ben.knoble@gmail.com> wrote:\n>\n> On Sat, Oct 24, 2026 at 1:19 PM Harald Nordgren\n> <haraldnordgren@gmail.com> wrote:\n> >\n> > > remote.h says this procedure does \"For the given remote, reads the\n> > > refspec's src and sets the other fields.\" Ok, and glancing at\n> > > refspec_find_match the idea is that \"refspec\" input is a query (with a\n> > > src or dst) and we find the matching entry and fill out the input's\n> > > fields.\n> > >\n> > > So… I assume the usual idea would be that refmap is a \"superset\" of\n> > > the fetch refspec, since I might want to map refs that I don't\n> > > normally fetch. (Or it might be disjoint, if none of the mappings\n> > > apply to my usual fetches.) But that means that remote_find_tracking()\n> > > won't be able to find entries for the mapped refs that aren't part of\n> > > my \"normal\" fetches? Are there downstream consequences of that? I'm\n> > > not sure, I haven't looked at how else remote_find_tracking is used.\n> > >\n> > > Could that affect, for example, --delete-merged?\n> >\n> > Good point, I will change it like this:\n> >\n> > ```\n> >  1:  ba7f1354cd ! 16:  4603c84d68 fetch: add remote.<name>.refmap\n> >     @@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n> >       int remote_find_tracking(struct remote *remote, struct\n> > refspec_item *refspec)\n> >       {\n> >      -  return refspec_find_match(&remote->fetch, refspec);\n> >     -+  if (remote->fetch.nr)\n> >     -+          return refspec_find_match(&remote->fetch, refspec);\n> >     -+  return refspec_find_match(&remote->refmap, refspec);\n> >     ++  if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))\n> >     ++          return refspec_find_match(&remote->refmap, refspec);\n> >     ++  return 0;\n> >       }\n> >\n> >       static struct ref *alloc_ref_with_prefix(const char *prefix,\n> > size_t prefixlen,\n> > ```\n>\n> That does look like it would fall back on the refmap, but to be honest\n> I'm not sure what is \"correct\" here :) I was hoping for more\n> discussion of the questions I raised to see some thought about what we\n> should or shouldn't do. I suppose falling back might be important for\n> the later step with --limited-fetch, though?\n\nWithout the fallback, @{upstream}, --delete-merged and \"git branch\n--track\" fail for refs fetched through the refmap.\n\nI'll add tests to lock down that such refs are tracked through the\nrefmap, and that refs excluded by a negative refspec in\nremote.<name>.fetch are not.\n\n--limited-fetch does not need the fallback.\n\n\nHarald\n\n"},{"id":"556215","messageId":"CAHwyqnVELh2ZyCy5W8+jHHWN=UvcgMJP0k0i1PJKU5bU_AhUrw@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CAgS3=ne1RkT-DioLmY6SqhnOZ8NH5kxxCZA_1QFx-dBg@mail.gmail.com","subject":"Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-25T17:20:01Z","receivedAt":"2026-10-25T17:20:01Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > When a fetch has no refspec to work from, we fall back to guessing\n> > what to fetch: the remote's configured fetch refspec if there is one,\n> > and separately, whether the current branch's upstream lives on this\n> > remote.\n>\n> It took me a couple tries to parse this because the 2 halves (\"[one],\n> and separately, [two]\") are 2 different kinds of objects! I think what\n> you are trying to tell me is\n>\n>     …we fall back to guessing what to fetch: the remote's …\n> separately, the current branch's upstream if it lives on the fetched\n> remote.\n>\n> That way [two] is also a \"thing to fetch\" in the same class of objects\n> as [one]? Or have I misunderstood?\n\nI'll take a look!\n\n> > The latter is independent of the former on purpose: a narrow\n> > refspec (for example from \"git remote add -t\") can leave the current\n> > branch's own upstream uncovered, and we still need to be able to fetch\n> > it.\n>\n> I don't really understand the meaning here. I can guess that you mean\n> something like \"A remote with a narrow refspec, like what you get from\n> 'git remote add -t', can omit a branch's upstream from the remote's\n> fetch refspec, and we still want to fetch that upstream.\" But it's\n> taken me a while to put that together (after puzzling out the above)\n> after first not knowing what \"uncovered\" meant.\n>\n> I think the whole thing would be clearer with an example:\n>\n> \"\"\"\n> Imagine a remote with a narrow refspec:\n>\n>     git remote add -t main colleague <remote>\n>\n> and a branch whose upstream points at that remote, but isn't part of\n> the remote's refspec:\n>\n>     git fetch colleague work:refs/remotes/colleague/work\n>     git switch -c work colleague/work\n>\n> When we \"git fetch colleague\" without a refspec, we decide what to\n> fetch by combining the remote's refspec (main -> remote-tracking\n> branch, in this case) and the current branch's upstream, if it was\n> based on that remote (work -> remote-tracking branch, in this case).\n> \"\"\"\n>\n> Note: we aren't \"guessing\" what to fetch!\n\nThanks! Maybe this should be the example instead?\n\n        git clone --single-branch <url>\n        git switch -c work\n        git push -u origin work\n\n> I'm not sure if we have guidance about where in the parameter list\n> \"out\" parameters should go, but I would expect them to be last? Idk.\n>\n> Should the \"inputs\" be const to better communicate that we aren't\n> going to modify them? I'm not really sure of the const semantics of C\n> anyway, and I know there have been topics in the recent past around\n> const-modifier lints and so on.\n\nYes.\n\n> > +{\n> > +       struct branch *branch = repo_branch_get(repo, refname);\n> > +\n> > +       if (!branch_has_merge_config(branch) ||\n> > +           strcmp(branch->remote_name, remote->name))\n> > +               return;\n> > +\n> > +       for (int i = 0; i < branch->merge_nr; i++)\n> > +               string_list_insert(tracked, branch->merge[i]->src);\n> > +}\n>\n> We do trade some performance now, right? We first collect a\n> string_list, then push them into a strvec, which has some overhead\n> relative to skipping the intermediate step. Plus, string_lists sort,\n> which the original didn't do. And the caller uses the DUP version of\n> string_list (and IIUC strvec_push strdup's as well), so we end up\n> copying twice?\n>\n> If we used a strvec instead, we could use strvec_pushv, perhaps, being\n> a bit more efficient? (It would be nice if we had an ownership-taking\n> strvec_appendv or something?)\n>\n> But stepping back, if the caller just provides a strvec instead of a\n> string_list, we can push to it directly, since that's what happens\n> anyway! We don't do any filtering or anything in the new caller.\n\nYes, I'll handle this.\n\n\nHarald\n\n"},{"id":"556216","messageId":"CAHwyqnUQSninG=3hcxZOXUUwEXGbA6AT7FfmL36gwa9wV3ub4g@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CCJzzX1qKeX4AR-GViM4LJjKrfdj6iafVXUQZM-c2Su4A@mail.gmail.com","subject":"Re: [PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-25T17:27:05Z","receivedAt":"2026-10-25T17:27:05Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > Configuring remote.<name>.refmap without remote.<name>.fetch used to\n> > make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n> > only meaningful with command-line refspec(s)\", since a refmap only\n> > says where to put fetched refs, not what to fetch.\n>\n> This makes this commit sound like a fixup of [1/5], but I think it's\n> actually one of the main goals of the series! Can we flip the\n> justification around to say something like \"we want to allow refmaps\n> to do XYZ […] Incidentally, we also make sense of the case when refmap\n> is configured without fetch\" ? That would make the purpose clearer to\n> me, at least.\n\nGood idea.\n\n> > Make that case infer what to fetch: the local branches whose\n> > @{upstream} is already on that remote. This lets a remote be\n> > configured to fetch only the branches actually in use, without\n> > listing them by hand in remote.<name>.fetch, and without needing to\n> > touch the command line every time.\n> >\n> > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> > ---\n> >  Documentation/config/remote.adoc |   5 +-\n> >  Documentation/fetch-options.adoc |   6 +-\n> >  builtin/fetch.c                  |  75 +++++++++++++++----\n> >  remote.c                         |  24 +++++++\n> >  remote.h                         |   4 ++\n> >  t/meson.build                    |   1 +\n> >  t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n> >  7 files changed, 219 insertions(+), 15 deletions(-)\n> >  create mode 100755 t/t5586-fetch-refmap.sh\n> >\n> > diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\n> > index 103eada406..7c52e443a5 100644\n> > --- a/Documentation/config/remote.adoc\n> > +++ b/Documentation/config/remote.adoc\n> > @@ -36,7 +36,10 @@ remote.<name>.fetch::\n> >  remote.<name>.refmap::\n> >         The default value of the `--refmap` option for linkgit:git-fetch[1].\n> >         Used to map remote refs being fetched to remote-tracking refs to\n> > -       store. See the `--refmap` entry in linkgit:git-fetch[1].\n> > +       store. If `remote.<name>.fetch` is not set either, a refspec-less\n> > +       fetch infers what to fetch from local branches built on this\n> > +       remote, instead of fetching every branch it has. See the\n> > +       `--refmap` entry in linkgit:git-fetch[1].\n> >\n> >  remote.<name>.push::\n> >         The default set of \"refspec\" for linkgit:git-push[1]. See\n> > diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> > index c2101a7b39..90b9e13ac2 100644\n> > --- a/Documentation/fetch-options.adoc\n> > +++ b/Documentation/fetch-options.adoc\n> > @@ -245,8 +245,10 @@ endif::git-pull[]\n> >         command-line arguments. See section on \"Configured Remote-tracking\n> >         Branches\" for details.\n> >  +\n> > -`remote.<name>.refmap` provides the default value for this option, the\n> > -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n> > +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n> > +nothing to fetch is specified on the command line, nor is there a\n> > +`remote.<name>.fetch`, branches from the remote that are used as the\n>\n> \"remote-tracking branches\" ??\n\nYes, the next version will say \"remote-tracking branches from this\nremote that are also configured as the @{upstream} of a local branch\nare fetched\".\n\n> > +`@{upstream}` of our local branches are fetched.\n>\n> The clause here is complicated ':D I found \"infer what to fetch from\n> local branches built on this remote\" to be a bit clearer, but we\n> definitely still need to get to the point, which is that\n> remote-tracking branches from this remote that are also configured as\n> upstreams of local branches are fetched.\n>\n> Now, I wonder a bit: the doc says \"when a refmap is active\" we do that\n> \"fetch remote-tracking branches that are upstream of local branches\"\n> [all scoped to the current remote]---but it doesn't really say how the\n> refmap affects that? I mean, I imagine it's used for mapping (duh),\n> but I don't know if it also narrows the scope, or if the scope is\n> deliberately all remote-tracking branches that meet the requirements,\n> or…?\n>\n> IOW, it seems to beg the question \"why does the presence of a refmap\n> control this if it has no effect on the set of selected branches,\" and\n> I think the answer is partly \"because we designed it that way\" and\n> possibly found in the mails leading up to this point, but we might\n> want to try to connect the dots for folks? Idk.\n\nI'll take a look at improving this part.\n\n> >\n> >  `-t`::\n> >  `--tags`::\n> > diff --git a/builtin/fetch.c b/builtin/fetch.c\n> > index 136a59154a..0fc1fb66d5 100644\n> > --- a/builtin/fetch.c\n> > +++ b/builtin/fetch.c\n> > @@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n> >          */\n> >         struct refspec *effective_refmap =\n> >                 refmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n> > +       struct refspec inferred_rs;\n> > +       int infer_from_refmap = 0;\n> >\n> >         /* opportunistically-updated references: */\n> >         struct ref *orefs = NULL, **oref_tail = &orefs;\n> > @@ -531,15 +533,32 @@ static struct ref *get_ref_map(struct remote *remote,\n> >         struct hashmap existing_refs;\n> >         int existing_refs_populated = 0;\n> >\n> > +       refspec_init_fetch(&inferred_rs, the_hash_algo);\n> > +\n> >         filter_prefetch_refspec(rs);\n> >         if (remote)\n> >                 filter_prefetch_refspec(&remote->fetch);\n> >\n> > +       if (!rs->nr && remote && !remote->fetch.nr &&\n> > +           effective_refmap && effective_refmap->nr) {\n> > +               struct string_list tracked = STRING_LIST_INIT_DUP;\n> > +               struct string_list_item *item;\n> > +\n> > +               branches_tracking_remote(the_repository, remote, &tracked);\n> > +               for_each_string_list_item(item, &tracked)\n> > +                       refspec_append(&inferred_rs, item->string);\n> > +               string_list_clear(&tracked, 0);\n>\n> Interesting choice of string_list again ;) do we need the sorting\n> behavior? (In fact, since it could come from an \"ordered\" config,\n> perhaps we should use that order?) A strvec seems more appropriate\n> then…\n\nAgreed, the next version uses a strvec here too. Nothing is sorted\nanymore: the branches come in refname order, and each branch's merge\nentries in config order.\n\n> > +\n> > +               rs = &inferred_rs;\n> > +               infer_from_refmap = 1;\n> > +       }\n> > +\n> >         if (rs->nr) {\n> >                 struct refspec *fetch_refspec;\n> >\n> >                 for (i = 0; i < rs->nr; i++) {\n> > -                       get_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n> > +                       get_fetch_map(remote_refs, &rs->items[i], &tail,\n> > +                                     infer_from_refmap);\n> >                         if (rs->items[i].dst && rs->items[i].dst[0])\n> >                                 *autotags = 1;\n> >                 }\n> > @@ -574,6 +593,8 @@ static struct ref *get_ref_map(struct remote *remote,\n> >\n> >                 for (i = 0; i < fetch_refspec->nr; i++)\n> >                         get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n> > +       } else if (infer_from_refmap) {\n> > +               /* Already fully handled above. */\n> >         } else if (refmap.nr) {\n> >                 die(\"--refmap option is only meaningful with command-line refspec(s)\");\n> >         } else {\n> > @@ -669,6 +690,7 @@ static struct ref *get_ref_map(struct remote *remote,\n> >         if (existing_refs_populated)\n> >                 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n> >\n> > +       refspec_clear(&inferred_rs);\n> >         return ref_map;\n> >  }\n> >\n> > @@ -1978,13 +2000,47 @@ static int do_fetch(struct transport *transport,\n> >\n> >         if (rs->nr) {\n> >                 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n> > +       } else if (transport->remote->fetch.nr) {\n> > +               struct string_list tracked = STRING_LIST_INIT_DUP;\n> > +               struct string_list_item *item;\n> > +\n> > +               refspec_ref_prefixes(&transport->remote->fetch,\n> > +                                    &transport_ls_refs_options.ref_prefixes);\n> > +               /*\n> > +                * See remote.c's handling of remote.<name>.followRemoteHEAD\n> > +                * for the analogous, still-unresolved case.\n>\n> Is this a \"we haven't resolved what to do\" case, or a \"some remote (or\n> something) hasn't resolved\" case? I'm not sure what I should be\n> looking at here.\n>\n> Oh, I see. It's a duplicate of comment and code from the\n> else{effective_refmap->nr} block below. Hrm… probably it would be nice\n> not to duplicate that? I can't spot any differences between the code\n> blocks.\n\nYes, good idea!\n\n\nHarald\n\n"},{"id":"556220","messageId":"CAHwyqnVxK+hJXHTttWmswPjb8AzNhkazybOxwrmHG4yEtMOHSA@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CBA3oQMm--3VxhbUdDmXfAGjZo9H5bFxWOGUCY=GADk3w@mail.gmail.com","subject":"Re: [PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-25T18:12:03Z","receivedAt":"2026-10-25T18:12:03Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > Adding a second remote to a shallow, single-branch clone used to\n> > still fetch every branch that remote has, since \"git remote add\"\n> > always set up a wildcard remote.<name>.fetch refspec regardless of\n> > how shallow the repository already was. That defeats the purpose of\n> > having cloned shallow and single-branch in the first place, and can\n> > make a plain \"git fetch\" on that remote hang or take a very long\n> > time on a repository with many branches.\n>\n> First we focus on \"single-branch shallow clones\", but…\n>\n> > Turn --limited-fetch on by default when the repository is already\n> > shallow and neither -t/--track nor --mirror was given, so that\n> > adding a remote there does not by itself commit to following every\n> > branch it has. --no-limited-fetch keeps the previous behavior for\n> > whoever wants it.\n>\n> …now we only care about shallow. I think that's reasonable, since we\n> don't have a way to track \"was cloned with a single-branch\" (and it's\n> not really the key criterion anyway), but the wording begged the\n> question ;)\n\nGood point, I'll improve.\n\n\nHarald\n\n"},{"id":"556229","messageId":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v11 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:16Z","receivedAt":"2026-10-25T20:25:16Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v11:\n\n * remote_find_tracking() now falls back to remote.<name>.refmap when\n   remote.<name>.fetch doesn't match. Negative refspecs in fetch still\n   exclude refs, and both cases have tests\n * The helpers write straight into a strvec, take the output parameter last\n   and the remote as const\n * The followRemoteHEAD lookup is done once, not duplicated in two branches\n * Clearer commit messages and docs: a working example plus a reference to\n   49ca2fba39, the goal stated first, an incorrect \"used to die\" claim\n   dropped, and an explanation of why a refmap turns on the inference\n * use_limited_fetch is a bool\n\nChanges in v10:\n\n * Resolve merge conflict after master integrated\n   ch/fetch-followremotehead-lazy-validation.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.refmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   8 +++\n Documentation/fetch-options.adoc |  10 +++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  |  94 +++++++++++++++++-------\n builtin/remote.c                 |  31 ++++++--\n remote.c                         |  60 +++++++++++++++-\n remote.h                         |  20 ++++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++++\n t/t5510-fetch.sh                 | 101 ++++++++++++++++++++++++++\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 11 files changed, 501 insertions(+), 33 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v11\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v11\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v10:\n\n 1:  ba7f1354cd ! 1:  d66b3c0bb1 fetch: add remote.<name>.refmap\n     @@ remote.c: void ref_push_report_free(struct ref_push_report *report)\n       int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n       {\n      -\treturn refspec_find_match(&remote->fetch, refspec);\n     -+\tif (remote->fetch.nr)\n     -+\t\treturn refspec_find_match(&remote->fetch, refspec);\n     -+\treturn refspec_find_match(&remote->refmap, refspec);\n     ++\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n     ++\n     ++\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n     ++\t\treturn 0;\n     ++\tif (refspec_find_match(&remote->refmap, refspec))\n     ++\t\treturn -1;\n     ++\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n     ++\t\tFREE_AND_NULL(*result);\n     ++\t\treturn -1;\n     ++\t}\n     ++\treturn 0;\n       }\n       \n       static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t\tEOF\n      +\t)\n      +'\n     ++\n     ++test_expect_success 'remote.<name>.refmap tracks a branch outside remote.<name>.fetch' '\n     ++\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n     ++\tgit init -b main fetch-refmap-upstream &&\n     ++\ttest_commit -C fetch-refmap-upstream base &&\n     ++\tgit -C fetch-refmap-upstream branch other &&\n     ++\tgit init downstream &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n     ++\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit fetch origin other &&\n     ++\t\tgit branch --track topic origin/other &&\n     ++\t\ttest_cmp_config origin branch.topic.remote &&\n     ++\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n     ++\t\techo refs/remotes/origin/other >expect &&\n     ++\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n     ++\n     ++test_expect_success 'remote.<name>.refmap does not track a branch excluded by remote.<name>.fetch' '\n     ++\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n     ++\tgit init -b main fetch-refmap-upstream &&\n     ++\ttest_commit -C fetch-refmap-upstream base &&\n     ++\tgit -C fetch-refmap-upstream branch secret &&\n     ++\tgit init downstream &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\tgit remote add origin ../fetch-refmap-upstream &&\n     ++\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n     ++\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit fetch origin secret &&\n     ++\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n     ++\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n     ++\t\ttest_grep \"not a branch\" err\n     ++\t)\n     ++'\n      +\n       test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n       \tgit branch -f side &&\n 2:  aa20604144 ! 2:  f685051033 fetch: extract collect_upstream_from_remote() helper\n     @@ Metadata\n       ## Commit message ##\n          fetch: extract collect_upstream_from_remote() helper\n      \n     -    When a fetch has no refspec to work from, we fall back to guessing\n     -    what to fetch: the remote's configured fetch refspec if there is one,\n     -    and separately, whether the current branch's upstream lives on this\n     -    remote. The latter is independent of the former on purpose: a narrow\n     -    refspec (for example from \"git remote add -t\") can leave the current\n     -    branch's own upstream uncovered, and we still need to be able to fetch\n     -    it.\n     +    When a fetch has no refspec to work from, we decide what to fetch from\n     +    two sources: the remote's configured fetch refspec, and the current\n     +    branch's upstream, if it lives on that remote. For example, after\n     +\n     +        git clone --single-branch <url>\n     +        git switch -c work\n     +        git push -u origin work\n     +\n     +    the remote's refspec only covers \"main\", but a refspec-less \"git fetch\n     +    origin\" still fetches \"work\" into FETCH_HEAD, so that a plain \"git\n     +    pull\" finds the branch to merge. See 49ca2fba39 (fetch: add\n     +    branch.*.merge to default ref-prefix extension, 2022-09-08).\n      \n          That \"does this branch build on this remote\" check was inlined for\n          just the current branch. Move it into its own function,\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n       \t} else {\n      -\t\tstruct branch *branch = branch_get(NULL);\n     -+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\tstruct string_list_item *item;\n     - \n     +-\n       \t\tif (transport->remote->fetch.nr) {\n       \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     + \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \t\t\telse\n       \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      +\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n      +\t\t * make sure we can still fetch it regardless.\n      +\t\t */\n     -+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     -+\t\t\t\t\t      transport->remote, NULL);\n     -+\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -+\t\t\t\t    item->string);\n     -+\t\tstring_list_clear(&tracked, 0);\n     ++\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n     ++\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n       \n       \t\t/*\n       \t\t * If there are no refs specified to fetch, then we just\n     @@ remote.c: int branch_merge_matches(struct branch *branch,\n       }\n       \n      +void collect_upstream_from_remote(struct repository *repo,\n     -+\t\t\t\t   struct string_list *tracked,\n     -+\t\t\t\t   struct remote *remote,\n     -+\t\t\t\t   const char *refname)\n     ++\t\t\t\t   const struct remote *remote,\n     ++\t\t\t\t   const char *refname,\n     ++\t\t\t\t   struct strvec *tracked)\n      +{\n      +\tstruct branch *branch = repo_branch_get(repo, refname);\n      +\n     @@ remote.c: int branch_merge_matches(struct branch *branch,\n      +\t\treturn;\n      +\n      +\tfor (int i = 0; i < branch->merge_nr; i++)\n     -+\t\tstring_list_insert(tracked, branch->merge[i]->src);\n     ++\t\tstrvec_push(tracked, branch->merge[i]->src);\n      +}\n      +\n       __attribute__((format (printf,2,3)))\n     @@ remote.h: int branch_has_merge_config(struct branch *branch);\n      + * tracked. A NULL refname means the current branch.\n      + */\n      +void collect_upstream_from_remote(struct repository *repo,\n     -+\t\t\t\t   struct string_list *tracked,\n     -+\t\t\t\t   struct remote *remote,\n     -+\t\t\t\t   const char *refname);\n     ++\t\t\t\t   const struct remote *remote,\n     ++\t\t\t\t   const char *refname,\n     ++\t\t\t\t   struct strvec *tracked);\n      +\n       /* list of the remote in a group as configured */\n       struct remote_group_data {\n 3:  9fb7e77a86 ! 3:  5c76b3b88d fetch: infer branches to fetch from a refmap-only remote\n     @@ Metadata\n       ## Commit message ##\n          fetch: infer branches to fetch from a refmap-only remote\n      \n     -    Configuring remote.<name>.refmap without remote.<name>.fetch used to\n     -    make a refspec-less \"git fetch <name>\" fail with \"--refmap option is\n     -    only meaningful with command-line refspec(s)\", since a refmap only\n     -    says where to put fetched refs, not what to fetch.\n     +    We want remote.<name>.refmap to work on its own, without also setting\n     +    remote.<name>.fetch, so a remote can be limited to the branches\n     +    already in use locally, without listing them by hand or touching the\n     +    command line every time.\n      \n     -    Make that case infer what to fetch: the local branches whose\n     -    @{upstream} is already on that remote. This lets a remote be\n     -    configured to fetch only the branches actually in use, without\n     -    listing them by hand in remote.<name>.fetch, and without needing to\n     -    touch the command line every time.\n     +    Infer what to fetch from the local branches whose @{upstream} is\n     +    already on that remote. The refmap itself still only controls where\n     +    those fetched refs land, not which branches are selected.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/config/remote.adoc: remote.<name>.fetch::\n      -\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n      +\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n      +\tfetch infers what to fetch from local branches built on this\n     -+\tremote, instead of fetching every branch it has. See the\n     -+\t`--refmap` entry in linkgit:git-fetch[1].\n     ++\tremote, instead of needing `remote.<name>.fetch` listed by hand.\n     ++\tSee the `--refmap` entry in linkgit:git-fetch[1].\n       \n       remote.<name>.push::\n       \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n     @@ Documentation/fetch-options.adoc: endif::git-pull[]\n      -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n      +When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n      +nothing to fetch is specified on the command line, nor is there a\n     -+`remote.<name>.fetch`, branches from the remote that are used as the\n     -+`@{upstream}` of our local branches are fetched.\n     ++`remote.<name>.fetch`, infer what to fetch from local branches built on\n     ++this remote: remote-tracking branches from this remote that are also\n     ++configured as the `@{upstream}` of a local branch are fetched. The\n     ++refmap only decides where those fetched refs land, the same as it\n     ++would for an explicit refspec, and does not narrow which branches are\n     ++selected. It is what turns this on, since without it the fetched\n     ++branches would have nowhere to land.\n       \n       `-t`::\n       `--tags`::\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \n      +\tif (!rs->nr && remote && !remote->fetch.nr &&\n      +\t    effective_refmap && effective_refmap->nr) {\n     -+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\tstruct string_list_item *item;\n     ++\t\tstruct strvec tracked = STRVEC_INIT;\n      +\n      +\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n     -+\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\trefspec_append(&inferred_rs, item->string);\n     -+\t\tstring_list_clear(&tracked, 0);\n     ++\t\tfor (size_t i = 0; i < tracked.nr; i++)\n     ++\t\t\trefspec_append(&inferred_rs, tracked.v[i]);\n     ++\t\tstrvec_clear(&tracked);\n      +\n      +\t\trs = &inferred_rs;\n      +\t\tinfer_from_refmap = 1;\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \treturn ref_map;\n       }\n       \n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \tstruct strmap rejected_refs = STRMAP_INIT;\n     + \tint summary_width = 0;\n     + \tint follow_remote_head = FOLLOW_REMOTE_NEVER;\n     ++\tbool apply_follow_remote_head_config = false;\n     + \n     + \tif (tags == TAGS_DEFAULT) {\n     + \t\tif (transport->remote->fetch_tags == 2)\n      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n       \n       \tif (rs->nr) {\n       \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n     +-\t} else {\n     +-\t\tif (transport->remote->fetch.nr) {\n     +-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     +-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     +-\t\t\t/*\n     +-\t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n     +-\t\t\t * for the analogous, still-unresolved case.\n     +-\t\t\t */\n     +-\t\t\tif (transport->remote->follow_remote_head)\n     +-\t\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n     +-\t\t\telse if (config->follow_remote_head_seen)\n     +-\t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n     +-\t\t\telse\n     +-\t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n     +-\t\t}\n      +\t} else if (transport->remote->fetch.nr) {\n     -+\t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     -+\t\tstruct string_list_item *item;\n     -+\n      +\t\trefspec_ref_prefixes(&transport->remote->fetch,\n      +\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     ++\t\tapply_follow_remote_head_config = true;\n     + \n     + \t\t/*\n     + \t\t * The configured refspec may not cover the current\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t */\n     + \t\tcollect_upstream_from_remote(the_repository, transport->remote,\n     + \t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n     ++\t} else {\n     ++\t\t/*\n     ++\t\t * The --refmap command line option, if given, takes\n     ++\t\t * precedence over remote.<name>.refmap.\n     ++\t\t */\n     ++\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n     ++\t\t\t&transport->remote->refmap;\n     ++\n     ++\t\tif (effective_refmap->nr) {\n     ++\t\t\tbranches_tracking_remote(the_repository, transport->remote,\n     ++\t\t\t\t\t\t  &transport_ls_refs_options.ref_prefixes);\n     ++\t\t\tapply_follow_remote_head_config = true;\n     ++\t\t} else {\n     ++\t\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n     ++\t\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n     ++\t\t}\n     + \n     + \t\t/*\n     + \t\t * If there are no refs specified to fetch, then we just\n     +@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     + \t\t\t\t    \"HEAD\");\n     + \t}\n     + \n     ++\tif (apply_follow_remote_head_config) {\n      +\t\t/*\n      +\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n      +\t\t * for the analogous, still-unresolved case.\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      +\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n      +\t\telse\n      +\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n     ++\t}\n      +\n     -+\t\t/*\n     -+\t\t * The configured refspec may not cover the current\n     -+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     -+\t\t * make sure we can still fetch it regardless.\n     -+\t\t */\n     -+\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     -+\t\t\t\t\t      transport->remote, NULL);\n     -+\t\tfor_each_string_list_item(item, &tracked)\n     -+\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     -+\t\t\t\t    item->string);\n     -+\t\tstring_list_clear(&tracked, 0);\n     - \t} else {\n     -+\t\t/*\n     -+\t\t * The --refmap command line option, if given, takes\n     -+\t\t * precedence over remote.<name>.refmap.\n     -+\t\t */\n     -+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n     -+\t\t\t&transport->remote->refmap;\n     - \t\tstruct string_list tracked = STRING_LIST_INIT_DUP;\n     - \t\tstruct string_list_item *item;\n     - \n     --\t\tif (transport->remote->fetch.nr) {\n     --\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n     --\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n     -+\t\tif (effective_refmap->nr) {\n     -+\t\t\tbranches_tracking_remote(the_repository,\n     -+\t\t\t\t\t\t  transport->remote, &tracked);\n     - \t\t\t/*\n     - \t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n     - \t\t\t * for the analogous, still-unresolved case.\n     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n     - \t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n     - \t\t\telse\n     - \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n     -+\t\t} else {\n     -+\t\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     -+\t\t\t\t\t\t      transport->remote, NULL);\n     - \t\t}\n     --\n     --\t\t/*\n     --\t\t * The configured refspec may not cover the current\n     --\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n     --\t\t * make sure we can still fetch it regardless.\n     --\t\t */\n     --\t\tcollect_upstream_from_remote(the_repository, &tracked,\n     --\t\t\t\t\t      transport->remote, NULL);\n     - \t\tfor_each_string_list_item(item, &tracked)\n     - \t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     - \t\t\t\t    item->string);\n     + \tif (tags == TAGS_SET || tags == TAGS_DEFAULT)\n     + \t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n     + \t\t\t    \"refs/tags/\");\n      \n       ## remote.c ##\n      @@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n     - \t\tstring_list_insert(tracked, branch->merge[i]->src);\n     + \t\tstrvec_push(tracked, branch->merge[i]->src);\n       }\n       \n      +struct branches_tracking_remote_cb_data {\n      +\tstruct repository *repo;\n     -+\tstruct remote *remote;\n     -+\tstruct string_list *tracked;\n     ++\tconst struct remote *remote;\n     ++\tstruct strvec *tracked;\n      +};\n      +\n      +static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n      +{\n      +\tstruct branches_tracking_remote_cb_data *data = cb_data;\n      +\n     -+\tcollect_upstream_from_remote(data->repo, data->tracked, data->remote,\n     -+\t\t\t\t      ref->name);\n     ++\tcollect_upstream_from_remote(data->repo, data->remote, ref->name,\n     ++\t\t\t\t      data->tracked);\n      +\treturn 0;\n      +}\n      +\n     -+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n     -+\t\t\t       struct string_list *tracked)\n     ++void branches_tracking_remote(struct repository *repo,\n     ++\t\t\t       const struct remote *remote,\n     ++\t\t\t       struct strvec *tracked)\n      +{\n      +\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n      +\n     @@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n      \n       ## remote.h ##\n      @@ remote.h: void collect_upstream_from_remote(struct repository *repo,\n     - \t\t\t\t   struct remote *remote,\n     - \t\t\t\t   const char *refname);\n     + \t\t\t\t   const char *refname,\n     + \t\t\t\t   struct strvec *tracked);\n       \n     -+/* fills tracked with the refname of every local branch's upstream on remote */\n     -+void branches_tracking_remote(struct repository *repo, struct remote *remote,\n     -+\t\t\t       struct string_list *tracked);\n     ++/* Same as collect_upstream_from_remote(), but checked for every local branch. */\n     ++void branches_tracking_remote(struct repository *repo,\n     ++\t\t\t       const struct remote *remote,\n     ++\t\t\t       struct strvec *tracked);\n      +\n       /* list of the remote in a group as configured */\n       struct remote_group_data {\n 4:  80028d4f86 ! 4:  417f14d708 remote: add \"git remote add --limited-fetch\"\n     @@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix\n       \trepo_config_set(the_repository, buf.buf, url);\n       \n       \tif (!mirror || mirror & MIRROR_FETCH) {\n     -+\t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n     ++\t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n      +\t\t\tlimited_fetch == 1;\n      +\n       \t\tstrbuf_reset(&buf);\n 5:  552bf08971 ! 5:  de0daa8623 remote: default to --limited-fetch in a shallow repository\n     @@ Metadata\n       ## Commit message ##\n          remote: default to --limited-fetch in a shallow repository\n      \n     -    Adding a second remote to a shallow, single-branch clone used to\n     -    still fetch every branch that remote has, since \"git remote add\"\n     -    always set up a wildcard remote.<name>.fetch refspec regardless of\n     -    how shallow the repository already was. That defeats the purpose of\n     -    having cloned shallow and single-branch in the first place, and can\n     -    make a plain \"git fetch\" on that remote hang or take a very long\n     +    Adding a second remote to a shallow clone used to still fetch every\n     +    branch that remote has, since \"git remote add\" always set up a\n     +    wildcard remote.<name>.fetch refspec regardless of how shallow the\n     +    repository already was. That defeats the purpose of having cloned\n     +    shallow in the first place (for example with --single-branch), and\n     +    can make a plain \"git fetch\" on that remote hang or take a very long\n          time on a repository with many branches.\n      \n          Turn --limited-fetch on by default when the repository is already\n     @@ builtin/remote.c\n      @@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix,\n       \n       \tif (!mirror || mirror & MIRROR_FETCH) {\n     - \t\tint use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n     + \t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n      -\t\t\tlimited_fetch == 1;\n      +\t\t\t(limited_fetch == 1 ||\n      +\t\t\t (limited_fetch == -1 &&\n\n-- \ngitgitgadget\n\n"},{"id":"556230","messageId":"d66b3c0bb1b04c59412b80eba8be50c30c934f14.1792959921.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","subject":"[PATCH v11 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:17Z","receivedAt":"2026-10-25T20:25:17Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.refmap, that provides\nthe default value for --refmap the same way remote.<name>.fetch\nalready provides the default refspecs to fetch. Like --refmap itself,\nit only maps refs that are actually being fetched, so it has nothing\nto do when there is nothing explicit to fetch, on the command line or\nvia remote.<name>.fetch.\n\nA refspec-less fetch with a refmap configured but nothing else to say\nwhat to fetch falls back to the same defaults as a remote with no\nrefspec or refmap at all. Only an explicit --refmap given on the\ncommand line with no command-line refspec to go with it is still\nrejected, since that combination is a plain mistake to type, unlike a\nremote.<name>.refmap configured on its own. A remote.<name>.fetch\nthat is also configured is used as before.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 ++\n Documentation/fetch-options.adoc |   3 +\n builtin/fetch.c                  |  10 ++-\n remote.c                         |  20 +++++-\n remote.h                         |   6 ++\n t/t5510-fetch.sh                 | 101 +++++++++++++++++++++++++++++++\n 6 files changed, 142 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..103eada406 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,11 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.refmap::\n+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n+\tUsed to map remote refs being fetched to remote-tracking refs to\n+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 1f76ae6242..ffcb5ba29a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.refmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -561,8 +567,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \ndiff --git a/remote.c b/remote.c\nindex 49ee2ea318..06c61bbc5f 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"refmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n+\n+\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n+\t\treturn 0;\n+\tif (refspec_find_match(&remote->refmap, refspec))\n+\t\treturn -1;\n+\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n+\t\tFREE_AND_NULL(*result);\n+\t\treturn -1;\n+\t}\n+\treturn 0;\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 17c0be499c..994327c55b 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,107 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.refmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.refmap plays no part in deciding what to\n+\t\t# fetch, only in remapping something that is already being\n+\t\t# fetched by name.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.refmap tracks a branch outside remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin other &&\n+\t\tgit branch --track topic origin/other &&\n+\t\ttest_cmp_config origin branch.topic.remote &&\n+\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n+\t\techo refs/remotes/origin/other >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.refmap does not track a branch excluded by remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch secret &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin secret &&\n+\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n+\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n+\t\ttest_grep \"not a branch\" err\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"556231","messageId":"f6850510336cc69f84cc4c7230e2ec3a3fc44746.1792959921.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","subject":"[PATCH v11 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:18Z","receivedAt":"2026-10-25T20:25:18Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we decide what to fetch from\ntwo sources: the remote's configured fetch refspec, and the current\nbranch's upstream, if it lives on that remote. For example, after\n\n    git clone --single-branch <url>\n    git switch -c work\n    git push -u origin work\n\nthe remote's refspec only covers \"main\", but a refspec-less \"git fetch\norigin\" still fetches \"work\" into FETCH_HEAD, so that a plain \"git\npull\" finds the branch to merge. See 49ca2fba39 (fetch: add\nbranch.*.merge to default ref-prefix extension, 2022-09-08).\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 18 ++++++++----------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 32 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex ffcb5ba29a..51bd5b0838 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1979,8 +1979,6 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n-\n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n@@ -1995,14 +1993,14 @@ static int do_fetch(struct transport *transport,\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 06c61bbc5f..43441945cf 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstrvec_push(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..003c8fd180 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"556232","messageId":"5c76b3b88d3997e0fd9ae259d8fd5499560a1cb3.1792959921.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","subject":"[PATCH v11 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:19Z","receivedAt":"2026-10-25T20:25:19Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWe want remote.<name>.refmap to work on its own, without also setting\nremote.<name>.fetch, so a remote can be limited to the branches\nalready in use locally, without listing them by hand or touching the\ncommand line every time.\n\nInfer what to fetch from the local branches whose @{upstream} is\nalready on that remote. The refmap itself still only controls where\nthose fetched refs land, not which branches are selected.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   5 +-\n Documentation/fetch-options.adoc |  11 ++-\n builtin/fetch.c                  |  72 ++++++++++++++-----\n remote.c                         |  25 +++++++\n remote.h                         |   5 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 119 +++++++++++++++++++++++++++++++\n 7 files changed, 219 insertions(+), 19 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 103eada406..d524d599bc 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -36,7 +36,10 @@ remote.<name>.fetch::\n remote.<name>.refmap::\n \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n \tUsed to map remote refs being fetched to remote-tracking refs to\n-\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n+\tfetch infers what to fetch from local branches built on this\n+\tremote, instead of needing `remote.<name>.fetch` listed by hand.\n+\tSee the `--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..9c86570f8d 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,15 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, infer what to fetch from local branches built on\n+this remote: remote-tracking branches from this remote that are also\n+configured as the `@{upstream}` of a local branch are fetched. The\n+refmap only decides where those fetched refs land, the same as it\n+would for an explicit refspec, and does not narrow which branches are\n+selected. It is what turns this on, since without it the fetched\n+branches would have nowhere to land.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 51bd5b0838..ad676985b4 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -531,15 +533,31 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct strvec tracked = STRVEC_INIT;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor (size_t i = 0; i < tracked.nr; i++)\n+\t\t\trefspec_append(&inferred_rs, tracked.v[i]);\n+\t\tstrvec_clear(&tracked);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -574,6 +592,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -669,6 +689,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1961,6 +1982,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head = FOLLOW_REMOTE_NEVER;\n+\tbool apply_follow_remote_head_config = false;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -1978,21 +2000,10 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n-\t} else {\n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\t/*\n-\t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n-\t\t\t * for the analogous, still-unresolved case.\n-\t\t\t */\n-\t\t\tif (transport->remote->follow_remote_head)\n-\t\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n-\t\t\telse if (config->follow_remote_head_seen)\n-\t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n-\t\t\telse\n-\t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n-\t\t}\n+\t} else if (transport->remote->fetch.nr) {\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tapply_follow_remote_head_config = true;\n \n \t\t/*\n \t\t * The configured refspec may not cover the current\n@@ -2001,6 +2012,22 @@ static int do_fetch(struct transport *transport,\n \t\t */\n \t\tcollect_upstream_from_remote(the_repository, transport->remote,\n \t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.refmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t  &transport_ls_refs_options.ref_prefixes);\n+\t\t\tapply_follow_remote_head_config = true;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t\t}\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\n@@ -2011,6 +2038,19 @@ static int do_fetch(struct transport *transport,\n \t\t\t\t    \"HEAD\");\n \t}\n \n+\tif (apply_follow_remote_head_config) {\n+\t\t/*\n+\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n+\t\t * for the analogous, still-unresolved case.\n+\t\t */\n+\t\tif (transport->remote->follow_remote_head)\n+\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n+\t\telse if (config->follow_remote_head_seen)\n+\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n+\t\telse\n+\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\t}\n+\n \tif (tags == TAGS_SET || tags == TAGS_DEFAULT)\n \t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t    \"refs/tags/\");\ndiff --git a/remote.c b/remote.c\nindex 43441945cf..d9a14114a1 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstrvec_push(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tconst struct remote *remote;\n+\tstruct strvec *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->remote, ref->name,\n+\t\t\t\t      data->tracked);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 003c8fd180..6fd9b05a79 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   const char *refname,\n \t\t\t\t   struct strvec *tracked);\n \n+/* Same as collect_upstream_from_remote(), but checked for every local branch. */\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 11b3752f00..0953a4112f 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -736,6 +736,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..b81fc48cbe\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,119 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.refmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"556233","messageId":"417f14d70891957aa9fac296b63c461e9fda8e95.1792959921.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","subject":"[PATCH v11 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:20Z","receivedAt":"2026-10-25T20:25:20Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.refmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 315100409d..f8764394e7 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.refmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex fb7e5b114f..7e81a91aef 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..0168d5abfe 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"556234","messageId":"de0daa8623b76c47b5c822d0aaa4287950f5c780.1792959921.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com","subject":"[PATCH v11 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-25T20:25:21Z","receivedAt":"2026-10-25T20:25:21Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow clone used to still fetch every\nbranch that remote has, since \"git remote add\" always set up a\nwildcard remote.<name>.fetch refspec regardless of how shallow the\nrepository already was. That defeats the purpose of having cloned\nshallow in the first place (for example with --single-branch), and\ncan make a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex f8764394e7..f43d707c91 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.refmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 7e81a91aef..0104f23761 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 0168d5abfe..f3b5905e9e 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.refmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.refmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"556236","messageId":"xmqq33ttxw9h.fsf@gitster.g","threadId":"66353","inReplyTo":"CALnO6CBAEB8=FfZfde0OWvfLJikVkXQ8itP9wZpGHxfxK+y_LQ@mail.gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-25T22:15:06Z","receivedAt":"2026-10-25T22:15:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"D. Ben Knoble\" <ben.knoble@gmail.com> writes:\n\n> That does look like it would fall back on the refmap, but to be honest\n> I'm not sure what is \"correct\" here :) I was hoping for more\n> discussion of the questions I raised to see some thought about what we\n> should or shouldn't do. I suppose falling back might be important for\n> the later step with --limited-fetch, though?\n\nI just encountered a case where I wished to configure both refmap\nand fetch at the same time.  From a second remote (let's call it\n\"cabal\"), store any branch fetched from them in refs/remotes/cabal/\nbut I want to fetch only their 'master', 'next' and 'seen'.  I'd\nwant to be able to write something like:\n\n\t[remote \"cabal\"]\n\t\tfetch = master next seen\n\t\trefmap = +refs/heads/*:refs/remotes/cabal/*\n\nAnd, no, this does not require any inferring based on what is\nchecked out and what the checked out branch tracks.\n\n"},{"id":"556273","messageId":"CAHwyqnW=W=XnRs210h1VxA5KWxdObwihCty6Gthiwh6+hDkD8w@mail.gmail.com","threadId":"66353","inReplyTo":"xmqq33ttxw9h.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-26T08:21:04Z","receivedAt":"2026-10-26T08:21:04Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > That does look like it would fall back on the refmap, but to be honest\n> > I'm not sure what is \"correct\" here :) I was hoping for more\n> > discussion of the questions I raised to see some thought about what we\n> > should or shouldn't do. I suppose falling back might be important for\n> > the later step with --limited-fetch, though?\n>\n> I just encountered a case where I wished to configure both refmap\n> and fetch at the same time.  From a second remote (let's call it\n> \"cabal\"), store any branch fetched from them in refs/remotes/cabal/\n> but I want to fetch only their 'master', 'next' and 'seen'.  I'd\n> want to be able to write something like:\n>\n>         [remote \"cabal\"]\n>                 fetch = master next seen\n>                 refmap = +refs/heads/*:refs/remotes/cabal/*\n>\n> And, no, this does not require any inferring based on what is\n> checked out and what the checked out branch tracks.\n\nYes, I think we can handle this as well with some extra code.\n\n\nHarald\n\n"},{"id":"556294","messageId":"101723fc-d392-4000-b224-345c610b2aaa@gmail.com","threadId":"66353","inReplyTo":"xmqqcxt02eoa.fsf@gitster.g","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-26T09:43:07Z","receivedAt":"2026-10-26T09:43:07Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 24/10/2026 00:13, Junio C Hamano wrote:\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> > This topic seeems fairly well done, without seeing any additional\n> comments in the last two rounds, but I am highly disturbed that I\n> was practically the only person who reviewd its later iterations.\n> Because I am not a target audience of the feature, I would not be\n> surprised if I am missing something large.\n> > I would very much appreciate if somebody else can give the series\n> one more read and Ack them, so that we can move it forward.\n\nI've just read through the commit messages and documentation changes and I think it is a mistake to use a single config variable to configure where fetched refs are stored and which refs are fetched. The problem this series tries to solve is that the default refspec fetches all the branches from the remote and it's not possible to use that mapping and also restrict which branches are fetched. To fix that it introduces a new config setting to configure where the refs are stored which is good, but it then uses the presence of that setting to implicitly configure the default set of refs to fetch. To me that repeats the mistake of the existing fetch config.\n\nIt would be much more flexible if we had a separate config setting (maybe remote.<name>.fetchRefs) to describe which refs are fetched. That setting could accept something like \"*@{upstream}\" to \"mean fetch the upstream branches of my local branches that live on this remote\" which would cover what this series wants. I think it would be useful to support \"+\" and \"^\" prefixes on the refs to fetch as well, so that the refmap config controls the mapping and the \"what to fetch\" config handles negation and forced updates on a per-ref basis.\n\nSetting\n\n    remote.<name>.fetchRefmap refs/heads/*:refs/remotes/<name>/*\n    remote.<name>.fetchRefs +refs/heads/*\n\nwould be equivalent to\n\n    remote.<name>.fetch +refs/heads/*:refs/remotes/<name>/*\n\nIdeally when we're seeing which refs to fetch we'd use the last entry that matches so that\n\n   remote.<name>.fetchRefs +refs/head/*\n   remote.<name>.fetchRefs refs/heads/master\n\nwould refuse non-fast-forward updates for master. Currently we use the first refspec we find that matches which goes against the usual \"last one wins\" behavior for config settings and makes it impossible to add per-branch refspecs with \"git config\" because the default setting comes first in the config file and matches all branches.\n\nThanks\n\nPhillip\n\n\n"},{"id":"556295","messageId":"e6af5d1e-4ca1-48a5-8a84-5e0cefbdbc2b@gmail.com","threadId":"66353","inReplyTo":"ba7f1354cd5492ef964ad4d1d398b5ecfc4e0086.1792503778.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-26T09:44:36Z","receivedAt":"2026-10-26T09:44:36Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> > Add a per-remote config variable, remote.<name>.refmap, that provides\n> the default value for --refmap the same way remote.<name>.fetch\n> already provides the default refspecs to fetch.\n\nThe second half of this sentence seems redundant to me. Can we please call the new config setting \"remote.<name>.fetchRefmap\" so that it is clear it applies to fetches. That would also allow us to add a matching \"remote.<name>.pushRefmap\" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push.\n\n> Like --refmap itself,\n> it only maps refs that are actually being fetched, so it has nothing\n> to do when there is nothing explicit to fetch, on the command line or\n> via remote.<name>.fetch.\n\nSo the default value behaves like the option - why wouldn't it?\n\n> A refspec-less fetch with a refmap configured but nothing else to say\n> what to fetch falls back to the same defaults as a remote with no\n> refspec or refmap at all. Only an explicit --refmap given on the\n> command line with no command-line refspec to go with it is still\n> rejected, since that combination is a plain mistake to type, unlike a\n> remote.<name>.refmap configured on its own. A remote.<name>.fetch\n> that is also configured is used as before.\n\nI'm afraid I cannot understand what this paragraph is trying to say.\n\n> diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\n>   > +remote.<name>.refmap::\n> +\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n> +\tUsed to map remote refs being fetched to remote-tracking refs to\n> +\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n\nI'm not sure we need to link to the git fetch documentation twice. It would be useful to explain how this interacts with remote.<name>.fetch. The documentation for \"--refmap\" says that if it is given the refspec config is ignored - so does setting this mean remote.<name>.fetch is ignored?\n\n> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> ++\n> +`remote.<name>.refmap` provides the default value for this option, the\n> +same way `remote.<name>.fetch` provides the default refspecs to fetch.\n\nAs above the second half of this sentence doesn't really add anything.\n\nThanks\n\nPhillip\n\n>   >   `-t`::\n>   `--tags`::\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 1f76ae6242..ffcb5ba29a 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \tstruct ref *rm;\n>   \tstruct ref *ref_map = NULL;\n>   \tstruct ref **tail = &ref_map;\n> +\t/*\n> +\t * The --refmap command line option, if given, takes precedence\n> +\t * over remote.<name>.refmap.\n> +\t */\n> +\tstruct refspec *effective_refmap =\n> +\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n>   >   \t/* opportunistically-updated references: */\n>   \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n> @@ -561,8 +567,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \t\t * by ref_remove_duplicates() in favor of one of these\n>   \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n>   \t\t */\n> -\t\tif (refmap.nr)\n> -\t\t\tfetch_refspec = &refmap;\n> +\t\tif (effective_refmap && effective_refmap->nr)\n> +\t\t\tfetch_refspec = effective_refmap;\n>   \t\telse\n>   \t\t\tfetch_refspec = &remote->fetch;\n>   > diff --git a/remote.c b/remote.c\n> index 49ee2ea318..c9252a90f4 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n>   \tret->name = xstrndup(name, len);\n>   \trefspec_init_push(&ret->push, the_hash_algo);\n>   \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n> +\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n>   \tstring_list_init_dup(&ret->server_options);\n>   \tstring_list_init_dup(&ret->negotiation_restrict);\n>   \tstring_list_init_dup(&ret->negotiation_include);\n> @@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n>   >   \trefspec_clear(&remote->push);\n>   \trefspec_clear(&remote->fetch);\n> +\trefspec_clear(&remote->refmap);\n>   >   \tfree((char *)remote->receivepack);\n>   \tfree((char *)remote->uploadpack);\n> @@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n>   \t\t\treturn -1;\n>   \t\trefspec_append(&remote->fetch, v);\n>   \t\tfree(v);\n> +\t} else if (!strcmp(subkey, \"refmap\")) {\n> +\t\tchar *v;\n> +\t\tif (git_config_string(&v, key, value))\n> +\t\t\treturn -1;\n> +\t\trefspec_append(&remote->refmap, v);\n> +\t\tfree(v);\n>   \t} else if (!strcmp(subkey, \"receivepack\")) {\n>   \t\tchar *v;\n>   \t\tif (git_config_string(&v, key, value))\n> @@ -995,7 +1003,9 @@ void ref_push_report_free(struct ref_push_report *report)\n>   >   int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n>   {\n> -\treturn refspec_find_match(&remote->fetch, refspec);\n> +\tif (remote->fetch.nr)\n> +\t\treturn refspec_find_match(&remote->fetch, refspec);\n> +\treturn refspec_find_match(&remote->refmap, refspec);\n>   }\n>   >   static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n> diff --git a/remote.h b/remote.h\n> index cca02033b9..ac485a584d 100644\n> --- a/remote.h\n> +++ b/remote.h\n> @@ -90,6 +90,12 @@ struct remote {\n>   >   \tstruct refspec fetch;\n>   > +\t/*\n> +\t * How to map refs fetched without an explicit destination into our\n> +\t * own namespace, the same as the --refmap command line option.\n> +\t */\n> +\tstruct refspec refmap;\n> +\n>   \t/*\n>   \t * The setting for whether to fetch tags (as a separate rule from the\n>   \t * configured refspecs);\n> diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\n> index 17c0be499c..ec5cd9dca6 100755\n> --- a/t/t5510-fetch.sh\n> +++ b/t/t5510-fetch.sh\n> @@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n>   \t)\n>   '\n>   > +test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n> +\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n> +\tgit branch -f side &&\n> +\tgit -C three config remote.origin.refmap \\\n> +\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n> +\t(\n> +\t\tcd three &&\n> +\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n> +\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n> +\t\tgit fetch origin main &&\n> +\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n> +\t\ttest \"$o\" = \"$n\" &&\n> +\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n> +\t\tgit rev-parse --verify refs/remotes/other/main\n> +\t)\n> +'\n> +\n> +check_fetched_refs () {\n> +\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n> +\tcat >expect &&\n> +\ttest_cmp expect actual\n> +}\n> +\n> +test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n> +\tgit init -b main fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit -C fetch-refmap-upstream branch other &&\n> +\tgit init fetch-refmap-baseline &&\n> +\t(\n> +\t\tcd fetch-refmap-baseline &&\n> +\t\tgit remote add origin ../fetch-refmap-upstream &&\n> +\n> +\t\t# Without fetch refspec, but with fetch refmap.\n> +\t\tgit config --unset-all remote.origin.fetch &&\n> +\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\n> +\t\t# Nothing tracked, nothing fetched, no error.\n> +\t\tgit fetch origin &&\n> +\t\tcheck_fetched_refs <<-\\EOF &&\n> +\t\tEOF\n> +\n> +\t\t# Nothing tracked, explicit ref on the command line.\n> +\t\tgit fetch origin main &&\n> +\t\tcheck_fetched_refs <<-\\EOF &&\n> +\t\trefs/remotes/origin/main\n> +\t\tEOF\n> +\n> +\t\t# With both refmap and fetch configured, remote.<name>.fetch\n> +\t\t# wins: a refspec-less fetch follows it as usual, and\n> +\t\t# remote.<name>.refmap plays no part in deciding what to\n> +\t\t# fetch, only in remapping something that is already being\n> +\t\t# fetched by name.\n> +\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\t\tgit fetch origin &&\n> +\t\tcheck_fetched_refs <<-\\EOF\n> +\t\trefs/remotes/origin/HEAD\n> +\t\trefs/remotes/origin/main\n> +\t\trefs/remotes/origin/other\n> +\t\tEOF\n> +\t)\n> +'\n> +\n>   test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n>   \tgit branch -f side &&\n>   \t(\n\n\n"},{"id":"556305","messageId":"af521f7f-b05a-487e-9972-b8cc2dc02ef7@gmail.com","threadId":"66353","inReplyTo":"xmqq33ttxw9h.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-26T10:44:31Z","receivedAt":"2026-10-26T10:44:31Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 25/10/2026 22:15, Junio C Hamano wrote:\n> > I just encountered a case where I wished to configure both refmap\n> and fetch at the same time.  From a second remote (let's call it\n> \"cabal\"), store any branch fetched from them in refs/remotes/cabal/\n> but I want to fetch only their 'master', 'next' and 'seen'.  I'd\n> want to be able to write something like:\n> > \t[remote \"cabal\"]\n> \t\tfetch = master next seen\n> \t\trefmap = +refs/heads/*:refs/remotes/cabal/*\n\nThat's a coincidence, I sent a message earlier [1] arguing that we should have separate config settings for \"what to fetch\" and \"where to store it\"\n\nThanks\n\nPhillip\n\n[1] https://lore.kernel.org/git/e6af5d1e-4ca1-48a5-8a84-5e0cefbdbc2b@gmail.com\n> > And, no, this does not require any inferring based on what is\n> checked out and what the checked out branch tracks.\n\n\n"},{"id":"556358","messageId":"xmqqh5i8tqjx.fsf@gitster.g","threadId":"66353","inReplyTo":"101723fc-d392-4000-b224-345c610b2aaa@gmail.com","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-26T15:44:18Z","receivedAt":"2026-10-26T15:44:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> I've just read through the commit messages and documentation changes and \n> I think it is a mistake to use a single config variable to configure \n> where fetched refs are stored and which refs are fetched. The problem \n> this series tries to solve is that the default refspec fetches all the \n> branches from the remote and it's not possible to use that mapping and \n> also restrict which branches are fetched. To fix that it introduces a \n> new config setting to configure where the refs are stored which is good, \n> but it then uses the presence of that setting to implicitly configure \n> the default set of refs to fetch. To me that repeats the mistake of the \n> existing fetch config.\n>\n> It would be much more flexible if we had a separate config setting \n> (maybe remote.<name>.fetchRefs) to describe which refs are fetched.\n\nGood point.\n\nWhen I wrote the refmap feature (without configuration variable)\nlong time ago, one of the goals was to allow me to say\n\n    $ git fetch cabal \\\n        --refmap=\"+refs/heads/*:refs/remotes/cabal/*\" \\\n\tmaint master next\n\nThat is, \"here is the place I grab from there are stored\" and\n\"maint master next are the things I grab from there\" are specified\nseparately.  With the configuration variable added in this series,\n\n    $ git config set remote.cabal.refmap \"+refs/heads/*:refs/remotes/cabal/*\"\n    $ git fetch cabal master next seen\n\nshould become possible with the same behaviour.  I am not sure\noffhand if this series does that, but I would consider it a bug if\nit does not.\n\nIn any case, this series, since it lacks the separate setting, takes\nthe lack of \"what to fetch\" on the command line (and perhaps lack of\nremote.cabal.fetch???) as a sign to trigger this new feature to\nguess what to fetch based on what is tracked by the local branches.\n\nFor now, as this \"what is tracked by the local branches\" is the only\nkind of smart that was designed to decide what to fetch, it may be OK\nto use \"you gave me nothing to specify what to fetch, so I'll guess\nusing the smart\".  But when we introduce more kinds of smart later,\nthis becomes a problem.  So I do agree with you that we should have\nsomething to specifically say \"when I do not explicitly give you\nwhat to fetch, decide based on what local branches track\".\n\n> That \n> setting could accept something like \"*@{upstream}\" to \"mean fetch the \n> upstream branches of my local branches that live on this remote\" which \n> would cover what this series wants. I think it would be useful to \n> support \"+\" and \"^\" prefixes on the refs to fetch as well, so that the \n> refmap config controls the mapping and the \"what to fetch\" config \n> handles negation and forced updates on a per-ref basis.\n\nThe reasoning here is slightly wrong.  \"^\" (not) may be good thing\nto consider as it is to decide what to fetch, but \"+\" (overwrite)\nshould be on the refmap side, i.e., where to store.  If you are not\nstoring, then \"overwrite what I have with non-fast-forward\" becomes\na nonsense request, and that is why it does not belong to \"what to\nfetch\".\n\n"},{"id":"556364","messageId":"xmqqcxswtqd0.fsf@gitster.g","threadId":"66353","inReplyTo":"CAHwyqnW=W=XnRs210h1VxA5KWxdObwihCty6Gthiwh6+hDkD8w@mail.gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-26T15:48:27Z","receivedAt":"2026-10-26T15:48:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n>> > That does look like it would fall back on the refmap, but to be honest\n>> > I'm not sure what is \"correct\" here :) I was hoping for more\n>> > discussion of the questions I raised to see some thought about what we\n>> > should or shouldn't do. I suppose falling back might be important for\n>> > the later step with --limited-fetch, though?\n>>\n>> I just encountered a case where I wished to configure both refmap\n>> and fetch at the same time.  From a second remote (let's call it\n>> \"cabal\"), store any branch fetched from them in refs/remotes/cabal/\n>> but I want to fetch only their 'master', 'next' and 'seen'.  I'd\n>> want to be able to write something like:\n>>\n>>         [remote \"cabal\"]\n>>                 fetch = master next seen\n>>                 refmap = +refs/heads/*:refs/remotes/cabal/*\n>>\n>> And, no, this does not require any inferring based on what is\n>> checked out and what the checked out branch tracks.\n>\n> Yes, I think we can handle this as well with some extra code.\n\nI actually do not think we should have master next seen on the same\nremote.cabal.fetch configuration variable.  It is a multi-valued\nvariable, so it should read more like\n\n         [remote \"cabal\"]\n                 fetch = refs/heads/master\n                 fetch = refs/heads/next\n                 fetch = refs/heads/seen\n                 refmap = +refs/heads/*:refs/remotes/cabal/*\n\nAnd without these remote.cabal.fetch configured, but with\nremote.cabal.refmap configured, this command line\n\n\t$ git fetch cabal next\n\nshould work just like\n\n\t$ git fetch cabal \\\n\t\t--refmap=\"+refs/heads/*:refs/remotes/cabal/*\" \\\n\t\tnext\n\nI think.\n\nThanks.\n\n"},{"id":"556365","messageId":"xmqq8q3ktq8m.fsf@gitster.g","threadId":"66353","inReplyTo":"af521f7f-b05a-487e-9972-b8cc2dc02ef7@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-26T15:51:05Z","receivedAt":"2026-10-26T15:51:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> On 25/10/2026 22:15, Junio C Hamano wrote:\n>> \n>> I just encountered a case where I wished to configure both refmap\n>> and fetch at the same time.  From a second remote (let's call it\n>> \"cabal\"), store any branch fetched from them in refs/remotes/cabal/\n>> but I want to fetch only their 'master', 'next' and 'seen'.  I'd\n>> want to be able to write something like:\n>> \n>> \t[remote \"cabal\"]\n>> \t\tfetch = master next seen\n>> \t\trefmap = +refs/heads/*:refs/remotes/cabal/*\n>\n> That's a coincidence, I sent a message earlier [1] arguing that we \n> should have separate config settings for \"what to fetch\" and \"where to \n> store it\"\n\nIn other words, \"what to fetch\" and \"where to store them\" have\nseparate configuration.  They should be made to interact correctly\nand the current patchset may or may not work the way we want.\n\nWhat we need a separate setting is probably what should happen when\nwe do not specify \"what to fetch\" at all.  One heuristics is what\nthis series proposes i.e., \"what our local branches build on\".  But\nthat does not have to stay the only option forever.\n\n"},{"id":"556492","messageId":"CAHwyqnWBzrj=OVzRWWTwbSU-zG-t_2wbfg+cdTaCWgYstV78mA@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqh5i8tqjx.fsf@gitster.g","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-27T12:22:25Z","receivedAt":"2026-10-27T12:22:25Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > I've just read through the commit messages and documentation changes and\n> > I think it is a mistake to use a single config variable to configure\n> > where fetched refs are stored and which refs are fetched. The problem\n> > this series tries to solve is that the default refspec fetches all the\n> > branches from the remote and it's not possible to use that mapping and\n> > also restrict which branches are fetched. To fix that it introduces a\n> > new config setting to configure where the refs are stored which is good,\n> > but it then uses the presence of that setting to implicitly configure\n> > the default set of refs to fetch. To me that repeats the mistake of the\n> > existing fetch config.\n> >\n> > It would be much more flexible if we had a separate config setting\n> > (maybe remote.<name>.fetchRefs) to describe which refs are fetched.\n>\n> Good point.\n\nOk.\n\n> When I wrote the refmap feature (without configuration variable)\n> long time ago, one of the goals was to allow me to say\n>\n>     $ git fetch cabal \\\n>         --refmap=\"+refs/heads/*:refs/remotes/cabal/*\" \\\n>         maint master next\n>\n> That is, \"here is the place I grab from there are stored\" and\n> \"maint master next are the things I grab from there\" are specified\n> separately.  With the configuration variable added in this series,\n>\n>     $ git config set remote.cabal.refmap \"+refs/heads/*:refs/remotes/cabal/*\"\n>     $ git fetch cabal master next seen\n>\n> should become possible with the same behaviour.  I am not sure\n> offhand if this series does that, but I would consider it a bug if\n> it does not.\n\nIt will do that in my next version and I added a test specifically for this.\n\n\nHarald\n\n"},{"id":"556493","messageId":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v12 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:39Z","receivedAt":"2026-10-27T12:24:39Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v12:\n\n * Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout\n   code, docs, and tests, so it reads unambiguously as fetch-only.\n * Added the missing code path in get_ref_map() so a branch fetched by name\n   via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap,\n   with a new test (the \"cabal\" example: fetch = master/next/seen plus a\n   refmap storing them all under one namespace).\n * Reworded the fetchRefmap commit message and doc entry to drop a redundant\n   sentence, fix a confusing paragraph, and spell out that\n   remote.<name>.fetch, when configured, still decides what's fetched while\n   the refmap only remaps it.\n * Added a prefetch coverage test to t5586-fetch-refmap.sh.\n\nChanges in v11:\n\n * remote_find_tracking() now falls back to remote.<name>.refmap when\n   remote.<name>.fetch doesn't match. Negative refspecs in fetch still\n   exclude refs, and both cases have tests\n * The helpers write straight into a strvec, take the output parameter last\n   and the remote as const\n * The followRemoteHEAD lookup is done once, not duplicated in two branches\n * Clearer commit messages and docs: a working example plus a reference to\n   49ca2fba39, the goal stated first, an incorrect \"used to die\" claim\n   dropped, and an explanation of why a refmap turns on the inference\n * use_limited_fetch is a bool\n\nChanges in v10:\n\n * Resolve merge conflict after master integrated\n   ch/fetch-followremotehead-lazy-validation.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.fetchRefmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   9 +++\n Documentation/fetch-options.adoc |  10 +++\n Documentation/git-remote.adoc    |  14 +++-\n builtin/fetch.c                  | 100 +++++++++++++++++------\n builtin/remote.c                 |  31 +++++--\n remote.c                         |  60 +++++++++++++-\n remote.h                         |  20 +++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++++\n t/t5510-fetch.sh                 | 130 ++++++++++++++++++++++++++++++\n t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++++++\n 11 files changed, 551 insertions(+), 33 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 37b3475fb530c14109211faca33716a173db61ed\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v12\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v12\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v11:\n\n 1:  d66b3c0bb1 ! 1:  a1f5e81fdc fetch: add remote.<name>.refmap\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    fetch: add remote.<name>.refmap\n     +    fetch: add remote.<name>.fetchRefmap\n      \n     -    Add a per-remote config variable, remote.<name>.refmap, that provides\n     -    the default value for --refmap the same way remote.<name>.fetch\n     -    already provides the default refspecs to fetch. Like --refmap itself,\n     -    it only maps refs that are actually being fetched, so it has nothing\n     -    to do when there is nothing explicit to fetch, on the command line or\n     -    via remote.<name>.fetch.\n     +    Add a per-remote config variable, remote.<name>.fetchRefmap, that\n     +    provides the default value for --refmap the same way\n     +    remote.<name>.fetch already provides the default refspecs to fetch.\n      \n     -    A refspec-less fetch with a refmap configured but nothing else to say\n     -    what to fetch falls back to the same defaults as a remote with no\n     -    refspec or refmap at all. Only an explicit --refmap given on the\n     -    command line with no command-line refspec to go with it is still\n     -    rejected, since that combination is a plain mistake to type, unlike a\n     -    remote.<name>.refmap configured on its own. A remote.<name>.fetch\n     -    that is also configured is used as before.\n     +    Unlike passing --refmap on the command line, which requires an\n     +    explicit refspec to go with it, configuring remote.<name>.fetchRefmap\n     +    on its own does not change what a refspec-less fetch does: it falls\n     +    back to the same defaults as a remote with no fetchRefmap at all.\n     +\n     +    When remote.<name>.fetch is also configured, it still decides what to\n     +    fetch, and the refmap maps what it fetches. This lets a remote name\n     +    only a few branches and store them all through one refmap:\n     +\n     +        [remote \"cabal\"]\n     +            fetch = master\n     +            fetch = next\n     +            fetch = seen\n     +            fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n     +\n     +    Looking up the remote-tracking branch for a remote branch, as\n     +    @{upstream} and \"git branch --track\" do, falls back to the refmap when\n     +    remote.<name>.fetch has no match, so that these work for such\n     +    branches too. A branch excluded by a negative refspec in\n     +    remote.<name>.fetch stays excluded.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ Documentation/config/remote.adoc: remote.<name>.fetch::\n       \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n       \tlinkgit:git-fetch[1].\n       \n     -+remote.<name>.refmap::\n     -+\tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n     -+\tUsed to map remote refs being fetched to remote-tracking refs to\n     -+\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n     ++remote.<name>.fetchRefmap::\n     ++\tThe default value of the `--refmap` option, used to map refs being\n     ++\tfetched to remote-tracking refs to store, the same way\n     ++\t`remote.<name>.fetch` does for an explicit refspec.\n     ++\t`remote.<name>.fetch`, if also configured, still decides what is\n     ++\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n      +\n       remote.<name>.push::\n       \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \tstruct ref **tail = &ref_map;\n      +\t/*\n      +\t * The --refmap command line option, if given, takes precedence\n     -+\t * over remote.<name>.refmap.\n     ++\t * over remote.<name>.fetchRefmap.\n      +\t */\n      +\tstruct refspec *effective_refmap =\n      +\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n       \n       \t/* opportunistically-updated references: */\n       \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \tfilter_prefetch_refspec(rs);\n     + \tif (remote)\n     + \t\tfilter_prefetch_refspec(&remote->fetch);\n     ++\tif (effective_refmap)\n     ++\t\tfilter_prefetch_refspec(effective_refmap);\n     + \n     + \tif (rs->nr) {\n     + \t\tstruct refspec *fetch_refspec;\n      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \t\t * by ref_remove_duplicates() in favor of one of these\n       \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \t\telse\n       \t\t\tfetch_refspec = &remote->fetch;\n       \n     +@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n     + \t\t\tif (has_merge &&\n     + \t\t\t    !strcmp(branch->remote_name, remote->name))\n     + \t\t\t\tadd_merge_config(&ref_map, remote_refs, branch, &tail);\n     ++\n     ++\t\t\tfor (i = 0; i < remote->refmap.nr; i++)\n     ++\t\t\t\tget_fetch_map(ref_map, &remote->refmap.items[i],\n     ++\t\t\t\t\t      &oref_tail, 1);\n     + \t\t} else if (!prefetch) {\n     + \t\t\tref_map = get_remote_ref(remote_refs, \"HEAD\");\n     + \t\t\tif (!ref_map)\n      \n       ## remote.c ##\n      @@ remote.c: static struct remote *make_remote(struct remote_state *remote_state,\n     @@ remote.c: static int handle_config(const char *key, const char *value,\n       \t\t\treturn -1;\n       \t\trefspec_append(&remote->fetch, v);\n       \t\tfree(v);\n     -+\t} else if (!strcmp(subkey, \"refmap\")) {\n     ++\t} else if (!strcmp(subkey, \"fetchrefmap\")) {\n      +\t\tchar *v;\n      +\t\tif (git_config_string(&v, key, value))\n      +\t\t\treturn -1;\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n       \t)\n       '\n       \n     -+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '\n     -+\ttest_when_finished \"git -C three config --unset remote.origin.refmap\" &&\n     ++test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '\n     ++\ttest_when_finished \"git -C three config --unset remote.origin.fetchRefmap\" &&\n      +\tgit branch -f side &&\n     -+\tgit -C three config remote.origin.refmap \\\n     ++\tgit -C three config remote.origin.fetchRefmap \\\n      +\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n      +\t(\n      +\t\tcd three &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\ttest_cmp expect actual\n      +}\n      +\n     -+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '\n     ++test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '\n      +\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n      +\tgit init -b main fetch-refmap-upstream &&\n      +\ttest_commit -C fetch-refmap-upstream base &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\n      +\t\t# Without fetch refspec, but with fetch refmap.\n      +\t\tgit config --unset-all remote.origin.fetch &&\n     -+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n      +\n      +\t\t# Nothing tracked, nothing fetched, no error.\n      +\t\tgit fetch origin &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\n      +\t\t# With both refmap and fetch configured, remote.<name>.fetch\n      +\t\t# wins: a refspec-less fetch follows it as usual, and\n     -+\t\t# remote.<name>.refmap plays no part in deciding what to\n     -+\t\t# fetch, only in remapping something that is already being\n     -+\t\t# fetched by name.\n     ++\t\t# remote.<name>.fetchRefmap plays no part in deciding what to\n     ++\t\t# fetch, only in mapping what is fetched.\n      +\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n      +\t\tgit fetch origin &&\n      +\t\tcheck_fetched_refs <<-\\EOF\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t)\n      +'\n      +\n     -+test_expect_success 'remote.<name>.refmap tracks a branch outside remote.<name>.fetch' '\n     ++test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '\n     ++\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n     ++\tgit init -b master fetch-refmap-upstream &&\n     ++\ttest_commit -C fetch-refmap-upstream base &&\n     ++\tgit -C fetch-refmap-upstream branch next &&\n     ++\tgit -C fetch-refmap-upstream branch seen &&\n     ++\tgit -C fetch-refmap-upstream branch other &&\n     ++\tgit init downstream &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\tgit remote add cabal ../fetch-refmap-upstream &&\n     ++\t\tgit config --unset-all remote.cabal.fetch &&\n     ++\t\tgit config --add remote.cabal.fetch master &&\n     ++\t\tgit config --add remote.cabal.fetch next &&\n     ++\t\tgit config --add remote.cabal.fetch seen &&\n     ++\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n     ++\t\tgit fetch cabal &&\n     ++\t\tcheck_fetched_refs <<-\\EOF &&\n     ++\t\trefs/remotes/cabal/HEAD\n     ++\t\trefs/remotes/cabal/master\n     ++\t\trefs/remotes/cabal/next\n     ++\t\trefs/remotes/cabal/seen\n     ++\t\tEOF\n     ++\t\tgit branch --track topic cabal/next &&\n     ++\t\techo refs/remotes/cabal/next >expect &&\n     ++\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n     ++\n     ++test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n      +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n      +\tgit init -b main fetch-refmap-upstream &&\n      +\ttest_commit -C fetch-refmap-upstream base &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t(\n      +\t\tcd downstream &&\n      +\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n     -+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n      +\t\tgit fetch origin other &&\n      +\t\tgit branch --track topic origin/other &&\n      +\t\ttest_cmp_config origin branch.topic.remote &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t)\n      +'\n      +\n     -+test_expect_success 'remote.<name>.refmap does not track a branch excluded by remote.<name>.fetch' '\n     ++test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '\n      +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n      +\tgit init -b main fetch-refmap-upstream &&\n      +\ttest_commit -C fetch-refmap-upstream base &&\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t\tcd downstream &&\n      +\t\tgit remote add origin ../fetch-refmap-upstream &&\n      +\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n     -+\t\tgit config remote.origin.refmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n     ++\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n      +\t\tgit fetch origin secret &&\n      +\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n      +\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n 2:  f685051033 = 2:  e8af5bc76b fetch: extract collect_upstream_from_remote() helper\n 3:  5c76b3b88d ! 3:  aad6e5f66f fetch: infer branches to fetch from a refmap-only remote\n     @@ Metadata\n       ## Commit message ##\n          fetch: infer branches to fetch from a refmap-only remote\n      \n     -    We want remote.<name>.refmap to work on its own, without also setting\n     -    remote.<name>.fetch, so a remote can be limited to the branches\n     -    already in use locally, without listing them by hand or touching the\n     -    command line every time.\n     +    We want remote.<name>.fetchRefmap to work on its own, without also\n     +    setting remote.<name>.fetch, so a remote can be limited to the\n     +    branches already in use locally, without listing them by hand or\n     +    touching the command line every time.\n      \n          Infer what to fetch from the local branches whose @{upstream} is\n          already on that remote. The refmap itself still only controls where\n     @@ Commit message\n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/config/remote.adoc ##\n     -@@ Documentation/config/remote.adoc: remote.<name>.fetch::\n     - remote.<name>.refmap::\n     - \tThe default value of the `--refmap` option for linkgit:git-fetch[1].\n     - \tUsed to map remote refs being fetched to remote-tracking refs to\n     --\tstore. See the `--refmap` entry in linkgit:git-fetch[1].\n     -+\tstore. If `remote.<name>.fetch` is not set either, a refspec-less\n     -+\tfetch infers what to fetch from local branches built on this\n     -+\tremote, instead of needing `remote.<name>.fetch` listed by hand.\n     +@@ Documentation/config/remote.adoc: remote.<name>.fetchRefmap::\n     + \tfetched to remote-tracking refs to store, the same way\n     + \t`remote.<name>.fetch` does for an explicit refspec.\n     + \t`remote.<name>.fetch`, if also configured, still decides what is\n     +-\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n     ++\tfetched. If it is not set either, a refspec-less fetch instead\n     ++\tinfers what to fetch from local branches built on this remote.\n      +\tSee the `--refmap` entry in linkgit:git-fetch[1].\n       \n       remote.<name>.push::\n     @@ Documentation/fetch-options.adoc: endif::git-pull[]\n       +\n      -`remote.<name>.refmap` provides the default value for this option, the\n      -same way `remote.<name>.fetch` provides the default refspecs to fetch.\n     -+When a refmap is active (from `--refmap` or `remote.<name>.refmap`) but\n     ++When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but\n      +nothing to fetch is specified on the command line, nor is there a\n      +`remote.<name>.fetch`, infer what to fetch from local branches built on\n      +this remote: remote-tracking branches from this remote that are also\n     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,\n       \tfilter_prefetch_refspec(rs);\n       \tif (remote)\n       \t\tfilter_prefetch_refspec(&remote->fetch);\n     + \tif (effective_refmap)\n     + \t\tfilter_prefetch_refspec(effective_refmap);\n       \n      +\tif (!rs->nr && remote && !remote->fetch.nr &&\n      +\t    effective_refmap && effective_refmap->nr) {\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      +\t} else {\n      +\t\t/*\n      +\t\t * The --refmap command line option, if given, takes\n     -+\t\t * precedence over remote.<name>.refmap.\n     ++\t\t * precedence over remote.<name>.fetchRefmap.\n      +\t\t */\n      +\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n      +\t\t\t&transport->remote->refmap;\n     @@ t/t5586-fetch-refmap.sh (new)\n      @@\n      +#!/bin/sh\n      +\n     -+test_description='\"git fetch\" with a remote.<name>.refmap but no remote.<name>.fetch\n     ++test_description='\"git fetch\" with a remote.<name>.fetchRefmap but no remote.<name>.fetch\n      +\n      +When a remote has a refmap configured but no fetch refspec, a\n      +refspec-less fetch infers what to fetch from the local branches whose\n     @@ t/t5586-fetch-refmap.sh (new)\n      +\t\tcd client &&\n      +\t\tgit remote add upstream .. &&\n      +\t\ttest_might_fail git config unset remote.upstream.fetch &&\n     -+\t\tgit config remote.upstream.refmap \\\n     ++\t\tgit config remote.upstream.fetchRefmap \\\n      +\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n      +\t)\n      +'\n     @@ t/t5586-fetch-refmap.sh (new)\n      +\t)\n      +'\n      +\n     ++test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '\n     ++\ttest_commit main-3 &&\n     ++\t(\n     ++\t\tcd client &&\n     ++\t\tgit rev-parse refs/remotes/upstream/main >expect &&\n     ++\t\tgit fetch --prefetch upstream &&\n     ++\t\tgit rev-parse refs/remotes/upstream/main >actual &&\n     ++\t\ttest_cmp expect actual &&\n     ++\t\tgit -C .. rev-parse main >expect &&\n     ++\t\tgit rev-parse refs/prefetch/remotes/upstream/main >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n     ++\n      +test_expect_success 'git remote show does not choke on a refmap-only remote' '\n      +\t(\n      +\t\tcd client &&\n 4:  417f14d708 ! 4:  97650da870 remote: add \"git remote add --limited-fetch\"\n     @@ Commit message\n          branches nobody asked for.\n      \n          Give \"git remote add\" a --limited-fetch option that sets up\n     -    remote.<name>.refmap instead of remote.<name>.fetch, so that\n     +    remote.<name>.fetchRefmap instead of remote.<name>.fetch, so that\n          \"git fetch <name>\" only fetches the branches already tracked, as\n          described in the previous commit. It is rejected together with\n          -t/--track or --mirror, since those already say explicitly what to\n     @@ Documentation/git-remote.adoc: the `refs/remotes/<name>/` namespace, a refspec t\n       multiple branches without grabbing all branches.\n       +\n      +With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n     -+that tracks all branches, `remote.<name>.refmap` is set up so that a\n     ++that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n      +refspec-less `git fetch <name>` only fetches branches our local branches\n      +are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n      +details.\n     @@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix\n      -\t\t\tadd_branch(buf.buf, track.items[i].string,\n      -\t\t\t\t   name, mirror, &buf2);\n      +\t\tif (use_limited_fetch) {\n     -+\t\t\tstrbuf_addf(&buf, \"remote.%s.refmap\", name);\n     ++\t\t\tstrbuf_addf(&buf, \"remote.%s.fetchRefmap\", name);\n      +\t\t\tstrbuf_reset(&buf2);\n      +\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n      +\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only\n      +\t\tcd full-add &&\n      +\t\tgit remote add --limited-fetch upstream ../two &&\n      +\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n     -+\t\t\tremote.upstream.refmap &&\n     ++\t\t\tremote.upstream.fetchRefmap &&\n      +\t\ttest_must_fail git config get remote.upstream.fetch\n      +\t)\n      +'\n 5:  de0daa8623 ! 5:  a802c9f82f remote: default to --limited-fetch in a shallow repository\n     @@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at\n       the remote information is set up.\n       +\n      @@ Documentation/git-remote.adoc: With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n     - that tracks all branches, `remote.<name>.refmap` is set up so that a\n     + that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n       refspec-less `git fetch <name>` only fetches branches our local branches\n       are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n      -details.\n     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only\n      +\t\tcd shallow-add &&\n      +\t\tgit remote add upstream ../two &&\n      +\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n     -+\t\t\tremote.upstream.refmap &&\n     ++\t\t\tremote.upstream.fetchRefmap &&\n      +\t\ttest_must_fail git config get remote.upstream.fetch &&\n      +\t\tgit fetch upstream main &&\n      +\t\tgit branch --set-upstream-to=upstream/main &&\n     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only\n      +\t\tgit remote add --no-limited-fetch upstream ../two &&\n      +\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n      +\t\t\tremote.upstream.fetch &&\n     -+\t\ttest_must_fail git config get remote.upstream.refmap\n     ++\t\ttest_must_fail git config get remote.upstream.fetchRefmap\n      +\t)\n      +'\n      +\n\n-- \ngitgitgadget\n\n"},{"id":"556494","messageId":"a1f5e81fdcee8fb27045a6bbc7da8c91d803773f.1793103884.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","subject":"[PATCH v12 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:40Z","receivedAt":"2026-10-27T12:24:40Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.fetchRefmap, that\nprovides the default value for --refmap the same way\nremote.<name>.fetch already provides the default refspecs to fetch.\n\nUnlike passing --refmap on the command line, which requires an\nexplicit refspec to go with it, configuring remote.<name>.fetchRefmap\non its own does not change what a refspec-less fetch does: it falls\nback to the same defaults as a remote with no fetchRefmap at all.\n\nWhen remote.<name>.fetch is also configured, it still decides what to\nfetch, and the refmap maps what it fetches. This lets a remote name\nonly a few branches and store them all through one refmap:\n\n    [remote \"cabal\"]\n        fetch = master\n        fetch = next\n        fetch = seen\n        fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n\nLooking up the remote-tracking branch for a remote branch, as\n@{upstream} and \"git branch --track\" do, falls back to the refmap when\nremote.<name>.fetch has no match, so that these work for such\nbranches too. A branch excluded by a negative refspec in\nremote.<name>.fetch stays excluded.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   7 ++\n Documentation/fetch-options.adoc |   3 +\n builtin/fetch.c                  |  16 +++-\n remote.c                         |  20 ++++-\n remote.h                         |   6 ++\n t/t5510-fetch.sh                 | 130 +++++++++++++++++++++++++++++++\n 6 files changed, 179 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..236ccca93e 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,13 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.fetchRefmap::\n+\tThe default value of the `--refmap` option, used to map refs being\n+\tfetched to remote-tracking refs to store, the same way\n+\t`remote.<name>.fetch` does for an explicit refspec.\n+\t`remote.<name>.fetch`, if also configured, still decides what is\n+\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 1f76ae6242..305934f5f4 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.fetchRefmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n+\tif (effective_refmap)\n+\t\tfilter_prefetch_refspec(effective_refmap);\n \n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t\tif (has_merge &&\n \t\t\t    !strcmp(branch->remote_name, remote->name))\n \t\t\t\tadd_merge_config(&ref_map, remote_refs, branch, &tail);\n+\n+\t\t\tfor (i = 0; i < remote->refmap.nr; i++)\n+\t\t\t\tget_fetch_map(ref_map, &remote->refmap.items[i],\n+\t\t\t\t\t      &oref_tail, 1);\n \t\t} else if (!prefetch) {\n \t\t\tref_map = get_remote_ref(remote_refs, \"HEAD\");\n \t\t\tif (!ref_map)\ndiff --git a/remote.c b/remote.c\nindex 49ee2ea318..9acab2e01a 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"fetchrefmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n+\n+\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n+\t\treturn 0;\n+\tif (refspec_find_match(&remote->refmap, refspec))\n+\t\treturn -1;\n+\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n+\t\tFREE_AND_NULL(*result);\n+\t\treturn -1;\n+\t}\n+\treturn 0;\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 17c0be499c..aaa476f86b 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,136 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.fetchRefmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.fetchRefmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.fetchRefmap plays no part in deciding what to\n+\t\t# fetch, only in mapping what is fetched.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b master fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch next &&\n+\tgit -C fetch-refmap-upstream branch seen &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add cabal ../fetch-refmap-upstream &&\n+\t\tgit config --unset-all remote.cabal.fetch &&\n+\t\tgit config --add remote.cabal.fetch master &&\n+\t\tgit config --add remote.cabal.fetch next &&\n+\t\tgit config --add remote.cabal.fetch seen &&\n+\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n+\t\tgit fetch cabal &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/cabal/HEAD\n+\t\trefs/remotes/cabal/master\n+\t\trefs/remotes/cabal/next\n+\t\trefs/remotes/cabal/seen\n+\t\tEOF\n+\t\tgit branch --track topic cabal/next &&\n+\t\techo refs/remotes/cabal/next >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin other &&\n+\t\tgit branch --track topic origin/other &&\n+\t\ttest_cmp_config origin branch.topic.remote &&\n+\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n+\t\techo refs/remotes/origin/other >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch secret &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin secret &&\n+\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n+\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n+\t\ttest_grep \"not a branch\" err\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"556495","messageId":"e8af5bc76bcdbc393b2bb97e153db5446d1c88e9.1793103884.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","subject":"[PATCH v12 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:41Z","receivedAt":"2026-10-27T12:24:41Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we decide what to fetch from\ntwo sources: the remote's configured fetch refspec, and the current\nbranch's upstream, if it lives on that remote. For example, after\n\n    git clone --single-branch <url>\n    git switch -c work\n    git push -u origin work\n\nthe remote's refspec only covers \"main\", but a refspec-less \"git fetch\norigin\" still fetches \"work\" into FETCH_HEAD, so that a plain \"git\npull\" finds the branch to merge. See 49ca2fba39 (fetch: add\nbranch.*.merge to default ref-prefix extension, 2022-09-08).\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 18 ++++++++----------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 32 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 305934f5f4..31ec963db7 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1985,8 +1985,6 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n-\n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n@@ -2001,14 +1999,14 @@ static int do_fetch(struct transport *transport,\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 9acab2e01a..d4259fe7f3 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstrvec_push(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..003c8fd180 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"556496","messageId":"aad6e5f66f6a5441dd096cbd70fcfbd7198fbb0f.1793103884.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","subject":"[PATCH v12 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:42Z","receivedAt":"2026-10-27T12:24:42Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWe want remote.<name>.fetchRefmap to work on its own, without also\nsetting remote.<name>.fetch, so a remote can be limited to the\nbranches already in use locally, without listing them by hand or\ntouching the command line every time.\n\nInfer what to fetch from the local branches whose @{upstream} is\nalready on that remote. The refmap itself still only controls where\nthose fetched refs land, not which branches are selected.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   4 +-\n Documentation/fetch-options.adoc |  11 ++-\n builtin/fetch.c                  |  72 +++++++++++++----\n remote.c                         |  25 ++++++\n remote.h                         |   5 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++++++\n 7 files changed, 232 insertions(+), 19 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 236ccca93e..0d24abfcbd 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -38,7 +38,9 @@ remote.<name>.fetchRefmap::\n \tfetched to remote-tracking refs to store, the same way\n \t`remote.<name>.fetch` does for an explicit refspec.\n \t`remote.<name>.fetch`, if also configured, still decides what is\n-\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tfetched. If it is not set either, a refspec-less fetch instead\n+\tinfers what to fetch from local branches built on this remote.\n+\tSee the `--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..be27c03de7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,15 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, infer what to fetch from local branches built on\n+this remote: remote-tracking branches from this remote that are also\n+configured as the `@{upstream}` of a local branch are fetched. The\n+refmap only decides where those fetched refs land, the same as it\n+would for an explicit refspec, and does not narrow which branches are\n+selected. It is what turns this on, since without it the fetched\n+branches would have nowhere to land.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 31ec963db7..3a3cb311b7 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -531,17 +533,33 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \tif (effective_refmap)\n \t\tfilter_prefetch_refspec(effective_refmap);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct strvec tracked = STRVEC_INIT;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor (size_t i = 0; i < tracked.nr; i++)\n+\t\t\trefspec_append(&inferred_rs, tracked.v[i]);\n+\t\tstrvec_clear(&tracked);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -576,6 +594,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -675,6 +695,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1967,6 +1988,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head = FOLLOW_REMOTE_NEVER;\n+\tbool apply_follow_remote_head_config = false;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -1984,21 +2006,10 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n-\t} else {\n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\t/*\n-\t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n-\t\t\t * for the analogous, still-unresolved case.\n-\t\t\t */\n-\t\t\tif (transport->remote->follow_remote_head)\n-\t\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n-\t\t\telse if (config->follow_remote_head_seen)\n-\t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n-\t\t\telse\n-\t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n-\t\t}\n+\t} else if (transport->remote->fetch.nr) {\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tapply_follow_remote_head_config = true;\n \n \t\t/*\n \t\t * The configured refspec may not cover the current\n@@ -2007,6 +2018,22 @@ static int do_fetch(struct transport *transport,\n \t\t */\n \t\tcollect_upstream_from_remote(the_repository, transport->remote,\n \t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.fetchRefmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t  &transport_ls_refs_options.ref_prefixes);\n+\t\t\tapply_follow_remote_head_config = true;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t\t}\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\n@@ -2017,6 +2044,19 @@ static int do_fetch(struct transport *transport,\n \t\t\t\t    \"HEAD\");\n \t}\n \n+\tif (apply_follow_remote_head_config) {\n+\t\t/*\n+\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n+\t\t * for the analogous, still-unresolved case.\n+\t\t */\n+\t\tif (transport->remote->follow_remote_head)\n+\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n+\t\telse if (config->follow_remote_head_seen)\n+\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n+\t\telse\n+\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\t}\n+\n \tif (tags == TAGS_SET || tags == TAGS_DEFAULT)\n \t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t    \"refs/tags/\");\ndiff --git a/remote.c b/remote.c\nindex d4259fe7f3..15cd96aa8e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstrvec_push(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tconst struct remote *remote;\n+\tstruct strvec *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->remote, ref->name,\n+\t\t\t\t      data->tracked);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 003c8fd180..6fd9b05a79 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   const char *refname,\n \t\t\t\t   struct strvec *tracked);\n \n+/* Same as collect_upstream_from_remote(), but checked for every local branch. */\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 11b3752f00..0953a4112f 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -736,6 +736,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..d4214b12b0\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,133 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.fetchRefmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.fetchRefmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '\n+\ttest_commit main-3 &&\n+\t(\n+\t\tcd client &&\n+\t\tgit rev-parse refs/remotes/upstream/main >expect &&\n+\t\tgit fetch --prefetch upstream &&\n+\t\tgit rev-parse refs/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit -C .. rev-parse main >expect &&\n+\t\tgit rev-parse refs/prefetch/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"556497","messageId":"97650da87045958a769c3ac66ca5afd1d3ec390a.1793103884.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","subject":"[PATCH v12 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:43Z","receivedAt":"2026-10-27T12:24:43Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.fetchRefmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 315100409d..1f490819ba 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex fb7e5b114f..5f8a8a2f71 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetchRefmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..90cba75f93 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"556498","messageId":"a802c9f82f26c8dadf2bf3dd90de33d589561740.1793103884.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com","subject":"[PATCH v12 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T12:24:44Z","receivedAt":"2026-10-27T12:24:44Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow clone used to still fetch every\nbranch that remote has, since \"git remote add\" always set up a\nwildcard remote.<name>.fetch refspec regardless of how shallow the\nrepository already was. That defeats the purpose of having cloned\nshallow in the first place (for example with --single-branch), and\ncan make a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 1f490819ba..245c0c4558 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 5f8a8a2f71..5d2388a26c 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 90cba75f93..68063a47b8 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.fetchRefmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"556533","messageId":"7438f8e3-4969-49a9-a0b0-41520ee35aa3@gmail.com","threadId":"66353","inReplyTo":"xmqqcxswtqd0.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-27T17:03:51Z","receivedAt":"2026-10-27T17:03:51Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 26/10/2026 15:48, Junio C Hamano wrote:\n> > I actually do not think we should have master next seen on the same\n> remote.cabal.fetch configuration variable.  It is a multi-valued\n> variable, so it should read more like\n> >           [remote \"cabal\"]\n>                   fetch = refs/heads/master\n>                   fetch = refs/heads/next\n>                   fetch = refs/heads/seen\n>                   refmap = +refs/heads/*:refs/remotes/cabal/*\n\nDoesn't that cause older versions of git to try and update the local branch when fetching, or is it only when pushing that we'll fill in the right hand side of the refspec with a copy of the left hand side? If we use a different config setting for \"what to fetch\" then users that want the new behavior can unset remote.<name>.fetch and older versions of git will fail rather than overwriting local branches when there is no refspec on the commandline.\n\nThanks\n\nPhillip\n\n> > And without these remote.cabal.fetch configured, but with\n> remote.cabal.refmap configured, this command line\n> > \t$ git fetch cabal next\n> > should work just like\n> > \t$ git fetch cabal \\\n> \t\t--refmap=\"+refs/heads/*:refs/remotes/cabal/*\" \\\n> \t\tnext\n> > I think.\n> > Thanks.\n\n\n"},{"id":"556534","messageId":"48e21b4b-6762-4e76-a817-1be6bb31bbb7@gmail.com","threadId":"66353","inReplyTo":"xmqqh5i8tqjx.fsf@gitster.g","subject":"Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-27T17:04:00Z","receivedAt":"2026-10-27T17:04:00Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 26/10/2026 15:44, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> > When I wrote the refmap feature (without configuration variable)\n> long time ago, one of the goals was to allow me to say\n> >      $ git fetch cabal \\\n>          --refmap=\"+refs/heads/*:refs/remotes/cabal/*\" \\\n> \tmaint master next\n> > That is, \"here is the place I grab from there are stored\" and\n> \"maint master next are the things I grab from there\" are specified\n> separately.  With the configuration variable added in this series,\n> >      $ git config set remote.cabal.refmap \"+refs/heads/*:refs/remotes/cabal/*\"\n>      $ git fetch cabal master next seen\n> > should become possible with the same behaviour.\n\nThat makes sense\n\n>> That\n>> setting could accept something like \"*@{upstream}\" to \"mean fetch the\n>> upstream branches of my local branches that live on this remote\" which\n>> would cover what this series wants. I think it would be useful to\n>> support \"+\" and \"^\" prefixes on the refs to fetch as well, so that the\n>> refmap config controls the mapping and the \"what to fetch\" config\n>> handles negation and forced updates on a per-ref basis.\n> > The reasoning here is slightly wrong.  \"^\" (not) may be good thing\n> to consider as it is to decide what to fetch, but \"+\" (overwrite)\n> should be on the refmap side, i.e., where to store.  If you are not\n> storing, then \"overwrite what I have with non-fast-forward\" becomes\n> a nonsense request, and that is why it does not belong to \"what to\n> fetch\".\n\nI see what you're saying. It is unfortunate though that we then need separate refspecs if want to reject non fast-forward updates of some branches but map the refs in the same way for the fast-forward and non-fast-forward cases.\n\nThanks\n\nPhillip\n\n"},{"id":"556536","messageId":"xmqqbj8fm54y.fsf@gitster.g","threadId":"66353","inReplyTo":"7438f8e3-4969-49a9-a0b0-41520ee35aa3@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-27T17:21:01Z","receivedAt":"2026-10-27T17:21:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> On 26/10/2026 15:48, Junio C Hamano wrote:\n>> \n>> I actually do not think we should have master next seen on the same\n>> remote.cabal.fetch configuration variable.  It is a multi-valued\n>> variable, so it should read more like\n>> \n>>           [remote \"cabal\"]\n>>                   fetch = refs/heads/master\n>>                   fetch = refs/heads/next\n>>                   fetch = refs/heads/seen\n>>                   refmap = +refs/heads/*:refs/remotes/cabal/*\n>\n> Doesn't that cause older versions of git to try and update the local \n> branch when fetching, or is it only when pushing that we'll fill in the \n> right hand side of the refspec with a copy of the left hand side?\n\n\"git fetch --help\" is fairly clear on what should happen in its\ndescription on <refspec> command line arguments, and \n\nI meant in the above example that remote.cabal.fetch should be used\nas if these three refnames are written on the fetch command line\nwhen nothing is given from the command line.  Now, <refspec> that\ndoes not have :dst side for fetch stores the result nowhere but\nFETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of\ngit), you will get them in FETCH_HEAD, so your \"git pull\" will still\nwork to create an octopus merge ;-)\n\nI was hoping that with refmap, the users can tell the system where\nto store them.  As specifying a shorthand also works,\n\n           [remote \"cabal\"]\n                   fetch = master\n                   fetch = next\n                   fetch = seen\n                   refmap = +refs/heads/*:refs/remotes/cabal/*\n\nwould be an easy layout to allow adding different branches than\nhaving to say\n\n           [remote \"cabal\"]\n                   fetch = +refs/heads/master:refs/remotes/cabal/master\n                   fetch = +refs/heads/next:refs/remotes/cabal/next\n                   fetch = +refs/heads/seen:refs/remotes/cabal/seen\n\n\nThanks.\n\n"},{"id":"556570","messageId":"CALnO6CBEPcPeTmZj2_O6WMXdjo4e6YiQCctN3Bs-o9LSN2rsrw@mail.gmail.com","threadId":"66353","inReplyTo":"CAHwyqnVELh2ZyCy5W8+jHHWN=UvcgMJP0k0i1PJKU5bU_AhUrw@mail.gmail.com","subject":"Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-27T19:41:52Z","receivedAt":"2026-10-27T19:41:52Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Sun, Oct 25, 2026 at 1:20 PM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> > > The latter is independent of the former on purpose: a narrow\n> > > refspec (for example from \"git remote add -t\") can leave the current\n> > > branch's own upstream uncovered, and we still need to be able to fetch\n> > > it.\n> >\n> > I don't really understand the meaning here. I can guess that you mean\n> > something like \"A remote with a narrow refspec, like what you get from\n> > 'git remote add -t', can omit a branch's upstream from the remote's\n> > fetch refspec, and we still want to fetch that upstream.\" But it's\n> > taken me a while to put that together (after puzzling out the above)\n> > after first not knowing what \"uncovered\" meant.\n> >\n> > I think the whole thing would be clearer with an example:\n> >\n> > \"\"\"\n> > Imagine a remote with a narrow refspec:\n> >\n> >     git remote add -t main colleague <remote>\n> >\n> > and a branch whose upstream points at that remote, but isn't part of\n> > the remote's refspec:\n> >\n> >     git fetch colleague work:refs/remotes/colleague/work\n> >     git switch -c work colleague/work\n> >\n> > When we \"git fetch colleague\" without a refspec, we decide what to\n> > fetch by combining the remote's refspec (main -> remote-tracking\n> > branch, in this case) and the current branch's upstream, if it was\n> > based on that remote (work -> remote-tracking branch, in this case).\n> > \"\"\"\n> >\n> > Note: we aren't \"guessing\" what to fetch!\n>\n> Thanks! Maybe this should be the example instead?\n>\n>         git clone --single-branch <url>\n>         git switch -c work\n>         git push -u origin work\n>\n\nI'm not sure if that captures the same \"narrow refspec\" remote or not,\nto be honest, though it might be simpler? I'm not sure what the push\nhas to do with it, though, and I don't know what you think about the\nrest of the proposed wording ;)\n\n[later]\n\nReading the new version, I think I see what you're after. I personally\nwould love to see an example adding a remote, since I tend to want\n\"big\" fetches from my primary remotes, but that's fine.\n\n"},{"id":"556679","messageId":"241e7bbc-7144-4ebf-af76-84a06a982574@gmail.com","threadId":"66353","inReplyTo":"xmqqbj8fm54y.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-28T16:16:13Z","receivedAt":"2026-10-28T16:16:13Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 27/10/2026 17:21, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>>\n>> Doesn't that cause older versions of git to try and update the local\n>> branch when fetching, or is it only when pushing that we'll fill in the\n>> right hand side of the refspec with a copy of the left hand side?\n> > I meant in the above example that remote.cabal.fetch should be used\n> as if these three refnames are written on the fetch command line\n> when nothing is given from the command line.  Now, <refspec> that\n> does not have :dst side for fetch stores the result nowhere but\n> FETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of\n> git), you will get them in FETCH_HEAD, so your \"git pull\" will still\n> work to create an octopus merge ;-)\n\nThanks for clarifying that\n\n> I was hoping that with refmap, the users can tell the system where\n> to store them.  As specifying a shorthand also works,\n> >             [remote \"cabal\"]\n>                     fetch = master\n>                     fetch = next\n>                     fetch = seen\n>                     refmap = +refs/heads/*:refs/remotes/cabal/*\n> > would be an easy layout to allow adding different branches than\n> having to say\n> >             [remote \"cabal\"]\n>                     fetch = +refs/heads/master:refs/remotes/cabal/master\n>                     fetch = +refs/heads/next:refs/remotes/cabal/next\n>                     fetch = +refs/heads/seen:refs/remotes/cabal/seen\nThat makes sense. What should we do if \"remote.cabal.refmap\" exists and \"remote.cabal.fetch\" contains a colon? We could just ignore that entry but it might be better to error out.\n\nThanks\n\nPhillip\n\n"},{"id":"556690","messageId":"xmqqld7hafhy.fsf@gitster.g","threadId":"66353","inReplyTo":"241e7bbc-7144-4ebf-af76-84a06a982574@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-28T17:42:33Z","receivedAt":"2026-10-28T17:42:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> I was hoping that with refmap, the users can tell the system where\n>> to store them.  As specifying a shorthand also works,\n>> \n>>             [remote \"cabal\"]\n>>                     fetch = master\n>>                     fetch = next\n>>                     fetch = seen\n>>                     refmap = +refs/heads/*:refs/remotes/cabal/*\n>> \n>> would be an easy layout to allow adding different branches than\n>> having to say\n>> \n>>             [remote \"cabal\"]\n>>                     fetch = +refs/heads/master:refs/remotes/cabal/master\n>>                     fetch = +refs/heads/next:refs/remotes/cabal/next\n>>                     fetch = +refs/heads/seen:refs/remotes/cabal/seen\n>\n> That makes sense. What should we do if \"remote.cabal.refmap\" exists and \n> \"remote.cabal.fetch\" contains a colon? We could just ignore that entry \n> but it might be better to error out.\n\nPossible alternatives include \n\n - giving fetch with dst take precedence over refmap\n - storing the result in two locations\n - error out not knowing what is the right thing to do\n\nI think the command line version, i.e.\n\n    $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \\\n\t\trefs/heads/master:refs/remotes/origin/master \\\n\t\tnext\n\nstores 'master' as alt/master and origin/master, and 'next' as\nalt/next, i.e., takes the second option.\n\nThanks.\n\n"},{"id":"556708","messageId":"CAHwyqnWyFe1Ep_0wqZqTVg+2tQvpt=i9zyRutV=EZkvPt4Xr8Q@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqld7hafhy.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-28T20:57:22Z","receivedAt":"2026-10-28T20:57:22Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Wed, Oct 28, 2026 at 7:42 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> >> I was hoping that with refmap, the users can tell the system where\n> >> to store them.  As specifying a shorthand also works,\n> >>\n> >>             [remote \"cabal\"]\n> >>                     fetch = master\n> >>                     fetch = next\n> >>                     fetch = seen\n> >>                     refmap = +refs/heads/*:refs/remotes/cabal/*\n> >>\n> >> would be an easy layout to allow adding different branches than\n> >> having to say\n> >>\n> >>             [remote \"cabal\"]\n> >>                     fetch = +refs/heads/master:refs/remotes/cabal/master\n> >>                     fetch = +refs/heads/next:refs/remotes/cabal/next\n> >>                     fetch = +refs/heads/seen:refs/remotes/cabal/seen\n> >\n> > That makes sense. What should we do if \"remote.cabal.refmap\" exists and\n> > \"remote.cabal.fetch\" contains a colon? We could just ignore that entry\n> > but it might be better to error out.\n>\n> Possible alternatives include\n>\n>  - giving fetch with dst take precedence over refmap\n>  - storing the result in two locations\n>  - error out not knowing what is the right thing to do\n\nThe answer seems to be the second one, with my v12, and I wrote a test\nnow to confirm it, I can include it in a v13 when it's time:\n\n```\n+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst\nremote.<name>.fetch entry in both places' '\n+       test_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+       git init -b master fetch-refmap-upstream &&\n+       test_commit -C fetch-refmap-upstream base &&\n+       git init downstream &&\n+       (\n+               cd downstream &&\n+               git remote add cabal ../fetch-refmap-upstream &&\n+               git config --unset-all remote.cabal.fetch &&\n+               git config --add remote.cabal.fetch \\\n+                       \"+refs/heads/master:refs/remotes/custom/master\" &&\n+               git config remote.cabal.fetchRefmap\n\"+refs/heads/*:refs/remotes/cabal/*\" &&\n+               git fetch cabal &&\n+               check_fetched_refs <<-\\EOF\n+               refs/remotes/cabal/HEAD\n+               refs/remotes/cabal/master\n+               refs/remotes/custom/master\n+               EOF\n+       )\n+'\n```\n\n\nHarald\n\n"},{"id":"556793","messageId":"E50044F1-41F6-4FD8-9A22-A43FF36932F2@gmail.com","threadId":"66353","inReplyTo":"e6af5d1e-4ca1-48a5-8a84-5e0cefbdbc2b@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-29T14:30:10Z","receivedAt":"2026-10-29T14:30:10Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"\n> Le 26 oct. 2026 à 05:44, Phillip Wood <phillip.wood123@gmail.com> a écrit :\n> \n> ﻿Hi Harald\n> \n>> On 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote:\n>> From: Harald Nordgren <haraldnordgren@gmail.com>\n>> Add a per-remote config variable, remote.<name>.refmap, that provides\n>> the default value for --refmap the same way remote.<name>.fetch\n>> already provides the default refspecs to fetch.\n> \n> The second half of this sentence seems redundant to me. Can we please call the new config setting \"remote.<name>.fetchRefmap\" so that it is clear it applies to fetches. That would also allow us to add a matching \"remote.<name>.pushRefmap\" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push.\n\nYes, I would love to add pushRefmap atop later! It would\nease my working with notes, I think.\n\nI’m on mobile now, but if I remember I’ll try to spell out what I mean\nmore concretely. Thanks for the reminder!\n"},{"id":"556797","messageId":"CAHwyqnU7apDDjR2WprKu_Hf9eUD_GM+yfmY=VBFJW5XKKpYHhg@mail.gmail.com","threadId":"66353","inReplyTo":"E50044F1-41F6-4FD8-9A22-A43FF36932F2@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-29T14:50:36Z","receivedAt":"2026-10-29T14:50:36Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Thu, Oct 29, 2026 at 4:30 PM Ben Knoble <ben.knoble@gmail.com> wrote:\n>\n>\n> > Le 26 oct. 2026 à 05:44, Phillip Wood <phillip.wood123@gmail.com> a écrit :\n> >\n> > ﻿Hi Harald\n> >\n> >> On 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote:\n> >> From: Harald Nordgren <haraldnordgren@gmail.com>\n> >> Add a per-remote config variable, remote.<name>.refmap, that provides\n> >> the default value for --refmap the same way remote.<name>.fetch\n> >> already provides the default refspecs to fetch.\n> >\n> > The second half of this sentence seems redundant to me. Can we please call the new config setting \"remote.<name>.fetchRefmap\" so that it is clear it applies to fetches. That would also allow us to add a matching \"remote.<name>.pushRefmap\" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push.\n>\n> Yes, I would love to add pushRefmap atop later! It would\n> ease my working with notes, I think.\n>\n> I’m on mobile now, but if I remember I’ll try to spell out what I mean\n> more concretely. Thanks for the reminder!\n\nIt has already been renamed to fetchRefmap in v12.\n\n\nHarald\n\n"},{"id":"556801","messageId":"5a1fc2ca-0ef9-41d2-a02a-a6023cbe09dd@gmail.com","threadId":"66353","inReplyTo":"xmqqld7hafhy.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-29T16:26:56Z","receivedAt":"2026-10-29T16:26:56Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 28/10/2026 17:42, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >>\n>> That makes sense. What should we do if \"remote.cabal.refmap\" exists and\n>> \"remote.cabal.fetch\" contains a colon? We could just ignore that entry\n>> but it might be better to error out.\n> > Possible alternatives include\n> >   - giving fetch with dst take precedence over refmap\n>   - storing the result in two locations\n>   - error out not knowing what is the right thing to do\n> > I think the command line version, i.e.\n> >      $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \\\n> \t\trefs/heads/master:refs/remotes/origin/master \\\n> \t\tnext\n> > stores 'master' as alt/master and origin/master, and 'next' as\n> alt/next, i.e., takes the second option.\n\nThat's surprising, I'd naively assumed a refspec on the command line would override the refmap. It feels problematic if we cannot override the fetchRefmap config with a refspec on the command line, but need to use \"--refmap\" instead.\n\nI think there are at least a couple of ways of thinking about the interaction of \"remote.<name>.fetchRefmap\" and \"remote.<name>.fetch\". The first is to consider that the \"fetchRefmap\" config is for \"where to store the fetched refs\" and the \"fetch\" config is for \"what to fetch\". In that case we'd reject values containing colons in the \"fetch\" config as any mapping should be done by the \"fetchRefmap\" config. The other way is to consider that the \"fetchRefmap\" config holds the default argument for \"--refmap\" and \"fetch\" holds the default command line refspecs to use when the user doesn't specify any. In that case we should accept \"fetch\" values containing colons and behave as if those values had been given as command line arguments. I'm leaning towards the latter, but if we do decide to do that, I think we should make sure\n\n  git -c remote.cabal.fetchRefmap='refs/heads/*:refs/remotes/cabal/*' \\\n    fetch cabal refs/heads/master:refs/remotes/origin/master\n\nonly updates origin/master and not cabal/master.\n\nThanks\n\nPhillip\n\n\n"},{"id":"556839","messageId":"xmqqqzh8ywu2.fsf@gitster.g","threadId":"66353","inReplyTo":"5a1fc2ca-0ef9-41d2-a02a-a6023cbe09dd@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-29T22:18:45Z","receivedAt":"2026-10-29T22:18:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> I think the command line version, i.e.\n>> \n>>      $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \\\n>> \t\trefs/heads/master:refs/remotes/origin/master \\\n>> \t\tnext\n>> \n>> stores 'master' as alt/master and origin/master, and 'next' as\n>> alt/next, i.e., takes the second option.\n>\n> That's surprising, I'd naively assumed a refspec on the command line \n> would override the refmap. It feels problematic if we cannot override \n> the fetchRefmap config with a refspec on the command line, but need to \n> use \"--refmap\" instead.\n\nMaybe, maybe not.  The command line refmap was meant to primarily\nhelp use case where the \"what to fetch\" was to be specified without\ncolon, so when fetching multiple branches, you do not have to repeat\nthe refs/remotes/origin/ all over again.  So if you are giving a\nrefspec with a colon (like 'master' in the above example) to specify\nwhere to store, it is an unusual request to do something different.\n\nSomething different could be \"do more than what the refmap alone\nwould do\", which led to the behaviour implemented when refmap was\nintroduced.  But it could be \"refrain from what the refmap alone\nwould do and do something else\", and if you take that position,\nyou would get the \"overriding\" behaviour.\n\n\n\n\n\n\n\n\n"},{"id":"556840","messageId":"xmqqmrrwywq8.fsf@gitster.g","threadId":"66353","inReplyTo":"E50044F1-41F6-4FD8-9A22-A43FF36932F2@gmail.com","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-29T22:21:03Z","receivedAt":"2026-10-29T22:21:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ben Knoble <ben.knoble@gmail.com> writes:\n\n> Yes, I would love to add pushRefmap atop later! It would\n> ease my working with notes, I think.\n\n> Yes, I would love to add pushRefmap atop later! It would\n> ease my working with notes, I think.\n\nTell us more when you are off mobile ;-).\n\nI do not think I planned to add '--refmap' support on the push side,\neven from the command line, but I can imagine there would be\nreasonable workflows that benefit from having one.\n\n"},{"id":"556876","messageId":"CAHwyqnUzKAD42qTMbAV_9DttzP91PUTjHxXehy1cX0=B7e0cbg@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqmrrwywq8.fsf@gitster.g","subject":"Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-30T10:14:09Z","receivedAt":"2026-10-30T10:14:09Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Fri, Oct 30, 2026 at 12:21 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Ben Knoble <ben.knoble@gmail.com> writes:\n>\n> > Yes, I would love to add pushRefmap atop later! It would\n> > ease my working with notes, I think.\n>\n> > Yes, I would love to add pushRefmap atop later! It would\n> > ease my working with notes, I think.\n>\n> Tell us more when you are off mobile ;-).\n>\n> I do not think I planned to add '--refmap' support on the push side,\n> even from the command line, but I can imagine there would be\n> reasonable workflows that benefit from having one.\n\nNot sure if it's related to this specific conversation, but I realized\nthat @{push} is not being resolved here, so I plan on adding a small\nfix in a v13:\n\n```\n--- a/remote.c\n+++ b/remote.c\n@@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct\nrepository *repo,\n\n        remote = repo_remote_for_push_tracking(repo, remote);\n        ret = apply_refspecs(&remote->fetch, refname);\n+       if (!ret)\n+               ret = apply_refspecs(&remote->refmap, refname);\n        if (!ret)\n                return error_buf(err,\n                                 _(\"push destination '%s' on remote\n'%s' has no local tracking branch\"),\n```\n\n\nHarald\n\n"},{"id":"556895","messageId":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:24Z","receivedAt":"2026-10-30T10:52:24Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Avoid fetching every branch of a new remote in a shallow repo.\n\nChanges in v13:\n\n * Added a new commit so @{push} resolves correctly for a --limited-fetch\n   remote, falling back to remote.<name>.fetchRefmap the same way fetch\n   already does when remote.<name>.fetch doesn't cover the branch being\n   pushed.\n * Added a regression test to fetch: add remote.<name>.fetchRefmap covering\n   a remote.<name>.fetch entry with an explicit destination, confirming it's\n   stored in both that destination and the refmap's.\n\nChanges in v12:\n\n * Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout\n   code, docs, and tests, so it reads unambiguously as fetch-only.\n * Added the missing code path in get_ref_map() so a branch fetched by name\n   via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap,\n   with a new test (the \"cabal\" example: fetch = master/next/seen plus a\n   refmap storing them all under one namespace).\n * Reworded the fetchRefmap commit message and doc entry to drop a redundant\n   sentence, fix a confusing paragraph, and spell out that\n   remote.<name>.fetch, when configured, still decides what's fetched while\n   the refmap only remaps it.\n * Added a prefetch coverage test to t5586-fetch-refmap.sh.\n\nChanges in v11:\n\n * remote_find_tracking() now falls back to remote.<name>.refmap when\n   remote.<name>.fetch doesn't match. Negative refspecs in fetch still\n   exclude refs, and both cases have tests\n * The helpers write straight into a strvec, take the output parameter last\n   and the remote as const\n * The followRemoteHEAD lookup is done once, not duplicated in two branches\n * Clearer commit messages and docs: a working example plus a reference to\n   49ca2fba39, the goal stated first, an incorrect \"used to die\" claim\n   dropped, and an explanation of why a refmap turns on the inference\n * use_limited_fetch is a bool\n\nChanges in v10:\n\n * Resolve merge conflict after master integrated\n   ch/fetch-followremotehead-lazy-validation.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (6):\n  fetch: add remote.<name>.fetchRefmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n  remote: resolve @{push} through remote.<name>.fetchRefmap\n\n Documentation/config/remote.adoc |   9 ++\n Documentation/fetch-options.adoc |  10 ++\n Documentation/git-remote.adoc    |  14 ++-\n builtin/fetch.c                  | 100 +++++++++++++++-----\n builtin/remote.c                 |  31 +++++--\n remote.c                         |  62 ++++++++++++-\n remote.h                         |  20 ++++\n t/meson.build                    |   1 +\n t/t1514-rev-parse-push.sh        |  19 ++++\n t/t5505-remote.sh                |  76 ++++++++++++++++\n t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++\n t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++\n 12 files changed, 593 insertions(+), 33 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: b9e5cff1803ab24582201ef059516eb16014b9d9\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v13\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v13\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v12:\n\n 1:  a1f5e81fdc ! 1:  07081828d1 fetch: add remote.<name>.fetchRefmap\n     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote\n      +\t)\n      +'\n      +\n     ++test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '\n     ++\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n     ++\tgit init -b master fetch-refmap-upstream &&\n     ++\ttest_commit -C fetch-refmap-upstream base &&\n     ++\tgit init downstream &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\tgit remote add cabal ../fetch-refmap-upstream &&\n     ++\t\tgit config --unset-all remote.cabal.fetch &&\n     ++\t\tgit config --add remote.cabal.fetch \\\n     ++\t\t\t\"+refs/heads/master:refs/remotes/custom/master\" &&\n     ++\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n     ++\t\tgit fetch cabal &&\n     ++\t\tcheck_fetched_refs <<-\\EOF\n     ++\t\trefs/remotes/cabal/HEAD\n     ++\t\trefs/remotes/cabal/master\n     ++\t\trefs/remotes/custom/master\n     ++\t\tEOF\n     ++\t)\n     ++'\n     ++\n      +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n      +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n      +\tgit init -b main fetch-refmap-upstream &&\n 2:  e8af5bc76b = 2:  3f5808cbf3 fetch: extract collect_upstream_from_remote() helper\n 3:  aad6e5f66f = 3:  b02e0a5397 fetch: infer branches to fetch from a refmap-only remote\n 4:  97650da870 = 4:  3ee427de87 remote: add \"git remote add --limited-fetch\"\n 5:  a802c9f82f = 5:  2cba4623ee remote: default to --limited-fetch in a shallow repository\n -:  ---------- > 6:  25eec8d967 remote: resolve @{push} through remote.<name>.fetchRefmap\n\n-- \ngitgitgadget\n\n"},{"id":"556896","messageId":"07081828d1d65373bee4b2ef5e1916e65a217db6.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:25Z","receivedAt":"2026-10-30T10:52:25Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.fetchRefmap, that\nprovides the default value for --refmap the same way\nremote.<name>.fetch already provides the default refspecs to fetch.\n\nUnlike passing --refmap on the command line, which requires an\nexplicit refspec to go with it, configuring remote.<name>.fetchRefmap\non its own does not change what a refspec-less fetch does: it falls\nback to the same defaults as a remote with no fetchRefmap at all.\n\nWhen remote.<name>.fetch is also configured, it still decides what to\nfetch, and the refmap maps what it fetches. This lets a remote name\nonly a few branches and store them all through one refmap:\n\n    [remote \"cabal\"]\n        fetch = master\n        fetch = next\n        fetch = seen\n        fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n\nLooking up the remote-tracking branch for a remote branch, as\n@{upstream} and \"git branch --track\" do, falls back to the refmap when\nremote.<name>.fetch has no match, so that these work for such\nbranches too. A branch excluded by a negative refspec in\nremote.<name>.fetch stays excluded.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   7 ++\n Documentation/fetch-options.adoc |   3 +\n builtin/fetch.c                  |  16 +++-\n remote.c                         |  20 +++-\n remote.h                         |   6 ++\n t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++\n 6 files changed, 200 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..236ccca93e 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,13 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.fetchRefmap::\n+\tThe default value of the `--refmap` option, used to map refs being\n+\tfetched to remote-tracking refs to store, the same way\n+\t`remote.<name>.fetch` does for an explicit refspec.\n+\t`remote.<name>.fetch`, if also configured, still decides what is\n+\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 1f76ae6242..305934f5f4 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.fetchRefmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n+\tif (effective_refmap)\n+\t\tfilter_prefetch_refspec(effective_refmap);\n \n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t\tif (has_merge &&\n \t\t\t    !strcmp(branch->remote_name, remote->name))\n \t\t\t\tadd_merge_config(&ref_map, remote_refs, branch, &tail);\n+\n+\t\t\tfor (i = 0; i < remote->refmap.nr; i++)\n+\t\t\t\tget_fetch_map(ref_map, &remote->refmap.items[i],\n+\t\t\t\t\t      &oref_tail, 1);\n \t\t} else if (!prefetch) {\n \t\t\tref_map = get_remote_ref(remote_refs, \"HEAD\");\n \t\t\tif (!ref_map)\ndiff --git a/remote.c b/remote.c\nindex 49ee2ea318..9acab2e01a 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"fetchrefmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n+\n+\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n+\t\treturn 0;\n+\tif (refspec_find_match(&remote->refmap, refspec))\n+\t\treturn -1;\n+\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n+\t\tFREE_AND_NULL(*result);\n+\t\treturn -1;\n+\t}\n+\treturn 0;\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 17c0be499c..b5eba020ad 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,157 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.fetchRefmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.fetchRefmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.fetchRefmap plays no part in deciding what to\n+\t\t# fetch, only in mapping what is fetched.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b master fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch next &&\n+\tgit -C fetch-refmap-upstream branch seen &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add cabal ../fetch-refmap-upstream &&\n+\t\tgit config --unset-all remote.cabal.fetch &&\n+\t\tgit config --add remote.cabal.fetch master &&\n+\t\tgit config --add remote.cabal.fetch next &&\n+\t\tgit config --add remote.cabal.fetch seen &&\n+\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n+\t\tgit fetch cabal &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/cabal/HEAD\n+\t\trefs/remotes/cabal/master\n+\t\trefs/remotes/cabal/next\n+\t\trefs/remotes/cabal/seen\n+\t\tEOF\n+\t\tgit branch --track topic cabal/next &&\n+\t\techo refs/remotes/cabal/next >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b master fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add cabal ../fetch-refmap-upstream &&\n+\t\tgit config --unset-all remote.cabal.fetch &&\n+\t\tgit config --add remote.cabal.fetch \\\n+\t\t\t\"+refs/heads/master:refs/remotes/custom/master\" &&\n+\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n+\t\tgit fetch cabal &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/cabal/HEAD\n+\t\trefs/remotes/cabal/master\n+\t\trefs/remotes/custom/master\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin other &&\n+\t\tgit branch --track topic origin/other &&\n+\t\ttest_cmp_config origin branch.topic.remote &&\n+\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n+\t\techo refs/remotes/origin/other >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch secret &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin secret &&\n+\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n+\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n+\t\ttest_grep \"not a branch\" err\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"556897","messageId":"3f5808cbf390f48ea41325dbe75ec76b17f6e97d.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 2/6] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:26Z","receivedAt":"2026-10-30T10:52:26Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we decide what to fetch from\ntwo sources: the remote's configured fetch refspec, and the current\nbranch's upstream, if it lives on that remote. For example, after\n\n    git clone --single-branch <url>\n    git switch -c work\n    git push -u origin work\n\nthe remote's refspec only covers \"main\", but a refspec-less \"git fetch\norigin\" still fetches \"work\" into FETCH_HEAD, so that a plain \"git\npull\" finds the branch to merge. See 49ca2fba39 (fetch: add\nbranch.*.merge to default ref-prefix extension, 2022-09-08).\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 18 ++++++++----------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 32 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 305934f5f4..31ec963db7 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1985,8 +1985,6 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n-\n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n@@ -2001,14 +1999,14 @@ static int do_fetch(struct transport *transport,\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 9acab2e01a..d4259fe7f3 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstrvec_push(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..003c8fd180 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"556898","messageId":"b02e0a53972a294d80618be7f7f3bbf2d2710234.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 3/6] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:27Z","receivedAt":"2026-10-30T10:52:27Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWe want remote.<name>.fetchRefmap to work on its own, without also\nsetting remote.<name>.fetch, so a remote can be limited to the\nbranches already in use locally, without listing them by hand or\ntouching the command line every time.\n\nInfer what to fetch from the local branches whose @{upstream} is\nalready on that remote. The refmap itself still only controls where\nthose fetched refs land, not which branches are selected.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   4 +-\n Documentation/fetch-options.adoc |  11 ++-\n builtin/fetch.c                  |  72 +++++++++++++----\n remote.c                         |  25 ++++++\n remote.h                         |   5 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++++++\n 7 files changed, 232 insertions(+), 19 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 236ccca93e..0d24abfcbd 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -38,7 +38,9 @@ remote.<name>.fetchRefmap::\n \tfetched to remote-tracking refs to store, the same way\n \t`remote.<name>.fetch` does for an explicit refspec.\n \t`remote.<name>.fetch`, if also configured, still decides what is\n-\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tfetched. If it is not set either, a refspec-less fetch instead\n+\tinfers what to fetch from local branches built on this remote.\n+\tSee the `--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..be27c03de7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,15 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, infer what to fetch from local branches built on\n+this remote: remote-tracking branches from this remote that are also\n+configured as the `@{upstream}` of a local branch are fetched. The\n+refmap only decides where those fetched refs land, the same as it\n+would for an explicit refspec, and does not narrow which branches are\n+selected. It is what turns this on, since without it the fetched\n+branches would have nowhere to land.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 31ec963db7..3a3cb311b7 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -531,17 +533,33 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \tif (effective_refmap)\n \t\tfilter_prefetch_refspec(effective_refmap);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct strvec tracked = STRVEC_INIT;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor (size_t i = 0; i < tracked.nr; i++)\n+\t\t\trefspec_append(&inferred_rs, tracked.v[i]);\n+\t\tstrvec_clear(&tracked);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -576,6 +594,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -675,6 +695,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1967,6 +1988,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head = FOLLOW_REMOTE_NEVER;\n+\tbool apply_follow_remote_head_config = false;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -1984,21 +2006,10 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n-\t} else {\n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\t/*\n-\t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n-\t\t\t * for the analogous, still-unresolved case.\n-\t\t\t */\n-\t\t\tif (transport->remote->follow_remote_head)\n-\t\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n-\t\t\telse if (config->follow_remote_head_seen)\n-\t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n-\t\t\telse\n-\t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n-\t\t}\n+\t} else if (transport->remote->fetch.nr) {\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tapply_follow_remote_head_config = true;\n \n \t\t/*\n \t\t * The configured refspec may not cover the current\n@@ -2007,6 +2018,22 @@ static int do_fetch(struct transport *transport,\n \t\t */\n \t\tcollect_upstream_from_remote(the_repository, transport->remote,\n \t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.fetchRefmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t  &transport_ls_refs_options.ref_prefixes);\n+\t\t\tapply_follow_remote_head_config = true;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t\t}\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\n@@ -2017,6 +2044,19 @@ static int do_fetch(struct transport *transport,\n \t\t\t\t    \"HEAD\");\n \t}\n \n+\tif (apply_follow_remote_head_config) {\n+\t\t/*\n+\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n+\t\t * for the analogous, still-unresolved case.\n+\t\t */\n+\t\tif (transport->remote->follow_remote_head)\n+\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n+\t\telse if (config->follow_remote_head_seen)\n+\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n+\t\telse\n+\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\t}\n+\n \tif (tags == TAGS_SET || tags == TAGS_DEFAULT)\n \t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t    \"refs/tags/\");\ndiff --git a/remote.c b/remote.c\nindex d4259fe7f3..15cd96aa8e 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstrvec_push(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tconst struct remote *remote;\n+\tstruct strvec *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->remote, ref->name,\n+\t\t\t\t      data->tracked);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex 003c8fd180..6fd9b05a79 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   const char *refname,\n \t\t\t\t   struct strvec *tracked);\n \n+/* Same as collect_upstream_from_remote(), but checked for every local branch. */\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 11b3752f00..0953a4112f 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -736,6 +736,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..d4214b12b0\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,133 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.fetchRefmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.fetchRefmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '\n+\ttest_commit main-3 &&\n+\t(\n+\t\tcd client &&\n+\t\tgit rev-parse refs/remotes/upstream/main >expect &&\n+\t\tgit fetch --prefetch upstream &&\n+\t\tgit rev-parse refs/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit -C .. rev-parse main >expect &&\n+\t\tgit rev-parse refs/prefetch/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"556899","messageId":"3ee427de87a0de19774fb9385f4f7496cc45ea11.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 4/6] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:28Z","receivedAt":"2026-10-30T10:52:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.fetchRefmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 315100409d..1f490819ba 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex fb7e5b114f..5f8a8a2f71 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetchRefmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..90cba75f93 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"556900","messageId":"2cba4623ee743a44a44f48e9be61dc8b1b99a913.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 5/6] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:29Z","receivedAt":"2026-10-30T10:52:29Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow clone used to still fetch every\nbranch that remote has, since \"git remote add\" always set up a\nwildcard remote.<name>.fetch refspec regardless of how shallow the\nrepository already was. That defeats the purpose of having cloned\nshallow in the first place (for example with --single-branch), and\ncan make a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 1f490819ba..245c0c4558 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 5f8a8a2f71..5d2388a26c 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 90cba75f93..68063a47b8 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.fetchRefmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n\n"},{"id":"556901","messageId":"25eec8d9676d4adf82570616ca88a2b38d20afa4.1793357550.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"[PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-30T10:52:30Z","receivedAt":"2026-10-30T10:52:30Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA branch pushed to a remote set up with \"git remote add\n--limited-fetch\" has no remote.<name>.fetch entry to map it\nthrough, so @{push} cannot tell where it landed and fails with\n\"has no local tracking branch\".\n\nGive tracking_for_push_dest() the same fallback to\nremote.<name>.fetchRefmap that get_ref_map() uses on the fetch\nside, so a --limited-fetch remote supports @{push} resolution\nthe same way any other remote does.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n remote.c                  |  2 ++\n t/t1514-rev-parse-push.sh | 19 +++++++++++++++++++\n 2 files changed, 21 insertions(+)\n\ndiff --git a/remote.c b/remote.c\nindex 15cd96aa8e..ecd104766c 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,\n \n \tremote = repo_remote_for_push_tracking(repo, remote);\n \tret = apply_refspecs(&remote->fetch, refname);\n+\tif (!ret)\n+\t\tret = apply_refspecs(&remote->refmap, refname);\n \tif (!ret)\n \t\treturn error_buf(err,\n \t\t\t\t _(\"push destination '%s' on remote '%s' has no local tracking branch\"),\ndiff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh\nindex 5a4f16867a..bcf00107f5 100755\n--- a/t/t1514-rev-parse-push.sh\n+++ b/t/t1514-rev-parse-push.sh\n@@ -80,4 +80,23 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '\n \ttest_must_fail git rev-parse @{push}\n '\n \n+test_expect_success '@{push} resolves after pushing to a --limited-fetch remote' '\n+\ttest_when_finished \"rm -fr upstream fork downstream\" &&\n+\tgit init -b main upstream &&\n+\ttest_commit -C upstream base &&\n+\tgit init --bare fork &&\n+\tgit clone --sparse upstream downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add --limited-fetch fork ../fork &&\n+\t\tgit config push.default current &&\n+\t\tgit config remote.pushDefault fork &&\n+\t\tgit checkout -b topic main &&\n+\t\ttest_commit on-topic &&\n+\t\tgit push &&\n+\t\tresolve topic@{push} refs/remotes/fork/topic &&\n+\t\ttest_cmp_rev topic topic@{push}\n+\t)\n+'\n+\n test_done\n-- \ngitgitgadget\n\n"},{"id":"557813","messageId":"CALnO6CB5uqHpeJm9QUC-J7n=RBVB+0pXRuMfaSQZt7mHoZbasg@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqmrrwywq8.fsf@gitster.g","subject":"Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-11-07T20:20:22Z","receivedAt":"2026-11-07T20:20:22Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"+Cc Kristoffer Haugsbakk as a fellow notes user ;)\n\nOn Thu, Oct 29, 2026 at 6:21 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Ben Knoble <ben.knoble@gmail.com> writes:\n>\n> > Yes, I would love to add pushRefmap atop later! It would\n> > ease my working with notes, I think.\n>\n> > Yes, I would love to add pushRefmap atop later! It would\n> > ease my working with notes, I think.\n>\n> Tell us more when you are off mobile ;-).\n>\n> I do not think I planned to add '--refmap' support on the push side,\n> even from the command line, but I can imagine there would be\n> reasonable workflows that benefit from having one.\n\nIn my local copy of git.git, I want to have both Junio's amlog for\nspelunking and my own notes for patches. I only want to push out my\nnotes + topics to my remote, though (I work on Git from different\nmachines and it's nice to have the notes sync'd on both). Here's the\nhack I ended up with, and it has some downsides.\n\nIn particular, \"git push\" in this configuration does too much, so I\nusually have to think about whether that's ok. A pushRefmap would let\nme separate what to push by default (push.default!) from how to map\npushing when I want to push, say, notes or other things. I have to be\nespecially careful with a \"git pf\" (push, force-with-lease,\nforce-if-includes), since I usually do *not* want to forcibly\noverwrite the remote notes---instead I should pull those down and\nmerge them†.\n\nSo, here's the relevant config (I have a remote \"benknoble\" and a\nremote \"origin\", among others):\n\n[core]\n        notesRef = refs/notes/benknoble/commits\n[notes]\n        rewriteRef = refs/notes/benknoble/commits\n        displayRef = refs/notes/origin/amlog\n[remote \"origin\"]\n        fetch = +refs/notes/amlog:refs/notes/origin/amlog\n\nThe \"usual\" stuff to make my notes important, but still show amlog\nnotes when browsing history. (The origin remote section is in addition\nto the normal remote section with it's url and heads->origin/\nmapping.)\n\n[remote]\n        pushDefault = benknoble\n\nPush to my remote.\n\n[remote \"benknoble\"]\n        fetch = refs/notes/*:refs/notes/benknoble/*\n        push = refs/heads/*:refs/heads/*\n        push = refs/notes/benknoble/*:refs/notes/*\n        push = ^refs/notes/origin/amlog\n\nIn addition to the usual config added by \"git remote add\", fetch\nremote notes into a local hierarchy, and push them out, but ignore\namlog, and don't forget to also be able to push heads. Without the\nheads map, \"git push\" will not push branches!\n\nNow, maybe I'm holding something in a way it wasn't intended and\nthat's made my life harder, I dunno.\n\n†: There isn't a great story for that, either :/ When \"git fetch\"\nfails to fetch notes because of divergence with local, I end up saying\n\n    git fetch benknoble refs/notes/commits\n    git range-diff refs/notes/benknoble/commits...FETCH_HEAD\n    git notes merge FETCH_HEAD\n\nor something.\n\nIt's *almost* like we need remote-tracking branches for notes, ahaha…\nat that point, Scott's git-meta work becomes possibly more appealing\nto me, even if a part of me wants to \"fix\" notes so everyone can use\nthem easily.\n\n-- \nD. Ben Knoble\n\n"},{"id":"558165","messageId":"CAHwyqnVJ3eUQ9tkQE3rhQ8jFZjaD0tFUv41unE-NQ1WRiyfjBA@mail.gmail.com","threadId":"66353","inReplyTo":"pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-11-11T08:31:29Z","receivedAt":"2026-11-11T08:31:29Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Hi, could I get some (hopefully final) reviews here?\n\n\n\nHarald\n\n"},{"id":"558216","messageId":"b64387b3-294e-49da-b40a-0e757956bafd@gmail.com","threadId":"66353","inReplyTo":"CAHwyqnVJ3eUQ9tkQE3rhQ8jFZjaD0tFUv41unE-NQ1WRiyfjBA@mail.gmail.com","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-11T14:12:50Z","receivedAt":"2026-11-11T14:12:50Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 11/11/2026 08:31, Harald Nordgren wrote:\n> Hi, could I get some (hopefully final) reviews here?\n\nThe diffstat for this series is\n\n12 files changed, 593 insertions(+), 33 deletions(-)\n\nwhich will take a considerable amount of time to review properly. Yet the cover letter which should explain the problem being solved, how it is solved (i.e. the design and any trade-offs that entails, alternatives that were discarded etc.) and anything notable in the implementation is just\n\nAvoid fetching every branch of a new remote in a shallow repo.\n\nI've no idea from that how these patches relate to the discussion on separating \"which refs to fetch\" from \"where to store those refs\" or anything else. If you want people to volunteer their time to review your patches you need to make a effort to explain what those patches do.\n\nThanks\n\nPhillip\n\n\n"},{"id":"558240","messageId":"xmqqcxsbnub5.fsf@gitster.g","threadId":"66353","inReplyTo":"b64387b3-294e-49da-b40a-0e757956bafd@gmail.com","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-11T17:37:18Z","receivedAt":"2026-11-11T17:37:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Hi Harald\n>\n> On 11/11/2026 08:31, Harald Nordgren wrote:\n>> Hi, could I get some (hopefully final) reviews here?\n>\n> The diffstat for this series is\n>\n> 12 files changed, 593 insertions(+), 33 deletions(-)\n>\n> which will take a considerable amount of time to review properly. Yet \n> the cover letter which should explain the problem being solved, how it \n> is solved (i.e. the design and any trade-offs that entails, alternatives \n> that were discarded etc.) and anything notable in the implementation is just\n>\n> Avoid fetching every branch of a new remote in a shallow repo.\n>\n> I've no idea from that how these patches relate to the discussion on \n> separating \"which refs to fetch\" from \"where to store those refs\" or \n> anything else. If you want people to volunteer their time to review your \n> patches you need to make a effort to explain what those patches do.\n\nI would say that it is a fair comment.\n\nI understand what the first three wants to do.\n\n 1. We have had for a long time \"git fetch --refmap=from:to remote refs...\"\n    given on the command line to decide how refs fetched are stored\n    separately from what refs are fetched.  This allows a configuration\n    to allow you to pretend that you give it all the time.\n\n 2. Preliminary refactoring of a helper that will gain second\n    callsite in the next step.\n\n 3. This allows \"git fetch --refmap=from:to remote\" that does not\n    specify what to fetch (contrast it with 1. above) to default to\n    fetch refs that our local branches build on.  The net effect\n    with configured remote.<remote>.fetchRefmap is that \"git fetch\n    remote\" would only fetch and store selected refs from them, only\n    the ones that we \"care about\" (aka \"build on\").\n    \nI do not have strong opinion on the other 3 patches.  4. and 5. may\nhelp ergonomics by allowing to set up refmap from the command line\noption, but from my point of view it looks more like icing on the\ncake.\n\nWhen we are working with only a subset of their branches, the first\nthing that comes to my mind is a \"matching push\" that does not need\nany fancy @{push} support.  Because of that, I am not interested in\n6. very much.\n\nIn any case, this topic one is keeping a topic by another author\nwaiting to settle, so we have stronger incentive to see it move past\nthe finish line.\n\nThanks.\n\n\n\n"},{"id":"558243","messageId":"CALnO6CB+AP=osF8mjkGGHWZz8YX322UoSjE6DCfYZkZ0rXxVcg@mail.gmail.com","threadId":"66353","inReplyTo":"25eec8d9676d4adf82570616ca88a2b38d20afa4.1793357550.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-11-11T18:27:02Z","receivedAt":"2026-11-11T18:27:02Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"Hi Harald,\n\nOn Fri, Oct 30, 2026 at 6:52 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> A branch pushed to a remote set up with \"git remote add\n> --limited-fetch\" has no remote.<name>.fetch entry to map it\n> through, so @{push} cannot tell where it landed and fails with\n> \"has no local tracking branch\".\n\nI don't have the rest of the series paged in right now (esp. patches 4\n& 5 which deal with --limited-fetch), so forgive me if this comes from\nignorance.\n\nWouldn't the same problem happen if we applied the first 3 patches,\nthen setup a repository's remote with a fetchRefmap and limited (or\nnone) default fetch?  If so, that almost makes this seem like a bugfix\nfor an earlier patch, and perhaps it belongs together? It also would\nthen be better justified in those terms, I think.\n\nSuch a reorganization would maybe let the early part of the series be\nmostly about fetchRefmap (which I'm keen on) and let the end be about\n--limited-fetch, which seems nice but I don't have a strong motivation\nto review right now (personally---it's just limited time to spend!).\n\n> Give tracking_for_push_dest() the same fallback to\n> remote.<name>.fetchRefmap that get_ref_map() uses on the fetch\n> side, so a --limited-fetch remote supports @{push} resolution\n> the same way any other remote does.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  remote.c                  |  2 ++\n>  t/t1514-rev-parse-push.sh | 19 +++++++++++++++++++\n>  2 files changed, 21 insertions(+)\n>\n> diff --git a/remote.c b/remote.c\n> index 15cd96aa8e..ecd104766c 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,\n>\n>         remote = repo_remote_for_push_tracking(repo, remote);\n>         ret = apply_refspecs(&remote->fetch, refname);\n> +       if (!ret)\n> +               ret = apply_refspecs(&remote->refmap, refname);\n\nOk, if we don't find the information in the fetch spec, we fallback to\nthe refmap. That also means the former overrides the latter in case\nboth are configured.\n\nDoes that match what a fetch does if both are configured and overlap?\n(I'm not sure, as I can't recall what we decided the fetching behavior\nis!)\n\n>         if (!ret)\n>                 return error_buf(err,\n>                                  _(\"push destination '%s' on remote '%s' has no local tracking branch\"),\n> diff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh\n> index 5a4f16867a..bcf00107f5 100755\n> --- a/t/t1514-rev-parse-push.sh\n> +++ b/t/t1514-rev-parse-push.sh\n> @@ -80,4 +80,23 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '\n>         test_must_fail git rev-parse @{push}\n>  '\n>\n> +test_expect_success '@{push} resolves after pushing to a --limited-fetch remote' '\n> +       test_when_finished \"rm -fr upstream fork downstream\" &&\n> +       git init -b main upstream &&\n> +       test_commit -C upstream base &&\n> +       git init --bare fork &&\n> +       git clone --sparse upstream downstream &&\n> +       (\n> +               cd downstream &&\n> +               git remote add --limited-fetch fork ../fork &&\n> +               git config push.default current &&\n> +               git config remote.pushDefault fork &&\n> +               git checkout -b topic main &&\n> +               test_commit on-topic &&\n> +               git push &&\n> +               resolve topic@{push} refs/remotes/fork/topic &&\n> +               test_cmp_rev topic topic@{push}\n> +       )\n> +'\n> +\n\nIf you take my suggestion to make this change with the early parts of\nthe series, the test would probably need to be in terms of fetchRefmap\ninstead of --limited-fetch, as well. That might be one reason to\nconsider leaving it at the end, but… really the \"problem\" is the\ninteraction of the earlier feature more than --limited-fetch, right?\n\nHTH,\n\n-- \nD. Ben Knoble\n\n"},{"id":"558244","messageId":"CALnO6CCj3Jgzz4OKyRA23UPvHCtigmWg+Y9WXvKTHBa7PTV=Tw@mail.gmail.com","threadId":"66353","inReplyTo":"xmqqcxsbnub5.fsf@gitster.g","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-11-11T18:35:07Z","receivedAt":"2026-11-11T18:35:07Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Wed, Nov 11, 2026 at 12:37 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> > Hi Harald\n> >\n> > On 11/11/2026 08:31, Harald Nordgren wrote:\n> >> Hi, could I get some (hopefully final) reviews here?\n> >\n> > The diffstat for this series is\n> >\n> > 12 files changed, 593 insertions(+), 33 deletions(-)\n> >\n> > which will take a considerable amount of time to review properly. Yet\n> > the cover letter which should explain the problem being solved, how it\n> > is solved (i.e. the design and any trade-offs that entails, alternatives\n> > that were discarded etc.) and anything notable in the implementation is just\n> >\n> > Avoid fetching every branch of a new remote in a shallow repo.\n> >\n> > I've no idea from that how these patches relate to the discussion on\n> > separating \"which refs to fetch\" from \"where to store those refs\" or\n> > anything else. If you want people to volunteer their time to review your\n> > patches you need to make a effort to explain what those patches do.\n>\n> I would say that it is a fair comment.\n\nAgreed. Sometimes the cover letter itself should evolve (beyond\n\"Changes since…\") as a series does.\n\n> I understand what the first three wants to do.\n>\n>  1. We have had for a long time \"git fetch --refmap=from:to remote refs...\"\n>     given on the command line to decide how refs fetched are stored\n>     separately from what refs are fetched.  This allows a configuration\n>     to allow you to pretend that you give it all the time.\n>\n>  2. Preliminary refactoring of a helper that will gain second\n>     callsite in the next step.\n>\n>  3. This allows \"git fetch --refmap=from:to remote\" that does not\n>     specify what to fetch (contrast it with 1. above) to default to\n>     fetch refs that our local branches build on.  The net effect\n>     with configured remote.<remote>.fetchRefmap is that \"git fetch\n>     remote\" would only fetch and store selected refs from them, only\n>     the ones that we \"care about\" (aka \"build on\").\n\nI like these 3 as well, at least in terms of direction. I suspect\nwe're approaching diminishing returns there?\n\n> I do not have strong opinion on the other 3 patches.  4. and 5. may\n> help ergonomics by allowing to set up refmap from the command line\n> option, but from my point of view it looks more like icing on the\n> cake.\n\nI think these are a reasonable idea, but I do not have time +\nmotivation to review them further.\n\n> When we are working with only a subset of their branches, the first\n> thing that comes to my mind is a \"matching push\" that does not need\n> any fancy @{push} support.  Because of that, I am not interested in\n> 6. very much.\n\nAs [6] was new to me, I've just given it a glance. It reminded me that\nI'm not sure how an overlapping fetch + fetchRefmap configuration\nbehaves (I think previously we said the command line version does both\nmappings). It's probably a good idea to make @{push} line up, but I'm\nnot sure how to manage the overlapping case.\n\n> In any case, this topic one is keeping a topic by another author\n> waiting to settle, so we have stronger incentive to see it move past\n> the finish line.\n\nI'm a bit out of sorts and don't recall what topic depends on this one\n(and \"git branch --remotes --contains …/hn/fetch-refmap\" didn't show\nany); is the dependent topic waiting on --limited-fetch or just the\nfetchRefmap parts? If the latter, after wrangling patch [6] we could\nmaybe merge those parts and give --limited-fetch time for more review?\nidk.\n\n-- \nD. Ben Knoble\n\n"},{"id":"558254","messageId":"xmqqeccrm33j.fsf@gitster.g","threadId":"66353","inReplyTo":"CALnO6CB+AP=osF8mjkGGHWZz8YX322UoSjE6DCfYZkZ0rXxVcg@mail.gmail.com","subject":"Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-11T22:10:24Z","receivedAt":"2026-11-11T22:10:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"D. Ben Knoble\" <ben.knoble@gmail.com> writes:\n\n>> A branch pushed to a remote set up with \"git remote add\n>> --limited-fetch\" has no remote.<name>.fetch entry to map it\n>> through, so @{push} cannot tell where it landed and fails with\n>> \"has no local tracking branch\".\n>\n> I don't have the rest of the series paged in right now (esp. patches 4\n> & 5 which deal with --limited-fetch), so forgive me if this comes from\n> ignorance.\n>\n> Wouldn't the same problem happen if we applied the first 3 patches,\n> then setup a repository's remote with a fetchRefmap and limited (or\n> none) default fetch?\n\nI think [4/6] and [5/6] would not change the picture, as they are\nabout making it easier to set remote.*.fetchRefmap configuration\nfrom a command.  You can do exactly the same with \"git config\" to\nset up the configuration and you will see the same thing.\n\n> If so, that almost makes this seem like a bugfix\n> for an earlier patch, and perhaps it belongs together?\n\nHmph, you may have a point.\n\nI am not a heavy user of @{push} to begin with, but if the chain of\nconfiguration through which we map the current branch to where it is\npushed to always assume remote.<remote>.fetch to exist, and does not\nconsult remote.<remote>.fetchRefmap, then yes, you could claim it as\na bug that is introduced by [3/6], I think.  \n\n> It also would then be better justified in those terms, I think.\n\nIt is unclear what you exactly mean by \"in those terms\", but I agree\nthat tweaking @{push} to take the new remote.<remote>.fetchRefmap\ninto account is a logical part of that.\n\n"},{"id":"558284","messageId":"01a799bc-5493-4984-8b83-7f2aef698f85@gmail.com","threadId":"66353","inReplyTo":"07081828d1d65373bee4b2ef5e1916e65a217db6.1793357550.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-12T11:00:37Z","receivedAt":"2026-11-12T11:00:37Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 30/10/2026 10:52, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> > Add a per-remote config variable, remote.<name>.fetchRefmap, that\n> provides the default value for --refmap the same way\n> remote.<name>.fetch already provides the default refspecs to fetch.\n> > Unlike passing --refmap on the command line, which requires an\n> explicit refspec to go with it, configuring remote.<name>.fetchRefmap\n> on its own does not change what a refspec-less fetch does: it falls\n> back to the same defaults as a remote with no fetchRefmap at all.\n> > When remote.<name>.fetch is also configured, it still decides what to\n> fetch, and the refmap maps what it fetches. This lets a remote name\n> only a few branches and store them all through one refmap:\n> >      [remote \"cabal\"]\n>          fetch = master\n>          fetch = next\n>          fetch = seen\n>          fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n\nWhat happens if remote.cabal.fetch is a refspec and remote.cabal.fetchRefmap is set?\n\nThanks\n\nPhillip\n\n> Looking up the remote-tracking branch for a remote branch, as\n> @{upstream} and \"git branch --track\" do, falls back to the refmap when\n> remote.<name>.fetch has no match, so that these work for such\n> branches too. A branch excluded by a negative refspec in\n> remote.<name>.fetch stays excluded.\n> > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>   Documentation/config/remote.adoc |   7 ++\n>   Documentation/fetch-options.adoc |   3 +\n>   builtin/fetch.c                  |  16 +++-\n>   remote.c                         |  20 +++-\n>   remote.h                         |   6 ++\n>   t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++\n>   6 files changed, 200 insertions(+), 3 deletions(-)\n> > diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\n> index 3a20d0f752..236ccca93e 100644\n> --- a/Documentation/config/remote.adoc\n> +++ b/Documentation/config/remote.adoc\n> @@ -33,6 +33,13 @@ remote.<name>.fetch::\n>   \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n>   \tlinkgit:git-fetch[1].\n>   > +remote.<name>.fetchRefmap::\n> +\tThe default value of the `--refmap` option, used to map refs being\n> +\tfetched to remote-tracking refs to store, the same way\n> +\t`remote.<name>.fetch` does for an explicit refspec.\n> +\t`remote.<name>.fetch`, if also configured, still decides what is\n> +\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n> +\n>   remote.<name>.push::\n>   \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n>   \tlinkgit:git-push[1].\n> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\n> index 47dea1de8e..c2101a7b39 100644\n> --- a/Documentation/fetch-options.adoc\n> +++ b/Documentation/fetch-options.adoc\n> @@ -244,6 +244,9 @@ endif::git-pull[]\n>   \trefspecs and rely entirely on the refspecs supplied as\n>   \tcommand-line arguments. See section on \"Configured Remote-tracking\n>   \tBranches\" for details.\n> ++\n> +`remote.<name>.refmap` provides the default value for this option, the\n> +same way `remote.<name>.fetch` provides the default refspecs to fetch.\n>   >   `-t`::\n>   `--tags`::\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 1f76ae6242..305934f5f4 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \tstruct ref *rm;\n>   \tstruct ref *ref_map = NULL;\n>   \tstruct ref **tail = &ref_map;\n> +\t/*\n> +\t * The --refmap command line option, if given, takes precedence\n> +\t * over remote.<name>.fetchRefmap.\n> +\t */\n> +\tstruct refspec *effective_refmap =\n> +\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n>   >   \t/* opportunistically-updated references: */\n>   \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n> @@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \tfilter_prefetch_refspec(rs);\n>   \tif (remote)\n>   \t\tfilter_prefetch_refspec(&remote->fetch);\n> +\tif (effective_refmap)\n> +\t\tfilter_prefetch_refspec(effective_refmap);\n>   >   \tif (rs->nr) {\n>   \t\tstruct refspec *fetch_refspec;\n> @@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \t\t * by ref_remove_duplicates() in favor of one of these\n>   \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n>   \t\t */\n> -\t\tif (refmap.nr)\n> -\t\t\tfetch_refspec = &refmap;\n> +\t\tif (effective_refmap && effective_refmap->nr)\n> +\t\t\tfetch_refspec = effective_refmap;\n>   \t\telse\n>   \t\t\tfetch_refspec = &remote->fetch;\n>   > @@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,\n>   \t\t\tif (has_merge &&\n>   \t\t\t    !strcmp(branch->remote_name, remote->name))\n>   \t\t\t\tadd_merge_config(&ref_map, remote_refs, branch, &tail);\n> +\n> +\t\t\tfor (i = 0; i < remote->refmap.nr; i++)\n> +\t\t\t\tget_fetch_map(ref_map, &remote->refmap.items[i],\n> +\t\t\t\t\t      &oref_tail, 1);\n>   \t\t} else if (!prefetch) {\n>   \t\t\tref_map = get_remote_ref(remote_refs, \"HEAD\");\n>   \t\t\tif (!ref_map)\n> diff --git a/remote.c b/remote.c\n> index 49ee2ea318..9acab2e01a 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n>   \tret->name = xstrndup(name, len);\n>   \trefspec_init_push(&ret->push, the_hash_algo);\n>   \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n> +\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n>   \tstring_list_init_dup(&ret->server_options);\n>   \tstring_list_init_dup(&ret->negotiation_restrict);\n>   \tstring_list_init_dup(&ret->negotiation_include);\n> @@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n>   >   \trefspec_clear(&remote->push);\n>   \trefspec_clear(&remote->fetch);\n> +\trefspec_clear(&remote->refmap);\n>   >   \tfree((char *)remote->receivepack);\n>   \tfree((char *)remote->uploadpack);\n> @@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n>   \t\t\treturn -1;\n>   \t\trefspec_append(&remote->fetch, v);\n>   \t\tfree(v);\n> +\t} else if (!strcmp(subkey, \"fetchrefmap\")) {\n> +\t\tchar *v;\n> +\t\tif (git_config_string(&v, key, value))\n> +\t\t\treturn -1;\n> +\t\trefspec_append(&remote->refmap, v);\n> +\t\tfree(v);\n>   \t} else if (!strcmp(subkey, \"receivepack\")) {\n>   \t\tchar *v;\n>   \t\tif (git_config_string(&v, key, value))\n> @@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)\n>   >   int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n>   {\n> -\treturn refspec_find_match(&remote->fetch, refspec);\n> +\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n> +\n> +\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n> +\t\treturn 0;\n> +\tif (refspec_find_match(&remote->refmap, refspec))\n> +\t\treturn -1;\n> +\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n> +\t\tFREE_AND_NULL(*result);\n> +\t\treturn -1;\n> +\t}\n> +\treturn 0;\n>   }\n>   >   static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n> diff --git a/remote.h b/remote.h\n> index cca02033b9..ac485a584d 100644\n> --- a/remote.h\n> +++ b/remote.h\n> @@ -90,6 +90,12 @@ struct remote {\n>   >   \tstruct refspec fetch;\n>   > +\t/*\n> +\t * How to map refs fetched without an explicit destination into our\n> +\t * own namespace, the same as the --refmap command line option.\n> +\t */\n> +\tstruct refspec refmap;\n> +\n>   \t/*\n>   \t * The setting for whether to fetch tags (as a separate rule from the\n>   \t * configured refspecs);\n> diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\n> index 17c0be499c..b5eba020ad 100755\n> --- a/t/t5510-fetch.sh\n> +++ b/t/t5510-fetch.sh\n> @@ -927,6 +927,157 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n>   \t)\n>   '\n>   > +test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '\n> +\ttest_when_finished \"git -C three config --unset remote.origin.fetchRefmap\" &&\n> +\tgit branch -f side &&\n> +\tgit -C three config remote.origin.fetchRefmap \\\n> +\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n> +\t(\n> +\t\tcd three &&\n> +\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n> +\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n> +\t\tgit fetch origin main &&\n> +\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n> +\t\ttest \"$o\" = \"$n\" &&\n> +\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n> +\t\tgit rev-parse --verify refs/remotes/other/main\n> +\t)\n> +'\n> +\n> +check_fetched_refs () {\n> +\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n> +\tcat >expect &&\n> +\ttest_cmp expect actual\n> +}\n> +\n> +test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n> +\tgit init -b main fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit -C fetch-refmap-upstream branch other &&\n> +\tgit init fetch-refmap-baseline &&\n> +\t(\n> +\t\tcd fetch-refmap-baseline &&\n> +\t\tgit remote add origin ../fetch-refmap-upstream &&\n> +\n> +\t\t# Without fetch refspec, but with fetch refmap.\n> +\t\tgit config --unset-all remote.origin.fetch &&\n> +\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\n> +\t\t# Nothing tracked, nothing fetched, no error.\n> +\t\tgit fetch origin &&\n> +\t\tcheck_fetched_refs <<-\\EOF &&\n> +\t\tEOF\n> +\n> +\t\t# Nothing tracked, explicit ref on the command line.\n> +\t\tgit fetch origin main &&\n> +\t\tcheck_fetched_refs <<-\\EOF &&\n> +\t\trefs/remotes/origin/main\n> +\t\tEOF\n> +\n> +\t\t# With both refmap and fetch configured, remote.<name>.fetch\n> +\t\t# wins: a refspec-less fetch follows it as usual, and\n> +\t\t# remote.<name>.fetchRefmap plays no part in deciding what to\n> +\t\t# fetch, only in mapping what is fetched.\n> +\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\t\tgit fetch origin &&\n> +\t\tcheck_fetched_refs <<-\\EOF\n> +\t\trefs/remotes/origin/HEAD\n> +\t\trefs/remotes/origin/main\n> +\t\trefs/remotes/origin/other\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n> +\tgit init -b master fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit -C fetch-refmap-upstream branch next &&\n> +\tgit -C fetch-refmap-upstream branch seen &&\n> +\tgit -C fetch-refmap-upstream branch other &&\n> +\tgit init downstream &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\tgit remote add cabal ../fetch-refmap-upstream &&\n> +\t\tgit config --unset-all remote.cabal.fetch &&\n> +\t\tgit config --add remote.cabal.fetch master &&\n> +\t\tgit config --add remote.cabal.fetch next &&\n> +\t\tgit config --add remote.cabal.fetch seen &&\n> +\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n> +\t\tgit fetch cabal &&\n> +\t\tcheck_fetched_refs <<-\\EOF &&\n> +\t\trefs/remotes/cabal/HEAD\n> +\t\trefs/remotes/cabal/master\n> +\t\trefs/remotes/cabal/next\n> +\t\trefs/remotes/cabal/seen\n> +\t\tEOF\n> +\t\tgit branch --track topic cabal/next &&\n> +\t\techo refs/remotes/cabal/next >expect &&\n> +\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n> +\tgit init -b master fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit init downstream &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\tgit remote add cabal ../fetch-refmap-upstream &&\n> +\t\tgit config --unset-all remote.cabal.fetch &&\n> +\t\tgit config --add remote.cabal.fetch \\\n> +\t\t\t\"+refs/heads/master:refs/remotes/custom/master\" &&\n> +\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n> +\t\tgit fetch cabal &&\n> +\t\tcheck_fetched_refs <<-\\EOF\n> +\t\trefs/remotes/cabal/HEAD\n> +\t\trefs/remotes/cabal/master\n> +\t\trefs/remotes/custom/master\n> +\t\tEOF\n> +\t)\n> +'\n> +\n> +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n> +\tgit init -b main fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit -C fetch-refmap-upstream branch other &&\n> +\tgit init downstream &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n> +\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\t\tgit fetch origin other &&\n> +\t\tgit branch --track topic origin/other &&\n> +\t\ttest_cmp_config origin branch.topic.remote &&\n> +\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n> +\t\techo refs/remotes/origin/other >expect &&\n> +\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n> +\tgit init -b main fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit -C fetch-refmap-upstream branch secret &&\n> +\tgit init downstream &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\tgit remote add origin ../fetch-refmap-upstream &&\n> +\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n> +\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n> +\t\tgit fetch origin secret &&\n> +\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n> +\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n> +\t\ttest_grep \"not a branch\" err\n> +\t)\n> +'\n> +\n>   test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n>   \tgit branch -f side &&\n>   \t(\n\n\n"},{"id":"558285","messageId":"5566b994-1070-41d3-807b-5fac9ddaa3d8@gmail.com","threadId":"66353","inReplyTo":"xmqqcxsbnub5.fsf@gitster.g","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-12T11:16:54Z","receivedAt":"2026-11-12T11:16:54Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 11/11/2026 17:37, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>> On 11/11/2026 08:31, Harald Nordgren wrote:\n>>> Hi, could I get some (hopefully final) reviews here?\n>>\n>> The diffstat for this series is\n>>\n>> 12 files changed, 593 insertions(+), 33 deletions(-)\n>>\n>> which will take a considerable amount of time to review properly. Yet\n>> the cover letter which should explain the problem being solved, how it\n>> is solved (i.e. the design and any trade-offs that entails, alternatives\n>> that were discarded etc.) and anything notable in the implementation is just\n>>\n>> Avoid fetching every branch of a new remote in a shallow repo.\n>>\n>> I've no idea from that how these patches relate to the discussion on\n>> separating \"which refs to fetch\" from \"where to store those refs\" or\n>> anything else. If you want people to volunteer their time to review your\n>> patches you need to make a effort to explain what those patches do.\n> > I would say that it is a fair comment.\n> > I understand what the first three wants to do.\n> >   1. We have had for a long time \"git fetch --refmap=from:to remote refs...\"\n>      given on the command line to decide how refs fetched are stored\n>      separately from what refs are fetched.  This allows a configuration\n>      to allow you to pretend that you give it all the time.\n> >   2. Preliminary refactoring of a helper that will gain second\n>      callsite in the next step.\n> >   3. This allows \"git fetch --refmap=from:to remote\" that does not\n>      specify what to fetch (contrast it with 1. above) to default to\n>      fetch refs that our local branches build on.  The net effect\n>      with configured remote.<remote>.fetchRefmap is that \"git fetch\n>      remote\" would only fetch and store selected refs from them, only\n>      the ones that we \"care about\" (aka \"build on\").\n\nThanks for the summary. I'm still not keen on patch 3 as I said in [1] I think it is a mistake to use the same variable for both \"what to fetch\" and \"where to store it\". After patch 1 we have the remote.<name>.fetchRefmap for \"where to store it\" and can use use remote.<name>.fetch for \"what to fetch\" (though I am concerned about the doubling up that happens when \"fetchRefmap\" is set and \"fetch\" is a refspec). We don't need a special case for when there are no refspecs on the command line - we can use the default values of those config settings instead and the user can configure the behavior that they want.\n\n> I do not have strong opinion on the other 3 patches.  4. and 5. may\n> help ergonomics by allowing to set up refmap from the command line\n> option, but from my point of view it looks more like icing on the\n> cake.\n\nI think having a way to set up fetching a limited set of branches is worthwhile, but it does not need to be part of the same series.\n\n> When we are working with only a subset of their branches, the first\n> thing that comes to my mind is a \"matching push\" that does not need\n> any fancy @{push} support.  Because of that, I am not interested in\n> 6. very much.\n\nI've not looked at what that patch does\n\n> In any case, this topic one is keeping a topic by another author\n> waiting to settle, so we have stronger incentive to see it move past\n> the finish line.\n\nWhat topic is that? - I can't see anything marked as depending on this in the latest \"what's cooking\" email. Would merging the first part of this series unblock the other one?\n\nThanks\n\nPhillip\n\n"},{"id":"558297","messageId":"DMTC5DR3ROPP.5V7HNV9ERYFT@lfurio.us","threadId":"66353","inReplyTo":"5566b994-1070-41d3-807b-5fac9ddaa3d8@gmail.com","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Matt Hunter","fromEmail":"m@lfurio.us","sentAt":"2026-11-12T15:25:49Z","receivedAt":"2026-11-12T15:25:49Z","isPatch":true,"sender":{"key":"m@lfurio.us","avatar":"https://avatars.githubusercontent.com/u/14925125?v=4"},"body":"On Thu Nov 12, 2026 at 6:16 AM EST, Phillip Wood wrote:\n> On 11/11/2026 17:37, Junio C Hamano wrote:\n>\n>> In any case, this topic one is keeping a topic by another author\n>> waiting to settle, so we have stronger incentive to see it move past\n>> the finish line.\n>\n> What topic is that? - I can't see anything marked as depending on this\n> in the latest \"what's cooking\" email. Would merging the first part of\n> this series unblock the other one?\n\nI believe that is ch/remote-defer-follow-remote-head-validation, which\ndoesn't strictly depend on the changes from this topic, but the two have\ntextual conflicts that the other author is deferring to resolve when\nthis one lands.\n\n<CAHeTm9M5maWM=yrJPNzCibv_rewH3-yBK8J5-MDyDvPzfihe4A@mail.gmail.com>\n\nIt looks like if this topic were merged up to patch 3/6 (fetch: infer\nbranches to fetch from a refmap-only remote), the situation could\nresolve.\n\n"},{"id":"558334","messageId":"xmqqv761hltu.fsf@gitster.g","threadId":"66353","inReplyTo":"01a799bc-5493-4984-8b83-7f2aef698f85@gmail.com","subject":"Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-12T19:49:17Z","receivedAt":"2026-11-12T19:49:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> When remote.<name>.fetch is also configured, it still decides what to\n>> fetch, and the refmap maps what it fetches. This lets a remote name\n>> only a few branches and store them all through one refmap:\n>> \n>>      [remote \"cabal\"]\n>>          fetch = master\n>>          fetch = next\n>>          fetch = seen\n>>          fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n>\n> What happens if remote.cabal.fetch is a refspec and \n> remote.cabal.fetchRefmap is set?\n\nGood question.  But \"fetch = master\" is a refspec already ;-), so\nthe question needs to be clarified a bit.\n\n    If .fetch is a refspec with a right hand side that stores the\n    result somewhere, and that somewhere is different from what the\n    .fetchRefmap suggests should happen, what happens?\n\nI think we tried earlier and saw that we store them twice [*], even\nwithout this series (i.e., specifying both refspec and refmap from\nthe commmand line).  As it is a long-standing behavior since the\n--refmap feature was introduced, and I do not see huge problem with\nthat behavior, I am not sure if it is worth tweaking.  But if it is\nwroth tweaking, we probably should do so as a preliminary clean-up\nbefore all of this, since .fetchRefmap is merely a way to allow you\nto avoid having to specify --refmap from the command line all the\ntime, and this series does not change its semantics until step\n[3/6].\n\n[Reference]\n\n * https://lore.kernel.org/git/xmqqld7hafhy.fsf@gitster.g/\n\n"},{"id":"558375","messageId":"5125fcec-7764-416c-ab78-58cc22cd415d@gmail.com","threadId":"66353","inReplyTo":"xmqqv761hltu.fsf@gitster.g","subject":"Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-13T09:48:34Z","receivedAt":"2026-11-13T09:48:34Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 12/11/2026 19:49, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >>> When remote.<name>.fetch is also configured, it still decides what to\n>>> fetch, and the refmap maps what it fetches. This lets a remote name\n>>> only a few branches and store them all through one refmap:\n>>>\n>>>       [remote \"cabal\"]\n>>>           fetch = master\n>>>           fetch = next\n>>>           fetch = seen\n>>>           fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n>>\n> >      If .fetch is a refspec with a right hand side that stores the\n>      result somewhere, and that somewhere is different from what the\n>      .fetchRefmap suggests should happen, what happens?\n> > I think we tried earlier and saw that we store them twice [*], even\n> without this series (i.e., specifying both refspec and refmap from\n> the commmand line).  As it is a long-standing behavior since the\n> --refmap feature was introduced, and I do not see huge problem with\n> that behavior, I am not sure if it is worth tweaking.\n\nIf we want to be able to use \"remote.<name>.fetchRefmap\" to specify how to map the names of fetched refs without specifying the default set of refs to fetch then I don't think we want\n\n    git -c 'remote.cabal.fetchRefmap=refs/heads/*:refs/remotes/cabal/*' master feature:wip/feature\n\nto update \"refs/remotes/cabal/feature\" when we gave a destination for \"feature\" on the command line. The existing behavior is long-standing but I wonder how often people specify a destination in the commandline refspec when they pass --refmap, so would anyone really notice if we changed it?\n\n>  But if it is\n> wroth tweaking, we probably should do so as a preliminary clean-up\n> before all of this, since .fetchRefmap is merely a way to allow you\n> to avoid having to specify --refmap from the command line all the\n> time, and this series does not change its semantics until step\n> [3/6].\n\nI agree with having a separate patch to handle the cleanup, though as I said elsewhere I think we should drop patch 3 and use the combination of \".fetchRefmap\" and \".fetch\" to set the default behavior when there are no refspecs on the commandline.\n\nThanks\n\nPhillip\n\n> [Reference]\n> >   * https://lore.kernel.org/git/xmqqld7hafhy.fsf@gitster.g/\n\n\n"},{"id":"558462","messageId":"CAHwyqnWuQgBW=aDbGaCwm0mwCdDTQAZYBxJRZ18wwRC8Brbwrw@mail.gmail.com","threadId":"66353","inReplyTo":"CALnO6CB+AP=osF8mjkGGHWZz8YX322UoSjE6DCfYZkZ0rXxVcg@mail.gmail.com","subject":"Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-11-14T16:10:28Z","receivedAt":"2026-11-14T16:10:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> Wouldn't the same problem happen if we applied the first 3 patches,\n> then setup a repository's remote with a fetchRefmap and limited (or\n> none) default fetch?  If so, that almost makes this seem like a bugfix\n> for an earlier patch, and perhaps it belongs together? It also would\n> then be better justified in those terms, I think.\n\nThat's an important point, and I will take a look at fixing this. Good\nto have your keen eye here.\n\n\nHarald\n\n"},{"id":"558471","messageId":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.git.git.1789829246437.gitgitgadget@gmail.com","subject":"[PATCH v14 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:11Z","receivedAt":"2026-11-14T18:38:11Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"In a shallow single-branch clone of a large repository, git remote add\nupstream <url> sets up the default +refs/heads/*:refs/remotes/upstream/*\nrefspec. The next git fetch upstream then downloads every branch of that\nremote, which defeats the point of a shallow clone.\n\nThis series lets a remote fetch only the branches that local branches track:\n\ngit remote add --limited-fetch upstream <url>\ngit fetch upstream main\ngit branch --set-upstream-to=upstream/main\ngit fetch upstream    # fetches only main from now on\n\n\n * Patches 1 and 2 add remote.<name>.fetchRefmap, a configured default for\n   --refmap, and a small helper refactor.\n * Patch 3 makes a remote with a fetchRefmap and no fetch refspec work on\n   its own. A plain git fetch fetches the branches whose @{upstream} is on\n   that remote, and @{push} resolves through the refmap.\n * Patch 4 adds git remote add --limited-fetch to set up such a remote.\n * Patch 5 makes --limited-fetch the default for git remote add in a shallow\n   repository.\n\nChanges in v14:\n\n * Squashed the @{push} fix into \"fetch: infer branches to fetch from a\n   refmap-only remote\", so the series no longer adds the bug in one patch\n   and fixes it in a later one\n * Moved its test to t5586 using the refmap-only remote, so it no longer\n   depends on --limited-fetch\n * Dropped the separate \"remote: resolve @{push} through\n   remote..fetchRefmap\" commit\n\nChanges in v13:\n\n * Added a new commit so @{push} resolves correctly for a --limited-fetch\n   remote, falling back to remote.<name>.fetchRefmap the same way fetch\n   already does when remote.<name>.fetch doesn't cover the branch being\n   pushed.\n * Added a regression test to fetch: add remote.<name>.fetchRefmap covering\n   a remote.<name>.fetch entry with an explicit destination, confirming it's\n   stored in both that destination and the refmap's.\n\nChanges in v12:\n\n * Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout\n   code, docs, and tests, so it reads unambiguously as fetch-only.\n * Added the missing code path in get_ref_map() so a branch fetched by name\n   via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap,\n   with a new test (the \"cabal\" example: fetch = master/next/seen plus a\n   refmap storing them all under one namespace).\n * Reworded the fetchRefmap commit message and doc entry to drop a redundant\n   sentence, fix a confusing paragraph, and spell out that\n   remote.<name>.fetch, when configured, still decides what's fetched while\n   the refmap only remaps it.\n * Added a prefetch coverage test to t5586-fetch-refmap.sh.\n\nChanges in v11:\n\n * remote_find_tracking() now falls back to remote.<name>.refmap when\n   remote.<name>.fetch doesn't match. Negative refspecs in fetch still\n   exclude refs, and both cases have tests\n * The helpers write straight into a strvec, take the output parameter last\n   and the remote as const\n * The followRemoteHEAD lookup is done once, not duplicated in two branches\n * Clearer commit messages and docs: a working example plus a reference to\n   49ca2fba39, the goal stated first, an incorrect \"used to die\" claim\n   dropped, and an explanation of why a refmap turns on the inference\n * use_limited_fetch is a bool\n\nChanges in v10:\n\n * Resolve merge conflict after master integrated\n   ch/fetch-followremotehead-lazy-validation.\n\nChanges in v9:\n\n * Moved the do_fetch() if/else-if/else split out of the preliminary\n   helper-extraction commit and into the refmap-inference commit that needs\n   it. Preliminary commit is now a simpler refactor.\n\nChanges in v8:\n\n * Split into a preliminary collect_upstream_from_remote() helper commit\n   plus a simplified refmap-inference commit.\n * Fixed a regression that could drop the current branch's @{upstream} when\n   remote.<name>.fetch was narrowed.\n * Threaded struct repository * through instead of hardcoding\n   the_repository.\n * Minor naming and comment cleanups.\n\nChanges in v7:\n\n * Fixed fatal: --refmap option is only meaningful with command-line\n   refspec(s) firing just from configuring remote.<name>.refmap alone, with\n   nothing else to fetch.\n * When both remote.<name>.fetch and remote.<name>.refmap are configured, a\n   refspec-less fetch follows .fetch as usual, .refmap only remaps something\n   already being fetched by name. Added tests.\n\nChanges in v6:\n\n * Remove leftover reference to deleted default-branch logic in commit\n   message.\n\nChanges in v5:\n\n * Renumber test file from t5585 to t5586.\n\nChanges in v4:\n\n * Removed the automatic default-branch fetch. A fresh remote fetches\n   nothing until you track a branch explicitly.\n * Fixed fetch report showing \"new ref HEAD\" instead of \"new branch \"\n * Reworded remote..refmap docs and commit message.\n\nChanges in v3:\n\n * Replace the special \":\"/\"+:\" fetch refspec with remote.<name>.refmap,\n   reusing git's existing --refmap mechanism instead of inventing new\n   refspec syntax.\n * Split the change into 4 commits.\n\nChanges in v2:\n\n * Replaced the opt-in fetch.shallow config entirely with a new special\n   fetch refspec (+:) that git remote add now defaults new remotes to in a\n   shallow repository. The new refspec fetches whichever branches any local\n   branch tracks at that remote, plus the remote's default branch.\n\nHarald Nordgren (5):\n  fetch: add remote.<name>.fetchRefmap\n  fetch: extract collect_upstream_from_remote() helper\n  fetch: infer branches to fetch from a refmap-only remote\n  remote: add \"git remote add --limited-fetch\"\n  remote: default to --limited-fetch in a shallow repository\n\n Documentation/config/remote.adoc |   9 ++\n Documentation/fetch-options.adoc |  10 ++\n Documentation/git-remote.adoc    |  14 ++-\n builtin/fetch.c                  | 100 +++++++++++++++-----\n builtin/remote.c                 |  31 +++++--\n remote.c                         |  62 ++++++++++++-\n remote.h                         |  20 ++++\n t/meson.build                    |   1 +\n t/t5505-remote.sh                |  76 ++++++++++++++++\n t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++\n t/t5586-fetch-refmap.sh          | 147 ++++++++++++++++++++++++++++++\n 11 files changed, 588 insertions(+), 33 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\n\nbase-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v14\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v14\nPull-Request: https://github.com/git/git/pull/2412\n\nRange-diff vs v13:\n\n 1:  07081828d1 = 1:  c63699b402 fetch: add remote.<name>.fetchRefmap\n 2:  3f5808cbf3 = 2:  4ad94eefe9 fetch: extract collect_upstream_from_remote() helper\n 3:  b02e0a5397 ! 3:  6724152e69 fetch: infer branches to fetch from a refmap-only remote\n     @@ Commit message\n          already on that remote. The refmap itself still only controls where\n          those fetched refs land, not which branches are selected.\n      \n     +    Let @{push} use the refmap too, so a branch pushed to such a remote\n     +    resolves to the remote-tracking branch the push updated, instead of\n     +    failing with \"has no local tracking branch\".\n     +\n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Documentation/config/remote.adoc ##\n     @@ remote.c: void collect_upstream_from_remote(struct repository *repo,\n       __attribute__((format (printf,2,3)))\n       static char *error_buf(struct strbuf *err, const char *fmt, ...)\n       {\n     +@@ remote.c: static char *tracking_for_push_dest(struct repository *repo,\n     + \n     + \tremote = repo_remote_for_push_tracking(repo, remote);\n     + \tret = apply_refspecs(&remote->fetch, refname);\n     ++\tif (!ret)\n     ++\t\tret = apply_refspecs(&remote->refmap, refname);\n     + \tif (!ret)\n     + \t\treturn error_buf(err,\n     + \t\t\t\t _(\"push destination '%s' on remote '%s' has no local tracking branch\"),\n      \n       ## remote.h ##\n      @@ remote.h: void collect_upstream_from_remote(struct repository *repo,\n     @@ t/t5586-fetch-refmap.sh (new)\n      +\t)\n      +'\n      +\n     ++test_expect_success '@{push} resolves for a branch pushed to a refmap-only remote' '\n     ++\t(\n     ++\t\tcd client &&\n     ++\t\tgit config push.default current &&\n     ++\t\tgit checkout -b topic upstream/main &&\n     ++\t\ttest_commit topic-1 &&\n     ++\t\tgit push &&\n     ++\t\techo refs/remotes/upstream/topic >expect &&\n     ++\t\tgit rev-parse --symbolic-full-name topic@{push} >actual &&\n     ++\t\ttest_cmp expect actual &&\n     ++\t\ttest_cmp_rev topic topic@{push}\n     ++\t)\n     ++'\n     ++\n      +test_done\n 4:  3ee427de87 = 4:  6cff6786ba remote: add \"git remote add --limited-fetch\"\n 5:  2cba4623ee = 5:  00f9d44a88 remote: default to --limited-fetch in a shallow repository\n 6:  25eec8d967 < -:  ---------- remote: resolve @{push} through remote.<name>.fetchRefmap\n\n-- \ngitgitgadget\n\n"},{"id":"558472","messageId":"c63699b40228b1d47368778d7397b1ddc6ba2a36.1794681496.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","subject":"[PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:12Z","receivedAt":"2026-11-14T18:38:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdd a per-remote config variable, remote.<name>.fetchRefmap, that\nprovides the default value for --refmap the same way\nremote.<name>.fetch already provides the default refspecs to fetch.\n\nUnlike passing --refmap on the command line, which requires an\nexplicit refspec to go with it, configuring remote.<name>.fetchRefmap\non its own does not change what a refspec-less fetch does: it falls\nback to the same defaults as a remote with no fetchRefmap at all.\n\nWhen remote.<name>.fetch is also configured, it still decides what to\nfetch, and the refmap maps what it fetches. This lets a remote name\nonly a few branches and store them all through one refmap:\n\n    [remote \"cabal\"]\n        fetch = master\n        fetch = next\n        fetch = seen\n        fetchRefmap = +refs/heads/*:refs/remotes/cabal/*\n\nLooking up the remote-tracking branch for a remote branch, as\n@{upstream} and \"git branch --track\" do, falls back to the refmap when\nremote.<name>.fetch has no match, so that these work for such\nbranches too. A branch excluded by a negative refspec in\nremote.<name>.fetch stays excluded.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   7 ++\n Documentation/fetch-options.adoc |   3 +\n builtin/fetch.c                  |  16 +++-\n remote.c                         |  20 +++-\n remote.h                         |   6 ++\n t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++\n 6 files changed, 200 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 3a20d0f752..236ccca93e 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -33,6 +33,13 @@ remote.<name>.fetch::\n \tThe default set of \"refspec\" for linkgit:git-fetch[1]. See\n \tlinkgit:git-fetch[1].\n \n+remote.<name>.fetchRefmap::\n+\tThe default value of the `--refmap` option, used to map refs being\n+\tfetched to remote-tracking refs to store, the same way\n+\t`remote.<name>.fetch` does for an explicit refspec.\n+\t`remote.<name>.fetch`, if also configured, still decides what is\n+\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\n \tlinkgit:git-push[1].\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex 47dea1de8e..c2101a7b39 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -244,6 +244,9 @@ endif::git-pull[]\n \trefspecs and rely entirely on the refspecs supplied as\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n++\n+`remote.<name>.refmap` provides the default value for this option, the\n+same way `remote.<name>.fetch` provides the default refspecs to fetch.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex f588dba2f0..e54bf52d86 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct ref *rm;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n+\t/*\n+\t * The --refmap command line option, if given, takes precedence\n+\t * over remote.<name>.fetchRefmap.\n+\t */\n+\tstruct refspec *effective_refmap =\n+\t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n+\tif (effective_refmap)\n+\t\tfilter_prefetch_refspec(effective_refmap);\n \n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t * by ref_remove_duplicates() in favor of one of these\n \t\t * opportunistic entries with FETCH_HEAD_IGNORE.\n \t\t */\n-\t\tif (refmap.nr)\n-\t\t\tfetch_refspec = &refmap;\n+\t\tif (effective_refmap && effective_refmap->nr)\n+\t\t\tfetch_refspec = effective_refmap;\n \t\telse\n \t\t\tfetch_refspec = &remote->fetch;\n \n@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,\n \t\t\tif (has_merge &&\n \t\t\t    !strcmp(branch->remote_name, remote->name))\n \t\t\t\tadd_merge_config(&ref_map, remote_refs, branch, &tail);\n+\n+\t\t\tfor (i = 0; i < remote->refmap.nr; i++)\n+\t\t\t\tget_fetch_map(ref_map, &remote->refmap.items[i],\n+\t\t\t\t\t      &oref_tail, 1);\n \t\t} else if (!prefetch) {\n \t\t\tref_map = get_remote_ref(remote_refs, \"HEAD\");\n \t\t\tif (!ref_map)\ndiff --git a/remote.c b/remote.c\nindex 98665a3085..26f42fb6d1 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,\n \tret->name = xstrndup(name, len);\n \trefspec_init_push(&ret->push, the_hash_algo);\n \trefspec_init_fetch(&ret->fetch, the_hash_algo);\n+\trefspec_init_fetch(&ret->refmap, the_hash_algo);\n \tstring_list_init_dup(&ret->server_options);\n \tstring_list_init_dup(&ret->negotiation_restrict);\n \tstring_list_init_dup(&ret->negotiation_include);\n@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)\n \n \trefspec_clear(&remote->push);\n \trefspec_clear(&remote->fetch);\n+\trefspec_clear(&remote->refmap);\n \n \tfree((char *)remote->receivepack);\n \tfree((char *)remote->uploadpack);\n@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,\n \t\t\treturn -1;\n \t\trefspec_append(&remote->fetch, v);\n \t\tfree(v);\n+\t} else if (!strcmp(subkey, \"fetchrefmap\")) {\n+\t\tchar *v;\n+\t\tif (git_config_string(&v, key, value))\n+\t\t\treturn -1;\n+\t\trefspec_append(&remote->refmap, v);\n+\t\tfree(v);\n \t} else if (!strcmp(subkey, \"receivepack\")) {\n \t\tchar *v;\n \t\tif (git_config_string(&v, key, value))\n@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)\n \n int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)\n {\n-\treturn refspec_find_match(&remote->fetch, refspec);\n+\tchar **result = refspec->src ? &refspec->dst : &refspec->src;\n+\n+\tif (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))\n+\t\treturn 0;\n+\tif (refspec_find_match(&remote->refmap, refspec))\n+\t\treturn -1;\n+\tif (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {\n+\t\tFREE_AND_NULL(*result);\n+\t\treturn -1;\n+\t}\n+\treturn 0;\n }\n \n static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\ndiff --git a/remote.h b/remote.h\nindex cca02033b9..ac485a584d 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -90,6 +90,12 @@ struct remote {\n \n \tstruct refspec fetch;\n \n+\t/*\n+\t * How to map refs fetched without an explicit destination into our\n+\t * own namespace, the same as the --refmap command line option.\n+\t */\n+\tstruct refspec refmap;\n+\n \t/*\n \t * The setting for whether to fetch tags (as a separate rule from the\n \t * configured refspecs);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex ef260994a7..499d2a3649 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -927,6 +927,157 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '\n \t)\n '\n \n+test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '\n+\ttest_when_finished \"git -C three config --unset remote.origin.fetchRefmap\" &&\n+\tgit branch -f side &&\n+\tgit -C three config remote.origin.fetchRefmap \\\n+\t\t\"refs/heads/*:refs/remotes/other/*\" &&\n+\t(\n+\t\tcd three &&\n+\t\tgit update-ref refs/remotes/origin/main base-origin-main &&\n+\t\to=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\tgit fetch origin main &&\n+\t\tn=$(git rev-parse --verify refs/remotes/origin/main) &&\n+\t\ttest \"$o\" = \"$n\" &&\n+\t\ttest_must_fail git rev-parse --verify refs/remotes/origin/side &&\n+\t\tgit rev-parse --verify refs/remotes/other/main\n+\t)\n+'\n+\n+check_fetched_refs () {\n+\tgit for-each-ref --format=\"%(refname)\" refs/remotes/ >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '\n+\ttest_when_finished \"rm -fr fetch-refmap-baseline fetch-refmap-upstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init fetch-refmap-baseline &&\n+\t(\n+\t\tcd fetch-refmap-baseline &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\n+\t\t# Without fetch refspec, but with fetch refmap.\n+\t\tgit config --unset-all remote.origin.fetch &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\n+\t\t# Nothing tracked, nothing fetched, no error.\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\tEOF\n+\n+\t\t# Nothing tracked, explicit ref on the command line.\n+\t\tgit fetch origin main &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/origin/main\n+\t\tEOF\n+\n+\t\t# With both refmap and fetch configured, remote.<name>.fetch\n+\t\t# wins: a refspec-less fetch follows it as usual, and\n+\t\t# remote.<name>.fetchRefmap plays no part in deciding what to\n+\t\t# fetch, only in mapping what is fetched.\n+\t\tgit config remote.origin.fetch \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/origin/HEAD\n+\t\trefs/remotes/origin/main\n+\t\trefs/remotes/origin/other\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b master fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch next &&\n+\tgit -C fetch-refmap-upstream branch seen &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add cabal ../fetch-refmap-upstream &&\n+\t\tgit config --unset-all remote.cabal.fetch &&\n+\t\tgit config --add remote.cabal.fetch master &&\n+\t\tgit config --add remote.cabal.fetch next &&\n+\t\tgit config --add remote.cabal.fetch seen &&\n+\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n+\t\tgit fetch cabal &&\n+\t\tcheck_fetched_refs <<-\\EOF &&\n+\t\trefs/remotes/cabal/HEAD\n+\t\trefs/remotes/cabal/master\n+\t\trefs/remotes/cabal/next\n+\t\trefs/remotes/cabal/seen\n+\t\tEOF\n+\t\tgit branch --track topic cabal/next &&\n+\t\techo refs/remotes/cabal/next >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b master fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add cabal ../fetch-refmap-upstream &&\n+\t\tgit config --unset-all remote.cabal.fetch &&\n+\t\tgit config --add remote.cabal.fetch \\\n+\t\t\t\"+refs/heads/master:refs/remotes/custom/master\" &&\n+\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n+\t\tgit fetch cabal &&\n+\t\tcheck_fetched_refs <<-\\EOF\n+\t\trefs/remotes/cabal/HEAD\n+\t\trefs/remotes/cabal/master\n+\t\trefs/remotes/custom/master\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch other &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add -t main origin ../fetch-refmap-upstream &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin other &&\n+\t\tgit branch --track topic origin/other &&\n+\t\ttest_cmp_config origin branch.topic.remote &&\n+\t\ttest_cmp_config refs/heads/other branch.topic.merge &&\n+\t\techo refs/remotes/origin/other >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{upstream} >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '\n+\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n+\tgit init -b main fetch-refmap-upstream &&\n+\ttest_commit -C fetch-refmap-upstream base &&\n+\tgit -C fetch-refmap-upstream branch secret &&\n+\tgit init downstream &&\n+\t(\n+\t\tcd downstream &&\n+\t\tgit remote add origin ../fetch-refmap-upstream &&\n+\t\tgit config --add remote.origin.fetch \"^refs/heads/secret\" &&\n+\t\tgit config remote.origin.fetchRefmap \"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit fetch origin secret &&\n+\t\tgit rev-parse --verify refs/remotes/origin/secret &&\n+\t\ttest_must_fail git branch --track topic origin/secret 2>err &&\n+\t\ttest_grep \"not a branch\" err\n+\t)\n+'\n+\n test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '\n \tgit branch -f side &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"558473","messageId":"4ad94eefe97a2b92333c5675346d50756d0ff7b6.1794681496.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","subject":"[PATCH v14 2/5] fetch: extract collect_upstream_from_remote() helper","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:13Z","receivedAt":"2026-11-14T18:38:13Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWhen a fetch has no refspec to work from, we decide what to fetch from\ntwo sources: the remote's configured fetch refspec, and the current\nbranch's upstream, if it lives on that remote. For example, after\n\n    git clone --single-branch <url>\n    git switch -c work\n    git push -u origin work\n\nthe remote's refspec only covers \"main\", but a refspec-less \"git fetch\norigin\" still fetches \"work\" into FETCH_HEAD, so that a plain \"git\npull\" finds the branch to merge. See 49ca2fba39 (fetch: add\nbranch.*.merge to default ref-prefix extension, 2022-09-08).\n\nThat \"does this branch build on this remote\" check was inlined for\njust the current branch. Move it into its own function,\ncollect_upstream_from_remote(), so the same logic can be reused for\nbranches other than just the current one.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/fetch.c | 18 ++++++++----------\n remote.c        | 15 +++++++++++++++\n remote.h        |  9 +++++++++\n 3 files changed, 32 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex e54bf52d86..3614d576dc 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -2002,8 +2002,6 @@ static int do_fetch(struct transport *transport,\n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n \t} else {\n-\t\tstruct branch *branch = branch_get(NULL);\n-\n \t\tif (transport->remote->fetch.nr) {\n \t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n \t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n@@ -2018,14 +2016,14 @@ static int do_fetch(struct transport *transport,\n \t\t\telse\n \t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n \t\t}\n-\t\tif (branch && branch_has_merge_config(branch) &&\n-\t\t    !strcmp(branch->remote_name, transport->remote->name)) {\n-\t\t\tint i;\n-\t\t\tfor (i = 0; i < branch->merge_nr; i++) {\n-\t\t\t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n-\t\t\t\t\t    branch->merge[i]->src);\n-\t\t\t}\n-\t\t}\n+\n+\t\t/*\n+\t\t * The configured refspec may not cover the current\n+\t\t * branch's upstream (e.g. a narrowed -t refspec), so\n+\t\t * make sure we can still fetch it regardless.\n+\t\t */\n+\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\ndiff --git a/remote.c b/remote.c\nindex 26f42fb6d1..4e250ba393 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,\n \treturn refname_match(branch->merge[i]->src, refname);\n }\n \n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked)\n+{\n+\tstruct branch *branch = repo_branch_get(repo, refname);\n+\n+\tif (!branch_has_merge_config(branch) ||\n+\t    strcmp(branch->remote_name, remote->name))\n+\t\treturn;\n+\n+\tfor (int i = 0; i < branch->merge_nr; i++)\n+\t\tstrvec_push(tracked, branch->merge[i]->src);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\ndiff --git a/remote.h b/remote.h\nindex ac485a584d..003c8fd180 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);\n \n int branch_merge_matches(struct branch *, int n, const char *);\n \n+/*\n+ * If refname's branch builds on remote, add its upstream on remote to\n+ * tracked. A NULL refname means the current branch.\n+ */\n+void collect_upstream_from_remote(struct repository *repo,\n+\t\t\t\t   const struct remote *remote,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\n-- \ngitgitgadget\n\n\n"},{"id":"558474","messageId":"6724152e69a605a3677c26f221f9e9ef30f606b9.1794681496.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","subject":"[PATCH v14 3/5] fetch: infer branches to fetch from a refmap-only remote","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:14Z","receivedAt":"2026-11-14T18:38:14Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nWe want remote.<name>.fetchRefmap to work on its own, without also\nsetting remote.<name>.fetch, so a remote can be limited to the\nbranches already in use locally, without listing them by hand or\ntouching the command line every time.\n\nInfer what to fetch from the local branches whose @{upstream} is\nalready on that remote. The refmap itself still only controls where\nthose fetched refs land, not which branches are selected.\n\nLet @{push} use the refmap too, so a branch pushed to such a remote\nresolves to the remote-tracking branch the push updated, instead of\nfailing with \"has no local tracking branch\".\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/config/remote.adoc |   4 +-\n Documentation/fetch-options.adoc |  11 ++-\n builtin/fetch.c                  |  72 +++++++++++----\n remote.c                         |  27 ++++++\n remote.h                         |   5 ++\n t/meson.build                    |   1 +\n t/t5586-fetch-refmap.sh          | 147 +++++++++++++++++++++++++++++++\n 7 files changed, 248 insertions(+), 19 deletions(-)\n create mode 100755 t/t5586-fetch-refmap.sh\n\ndiff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc\nindex 236ccca93e..0d24abfcbd 100644\n--- a/Documentation/config/remote.adoc\n+++ b/Documentation/config/remote.adoc\n@@ -38,7 +38,9 @@ remote.<name>.fetchRefmap::\n \tfetched to remote-tracking refs to store, the same way\n \t`remote.<name>.fetch` does for an explicit refspec.\n \t`remote.<name>.fetch`, if also configured, still decides what is\n-\tfetched. See the `--refmap` entry in linkgit:git-fetch[1].\n+\tfetched. If it is not set either, a refspec-less fetch instead\n+\tinfers what to fetch from local branches built on this remote.\n+\tSee the `--refmap` entry in linkgit:git-fetch[1].\n \n remote.<name>.push::\n \tThe default set of \"refspec\" for linkgit:git-push[1]. See\ndiff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc\nindex c2101a7b39..be27c03de7 100644\n--- a/Documentation/fetch-options.adoc\n+++ b/Documentation/fetch-options.adoc\n@@ -245,8 +245,15 @@ endif::git-pull[]\n \tcommand-line arguments. See section on \"Configured Remote-tracking\n \tBranches\" for details.\n +\n-`remote.<name>.refmap` provides the default value for this option, the\n-same way `remote.<name>.fetch` provides the default refspecs to fetch.\n+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but\n+nothing to fetch is specified on the command line, nor is there a\n+`remote.<name>.fetch`, infer what to fetch from local branches built on\n+this remote: remote-tracking branches from this remote that are also\n+configured as the `@{upstream}` of a local branch are fetched. The\n+refmap only decides where those fetched refs land, the same as it\n+would for an explicit refspec, and does not narrow which branches are\n+selected. It is what turns this on, since without it the fetched\n+branches would have nowhere to land.\n \n `-t`::\n `--tags`::\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 3614d576dc..47c42b0c08 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -524,6 +524,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \t */\n \tstruct refspec *effective_refmap =\n \t\trefmap.nr ? &refmap : remote ? &remote->refmap : NULL;\n+\tstruct refspec inferred_rs;\n+\tint infer_from_refmap = 0;\n \n \t/* opportunistically-updated references: */\n \tstruct ref *orefs = NULL, **oref_tail = &orefs;\n@@ -531,17 +533,33 @@ static struct ref *get_ref_map(struct remote *remote,\n \tstruct hashmap existing_refs;\n \tint existing_refs_populated = 0;\n \n+\trefspec_init_fetch(&inferred_rs, the_hash_algo);\n+\n \tfilter_prefetch_refspec(rs);\n \tif (remote)\n \t\tfilter_prefetch_refspec(&remote->fetch);\n \tif (effective_refmap)\n \t\tfilter_prefetch_refspec(effective_refmap);\n \n+\tif (!rs->nr && remote && !remote->fetch.nr &&\n+\t    effective_refmap && effective_refmap->nr) {\n+\t\tstruct strvec tracked = STRVEC_INIT;\n+\n+\t\tbranches_tracking_remote(the_repository, remote, &tracked);\n+\t\tfor (size_t i = 0; i < tracked.nr; i++)\n+\t\t\trefspec_append(&inferred_rs, tracked.v[i]);\n+\t\tstrvec_clear(&tracked);\n+\n+\t\trs = &inferred_rs;\n+\t\tinfer_from_refmap = 1;\n+\t}\n+\n \tif (rs->nr) {\n \t\tstruct refspec *fetch_refspec;\n \n \t\tfor (i = 0; i < rs->nr; i++) {\n-\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail, 0);\n+\t\t\tget_fetch_map(remote_refs, &rs->items[i], &tail,\n+\t\t\t\t      infer_from_refmap);\n \t\t\tif (rs->items[i].dst && rs->items[i].dst[0])\n \t\t\t\t*autotags = 1;\n \t\t}\n@@ -576,6 +594,8 @@ static struct ref *get_ref_map(struct remote *remote,\n \n \t\tfor (i = 0; i < fetch_refspec->nr; i++)\n \t\t\tget_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);\n+\t} else if (infer_from_refmap) {\n+\t\t/* Already fully handled above. */\n \t} else if (refmap.nr) {\n \t\tdie(\"--refmap option is only meaningful with command-line refspec(s)\");\n \t} else {\n@@ -675,6 +695,7 @@ static struct ref *get_ref_map(struct remote *remote,\n \tif (existing_refs_populated)\n \t\thashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);\n \n+\trefspec_clear(&inferred_rs);\n \treturn ref_map;\n }\n \n@@ -1984,6 +2005,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head = FOLLOW_REMOTE_NEVER;\n+\tbool apply_follow_remote_head_config = false;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -2001,21 +2023,10 @@ static int do_fetch(struct transport *transport,\n \n \tif (rs->nr) {\n \t\trefspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);\n-\t} else {\n-\t\tif (transport->remote->fetch.nr) {\n-\t\t\trefspec_ref_prefixes(&transport->remote->fetch,\n-\t\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n-\t\t\t/*\n-\t\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n-\t\t\t * for the analogous, still-unresolved case.\n-\t\t\t */\n-\t\t\tif (transport->remote->follow_remote_head)\n-\t\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n-\t\t\telse if (config->follow_remote_head_seen)\n-\t\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n-\t\t\telse\n-\t\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n-\t\t}\n+\t} else if (transport->remote->fetch.nr) {\n+\t\trefspec_ref_prefixes(&transport->remote->fetch,\n+\t\t\t\t     &transport_ls_refs_options.ref_prefixes);\n+\t\tapply_follow_remote_head_config = true;\n \n \t\t/*\n \t\t * The configured refspec may not cover the current\n@@ -2024,6 +2035,22 @@ static int do_fetch(struct transport *transport,\n \t\t */\n \t\tcollect_upstream_from_remote(the_repository, transport->remote,\n \t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t} else {\n+\t\t/*\n+\t\t * The --refmap command line option, if given, takes\n+\t\t * precedence over remote.<name>.fetchRefmap.\n+\t\t */\n+\t\tstruct refspec *effective_refmap = refmap.nr ? &refmap :\n+\t\t\t&transport->remote->refmap;\n+\n+\t\tif (effective_refmap->nr) {\n+\t\t\tbranches_tracking_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t  &transport_ls_refs_options.ref_prefixes);\n+\t\t\tapply_follow_remote_head_config = true;\n+\t\t} else {\n+\t\t\tcollect_upstream_from_remote(the_repository, transport->remote,\n+\t\t\t\t\t\t      NULL, &transport_ls_refs_options.ref_prefixes);\n+\t\t}\n \n \t\t/*\n \t\t * If there are no refs specified to fetch, then we just\n@@ -2034,6 +2061,19 @@ static int do_fetch(struct transport *transport,\n \t\t\t\t    \"HEAD\");\n \t}\n \n+\tif (apply_follow_remote_head_config) {\n+\t\t/*\n+\t\t * See remote.c's handling of remote.<name>.followRemoteHEAD\n+\t\t * for the analogous, still-unresolved case.\n+\t\t */\n+\t\tif (transport->remote->follow_remote_head)\n+\t\t\tfollow_remote_head = transport->remote->follow_remote_head;\n+\t\telse if (config->follow_remote_head_seen)\n+\t\t\tfollow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);\n+\t\telse\n+\t\t\tfollow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;\n+\t}\n+\n \tif (tags == TAGS_SET || tags == TAGS_DEFAULT)\n \t\tstrvec_push(&transport_ls_refs_options.ref_prefixes,\n \t\t\t    \"refs/tags/\");\ndiff --git a/remote.c b/remote.c\nindex 4e250ba393..989986d2a2 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\tstrvec_push(tracked, branch->merge[i]->src);\n }\n \n+struct branches_tracking_remote_cb_data {\n+\tstruct repository *repo;\n+\tconst struct remote *remote;\n+\tstruct strvec *tracked;\n+};\n+\n+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)\n+{\n+\tstruct branches_tracking_remote_cb_data *data = cb_data;\n+\n+\tcollect_upstream_from_remote(data->repo, data->remote, ref->name,\n+\t\t\t\t      data->tracked);\n+\treturn 0;\n+}\n+\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked)\n+{\n+\tstruct branches_tracking_remote_cb_data data = { repo, remote, tracked };\n+\n+\trefs_for_each_branch_ref(get_main_ref_store(repo),\n+\t\t\t\t  add_if_tracking_remote, &data);\n+}\n+\n __attribute__((format (printf,2,3)))\n static char *error_buf(struct strbuf *err, const char *fmt, ...)\n {\n@@ -1993,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,\n \n \tremote = repo_remote_for_push_tracking(repo, remote);\n \tret = apply_refspecs(&remote->fetch, refname);\n+\tif (!ret)\n+\t\tret = apply_refspecs(&remote->refmap, refname);\n \tif (!ret)\n \t\treturn error_buf(err,\n \t\t\t\t _(\"push destination '%s' on remote '%s' has no local tracking branch\"),\ndiff --git a/remote.h b/remote.h\nindex 003c8fd180..6fd9b05a79 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,\n \t\t\t\t   const char *refname,\n \t\t\t\t   struct strvec *tracked);\n \n+/* Same as collect_upstream_from_remote(), but checked for every local branch. */\n+void branches_tracking_remote(struct repository *repo,\n+\t\t\t       const struct remote *remote,\n+\t\t\t       struct strvec *tracked);\n+\n /* list of the remote in a group as configured */\n struct remote_group_data {\n \tconst char *name;\ndiff --git a/t/meson.build b/t/meson.build\nindex 1cf1d95f70..92f877da1c 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -737,6 +737,7 @@ integration_tests = [\n   't5582-fetch-negative-refspec.sh',\n   't5583-push-branches.sh',\n   't5584-http-429-retry.sh',\n+  't5586-fetch-refmap.sh',\n   't5600-clone-fail-cleanup.sh',\n   't5601-clone.sh',\n   't5602-clone-remote-exec.sh',\ndiff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh\nnew file mode 100755\nindex 0000000000..7b54861fb1\n--- /dev/null\n+++ b/t/t5586-fetch-refmap.sh\n@@ -0,0 +1,147 @@\n+#!/bin/sh\n+\n+test_description='\"git fetch\" with a remote.<name>.fetchRefmap but no remote.<name>.fetch\n+\n+When a remote has a refmap configured but no fetch refspec, a\n+refspec-less fetch infers what to fetch from the local branches whose\n+@{upstream} is on that remote.\n+'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit main-1 &&\n+\ttest_commit main-2 &&\n+\tgit checkout -b side main-1 &&\n+\ttest_commit side-1 &&\n+\tgit checkout -b next main-1 &&\n+\ttest_commit next-1 &&\n+\tgit checkout main\n+'\n+\n+test_expect_success 'clone shallow and single-branch, then add a second remote' '\n+\tgit clone --no-local --depth=1 --branch main --single-branch . client &&\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add upstream .. &&\n+\t\ttest_might_fail git config unset remote.upstream.fetch &&\n+\t\tgit config remote.upstream.fetchRefmap \\\n+\t\t\t\"+refs/heads/*:refs/remotes/upstream/*\"\n+\t)\n+'\n+\n+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\ttest_must_be_empty actual\n+\t)\n+'\n+\n+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge\n+\t)\n+'\n+\n+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream side:refs/remotes/upstream/side &&\n+\t\tgit branch side-topic upstream/side\n+\t) &&\n+\tgit checkout side &&\n+\ttest_commit side-2 &&\n+\tgit checkout main &&\n+\t(\n+\t\tcd client &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse refs/remotes/upstream/side >actual-oid &&\n+\t\tgit -C .. rev-parse side >expect-oid &&\n+\t\ttest_cmp expect-oid actual-oid\n+\t)\n+'\n+\n+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '\n+\t(\n+\t\tcd client &&\n+\t\tgit branch side-topic-2 upstream/side &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote add other .. &&\n+\t\tgit fetch other next:refs/remotes/other/next &&\n+\t\tgit branch next-topic other/next &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\trefs/remotes/upstream/side\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '\n+\ttest_commit main-3 &&\n+\t(\n+\t\tcd client &&\n+\t\tgit rev-parse refs/remotes/upstream/main >expect &&\n+\t\tgit fetch --prefetch upstream &&\n+\t\tgit rev-parse refs/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit -C .. rev-parse main >expect &&\n+\t\tgit rev-parse refs/prefetch/remotes/upstream/main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git remote show does not choke on a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit remote show upstream\n+\t)\n+'\n+\n+test_expect_success '@{push} resolves for a branch pushed to a refmap-only remote' '\n+\t(\n+\t\tcd client &&\n+\t\tgit config push.default current &&\n+\t\tgit checkout -b topic upstream/main &&\n+\t\ttest_commit topic-1 &&\n+\t\tgit push &&\n+\t\techo refs/remotes/upstream/topic >expect &&\n+\t\tgit rev-parse --symbolic-full-name topic@{push} >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\ttest_cmp_rev topic topic@{push}\n+\t)\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n\n"},{"id":"558475","messageId":"6cff6786bae51c9a2ab73b522095dc63d1861010.1794681496.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","subject":"[PATCH v14 4/5] remote: add \"git remote add --limited-fetch\"","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:15Z","receivedAt":"2026-11-14T18:38:15Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nA remote added the ordinary way tracks every branch it has, via a\nwildcard remote.<name>.fetch refspec. That is wasteful for a remote\nwhose history is only worth following for the branches actually in\nuse locally, and it can make \"git fetch\" negotiate history for\nbranches nobody asked for.\n\nGive \"git remote add\" a --limited-fetch option that sets up\nremote.<name>.fetchRefmap instead of remote.<name>.fetch, so that\n\"git fetch <name>\" only fetches the branches already tracked, as\ndescribed in the previous commit. It is rejected together with\n-t/--track or --mirror, since those already say explicitly what to\nfetch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 +++++++-\n builtin/remote.c              | 28 ++++++++++++++++++++++------\n t/t5505-remote.sh             | 16 ++++++++++++++++\n 3 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 315100409d..1f490819ba 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [synopsis]\n git remote [-v | --verbose]\n-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>\n+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>\n git remote rename [--[no-]progress] <old> <new>\n git remote remove <name>\n git remote set-head <name> (-a | --auto | -d | --delete | <branch>)\n@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_\n is created.  You can give more than one `-t <branch>` to track\n multiple branches without grabbing all branches.\n +\n+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n+refspec-less `git fetch <name>` only fetches branches our local branches\n+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n+details.\n++\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\n +\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex fb7e5b114f..5f8a8a2f71 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,\n {\n \tint fetch = 0, fetch_tags = TAGS_DEFAULT;\n \tunsigned mirror = MIRROR_NONE;\n+\tint limited_fetch = -1; /* unspecified */\n \tstruct string_list track = STRING_LIST_INIT_NODUP;\n \tconst char *master = NULL;\n \tstruct remote *remote;\n@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tOPT_CALLBACK_F(0, \"mirror\", &mirror, \"(push|fetch)\",\n \t\t\tN_(\"set up remote as a mirror to push to or fetch from\"),\n \t\t\tPARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),\n+\t\tOPT_BOOL(0, \"limited-fetch\", &limited_fetch,\n+\t\t\tN_(\"fetch only the branches we build on, instead of every branch\")),\n \t\tOPT_END()\n \t};\n \n@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,\n \t\tdie(_(\"specifying a master branch makes no sense with --mirror\"));\n \tif (mirror && !(mirror & MIRROR_FETCH) && track.nr)\n \t\tdie(_(\"specifying branches to track makes sense only with fetch mirrors\"));\n+\tif (limited_fetch == 1 && track.nr)\n+\t\tdie(_(\"--limited-fetch does not make sense with -t/--track\"));\n+\tif (limited_fetch == 1 && mirror)\n+\t\tdie(_(\"--limited-fetch does not make sense with --mirror\"));\n \n \tname = argv[0];\n \turl = argv[1];\n@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,\n \trepo_config_set(the_repository, buf.buf, url);\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n+\t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n+\t\t\tlimited_fetch == 1;\n+\n \t\tstrbuf_reset(&buf);\n-\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n-\t\tif (track.nr == 0)\n-\t\t\tstring_list_append(&track, \"*\");\n-\t\tfor (size_t i = 0; i < track.nr; i++) {\n-\t\t\tadd_branch(buf.buf, track.items[i].string,\n-\t\t\t\t   name, mirror, &buf2);\n+\t\tif (use_limited_fetch) {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetchRefmap\", name);\n+\t\t\tstrbuf_reset(&buf2);\n+\t\t\tstrbuf_addf(&buf2, \"+refs/heads/*:refs/remotes/%s/*\", name);\n+\t\t\trepo_config_set(the_repository, buf.buf, buf2.buf);\n+\t\t} else {\n+\t\t\tstrbuf_addf(&buf, \"remote.%s.fetch\", name);\n+\t\t\tif (track.nr == 0)\n+\t\t\t\tstring_list_append(&track, \"*\");\n+\t\t\tfor (size_t i = 0; i < track.nr; i++)\n+\t\t\t\tadd_branch(buf.buf, track.items[i].string,\n+\t\t\t\t\t   name, mirror, &buf2);\n \t\t}\n \t}\n \ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 5fbfcb0848..90cba75f93 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success '--limited-fetch works in a full repository too' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add --limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success '--limited-fetch conflicts with -t' '\n+\ttest_must_fail git remote add --limited-fetch -t main upstream ../two\n+'\n+\n test_expect_success 'check remote-tracking' '\n \t(\n \t\tcd test &&\n-- \ngitgitgadget\n\n\n"},{"id":"558476","messageId":"00f9d44a88e2bb187a508869ba1b7bf937afc111.1794681496.git.gitgitgadget@gmail.com","threadId":"66353","inReplyTo":"pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com","subject":"[PATCH v14 5/5] remote: default to --limited-fetch in a shallow repository","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-14T18:38:16Z","receivedAt":"2026-11-14T18:38:16Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAdding a second remote to a shallow clone used to still fetch every\nbranch that remote has, since \"git remote add\" always set up a\nwildcard remote.<name>.fetch refspec regardless of how shallow the\nrepository already was. That defeats the purpose of having cloned\nshallow in the first place (for example with --single-branch), and\ncan make a plain \"git fetch\" on that remote hang or take a very long\ntime on a repository with many branches.\n\nTurn --limited-fetch on by default when the repository is already\nshallow and neither -t/--track nor --mirror was given, so that\nadding a remote there does not by itself commit to following every\nbranch it has. --no-limited-fetch keeps the previous behavior for\nwhoever wants it.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-remote.adoc |  8 ++++-\n builtin/remote.c              |  5 ++-\n t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++\n 3 files changed, 71 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc\nindex 1f490819ba..245c0c4558 100644\n--- a/Documentation/git-remote.adoc\n+++ b/Documentation/git-remote.adoc\n@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at\n _<URL>_.  The command `git fetch <name>` can then be used to create and\n update remote-tracking branches `<name>/<branch>`.\n +\n+If the repository is already a shallow repository (see linkgit:git-clone[1]\n+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,\n+`--limited-fetch` is turned on by default, so that `git fetch <name>` does\n+not need to negotiate history for every branch the remote has.\n++\n With `-f` option, `git fetch <name>` is run immediately after\n the remote information is set up.\n +\n@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec\n that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a\n refspec-less `git fetch <name>` only fetches branches our local branches\n are built on. See the `--refmap` entry in linkgit:git-fetch[1] for\n-details.\n+details. `--no-limited-fetch` explicitly disables this, overriding the\n+shallow-repository default described above.\n +\n With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set\n up to point at remote's _<master>_ branch. See also the set-head command.\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 5f8a8a2f71..5d2388a26c 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -16,6 +16,7 @@\n #include \"rebase.h\"\n #include \"refs.h\"\n #include \"refspec.h\"\n+#include \"shallow.h\"\n #include \"odb.h\"\n #include \"strvec.h\"\n #include \"commit-reach.h\"\n@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,\n \n \tif (!mirror || mirror & MIRROR_FETCH) {\n \t\tbool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&\n-\t\t\tlimited_fetch == 1;\n+\t\t\t(limited_fetch == 1 ||\n+\t\t\t (limited_fetch == -1 &&\n+\t\t\t  is_repository_shallow(the_repository)));\n \n \t\tstrbuf_reset(&buf);\n \t\tif (use_limited_fetch) {\ndiff --git a/t/t5505-remote.sh b/t/t5505-remote.sh\nindex 90cba75f93..68063a47b8 100755\n--- a/t/t5505-remote.sh\n+++ b/t/t5505-remote.sh\n@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '\n \ttest_grep ! \"\\[blob:none\\]\" out\n '\n \n+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add -t main upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/main:refs/remotes/upstream/main\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '\n+\ttest_when_finished \"rm -rf full-add\" &&\n+\tgit clone --no-local one full-add &&\n+\t(\n+\t\tcd full-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch\n+\t)\n+'\n+\n+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetchRefmap &&\n+\t\ttest_must_fail git config get remote.upstream.fetch &&\n+\t\tgit fetch upstream main &&\n+\t\tgit branch --set-upstream-to=upstream/main &&\n+\t\ttest_cmp_config upstream branch.main.remote &&\n+\t\ttest_cmp_config refs/heads/main branch.main.merge &&\n+\t\tgit fetch upstream &&\n+\t\tgit for-each-ref --format=\"%(refname)\" refs/remotes/upstream >actual &&\n+\t\tcat >expect <<-\\EOF &&\n+\t\trefs/remotes/upstream/HEAD\n+\t\trefs/remotes/upstream/main\n+\t\tEOF\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '\n+\ttest_when_finished \"rm -rf shallow-add\" &&\n+\tgit clone --no-local --depth=1 --branch main --single-branch \\\n+\t\tone shallow-add &&\n+\t(\n+\t\tcd shallow-add &&\n+\t\tgit remote add --no-limited-fetch upstream ../two &&\n+\t\ttest_cmp_config \"+refs/heads/*:refs/remotes/upstream/*\" \\\n+\t\t\tremote.upstream.fetch &&\n+\t\ttest_must_fail git config get remote.upstream.fetchRefmap\n+\t)\n+'\n+\n test_expect_success '--limited-fetch works in a full repository too' '\n \ttest_when_finished \"rm -rf full-add\" &&\n \tgit clone --no-local one full-add &&\n-- \ngitgitgadget\n\n"},{"id":"558608","messageId":"1276fcfe-9f2c-4a2c-ade9-b88f33ae5d84@gmail.com","threadId":"66353","inReplyTo":"DMTC5DR3ROPP.5V7HNV9ERYFT@lfurio.us","subject":"Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-17T11:08:36Z","receivedAt":"2026-11-17T11:08:36Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Matt\n\nOn 12/11/2026 15:25, Matt Hunter wrote:\n> On Thu Nov 12, 2026 at 6:16 AM EST, Phillip Wood wrote:\n>> On 11/11/2026 17:37, Junio C Hamano wrote:\n>>\n>>> In any case, this topic one is keeping a topic by another author\n>>> waiting to settle, so we have stronger incentive to see it move past\n>>> the finish line.\n>>\n>> What topic is that? - I can't see anything marked as depending on this\n>> in the latest \"what's cooking\" email. Would merging the first part of\n>> this series unblock the other one?\n> > I believe that is ch/remote-defer-follow-remote-head-validation, which\n> doesn't strictly depend on the changes from this topic, but the two have\n> textual conflicts that the other author is deferring to resolve when\n> this one lands.\n> > <CAHeTm9M5maWM=yrJPNzCibv_rewH3-yBK8J5-MDyDvPzfihe4A@mail.gmail.com>\n> > It looks like if this topic were merged up to patch 3/6 (fetch: infer\n> branches to fetch from a refmap-only remote), the situation could\n> resolve.\n\nThanks for the link. For this topic we're still waiting for a response to this part of [1] from three weeks ago\n\n> For now, as this \"what is tracked by the local branches\" is the only\n> kind of smart that was designed to decide what to fetch, it may be OK\n> to use \"you gave me nothing to specify what to fetch, so I'll guess\n> using the smart\".  But when we introduce more kinds of smart later,\n> this becomes a problem.  So I do agree with you that we should have\n> something to specifically say \"when I do not explicitly give you\n> what to fetch, decide based on what local branches track\".\n\nso perhaps we should give that other topic priority while we wait.\n\nThanks\n\nPhillip\n\n[1] https://lore.kernel.org/git/xmqqh5i8tqjx.fsf@gitster.g/\n\n"},{"id":"558621","messageId":"5a1a9f6b-48c8-4daa-9059-bf9459587a17@gmail.com","threadId":"66353","inReplyTo":"c63699b40228b1d47368778d7397b1ddc6ba2a36.1794681496.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-17T15:03:11Z","receivedAt":"2026-11-17T15:03:11Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 14/11/2026 18:38, Harald Nordgren via GitGitGadget wrote:\n> > +test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '\n> +\ttest_when_finished \"rm -fr fetch-refmap-upstream downstream\" &&\n> +\tgit init -b master fetch-refmap-upstream &&\n> +\ttest_commit -C fetch-refmap-upstream base &&\n> +\tgit init downstream &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\tgit remote add cabal ../fetch-refmap-upstream &&\n> +\t\tgit config --unset-all remote.cabal.fetch &&\n> +\t\tgit config --add remote.cabal.fetch \\\n> +\t\t\t\"+refs/heads/master:refs/remotes/custom/master\" &&\n> +\t\tgit config remote.cabal.fetchRefmap \"+refs/heads/*:refs/remotes/cabal/*\" &&\n> +\t\tgit fetch cabal &&\n> +\t\tcheck_fetched_refs <<-\\EOF\n> +\t\trefs/remotes/cabal/HEAD\n> +\t\trefs/remotes/cabal/master\n> +\t\trefs/remotes/custom/master\n> +\t\tEOF\n> +\t)\n> +'\n\nThis behavior arguably isn't too bad when \"--refmap\" is given on the command line because the user can simply adjust the refmap argument or the commandline refspec if they don't want to ref to be stored in two places. The same is true for when the refmap comes from the \".fetchRefmap\" config and the refspec comes form the \".fetch\" config. Both those cases could probably be filed under \"if it hurts don't do it\". However, I think it becomes confusing if we store the ref in two places when the user configures \".fetchRefmap\" and then passes \"src:dst\" on the commandline because users are used to a commandline \"src:dst\" overriding the \".fetch\" config, not adding to it. It is going to be very awkward for users if they cannot override \".fetchRefmap\" by passing \"src:dst\" on the commandline. I think we should change the behavior so that \"--refmap\" and the \".fetchRefmap\" config are ignored when the user includes an explicit destination on the commandline or the in \".fetch\" config.\n\nThanks\n\nPhillip\n\n\n"},{"id":"558624","messageId":"xmqq7bibh3f5.fsf@gitster.g","threadId":"66353","inReplyTo":"5a1a9f6b-48c8-4daa-9059-bf9459587a17@gmail.com","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-17T15:40:30Z","receivedAt":"2026-11-17T15:40:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> However, I think it becomes confusing if we store the ref in two \n> places when the user configures \".fetchRefmap\" and then passes \"src:dst\" \n> on the commandline because users are used to a commandline \"src:dst\" \n> overriding the \".fetch\" config, not adding to it.\n\nTrue, but the src:dst on the command line in your example is still\noverriding the .fetch configuration.  Wouldn't it make it confusing\nif it overrode .fetchRefmap configuration, since src:dst on the\ncommand line is what .fetch configuration gives the defaults for\n(and .fetchRefmap configuration gives the defaults for --refmap, not\nthe refspec like src:dst).  It may be just as confusing as if, say, a\ncommand line --refmap suddenly started overriding .fetch\nconfiguration, I am afraid.\n\nI can buy it if the suggestion was to allow passing --no-refmap from\nthe command line (or would --refmap=\"\" work?) to ignore .fetchRefmap\nconfiguration, though.\n\nThanks.\n\n"},{"id":"558628","messageId":"c40477c7-e1d8-4af1-859b-b739dac4bc86@gmail.com","threadId":"66353","inReplyTo":"xmqq7bibh3f5.fsf@gitster.g","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-17T16:28:42Z","receivedAt":"2026-11-17T16:28:42Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"\nOn 17/11/2026 15:40, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> However, I think it becomes confusing if we store the ref in two\n>> places when the user configures \".fetchRefmap\" and then passes \"src:dst\"\n>> on the commandline because users are used to a commandline \"src:dst\"\n>> overriding the \".fetch\" config, not adding to it.\n> > True, but the src:dst on the command line in your example is still\n> overriding the .fetch configuration.  Wouldn't it make it confusing\n> if it overrode .fetchRefmap configuration, since src:dst on the\n> command line is what .fetch configuration gives the defaults for\n> (and .fetchRefmap configuration gives the defaults for --refmap, not\n> the refspec like src:dst).  It may be just as confusing as if, say, a\n> command line --refmap suddenly started overriding .fetch\n> configuration, I am afraid.\n\nI think it depends on whether users already know how --refmap works. My contention is that they don't and are used to src:dst on the commandline overriding their config and will therefore be confused when it does not override .fetchRefmap. If one took the view that users are already familiar with what happens when they pass --refmap and src:dst then I agree changing the behavior would be confusing, but I don't think most users even know --refmap exists let alone what it does in this particular corner case. I had a look to see if the current behavior of --refmap combined with src:dst is documented, all I could see was\n\n    When  git  fetch  is run with explicit branches and/or tags\n    to fetch on the command line, e.g.  git fetch origin master,\n    the <refspec>s given on the command line determine what are to\n    be fetched (e.g.  master in the example, which is a short-hand\n    for master:, which in turn means \"fetch the master branch but I\n    do not explicitly say what remote-tracking branch to update with\n    it from the command line\"), and the example command  will  fetch\n    only the  master  branch.  The remote.<repository>.fetch values\n    determine which remote-tracking branch, if any, is updated. When\n    used in this way, the remote.<repository>.fetch values do not\n    have any effect in deciding what gets fetched (i.e. the values\n    are not used as refspecs when the command-line lists refspecs);\n    they are only used to decide where the refs that are fetched are\n    stored by acting as a mapping.\n\n    The latter use of the remote.<repository>.fetch values can be\n    overridden by giving the --refmap=<refspec> parameter(s) on the\n    command line.\n\nWhich reads to me that --refmap overrides remote.<repository>.fetch when remote.<repository>.fetch is used as a mapping. As remote.<repository>.fetch is not used as a mapping when there is a explicit destination on the commandline, it is not clear to me that the current behavior of --refmap combined with src:dst on the commandline matches the documentation.\n\n> I can buy it if the suggestion was to allow passing --no-refmap from\n> the command line (or would --refmap=\"\" work?) to ignore .fetchRefmap\n> configuration, though.\n\nI think --refmap=\"\" might work as --refmap overrides the config and --refmap=\"\" is the documented way to ignore the \".fetch\" config.\n\nThanks\n\nPhillip\n\n\n"},{"id":"558648","messageId":"xmqqfqwzcgmu.fsf@gitster.g","threadId":"66353","inReplyTo":"c40477c7-e1d8-4af1-859b-b739dac4bc86@gmail.com","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-17T21:06:17Z","receivedAt":"2026-11-17T21:06:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> I can buy it if the suggestion was to allow passing --no-refmap from\n>> the command line (or would --refmap=\"\" work?) to ignore .fetchRefmap\n>> configuration, though.\n>\n> I think --refmap=\"\" might work as --refmap overrides the config and \n> --refmap=\"\" is the documented way to ignore the \".fetch\" config.\n\nDo you mean ... ignore the .fetchRefmap config (not \".fetch\", which\nis used as the default for refspecs and not value to a dashed\noption)?\n\nAll that said, it feels much more flexible if a refspec with a\ndestination (whether on the command line or in the configuration)\nsimply ignores the refmap.  In other words, the refmap would only\napply to refspec entries that lack a destination, leaving elements\nwith their own destination untouched.\n\nThis way, we won't have to worry about what --refmap=\"\" means.\n\nSo I'm completely fine with going in that direction.  The only thing\nI care about is keeping a clear one-to-one relationship between the\nconfiguration and the command-line option: the configuration sets the\ndefault, and the command-line option overrides it.\n\nThanks.\n\n"},{"id":"558818","messageId":"2111d174-9571-4eb5-82cb-92f5330ae24c@gmail.com","threadId":"66353","inReplyTo":"xmqqfqwzcgmu.fsf@gitster.g","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-19T10:53:36Z","receivedAt":"2026-11-19T10:53:36Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 17/11/2026 21:06, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >>> I can buy it if the suggestion was to allow passing --no-refmap from\n>>> the command line (or would --refmap=\"\" work?) to ignore .fetchRefmap\n>>> configuration, though.\n>>\n>> I think --refmap=\"\" might work as --refmap overrides the config and\n>> --refmap=\"\" is the documented way to ignore the \".fetch\" config.\n> > Do you mean ... ignore the .fetchRefmap config (not \".fetch\", which\n> is used as the default for refspecs and not value to a dashed\n> option)?\n\nYes I meant --refmap overrides .fetchRefmap\n\n> All that said, it feels much more flexible if a refspec with a\n> destination (whether on the command line or in the configuration)\n> simply ignores the refmap.  In other words, the refmap would only\n> apply to refspec entries that lack a destination, leaving elements\n> with their own destination untouched.\n\nI agree\n\n> This way, we won't have to worry about what --refmap=\"\" means.\n> > So I'm completely fine with going in that direction.  The only thing\n> I care about is keeping a clear one-to-one relationship between the\n> configuration and the command-line option: the configuration sets the\n> default, and the command-line option overrides it.\n\nGreat. So I think we'd want a preliminary patch to ignore --refmap when there is a destination on the commandline and this patch to be tweaked so that\n\n    git config remote.cabal.fetch master\n    git fetch --refmap=refs/heads/*:refs/test/* cabal\n\nand\n\n    git config remote.cabal.fetch master\n    git config remote.cabal.fetchRefmap refs/heads/*:refs/test/*\n    git fetch\n\nboth fetch \"master\" and store it under \"refs/test/master\"\n\nThanks\n\nPhillip\n\n\n"},{"id":"558889","messageId":"xmqq1p8gwo08.fsf@gitster.g","threadId":"66353","inReplyTo":"2111d174-9571-4eb5-82cb-92f5330ae24c@gmail.com","subject":"Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-19T20:43:35Z","receivedAt":"2026-11-19T20:43:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Great. So I think we'd want a preliminary patch to ignore --refmap when \n> there is a destination on the commandline and this patch to be tweaked \n> so that\n>\n>      git config remote.cabal.fetch master\n>      git fetch --refmap=refs/heads/*:refs/test/* cabal\n>\n> and\n>\n>      git config remote.cabal.fetch master\n>      git config remote.cabal.fetchRefmap refs/heads/*:refs/test/*\n>      git fetch\n>\n> both fetch \"master\" and store it under \"refs/test/master\"\n\nI thought \"master\" as a fetch refspec element is a short-hand of\n\"master:\" that tells the machinery not to store the fetched result\nanywhere (other than FETCH_HEAD that is).  So the first one of the\nabove should be how the current code (without any of Harald's code\nfor configuration) behave, and the second one should be how the code\nwe have been reviewing should already behave, no?\n\nWith the first line in each sample replaced with (with or without\nthe leading '+')\n\n    git config remote.cabal.fetch +refs/heads/master:refs/heads/main\n\nthe first one should store what it grabbed in two places with the\ncurrent code, and the proposed update will stop touching refs/heads/main\nbecause refmap will take precedence.\n\nDo I understand what we agreed correctly?\n\nThanks.\n\n\n"},{"id":"558944","messageId":"f0dd8e55-5165-4a28-a623-748bf6c86d76@app.fastmail.com","threadId":"66353","inReplyTo":"CALnO6CB5uqHpeJm9QUC-J7n=RBVB+0pXRuMfaSQZt7mHoZbasg@mail.gmail.com","subject":"Re: Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-11-20T14:24:42Z","receivedAt":"2026-11-20T14:24:42Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Sat, Nov 7, 2026, at 21:20, D. Ben Knoble wrote:\n>[snip]\n>> Ben Knoble <ben.knoble@gmail.com> writes:\n>>\n>> > Yes, I would love to add pushRefmap atop later! It would\n>> > ease my working with notes, I think.\n>>\n>> > Yes, I would love to add pushRefmap atop later! It would\n>> > ease my working with notes, I think.\n>>\n>> Tell us more when you are off mobile ;-).\n>>\n>> I do not think I planned to add '--refmap' support on the push side,\n>> even from the command line, but I can imagine there would be\n>> reasonable workflows that benefit from having one.\n>\n> In my local copy of git.git, I want to have both Junio's amlog for\n> spelunking and my own notes for patches. I only want to push out my\n> notes + topics to my remote, though (I work on Git from different\n> machines and it's nice to have the notes sync'd on both). Here's the\n> hack I ended up with, and it has some downsides.\n>\n> In particular, \"git push\" in this configuration does too much, so I\n> usually have to think about whether that's ok. A pushRefmap would let\n> me separate what to push by default (push.default!) from how to map\n> pushing when I want to push, say, notes or other things. I have to be\n> especially careful with a \"git pf\" (push, force-with-lease,\n> force-if-includes), since I usually do *not* want to forcibly\n> overwrite the remote notes---instead I should pull those down and\n> merge them†.\n>\n> So, here's the relevant config (I have a remote \"benknoble\" and a\n> remote \"origin\", among others):\n>\n> [core]\n>         notesRef = refs/notes/benknoble/commits\n> [notes]\n>         rewriteRef = refs/notes/benknoble/commits\n>         displayRef = refs/notes/origin/amlog\n> [remote \"origin\"]\n>         fetch = +refs/notes/amlog:refs/notes/origin/amlog\n\nI don’t use multiple computers right now. But I do have both private and\npublic Git notes. In the work repository I have this\n\n    [remote \"o\"]\n            [...]\n            # NOTE “tracking” idea came from the Git mailing list\n            # NOTE We cannot use a negative refspec to exclude branch things here\n            #     (what I wanted) because neg. refspecs cannot have\n            #     the destination part\n            fetch = +refs/*:refs/tracking/o/refs/*\n\nThis is from some email thread(s) about using a namespace for tracking\nall remote refs instead of just branches. Now I have the state of the\nremote notes in case there is a divergence.\n\n(Realistically this would only check my own mistakes since no one else\nseems to use these Notes.)\n\nThere’s even a comment there about negative refspecs. I think something\nlike that got mentioned here in this thread or some months ago? I didn’t\nhave the opportunity to engage then.\n\n>\n> The \"usual\" stuff to make my notes important, but still show amlog\n> notes when browsing history. (The origin remote section is in addition\n> to the normal remote section with it's url and heads->origin/\n> mapping.)\n>\n> [remote]\n>         pushDefault = benknoble\n>\n> Push to my remote.\n>\n> [remote \"benknoble\"]\n>         fetch = refs/notes/*:refs/notes/benknoble/*\n>         push = refs/heads/*:refs/heads/*\n>         push = refs/notes/benknoble/*:refs/notes/*\n>         push = ^refs/notes/origin/amlog\n>\n> In addition to the usual config added by \"git remote add\", fetch\n> remote notes into a local hierarchy, and push them out, but ignore\n> amlog, and don't forget to also be able to push heads. Without the\n> heads map, \"git push\" will not push branches!\n>\n> Now, maybe I'm holding something in a way it wasn't intended and\n> that's made my life harder, I dunno.\n\nI haven’t tried the above but it doesn’t look like you have made it\nharder for yourself than you need to, considering your needs. ;)\n\n>\n> †: There isn't a great story for that, either :/ When \"git fetch\"\n> fails to fetch notes because of divergence with local, I end up saying\n>\n>     git fetch benknoble refs/notes/commits\n>     git range-diff refs/notes/benknoble/commits...FETCH_HEAD\n>     git notes merge FETCH_HEAD\n>\n> or something.\n>\n> It's *almost* like we need remote-tracking branches for notes, ahaha…\n> at that point, Scott's git-meta work becomes possibly more appealing\n> to me, even if a part of me wants to \"fix\" notes so everyone can use\n> them easily.\n\nI don’t know what git-meta is. But in any case, I haven’t seen a\nforcing-use case for making Notes simpler to use. Right now they are\nokay-enough for the simple needs that I have seen, which is\nsingle-author (program or person) or automatic-merge (git-appraise(1)\nuses json-lines so that cat-sort-uniq merge strategy works).\n\nBut imagine some kind of collaborative Notes usage, like multiple people\nposting commentaries on commits. In that case one would need better\ntooling, for sure. But collaborative metadata on commits is a bit like\nthe ideas like “history of history”.[1] It’s probably not going to\nhappen.\n\n[1] https://lwn.net/Articles/914041/\n\n"}]}