🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
6 messages
6 total messages Started by "Harald Nordgren via GitGitGadge... Sun, 22 Nov 2026 11:57
[PATCH v15 0/7] fetch: avoid fetching every branch of a new remote in a shallow repo
#3576
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
1082 lines
58378 bytes
In a shallow single-branch clone of a large repository, git remote add
upstream <url> sets up the default +refs/heads/*:refs/remotes/upstream/*
refspec. The next git fetch upstream then downloads every branch of that
remote, which defeats the point of a shallow clone.

This series lets a remote fetch only the branches that local branches track:

git remote add --limited-fetch upstream <url>
git fetch upstream main
git branch --set-upstream-to=upstream/main
git fetch upstream    # fetches only main from now on


What to fetch and where to store it are separate settings.
remote.<name>.fetchUpstreams says what to fetch, and
remote.<name>.fetchRefmap says where it is stored. --limited-fetch sets
both:

[remote "upstream"]
    url = <url>
    fetchRefmap = +refs/heads/*:refs/remotes/upstream/*
    fetchUpstreams = true


remote.<name>.fetchRefmap is the configured default for --refmap, the same
way remote.<name>.fetch is the default for the refspecs on the command line.
The command line overrides the configuration, and at the same level a
refspec's own destination wins over the refmap:

 * src:dst on the command line is stored at dst only, with --refmap or with
   fetchRefmap.
 * A destination in remote.<name>.fetch is stored only there when the refmap
   comes from fetchRefmap.
 * --refmap on the command line replaces the destinations in
   remote.<name>.fetch. It now also works without refspecs on the command
   line, where it used to die.

A refspec without a destination is stored through the refmap. So this stores
three branches under refs/remotes/cabal/:

[remote "cabal"]
    url = <url>
    fetch = master
    fetch = next
    fetch = seen
    fetchRefmap = +refs/heads/*:refs/remotes/cabal/*


In a shallow repository, git remote add turns --limited-fetch on by default.

Design notes:

 * A *@{upstream} entry in remote.<name>.fetch was considered for saying
   what to fetch. Released versions of Git die with "invalid refspec" on it,
   so a separate setting is used.
 * The opportunistic update through remote.<name>.fetch is unchanged. On an
   ordinary remote git fetch origin side:refs/heads/tmp still updates
   origin/side. On a remote that only has a fetchRefmap it stores only
   refs/heads/tmp.

Changes in v15:

 * Added remote.<name>.fetchUpstreams to say explicitly that the upstreams
   of local branches are fetched, in addition to remote.<name>.fetch. A
   fetchRefmap alone no longer changes what is fetched.
 * A refspec with its own destination on the command line is no longer
   mapped through --refmap or fetchRefmap. This is a new first patch. A
   destination in remote.<name>.fetch is no longer mapped through
   fetchRefmap either.
 * --refmap now works without refspecs on the command line. This is a new
   second patch. It stores what remote.<name>.fetch fetches through the
   given refmap, and it overrides fetchRefmap.
 * Fixed git pull merging every tracked branch into the current one.
 * git remote add --limited-fetch now sets both fetchRefmap and
   fetchUpstreams.
 * Fixed a leftover remote.<name>.refmap in the --refmap docs.
 * Tests share helpers for repeated setup and checks, drop two steps that
   other tests cover, and the "does not fetch it twice" test now checks
   FETCH_HEAD.

Changes in v14:

 * Squashed the @{push} fix into "fetch: infer branches to fetch from a
   refmap-only remote", so the series no longer adds the bug in one patch
   and fixes it in a later one
 * Moved its test to t5586 using the refmap-only remote, so it no longer
   depends on --limited-fetch
 * Dropped the separate "remote: resolve @{push} through
   remote..fetchRefmap" commit

Changes in v13:

 * Added a new commit so @{push} resolves correctly for a --limited-fetch
   remote, falling back to remote.<name>.fetchRefmap the same way fetch
   already does when remote.<name>.fetch doesn't cover the branch being
   pushed.
 * Added a regression test to fetch: add remote.<name>.fetchRefmap covering
   a remote.<name>.fetch entry with an explicit destination, confirming it's
   stored in both that destination and the refmap's.

Changes in v12:

 * Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout
   code, docs, and tests, so it reads unambiguously as fetch-only.
 * Added the missing code path in get_ref_map() so a branch fetched by name
   via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap,
   with a new test (the "cabal" example: fetch = master/next/seen plus a
   refmap storing them all under one namespace).
 * Reworded the fetchRefmap commit message and doc entry to drop a redundant
   sentence, fix a confusing paragraph, and spell out that
   remote.<name>.fetch, when configured, still decides what's fetched while
   the refmap only remaps it.
 * Added a prefetch coverage test to t5586-fetch-refmap.sh.

Changes in v11:

 * remote_find_tracking() now falls back to remote.<name>.refmap when
   remote.<name>.fetch doesn't match. Negative refspecs in fetch still
   exclude refs, and both cases have tests
 * The helpers write straight into a strvec, take the output parameter last
   and the remote as const
 * The followRemoteHEAD lookup is done once, not duplicated in two branches
 * Clearer commit messages and docs: a working example plus a reference to
   49ca2fba39, the goal stated first, an incorrect "used to die" claim
   dropped, and an explanation of why a refmap turns on the inference
 * use_limited_fetch is a bool

Changes in v10:

 * Resolve merge conflict after master integrated
   ch/fetch-followremotehead-lazy-validation.

Changes in v9:

 * Moved the do_fetch() if/else-if/else split out of the preliminary
   helper-extraction commit and into the refmap-inference commit that needs
   it. Preliminary commit is now a simpler refactor.

Changes in v8:

 * Split into a preliminary collect_upstream_from_remote() helper commit
   plus a simplified refmap-inference commit.
 * Fixed a regression that could drop the current branch's @{upstream} when
   remote.<name>.fetch was narrowed.
 * Threaded struct repository * through instead of hardcoding
   the_repository.
 * Minor naming and comment cleanups.

Changes in v7:

 * Fixed fatal: --refmap option is only meaningful with command-line
   refspec(s) firing just from configuring remote.<name>.refmap alone, with
   nothing else to fetch.
 * When both remote.<name>.fetch and remote.<name>.refmap are configured, a
   refspec-less fetch follows .fetch as usual, .refmap only remaps something
   already being fetched by name. Added tests.

Changes in v6:

 * Remove leftover reference to deleted default-branch logic in commit
   message.

Changes in v5:

 * Renumber test file from t5585 to t5586.

Changes in v4:

 * Removed the automatic default-branch fetch. A fresh remote fetches
   nothing until you track a branch explicitly.
 * Fixed fetch report showing "new ref HEAD" instead of "new branch "
 * Reworded remote..refmap docs and commit message.

Changes in v3:

 * Replace the special ":"/"+:" fetch refspec with remote.<name>.refmap,
   reusing git's existing --refmap mechanism instead of inventing new
   refspec syntax.
 * Split the change into 4 commits.

Changes in v2:

 * Replaced the opt-in fetch.shallow config entirely with a new special
   fetch refspec (+:) that git remote add now defaults new remotes to in a
   shallow repository. The new refspec fetches whichever branches any local
   branch tracks at that remote, plus the remote's default branch.

Harald Nordgren (7):
  fetch: do not apply --refmap to a refspec with a destination
  fetch: let --refmap apply to configured refspecs
  fetch: add remote.<name>.fetchRefmap
  fetch: extract collect_upstream_from_remote() helper
  fetch: add remote.<name>.fetchUpstreams
  remote: add "git remote add --limited-fetch"
  remote: default to --limited-fetch in a shallow repository

 Documentation/config/remote.adoc |  17 +++
 Documentation/fetch-options.adoc |  11 +-
 Documentation/git-remote.adoc    |  15 ++-
 builtin/fetch.c                  | 109 ++++++++++++++----
 builtin/remote.c                 |  34 +++++-
 remote.c                         |  64 ++++++++++-
 remote.h                         |  26 +++++
 t/meson.build                    |   1 +
 t/t5505-remote.sh                |  77 +++++++++++++
 t/t5510-fetch.sh                 | 175 ++++++++++++++++++++++++++++-
 t/t5586-fetch-refmap.sh          | 182 +++++++++++++++++++++++++++++++
 11 files changed, 678 insertions(+), 33 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh


base-commit: f7de81456940dd6f3274e461149bba12965de609
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v15
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v15
Pull-Request: https://github.com/git/git/pull/2412

Range-diff vs v14:

 -:  ---------- > 1:  02bffb6b35 fetch: do not apply --refmap to a refspec with a destination
 -:  ---------- > 2:  f848c9b0a8 fetch: let --refmap apply to configured refspecs
 1:  c63699b402 ! 3:  1169013f2e fetch: add remote.<name>.fetchRefmap
     @@ Commit message
          Add a per-remote config variable, remote.<name>.fetchRefmap, that
          provides the default value for --refmap the same way
          remote.<name>.fetch already provides the default refspecs to fetch.
     +    --refmap on the command line overrides it.
      
     -    Unlike passing --refmap on the command line, which requires an
     -    explicit refspec to go with it, configuring remote.<name>.fetchRefmap
     -    on its own does not change what a refspec-less fetch does: it falls
     -    back to the same defaults as a remote with no fetchRefmap at all.
     +    The refmap only says where fetched refs are stored. Configuring it
     +    does not change what a fetch without refspecs fetches.
      
          When remote.<name>.fetch is also configured, it still decides what to
     -    fetch, and the refmap maps what it fetches. This lets a remote name
     -    only a few branches and store them all through one refmap:
     +    fetch. Its entries without a destination are stored through the
     +    refmap, and an entry with its own destination is stored only there.
     +    This lets a remote name only a few branches and store them all
     +    through one refmap:
      
              [remote "cabal"]
                  fetch = master
     @@ Documentation/config/remote.adoc: remote.<name>.fetch::
      +	fetched to remote-tracking refs to store, the same way
      +	`remote.<name>.fetch` does for an explicit refspec.
      +	`remote.<name>.fetch`, if also configured, still decides what is
     -+	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
     ++	fetched, and an entry there with its own destination is stored
     ++	only at that destination. See the `--refmap` entry in
     ++	linkgit:git-fetch[1].
      +
       remote.<name>.push::
       	The default set of "refspec" for linkgit:git-push[1]. See
     @@ Documentation/fetch-options.adoc: endif::git-pull[]
       	command-line arguments. See section on "Configured Remote-tracking
       	Branches" for details.
      ++
     -+`remote.<name>.refmap` provides the default value for this option, the
     ++`remote.<name>.fetchRefmap` provides the default value for this option, the
      +same way `remote.<name>.fetch` provides the default refspecs to fetch.
       
       `-t`::
     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
      +		filter_prefetch_refspec(effective_refmap);
       
       	if (rs->nr) {
     - 		struct refspec *fetch_refspec;
     + 		for (i = 0; i < rs->nr; i++) {
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 		 * by ref_remove_duplicates() in favor of one of these
       		 * opportunistic entries with FETCH_HEAD_IGNORE.
     + 		 *
     + 		 * A refspec that names its own destination is not
     +-		 * mapped through --refmap.
     ++		 * mapped through the refmap.
       		 */
      -		if (refmap.nr)
     --			fetch_refspec = &refmap;
     +-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
      +		if (effective_refmap && effective_refmap->nr)
     -+			fetch_refspec = effective_refmap;
     ++			map_refs_without_dst(ref_map, effective_refmap,
     ++					     &oref_tail);
       		else
     - 			fetch_refspec = &remote->fetch;
     - 
     + 			for (i = 0; i < remote->fetch.nr; i++)
     + 				get_fetch_map(ref_map, &remote->fetch.items[i],
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 			if (has_merge &&
     - 			    !strcmp(branch->remote_name, remote->name))
     - 				add_merge_config(&ref_map, remote_refs, branch, &tail);
     -+
     -+			for (i = 0; i < remote->refmap.nr; i++)
     -+				get_fetch_map(ref_map, &remote->refmap.items[i],
     -+					      &oref_tail, 1);
     - 		} else if (!prefetch) {
     - 			ref_map = get_remote_ref(remote_refs, "HEAD");
     - 			if (!ref_map)
     + 			tail = &ref_map->next;
     + 		}
     + 
     +-		if (refmap.nr) {
     ++		if (refmap.nr)
     + 			drop_destinations(ref_map);
     +-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
     +-		}
     ++		if (effective_refmap)
     ++			map_refs_without_dst(ref_map, effective_refmap,
     ++					     &oref_tail);
     + 	}
     + 
     + 	if (tags == TAGS_SET) {
      
       ## remote.c ##
      @@ remote.c: static struct remote *make_remote(struct remote_state *remote_state,
     @@ remote.h: struct remote {
       	 * configured refspecs);
      
       ## t/t5510-fetch.sh ##
     -@@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
     +@@ t/t5510-fetch.sh: test_expect_success '--refmap is not applied to a refspec with a destination' '
       	)
       '
       
     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
      +	test_cmp expect actual
      +}
      +
     -+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
     -+	test_when_finished "rm -fr fetch-refmap-baseline fetch-refmap-upstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     ++setup_refmap_repos () {
     ++	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     ++	git init -b "$1" fetch-refmap-upstream &&
      +	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init fetch-refmap-baseline &&
     ++	shift &&
     ++	for branch
     ++	do
     ++		git -C fetch-refmap-upstream branch "$branch" || return 1
     ++	done &&
     ++	git init downstream &&
      +	(
     -+		cd fetch-refmap-baseline &&
     -+		git remote add origin ../fetch-refmap-upstream &&
     ++		cd downstream &&
     ++		git remote add cabal ../fetch-refmap-upstream &&
     ++		git config --unset-all remote.cabal.fetch &&
     ++		git config remote.cabal.fetchRefmap \
     ++			"+refs/heads/*:refs/remotes/cabal/*"
     ++	)
     ++}
      +
     -+		# Without fetch refspec, but with fetch refmap.
     -+		git config --unset-all remote.origin.fetch &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     ++test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
     ++	setup_refmap_repos main other &&
     ++	(
     ++		cd downstream &&
      +
      +		# Nothing tracked, nothing fetched, no error.
     -+		git fetch origin &&
     -+		check_fetched_refs <<-\EOF &&
     -+		EOF
     -+
     -+		# Nothing tracked, explicit ref on the command line.
     -+		git fetch origin main &&
     ++		git fetch cabal &&
      +		check_fetched_refs <<-\EOF &&
     -+		refs/remotes/origin/main
      +		EOF
      +
     -+		# With both refmap and fetch configured, remote.<name>.fetch
     -+		# wins: a refspec-less fetch follows it as usual, and
     -+		# remote.<name>.fetchRefmap plays no part in deciding what to
     -+		# fetch, only in mapping what is fetched.
     -+		git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin &&
     ++		# An ordinary remote.<name>.fetch still decides what is
     ++		# fetched and where it is stored.
     ++		git config remote.cabal.fetch "+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git fetch cabal &&
      +		check_fetched_refs <<-\EOF
     -+		refs/remotes/origin/HEAD
     -+		refs/remotes/origin/main
     -+		refs/remotes/origin/other
     ++		refs/remotes/cabal/HEAD
     ++		refs/remotes/cabal/main
     ++		refs/remotes/cabal/other
      +		EOF
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b master fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch next &&
     -+	git -C fetch-refmap-upstream branch seen &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init downstream &&
     ++	setup_refmap_repos master next seen other &&
      +	(
      +		cd downstream &&
     -+		git remote add cabal ../fetch-refmap-upstream &&
     -+		git config --unset-all remote.cabal.fetch &&
      +		git config --add remote.cabal.fetch master &&
      +		git config --add remote.cabal.fetch next &&
      +		git config --add remote.cabal.fetch seen &&
     -+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
      +		git fetch cabal &&
      +		check_fetched_refs <<-\EOF &&
      +		refs/remotes/cabal/HEAD
     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
      +	)
      +'
      +
     -+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b master fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git init downstream &&
     ++test_expect_success '--refmap on the command line overrides remote.<name>.fetchRefmap' '
     ++	setup_refmap_repos master next &&
     ++	(
     ++		cd downstream &&
     ++		git config --add remote.cabal.fetch master &&
     ++		git fetch --refmap="refs/heads/*:refs/remotes/other/*" cabal &&
     ++		check_fetched_refs <<-\EOF
     ++		refs/remotes/other/master
     ++		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'remote.<name>.fetchRefmap is not applied to a remote.<name>.fetch entry with a destination' '
     ++	setup_refmap_repos master next &&
      +	(
      +		cd downstream &&
     -+		git remote add cabal ../fetch-refmap-upstream &&
     -+		git config --unset-all remote.cabal.fetch &&
      +		git config --add remote.cabal.fetch \
      +			"+refs/heads/master:refs/remotes/custom/master" &&
     -+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git config --add remote.cabal.fetch next &&
      +		git fetch cabal &&
      +		check_fetched_refs <<-\EOF
     -+		refs/remotes/cabal/HEAD
     -+		refs/remotes/cabal/master
     ++		refs/remotes/cabal/next
     ++		refs/remotes/custom/master
     ++		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'remote.<name>.fetchRefmap is not applied to a command-line refspec with a destination' '
     ++	setup_refmap_repos master next &&
     ++	(
     ++		cd downstream &&
     ++		git fetch cabal master:refs/remotes/custom/master next &&
     ++		check_fetched_refs <<-\EOF
     ++		refs/remotes/cabal/next
      +		refs/remotes/custom/master
      +		EOF
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init downstream &&
     ++	setup_refmap_repos main other &&
      +	(
      +		cd downstream &&
     -+		git remote add -t main origin ../fetch-refmap-upstream &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin other &&
     -+		git branch --track topic origin/other &&
     -+		test_cmp_config origin branch.topic.remote &&
     ++		git config --add remote.cabal.fetch \
     ++			"+refs/heads/main:refs/remotes/cabal/main" &&
     ++		git fetch cabal other &&
     ++		git branch --track topic cabal/other &&
     ++		test_cmp_config cabal branch.topic.remote &&
      +		test_cmp_config refs/heads/other branch.topic.merge &&
     -+		echo refs/remotes/origin/other >expect &&
     ++		echo refs/remotes/cabal/other >expect &&
      +		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
      +		test_cmp expect actual
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch secret &&
     -+	git init downstream &&
     ++	setup_refmap_repos main secret &&
      +	(
      +		cd downstream &&
     -+		git remote add origin ../fetch-refmap-upstream &&
     -+		git config --add remote.origin.fetch "^refs/heads/secret" &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin secret &&
     -+		git rev-parse --verify refs/remotes/origin/secret &&
     -+		test_must_fail git branch --track topic origin/secret 2>err &&
     ++		git config --add remote.cabal.fetch \
     ++			"+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git config --add remote.cabal.fetch "^refs/heads/secret" &&
     ++		git fetch cabal secret &&
     ++		git rev-parse --verify refs/remotes/cabal/secret &&
     ++		test_must_fail git branch --track topic cabal/secret 2>err &&
      +		test_grep "not a branch" err
      +	)
      +'
 2:  4ad94eefe9 = 4:  2e2bc76f79 fetch: extract collect_upstream_from_remote() helper
 3:  6724152e69 ! 5:  ece6bd824b fetch: infer branches to fetch from a refmap-only remote
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    fetch: infer branches to fetch from a refmap-only remote
     +    fetch: add remote.<name>.fetchUpstreams
      
     -    We want remote.<name>.fetchRefmap to work on its own, without also
     -    setting remote.<name>.fetch, so a remote can be limited to the
     -    branches already in use locally, without listing them by hand or
     -    touching the command line every time.
     +    In a repository that only works with a few branches of a large
     +    remote, the default refspec makes every "git fetch" download all of
     +    that remote's branches. Narrowing remote.<name>.fetch by hand works,
     +    but the list has to be kept up to date whenever a branch is started
     +    or dropped.
      
     -    Infer what to fetch from the local branches whose @{upstream} is
     -    already on that remote. The refmap itself still only controls where
     -    those fetched refs land, not which branches are selected.
     +    Add remote.<name>.fetchUpstreams. When it is set, "git fetch <name>"
     +    without refspecs on the command line also fetches the upstream
     +    branches that local branches have on that remote, in addition to
     +    what remote.<name>.fetch asks for.
     +
     +    The setting only decides what to fetch. Where the result is stored
     +    is still up to remote.<name>.fetch and remote.<name>.fetchRefmap, so
     +    a remote with fetchUpstreams, a fetchRefmap and no fetch refspec
     +    follows exactly the branches in use locally. Only the upstream of
     +    the current branch is marked for merge, so "git pull" keeps
     +    integrating that branch alone.
      
          Let @{push} use the refmap too, so a branch pushed to such a remote
          resolves to the remote-tracking branch the push updated, instead of
     @@ Commit message
      
       ## Documentation/config/remote.adoc ##
      @@ Documentation/config/remote.adoc: remote.<name>.fetchRefmap::
     - 	fetched to remote-tracking refs to store, the same way
     - 	`remote.<name>.fetch` does for an explicit refspec.
     - 	`remote.<name>.fetch`, if also configured, still decides what is
     --	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
     -+	fetched. If it is not set either, a refspec-less fetch instead
     -+	infers what to fetch from local branches built on this remote.
     -+	See the `--refmap` entry in linkgit:git-fetch[1].
     + 	only at that destination. See the `--refmap` entry in
     + 	linkgit:git-fetch[1].
       
     ++remote.<name>.fetchUpstreams::
     ++	If true, a `git fetch <name>` without refspecs on the command
     ++	line also fetches the upstream branches that local branches
     ++	have on this remote, in addition to what `remote.<name>.fetch`
     ++	asks for. Combined with `remote.<name>.fetchRefmap` and no
     ++	`remote.<name>.fetch`, the remote is limited to the branches in
     ++	use locally. Defaults to false.
     ++
       remote.<name>.push::
       	The default set of "refspec" for linkgit:git-push[1]. See
     -
     - ## Documentation/fetch-options.adoc ##
     -@@ Documentation/fetch-options.adoc: endif::git-pull[]
     - 	command-line arguments. See section on "Configured Remote-tracking
     - 	Branches" for details.
     - +
     --`remote.<name>.refmap` provides the default value for this option, the
     --same way `remote.<name>.fetch` provides the default refspecs to fetch.
     -+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but
     -+nothing to fetch is specified on the command line, nor is there a
     -+`remote.<name>.fetch`, infer what to fetch from local branches built on
     -+this remote: remote-tracking branches from this remote that are also
     -+configured as the `@{upstream}` of a local branch are fetched. The
     -+refmap only decides where those fetched refs land, the same as it
     -+would for an explicit refspec, and does not narrow which branches are
     -+selected. It is what turns this on, since without it the fetched
     -+branches would have nowhere to land.
     - 
     - `-t`::
     - `--tags`::
     + 	linkgit:git-push[1].
      
       ## builtin/fetch.c ##
     -@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	 */
     - 	struct refspec *effective_refmap =
     - 		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
     -+	struct refspec inferred_rs;
     -+	int infer_from_refmap = 0;
     - 
     - 	/* opportunistically-updated references: */
     - 	struct ref *orefs = NULL, **oref_tail = &orefs;
     -@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	struct hashmap existing_refs;
     - 	int existing_refs_populated = 0;
     - 
     -+	refspec_init_fetch(&inferred_rs, the_hash_algo);
     -+
     - 	filter_prefetch_refspec(rs);
     - 	if (remote)
     - 		filter_prefetch_refspec(&remote->fetch);
     - 	if (effective_refmap)
     - 		filter_prefetch_refspec(effective_refmap);
     +@@ builtin/fetch.c: static void add_merge_config(struct ref **head,
     + 	}
     + }
       
     -+	if (!rs->nr && remote && !remote->fetch.nr &&
     -+	    effective_refmap && effective_refmap->nr) {
     -+		struct strvec tracked = STRVEC_INIT;
     ++static void add_branch_upstreams(struct ref **head,
     ++				 const struct ref *remote_refs,
     ++				 struct remote *remote,
     ++				 struct ref ***tail)
     ++{
     ++	struct strvec upstreams = STRVEC_INIT;
      +
     -+		branches_tracking_remote(the_repository, remote, &tracked);
     -+		for (size_t i = 0; i < tracked.nr; i++)
     -+			refspec_append(&inferred_rs, tracked.v[i]);
     -+		strvec_clear(&tracked);
     ++	branches_tracking_remote(the_repository, remote, &upstreams);
     ++	for (size_t i = 0; i < upstreams.nr; i++) {
     ++		struct refspec_item refspec = { .src = (char *)upstreams.v[i] };
     ++		struct ref *rm;
      +
     -+		rs = &inferred_rs;
     -+		infer_from_refmap = 1;
     ++		for (rm = *head; rm; rm = rm->next)
     ++			if (refname_match(upstreams.v[i], rm->name))
     ++				break;
     ++		if (!rm)
     ++			get_fetch_map(remote_refs, &refspec, tail, 1);
      +	}
     ++	strvec_clear(&upstreams);
     ++}
      +
     - 	if (rs->nr) {
     - 		struct refspec *fetch_refspec;
     - 
     - 		for (i = 0; i < rs->nr; i++) {
     --			get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
     -+			get_fetch_map(remote_refs, &rs->items[i], &tail,
     -+				      infer_from_refmap);
     - 			if (rs->items[i].dst && rs->items[i].dst[0])
     - 				*autotags = 1;
     - 		}
     + static void map_refs_without_dst(const struct ref *ref_map,
     + 				 const struct refspec *refmap,
     + 				 struct ref ***tail)
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 
     - 		for (i = 0; i < fetch_refspec->nr; i++)
     - 			get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
     -+	} else if (infer_from_refmap) {
     -+		/* Already fully handled above. */
     - 	} else if (refmap.nr) {
     - 		die("--refmap option is only meaningful with command-line refspec(s)");
     - 	} else {
     + 		struct branch *branch = branch_get(NULL);
     + 		int has_merge = branch_has_merge_config(branch);
     + 		if (remote &&
     +-		    (remote->fetch.nr ||
     ++		    (remote->fetch.nr || remote->fetch_upstreams ||
     + 		     /* Note: has_merge implies non-NULL branch->remote_name */
     + 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
     + 			for (i = 0; i < remote->fetch.nr; i++) {
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	if (existing_refs_populated)
     - 		hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
     - 
     -+	refspec_clear(&inferred_rs);
     - 	return ref_map;
     - }
     - 
     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 	struct strmap rejected_refs = STRMAP_INIT;
     - 	int summary_width = 0;
     - 	int follow_remote_head = FOLLOW_REMOTE_NEVER;
     -+	bool apply_follow_remote_head_config = false;
     - 
     - 	if (tags == TAGS_DEFAULT) {
     - 		if (transport->remote->fetch_tags == 2)
     + 				    !remote->fetch.items[0].pattern)
     + 					ref_map->fetch_head_status = FETCH_HEAD_MERGE;
     + 			}
     ++			if (remote->fetch_upstreams)
     ++				add_branch_upstreams(&ref_map, remote_refs,
     ++						     remote, &tail);
     + 			/*
     + 			 * if the remote we're fetching from is the same
     + 			 * as given in branch.<name>.remote, we add the
      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 
       	if (rs->nr) {
       		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
     --	} else {
     + 	} else {
      -		if (transport->remote->fetch.nr) {
     --			refspec_ref_prefixes(&transport->remote->fetch,
     --					     &transport_ls_refs_options.ref_prefixes);
     --			/*
     --			 * See remote.c's handling of remote.<name>.followRemoteHEAD
     --			 * for the analogous, still-unresolved case.
     --			 */
     --			if (transport->remote->follow_remote_head)
     --				follow_remote_head = transport->remote->follow_remote_head;
     --			else if (config->follow_remote_head_seen)
     --				follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
     --			else
     --				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
     --		}
     -+	} else if (transport->remote->fetch.nr) {
     -+		refspec_ref_prefixes(&transport->remote->fetch,
     -+				     &transport_ls_refs_options.ref_prefixes);
     -+		apply_follow_remote_head_config = true;
     - 
     - 		/*
     - 		 * The configured refspec may not cover the current
     ++		if (transport->remote->fetch.nr ||
     ++		    transport->remote->fetch_upstreams) {
     + 			refspec_ref_prefixes(&transport->remote->fetch,
     + 					     &transport_ls_refs_options.ref_prefixes);
     + 			/*
      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     + 		 * branch's upstream (e.g. a narrowed -t refspec), so
     + 		 * make sure we can still fetch it regardless.
       		 */
     - 		collect_upstream_from_remote(the_repository, transport->remote,
     - 					      NULL, &transport_ls_refs_options.ref_prefixes);
     -+	} else {
     -+		/*
     -+		 * The --refmap command line option, if given, takes
     -+		 * precedence over remote.<name>.fetchRefmap.
     -+		 */
     -+		struct refspec *effective_refmap = refmap.nr ? &refmap :
     -+			&transport->remote->refmap;
     -+
     -+		if (effective_refmap->nr) {
     +-		collect_upstream_from_remote(the_repository, transport->remote,
     +-					      NULL, &transport_ls_refs_options.ref_prefixes);
     ++		if (transport->remote->fetch_upstreams)
      +			branches_tracking_remote(the_repository, transport->remote,
      +						  &transport_ls_refs_options.ref_prefixes);
     -+			apply_follow_remote_head_config = true;
     -+		} else {
     ++		else
      +			collect_upstream_from_remote(the_repository, transport->remote,
      +						      NULL, &transport_ls_refs_options.ref_prefixes);
     -+		}
       
       		/*
       		 * If there are no refs specified to fetch, then we just
     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 				    "HEAD");
     - 	}
     - 
     -+	if (apply_follow_remote_head_config) {
     -+		/*
     -+		 * See remote.c's handling of remote.<name>.followRemoteHEAD
     -+		 * for the analogous, still-unresolved case.
     -+		 */
     -+		if (transport->remote->follow_remote_head)
     -+			follow_remote_head = transport->remote->follow_remote_head;
     -+		else if (config->follow_remote_head_seen)
     -+			follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
     -+		else
     -+			follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
     -+	}
     -+
     - 	if (tags == TAGS_SET || tags == TAGS_DEFAULT)
     - 		strvec_push(&transport_ls_refs_options.ref_prefixes,
     - 			    "refs/tags/");
      
       ## remote.c ##
     +@@ remote.c: static int handle_config(const char *key, const char *value,
     + 			return -1;
     + 		refspec_append(&remote->refmap, v);
     + 		free(v);
     ++	} else if (!strcmp(subkey, "fetchupstreams")) {
     ++		remote->fetch_upstreams = git_config_bool(key, value);
     + 	} else if (!strcmp(subkey, "receivepack")) {
     + 		char *v;
     + 		if (git_config_string(&v, key, value))
      @@ remote.c: void collect_upstream_from_remote(struct repository *repo,
       		strvec_push(tracked, branch->merge[i]->src);
       }
     @@ remote.c: static char *tracking_for_push_dest(struct repository *repo,
       				 _("push destination '%s' on remote '%s' has no local tracking branch"),
      
       ## remote.h ##
     +@@ remote.h: struct remote {
     + 	 */
     + 	struct refspec refmap;
     + 
     ++	/*
     ++	 * Whether a fetch without refspecs on the command line also
     ++	 * fetches the upstreams that local branches have on this remote.
     ++	 */
     ++	bool fetch_upstreams;
     ++
     + 	/*
     + 	 * The setting for whether to fetch tags (as a separate rule from the
     + 	 * configured refspecs);
      @@ remote.h: void collect_upstream_from_remote(struct repository *repo,
       				   const char *refname,
       				   struct strvec *tracked);
     @@ t/t5586-fetch-refmap.sh (new)
      @@
      +#!/bin/sh
      +
     -+test_description='"git fetch" with a remote.<name>.fetchRefmap but no remote.<name>.fetch
     ++test_description='"git fetch" with remote.<name>.fetchUpstreams
      +
     -+When a remote has a refmap configured but no fetch refspec, a
     -+refspec-less fetch infers what to fetch from the local branches whose
     -+@{upstream} is on that remote.
     ++With remote.<name>.fetchUpstreams set, a refspec-less fetch also
     ++fetches the upstreams that local branches have on that remote, and
     ++remote.<name>.fetchRefmap decides where they are stored.
      +'
      +
      +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
     @@ t/t5586-fetch-refmap.sh (new)
      +
      +. ./test-lib.sh
      +
     ++check_upstream_refs () {
     ++	git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     ++	cat >expect &&
     ++	test_cmp expect actual
     ++}
     ++
     ++check_upstream_tip () {
     ++	git -C .. rev-parse "$2" >expect &&
     ++	git rev-parse "$1" >actual &&
     ++	test_cmp expect actual
     ++}
     ++
      +test_expect_success 'setup' '
      +	test_commit main-1 &&
      +	test_commit main-2 &&
     @@ t/t5586-fetch-refmap.sh (new)
      +		git remote add upstream .. &&
      +		test_might_fail git config unset remote.upstream.fetch &&
      +		git config remote.upstream.fetchRefmap \
     -+			"+refs/heads/*:refs/remotes/upstream/*"
     ++			"+refs/heads/*:refs/remotes/upstream/*" &&
     ++		git config remote.upstream.fetchUpstreams true
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +	(
      +		cd client &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		test_must_be_empty actual
     ++		check_upstream_refs <<-\EOF
     ++		EOF
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +	(
      +		cd client &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     ++		check_upstream_refs <<-\EOF &&
      +		refs/remotes/upstream/HEAD
      +		refs/remotes/upstream/main
      +		refs/remotes/upstream/side
      +		EOF
     -+		test_cmp expect actual &&
     -+		git rev-parse refs/remotes/upstream/side >actual-oid &&
     -+		git -C .. rev-parse side >expect-oid &&
     -+		test_cmp expect-oid actual-oid
     ++		check_upstream_tip refs/remotes/upstream/side side
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +		cd client &&
      +		git branch side-topic-2 upstream/side &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     -+		refs/remotes/upstream/HEAD
     -+		refs/remotes/upstream/main
     -+		refs/remotes/upstream/side
     -+		EOF
     -+		test_cmp expect actual
     ++		grep side .git/FETCH_HEAD >fetched &&
     ++		test_line_count = 1 fetched
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +		git fetch other next:refs/remotes/other/next &&
      +		git branch next-topic other/next &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     ++		check_upstream_refs <<-\EOF
      +		refs/remotes/upstream/HEAD
      +		refs/remotes/upstream/main
      +		refs/remotes/upstream/side
      +		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'upstreams are not fetched without remote.<name>.fetchUpstreams' '
     ++	git checkout side &&
     ++	test_commit side-3 &&
     ++	git checkout main &&
     ++	(
     ++		cd client &&
     ++		git rev-parse refs/remotes/upstream/side >expect &&
     ++		git -c remote.upstream.fetchUpstreams=false fetch upstream &&
     ++		git rev-parse refs/remotes/upstream/side >actual &&
      +		test_cmp expect actual
      +	)
      +'
      +
     ++test_expect_success 'remote.<name>.fetch adds to the upstreams being fetched' '
     ++	(
     ++		cd client &&
     ++		git -c remote.upstream.fetch=next fetch upstream &&
     ++		check_upstream_refs <<-\EOF &&
     ++		refs/remotes/upstream/HEAD
     ++		refs/remotes/upstream/main
     ++		refs/remotes/upstream/next
     ++		refs/remotes/upstream/side
     ++		EOF
     ++		check_upstream_tip refs/remotes/upstream/side side
     ++	)
     ++'
     ++
      +test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '
      +	test_commit main-3 &&
      +	(
     @@ t/t5586-fetch-refmap.sh (new)
      +		git fetch --prefetch upstream &&
      +		git rev-parse refs/remotes/upstream/main >actual &&
      +		test_cmp expect actual &&
     -+		git -C .. rev-parse main >expect &&
     -+		git rev-parse refs/prefetch/remotes/upstream/main >actual &&
     -+		test_cmp expect actual
     ++		check_upstream_tip refs/prefetch/remotes/upstream/main main
     ++	)
     ++'
     ++
     ++test_expect_success 'git pull merges only the upstream of the current branch' '
     ++	(
     ++		cd client &&
     ++		git pull &&
     ++		check_upstream_tip HEAD main
      +	)
      +'
      +
 4:  6cff6786ba ! 6:  e60b61e117 remote: add "git remote add --limited-fetch"
     @@ Commit message
          branches nobody asked for.
      
          Give "git remote add" a --limited-fetch option that sets up
     -    remote.<name>.fetchRefmap instead of remote.<name>.fetch, so that
     -    "git fetch <name>" only fetches the branches already tracked, as
     -    described in the previous commit. It is rejected together with
     -    -t/--track or --mirror, since those already say explicitly what to
     -    fetch.
     +    remote.<name>.fetchRefmap and remote.<name>.fetchUpstreams instead
     +    of remote.<name>.fetch, so that "git fetch <name>" only fetches the
     +    branches already tracked, as described in the previous commit. It
     +    is rejected together with -t/--track or --mirror, since those
     +    already say explicitly what to fetch.
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
     @@ Documentation/git-remote.adoc: the `refs/remotes/<name>/` namespace, a refspec t
       multiple branches without grabbing all branches.
       +
      +With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
     -+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
     -+refspec-less `git fetch <name>` only fetches branches our local branches
     -+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
     -+details.
     ++that tracks all branches, `remote.<name>.fetchRefmap` and
     ++`remote.<name>.fetchUpstreams` are set up so that a refspec-less
     ++`git fetch <name>` only fetches branches our local branches are built
     ++on. See linkgit:git-config[1] for details.
      ++
       With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
       up to point at remote's _<master>_ branch. See also the set-head command.
     @@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix
      +			strbuf_reset(&buf2);
      +			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
      +			repo_config_set(the_repository, buf.buf, buf2.buf);
     ++			strbuf_reset(&buf);
     ++			strbuf_addf(&buf, "remote.%s.fetchUpstreams", name);
     ++			repo_config_set(the_repository, buf.buf, "true");
      +		} else {
      +			strbuf_addf(&buf, "remote.%s.fetch", name);
      +			if (track.nr == 0)
     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
      +		git remote add --limited-fetch upstream ../two &&
      +		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
      +			remote.upstream.fetchRefmap &&
     ++		test_cmp_config true remote.upstream.fetchUpstreams &&
      +		test_must_fail git config get remote.upstream.fetch
      +	)
      +'
 5:  00f9d44a88 ! 7:  3f06f7f9e2 remote: default to --limited-fetch in a shallow repository
     @@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at
       the remote information is set up.
       +
      @@ Documentation/git-remote.adoc: With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
     - that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
     - refspec-less `git fetch <name>` only fetches branches our local branches
     - are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
     --details.
     -+details. `--no-limited-fetch` explicitly disables this, overriding the
     -+shallow-repository default described above.
     + that tracks all branches, `remote.<name>.fetchRefmap` and
     + `remote.<name>.fetchUpstreams` are set up so that a refspec-less
     + `git fetch <name>` only fetches branches our local branches are built
     +-on. See linkgit:git-config[1] for details.
     ++on. See linkgit:git-config[1] for details. `--no-limited-fetch`
     ++explicitly disables this, overriding the shallow-repository default
     ++described above.
       +
       With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
       up to point at remote's _<master>_ branch. See also the set-head command.
     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
      +		git remote add upstream ../two &&
      +		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
      +			remote.upstream.fetchRefmap &&
     ++		test_cmp_config true remote.upstream.fetchUpstreams &&
      +		test_must_fail git config get remote.upstream.fetch &&
      +		git fetch upstream main &&
      +		git branch --set-upstream-to=upstream/main &&
     -+		test_cmp_config upstream branch.main.remote &&
     -+		test_cmp_config refs/heads/main branch.main.merge &&
      +		git fetch upstream &&
      +		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
      +		cat >expect <<-\EOF &&
     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
      +		git remote add --no-limited-fetch upstream ../two &&
      +		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
      +			remote.upstream.fetch &&
     -+		test_must_fail git config get remote.upstream.fetchRefmap
     ++		test_must_fail git config get remote.upstream.fetchRefmap &&
     ++		test_must_fail git config get remote.upstream.fetchUpstreams
      +	)
      +'
      +

-- 
gitgitgadget
[PATCH v15 3/7] fetch: add remote.<name>.fetchRefmap
#3577
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
365 lines
23360 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

Add a per-remote config variable, remote.<name>.fetchRefmap, that
provides the default value for --refmap the same way
remote.<name>.fetch already provides the default refspecs to fetch.
--refmap on the command line overrides it.

The refmap only says where fetched refs are stored. Configuring it
does not change what a fetch without refspecs fetches.

When remote.<name>.fetch is also configured, it still decides what to
fetch. Its entries without a destination are stored through the
refmap, and an entry with its own destination is stored only there.
This lets a remote name only a few branches and store them all
through one refmap:

    [remote "cabal"]
        fetch = master
        fetch = next
        fetch = seen
        fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

Looking up the remote-tracking branch for a remote branch, as
@{upstream} and "git branch --track" do, falls back to the refmap when
remote.<name>.fetch has no match, so that these work for such
branches too. A branch excluded by a negative refspec in
remote.<name>.fetch stays excluded.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/remote.adoc |   9 ++
 Documentation/fetch-options.adoc |   3 +
 builtin/fetch.c                  |  22 +++--
 remote.c                         |  20 +++-
 remote.h                         |   6 ++
 t/t5510-fetch.sh                 | 154 +++++++++++++++++++++++++++++++
 6 files changed, 207 insertions(+), 7 deletions(-)

diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
index 3a20d0f752..c088d6257d 100644
--- a/Documentation/config/remote.adoc
+++ b/Documentation/config/remote.adoc
@@ -33,6 +33,15 @@ remote.<name>.fetch::
 	The default set of "refspec" for linkgit:git-fetch[1]. See
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched, and an entry there with its own destination is stored
+	only at that destination. See the `--refmap` entry in
+	linkgit:git-fetch[1].
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
index 27f2ac6dc7..842bcc6fe5 100644
--- a/Documentation/fetch-options.adoc
+++ b/Documentation/fetch-options.adoc
@@ -250,6 +250,9 @@ endif::git-pull[]
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
++
+`remote.<name>.fetchRefmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
 `-t`::
 `--tags`::
diff --git a/builtin/fetch.c b/builtin/fetch.c
index ccff0075c9..b83f9e5cc9 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -543,6 +543,12 @@ static struct ref *get_ref_map(struct remote *remote,
 	struct ref *rm;
 	struct ref *ref_map = NULL;
 	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
 	/* opportunistically-updated references: */
 	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -553,6 +559,8 @@ static struct ref *get_ref_map(struct remote *remote,
 	filter_prefetch_refspec(rs);
 	if (remote)
 		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
 	if (rs->nr) {
 		for (i = 0; i < rs->nr; i++) {
@@ -585,10 +593,11 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
 		 *
 		 * A refspec that names its own destination is not
-		 * mapped through --refmap.
+		 * mapped through the refmap.
 		 */
-		if (refmap.nr)
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+		if (effective_refmap && effective_refmap->nr)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
 		else
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
@@ -628,10 +637,11 @@ static struct ref *get_ref_map(struct remote *remote,
 			tail = &ref_map->next;
 		}
 
-		if (refmap.nr) {
+		if (refmap.nr)
 			drop_destinations(ref_map);
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
-		}
+		if (effective_refmap)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
 	}
 
 	if (tags == TAGS_SET) {
diff --git a/remote.c b/remote.c
index df069e802f..6a6e2717c1 100644
--- a/remote.c
+++ b/remote.c
@@ -153,6 +153,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->name = xstrndup(name, len);
 	refspec_init_push(&ret->push, the_hash_algo);
 	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
 	string_list_init_dup(&ret->server_options);
 	string_list_init_dup(&ret->negotiation_restrict);
 	string_list_init_dup(&ret->negotiation_include);
@@ -177,6 +178,7 @@ static void remote_clear(struct remote *remote)
 
 	refspec_clear(&remote->push);
 	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
 	free((char *)remote->receivepack);
 	free((char *)remote->uploadpack);
@@ -540,6 +542,12 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->fetch, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -996,7 +1004,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
 {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
 }
 
 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
diff --git a/remote.h b/remote.h
index cca02033b9..ac485a584d 100644
--- a/remote.h
+++ b/remote.h
@@ -90,6 +90,12 @@ struct remote {
 
 	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index 412a5401a8..dd28639eac 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -944,6 +944,160 @@ test_expect_success '--refmap is not applied to a refspec with a destination' '
 	)
 '
 
+test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '
+	test_when_finished "git -C three config --unset remote.origin.fetchRefmap" &&
+	git branch -f side &&
+	git -C three config remote.origin.fetchRefmap \
+		"refs/heads/*:refs/remotes/other/*" &&
+	(
+		cd three &&
+		git update-ref refs/remotes/origin/main base-origin-main &&
+		o=$(git rev-parse --verify refs/remotes/origin/main) &&
+		git fetch origin main &&
+		n=$(git rev-parse --verify refs/remotes/origin/main) &&
+		test "$o" = "$n" &&
+		test_must_fail git rev-parse --verify refs/remotes/origin/side &&
+		git rev-parse --verify refs/remotes/other/main
+	)
+'
+
+check_fetched_refs () {
+	git for-each-ref --format="%(refname)" refs/remotes/ >actual &&
+	cat >expect &&
+	test_cmp expect actual
+}
+
+setup_refmap_repos () {
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b "$1" fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	shift &&
+	for branch
+	do
+		git -C fetch-refmap-upstream branch "$branch" || return 1
+	done &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add cabal ../fetch-refmap-upstream &&
+		git config --unset-all remote.cabal.fetch &&
+		git config remote.cabal.fetchRefmap \
+			"+refs/heads/*:refs/remotes/cabal/*"
+	)
+}
+
+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
+	setup_refmap_repos main other &&
+	(
+		cd downstream &&
+
+		# Nothing tracked, nothing fetched, no error.
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF &&
+		EOF
+
+		# An ordinary remote.<name>.fetch still decides what is
+		# fetched and where it is stored.
+		git config remote.cabal.fetch "+refs/heads/*:refs/remotes/cabal/*" &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/cabal/HEAD
+		refs/remotes/cabal/main
+		refs/remotes/cabal/other
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '
+	setup_refmap_repos master next seen other &&
+	(
+		cd downstream &&
+		git config --add remote.cabal.fetch master &&
+		git config --add remote.cabal.fetch next &&
+		git config --add remote.cabal.fetch seen &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF &&
+		refs/remotes/cabal/HEAD
+		refs/remotes/cabal/master
+		refs/remotes/cabal/next
+		refs/remotes/cabal/seen
+		EOF
+		git branch --track topic cabal/next &&
+		echo refs/remotes/cabal/next >expect &&
+		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success '--refmap on the command line overrides remote.<name>.fetchRefmap' '
+	setup_refmap_repos master next &&
+	(
+		cd downstream &&
+		git config --add remote.cabal.fetch master &&
+		git fetch --refmap="refs/heads/*:refs/remotes/other/*" cabal &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/other/master
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap is not applied to a remote.<name>.fetch entry with a destination' '
+	setup_refmap_repos master next &&
+	(
+		cd downstream &&
+		git config --add remote.cabal.fetch \
+			"+refs/heads/master:refs/remotes/custom/master" &&
+		git config --add remote.cabal.fetch next &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/cabal/next
+		refs/remotes/custom/master
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap is not applied to a command-line refspec with a destination' '
+	setup_refmap_repos master next &&
+	(
+		cd downstream &&
+		git fetch cabal master:refs/remotes/custom/master next &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/cabal/next
+		refs/remotes/custom/master
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '
+	setup_refmap_repos main other &&
+	(
+		cd downstream &&
+		git config --add remote.cabal.fetch \
+			"+refs/heads/main:refs/remotes/cabal/main" &&
+		git fetch cabal other &&
+		git branch --track topic cabal/other &&
+		test_cmp_config cabal branch.topic.remote &&
+		test_cmp_config refs/heads/other branch.topic.merge &&
+		echo refs/remotes/cabal/other >expect &&
+		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '
+	setup_refmap_repos main secret &&
+	(
+		cd downstream &&
+		git config --add remote.cabal.fetch \
+			"+refs/heads/*:refs/remotes/cabal/*" &&
+		git config --add remote.cabal.fetch "^refs/heads/secret" &&
+		git fetch cabal secret &&
+		git rev-parse --verify refs/remotes/cabal/secret &&
+		test_must_fail git branch --track topic cabal/secret 2>err &&
+		test_grep "not a branch" err
+	)
+'
+
 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
 	git branch -f side &&
 	(
-- 
gitgitgadget
[PATCH v15 4/7] fetch: extract collect_upstream_from_remote() helper
#3578
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
112 lines
14753 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

When a fetch has no refspec to work from, we decide what to fetch from
two sources: the remote's configured fetch refspec, and the current
branch's upstream, if it lives on that remote. For example, after

    git clone --single-branch <url>
    git switch -c work
    git push -u origin work

the remote's refspec only covers "main", but a refspec-less "git fetch
origin" still fetches "work" into FETCH_HEAD, so that a plain "git
pull" finds the branch to merge. See 49ca2fba39 (fetch: add
branch.*.merge to default ref-prefix extension, 2022-09-08).

That "does this branch build on this remote" check was inlined for
just the current branch. Move it into its own function,
collect_upstream_from_remote(), so the same logic can be reused for
branches other than just the current one.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 builtin/fetch.c | 18 ++++++++----------
 remote.c        | 15 +++++++++++++++
 remote.h        |  9 +++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/builtin/fetch.c b/builtin/fetch.c
index b83f9e5cc9..a9b7305f5f 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -2028,8 +2028,6 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		struct branch *branch = branch_get(NULL);
-
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
@@ -2044,14 +2042,14 @@ static int do_fetch(struct transport *transport,
 			else
 				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
 		}
-		if (branch && branch_has_merge_config(branch) &&
-		    !strcmp(branch->remote_name, transport->remote->name)) {
-			int i;
-			for (i = 0; i < branch->merge_nr; i++) {
-				strvec_push(&transport_ls_refs_options.ref_prefixes,
-					    branch->merge[i]->src);
-			}
-		}
+
+		/*
+		 * The configured refspec may not cover the current
+		 * branch's upstream (e.g. a narrowed -t refspec), so
+		 * make sure we can still fetch it regardless.
+		 */
+		collect_upstream_from_remote(the_repository, transport->remote,
+					      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
diff --git a/remote.c b/remote.c
index 6a6e2717c1..ffe892a326 100644
--- a/remote.c
+++ b/remote.c
@@ -1967,6 +1967,21 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname);
 }
 
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked)
+{
+	struct branch *branch = repo_branch_get(repo, refname);
+
+	if (!branch_has_merge_config(branch) ||
+	    strcmp(branch->remote_name, remote->name))
+		return;
+
+	for (int i = 0; i < branch->merge_nr; i++)
+		strvec_push(tracked, branch->merge[i]->src);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
diff --git a/remote.h b/remote.h
index ac485a584d..003c8fd180 100644
--- a/remote.h
+++ b/remote.h
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
 
 int branch_merge_matches(struct branch *, int n, const char *);
 
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
-- 
gitgitgadget
[PATCH v15 5/7] fetch: add remote.<name>.fetchUpstreams
#3579
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
416 lines
24506 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

In a repository that only works with a few branches of a large
remote, the default refspec makes every "git fetch" download all of
that remote's branches. Narrowing remote.<name>.fetch by hand works,
but the list has to be kept up to date whenever a branch is started
or dropped.

Add remote.<name>.fetchUpstreams. When it is set, "git fetch <name>"
without refspecs on the command line also fetches the upstream
branches that local branches have on that remote, in addition to
what remote.<name>.fetch asks for.

The setting only decides what to fetch. Where the result is stored
is still up to remote.<name>.fetch and remote.<name>.fetchRefmap, so
a remote with fetchUpstreams, a fetchRefmap and no fetch refspec
follows exactly the branches in use locally. Only the upstream of
the current branch is marked for merge, so "git pull" keeps
integrating that branch alone.

Let @{push} use the refmap too, so a branch pushed to such a remote
resolves to the remote-tracking branch the push updated, instead of
failing with "has no local tracking branch".

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/remote.adoc |   8 ++
 builtin/fetch.c                  |  37 ++++++-
 remote.c                         |  29 +++++
 remote.h                         |  11 ++
 t/meson.build                    |   1 +
 t/t5586-fetch-refmap.sh          | 182 +++++++++++++++++++++++++++++++
 6 files changed, 264 insertions(+), 4 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh

diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
index c088d6257d..cd9a86c8bb 100644
--- a/Documentation/config/remote.adoc
+++ b/Documentation/config/remote.adoc
@@ -42,6 +42,14 @@ remote.<name>.fetchRefmap::
 	only at that destination. See the `--refmap` entry in
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchUpstreams::
+	If true, a `git fetch <name>` without refspecs on the command
+	line also fetches the upstream branches that local branches
+	have on this remote, in addition to what `remote.<name>.fetch`
+	asks for. Combined with `remote.<name>.fetchRefmap` and no
+	`remote.<name>.fetch`, the remote is limited to the branches in
+	use locally. Defaults to false.
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a9b7305f5f..0cfcd5bdd1 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -274,6 +274,27 @@ static void add_merge_config(struct ref **head,
 	}
 }
 
+static void add_branch_upstreams(struct ref **head,
+				 const struct ref *remote_refs,
+				 struct remote *remote,
+				 struct ref ***tail)
+{
+	struct strvec upstreams = STRVEC_INIT;
+
+	branches_tracking_remote(the_repository, remote, &upstreams);
+	for (size_t i = 0; i < upstreams.nr; i++) {
+		struct refspec_item refspec = { .src = (char *)upstreams.v[i] };
+		struct ref *rm;
+
+		for (rm = *head; rm; rm = rm->next)
+			if (refname_match(upstreams.v[i], rm->name))
+				break;
+		if (!rm)
+			get_fetch_map(remote_refs, &refspec, tail, 1);
+	}
+	strvec_clear(&upstreams);
+}
+
 static void map_refs_without_dst(const struct ref *ref_map,
 				 const struct refspec *refmap,
 				 struct ref ***tail)
@@ -607,7 +628,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		struct branch *branch = branch_get(NULL);
 		int has_merge = branch_has_merge_config(branch);
 		if (remote &&
-		    (remote->fetch.nr ||
+		    (remote->fetch.nr || remote->fetch_upstreams ||
 		     /* Note: has_merge implies non-NULL branch->remote_name */
 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
 			for (i = 0; i < remote->fetch.nr; i++) {
@@ -619,6 +640,9 @@ static struct ref *get_ref_map(struct remote *remote,
 				    !remote->fetch.items[0].pattern)
 					ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			}
+			if (remote->fetch_upstreams)
+				add_branch_upstreams(&ref_map, remote_refs,
+						     remote, &tail);
 			/*
 			 * if the remote we're fetching from is the same
 			 * as given in branch.<name>.remote, we add the
@@ -2028,7 +2052,8 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		if (transport->remote->fetch.nr) {
+		if (transport->remote->fetch.nr ||
+		    transport->remote->fetch_upstreams) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
 			/*
@@ -2048,8 +2073,12 @@ static int do_fetch(struct transport *transport,
 		 * branch's upstream (e.g. a narrowed -t refspec), so
 		 * make sure we can still fetch it regardless.
 		 */
-		collect_upstream_from_remote(the_repository, transport->remote,
-					      NULL, &transport_ls_refs_options.ref_prefixes);
+		if (transport->remote->fetch_upstreams)
+			branches_tracking_remote(the_repository, transport->remote,
+						  &transport_ls_refs_options.ref_prefixes);
+		else
+			collect_upstream_from_remote(the_repository, transport->remote,
+						      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
diff --git a/remote.c b/remote.c
index ffe892a326..72b4c15a04 100644
--- a/remote.c
+++ b/remote.c
@@ -548,6 +548,8 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->refmap, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchupstreams")) {
+		remote->fetch_upstreams = git_config_bool(key, value);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -1982,6 +1984,31 @@ void collect_upstream_from_remote(struct repository *repo,
 		strvec_push(tracked, branch->merge[i]->src);
 }
 
+struct branches_tracking_remote_cb_data {
+	struct repository *repo;
+	const struct remote *remote;
+	struct strvec *tracked;
+};
+
+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)
+{
+	struct branches_tracking_remote_cb_data *data = cb_data;
+
+	collect_upstream_from_remote(data->repo, data->remote, ref->name,
+				      data->tracked);
+	return 0;
+}
+
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked)
+{
+	struct branches_tracking_remote_cb_data data = { repo, remote, tracked };
+
+	refs_for_each_branch_ref(get_main_ref_store(repo),
+				  add_if_tracking_remote, &data);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
@@ -2061,6 +2088,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
 	remote = repo_remote_for_push_tracking(repo, remote);
 	ret = apply_refspecs(&remote->fetch, refname);
+	if (!ret)
+		ret = apply_refspecs(&remote->refmap, refname);
 	if (!ret)
 		return error_buf(err,
 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
diff --git a/remote.h b/remote.h
index 003c8fd180..73bbe729f5 100644
--- a/remote.h
+++ b/remote.h
@@ -96,6 +96,12 @@ struct remote {
 	 */
 	struct refspec refmap;
 
+	/*
+	 * Whether a fetch without refspecs on the command line also
+	 * fetches the upstreams that local branches have on this remote.
+	 */
+	bool fetch_upstreams;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
@@ -368,6 +374,11 @@ void collect_upstream_from_remote(struct repository *repo,
 				   const char *refname,
 				   struct strvec *tracked);
 
+/* Same as collect_upstream_from_remote(), but checked for every local branch. */
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..92f877da1c 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -737,6 +737,7 @@ integration_tests = [
   't5582-fetch-negative-refspec.sh',
   't5583-push-branches.sh',
   't5584-http-429-retry.sh',
+  't5586-fetch-refmap.sh',
   't5600-clone-fail-cleanup.sh',
   't5601-clone.sh',
   't5602-clone-remote-exec.sh',
diff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh
new file mode 100755
index 0000000000..4af59868e6
--- /dev/null
+++ b/t/t5586-fetch-refmap.sh
@@ -0,0 +1,182 @@
+#!/bin/sh
+
+test_description='"git fetch" with remote.<name>.fetchUpstreams
+
+With remote.<name>.fetchUpstreams set, a refspec-less fetch also
+fetches the upstreams that local branches have on that remote, and
+remote.<name>.fetchRefmap decides where they are stored.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_upstream_refs () {
+	git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
+	cat >expect &&
+	test_cmp expect actual
+}
+
+check_upstream_tip () {
+	git -C .. rev-parse "$2" >expect &&
+	git rev-parse "$1" >actual &&
+	test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+	test_commit main-1 &&
+	test_commit main-2 &&
+	git checkout -b side main-1 &&
+	test_commit side-1 &&
+	git checkout -b next main-1 &&
+	test_commit next-1 &&
+	git checkout main
+'
+
+test_expect_success 'clone shallow and single-branch, then add a second remote' '
+	git clone --no-local --depth=1 --branch main --single-branch . client &&
+	(
+		cd client &&
+		git remote add upstream .. &&
+		test_might_fail git config unset remote.upstream.fetch &&
+		git config remote.upstream.fetchRefmap \
+			"+refs/heads/*:refs/remotes/upstream/*" &&
+		git config remote.upstream.fetchUpstreams true
+	)
+'
+
+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '
+	(
+		cd client &&
+		git fetch upstream &&
+		check_upstream_refs <<-\EOF
+		EOF
+	)
+'
+
+test_expect_success 'an explicit one-time fetch lets a branch be tracked' '
+	(
+		cd client &&
+		git fetch upstream main &&
+		git branch --set-upstream-to=upstream/main &&
+		test_cmp_config upstream branch.main.remote &&
+		test_cmp_config refs/heads/main branch.main.merge
+	)
+'
+
+test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' '
+	(
+		cd client &&
+		git fetch upstream side:refs/remotes/upstream/side &&
+		git branch side-topic upstream/side
+	) &&
+	git checkout side &&
+	test_commit side-2 &&
+	git checkout main &&
+	(
+		cd client &&
+		git fetch upstream &&
+		check_upstream_refs <<-\EOF &&
+		refs/remotes/upstream/HEAD
+		refs/remotes/upstream/main
+		refs/remotes/upstream/side
+		EOF
+		check_upstream_tip refs/remotes/upstream/side side
+	)
+'
+
+test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' '
+	(
+		cd client &&
+		git branch side-topic-2 upstream/side &&
+		git fetch upstream &&
+		grep side .git/FETCH_HEAD >fetched &&
+		test_line_count = 1 fetched
+	)
+'
+
+test_expect_success 'a branch tracking a different remote is not fetched from upstream' '
+	(
+		cd client &&
+		git remote add other .. &&
+		git fetch other next:refs/remotes/other/next &&
+		git branch next-topic other/next &&
+		git fetch upstream &&
+		check_upstream_refs <<-\EOF
+		refs/remotes/upstream/HEAD
+		refs/remotes/upstream/main
+		refs/remotes/upstream/side
+		EOF
+	)
+'
+
+test_expect_success 'upstreams are not fetched without remote.<name>.fetchUpstreams' '
+	git checkout side &&
+	test_commit side-3 &&
+	git checkout main &&
+	(
+		cd client &&
+		git rev-parse refs/remotes/upstream/side >expect &&
+		git -c remote.upstream.fetchUpstreams=false fetch upstream &&
+		git rev-parse refs/remotes/upstream/side >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'remote.<name>.fetch adds to the upstreams being fetched' '
+	(
+		cd client &&
+		git -c remote.upstream.fetch=next fetch upstream &&
+		check_upstream_refs <<-\EOF &&
+		refs/remotes/upstream/HEAD
+		refs/remotes/upstream/main
+		refs/remotes/upstream/next
+		refs/remotes/upstream/side
+		EOF
+		check_upstream_tip refs/remotes/upstream/side side
+	)
+'
+
+test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '
+	test_commit main-3 &&
+	(
+		cd client &&
+		git rev-parse refs/remotes/upstream/main >expect &&
+		git fetch --prefetch upstream &&
+		git rev-parse refs/remotes/upstream/main >actual &&
+		test_cmp expect actual &&
+		check_upstream_tip refs/prefetch/remotes/upstream/main main
+	)
+'
+
+test_expect_success 'git pull merges only the upstream of the current branch' '
+	(
+		cd client &&
+		git pull &&
+		check_upstream_tip HEAD main
+	)
+'
+
+test_expect_success 'git remote show does not choke on a refmap-only remote' '
+	(
+		cd client &&
+		git remote show upstream
+	)
+'
+
+test_expect_success '@{push} resolves for a branch pushed to a refmap-only remote' '
+	(
+		cd client &&
+		git config push.default current &&
+		git checkout -b topic upstream/main &&
+		test_commit topic-1 &&
+		git push &&
+		echo refs/remotes/upstream/topic >expect &&
+		git rev-parse --symbolic-full-name topic@{push} >actual &&
+		test_cmp expect actual &&
+		test_cmp_rev topic topic@{push}
+	)
+'
+
+test_done
-- 
gitgitgadget
[PATCH v15 6/7] remote: add "git remote add --limited-fetch"
#3581
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
143 lines
16781 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

A remote added the ordinary way tracks every branch it has, via a
wildcard remote.<name>.fetch refspec. That is wasteful for a remote
whose history is only worth following for the branches actually in
use locally, and it can make "git fetch" negotiate history for
branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up
remote.<name>.fetchRefmap and remote.<name>.fetchUpstreams instead
of remote.<name>.fetch, so that "git fetch <name>" only fetches the
branches already tracked, as described in the previous commit. It
is rejected together with -t/--track or --mirror, since those
already say explicitly what to fetch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/git-remote.adoc |  8 +++++++-
 builtin/remote.c              | 31 +++++++++++++++++++++++++------
 t/t5505-remote.sh             | 17 +++++++++++++++++
 3 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc
index 315100409d..39b9281293 100644
--- a/Documentation/git-remote.adoc
+++ b/Documentation/git-remote.adoc
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [synopsis]
 git remote [-v | --verbose]
-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>
 git remote rename [--[no-]progress] <old> <new>
 git remote remove <name>
 git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_
 is created.  You can give more than one `-t <branch>` to track
 multiple branches without grabbing all branches.
 +
+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
+that tracks all branches, `remote.<name>.fetchRefmap` and
+`remote.<name>.fetchUpstreams` are set up so that a refspec-less
+`git fetch <name>` only fetches branches our local branches are built
+on. See linkgit:git-config[1] for details.
++
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
 +
diff --git a/builtin/remote.c b/builtin/remote.c
index fb7e5b114f..18fe6f697c 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
+	int limited_fetch = -1; /* unspecified */
 	struct string_list track = STRING_LIST_INIT_NODUP;
 	const char *master = NULL;
 	struct remote *remote;
@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,
 		OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
 			N_("set up remote as a mirror to push to or fetch from"),
 			PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
+		OPT_BOOL(0, "limited-fetch", &limited_fetch,
+			N_("fetch only the branches we build on, instead of every branch")),
 		OPT_END()
 	};
 
@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,
 		die(_("specifying a master branch makes no sense with --mirror"));
 	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 		die(_("specifying branches to track makes sense only with fetch mirrors"));
+	if (limited_fetch == 1 && track.nr)
+		die(_("--limited-fetch does not make sense with -t/--track"));
+	if (limited_fetch == 1 && mirror)
+		die(_("--limited-fetch does not make sense with --mirror"));
 
 	name = argv[0];
 	url = argv[1];
@@ -230,13 +237,25 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
+			limited_fetch == 1;
+
 		strbuf_reset(&buf);
-		strbuf_addf(&buf, "remote.%s.fetch", name);
-		if (track.nr == 0)
-			string_list_append(&track, "*");
-		for (size_t i = 0; i < track.nr; i++) {
-			add_branch(buf.buf, track.items[i].string,
-				   name, mirror, &buf2);
+		if (use_limited_fetch) {
+			strbuf_addf(&buf, "remote.%s.fetchRefmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+			strbuf_reset(&buf);
+			strbuf_addf(&buf, "remote.%s.fetchUpstreams", name);
+			repo_config_set(the_repository, buf.buf, "true");
+		} else {
+			strbuf_addf(&buf, "remote.%s.fetch", name);
+			if (track.nr == 0)
+				string_list_append(&track, "*");
+			for (size_t i = 0; i < track.nr; i++)
+				add_branch(buf.buf, track.items[i].string,
+					   name, mirror, &buf2);
 		}
 	}
 
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 5fbfcb0848..73f0125907 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -137,6 +137,23 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success '--limited-fetch works in a full repository too' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add --limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_cmp_config true remote.upstream.fetchUpstreams &&
+		test_must_fail git config get remote.upstream.fetch
+	)
+'
+
+test_expect_success '--limited-fetch conflicts with -t' '
+	test_must_fail git remote add --limited-fetch -t main upstream ../two
+'
+
 test_expect_success 'check remote-tracking' '
 	(
 		cd test &&
-- 
gitgitgadget
[PATCH v15 7/7] remote: default to --limited-fetch in a shallow repository
#3580
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
147 lines
16614 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>

Adding a second remote to a shallow clone used to still fetch every
branch that remote has, since "git remote add" always set up a
wildcard remote.<name>.fetch refspec regardless of how shallow the
repository already was. That defeats the purpose of having cloned
shallow in the first place (for example with --single-branch), and
can make a plain "git fetch" on that remote hang or take a very long
time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already
shallow and neither -t/--track nor --mirror was given, so that
adding a remote there does not by itself commit to following every
branch it has. --no-limited-fetch keeps the previous behavior for
whoever wants it.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/git-remote.adoc |  9 +++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc
index 39b9281293..2216a2a752 100644
--- a/Documentation/git-remote.adoc
+++ b/Documentation/git-remote.adoc
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,9 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` and
 `remote.<name>.fetchUpstreams` are set up so that a refspec-less
 `git fetch <name>` only fetches branches our local branches are built
-on. See linkgit:git-config[1] for details.
+on. See linkgit:git-config[1] for details. `--no-limited-fetch`
+explicitly disables this, overriding the shallow-repository default
+described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
diff --git a/builtin/remote.c b/builtin/remote.c
index 18fe6f697c..8163fcc618 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 73f0125907..4df51255f6 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -137,6 +137,66 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success 'add remote -t keeps an explicit refspec in a shallow repository' '
+	test_when_finished "rm -rf shallow-add" &&
+	git clone --no-local --depth=1 --branch main --single-branch \
+		one shallow-add &&
+	(
+		cd shallow-add &&
+		git remote add -t main upstream ../two &&
+		test_cmp_config "+refs/heads/main:refs/remotes/upstream/main" \
+			remote.upstream.fetch
+	)
+'
+
+test_expect_success 'add remote keeps the wildcard refspec in a full repository' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetch
+	)
+'
+
+test_expect_success 'a remote added in a shallow repository defaults to --limited-fetch' '
+	test_when_finished "rm -rf shallow-add" &&
+	git clone --no-local --depth=1 --branch main --single-branch \
+		one shallow-add &&
+	(
+		cd shallow-add &&
+		git remote add upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_cmp_config true remote.upstream.fetchUpstreams &&
+		test_must_fail git config get remote.upstream.fetch &&
+		git fetch upstream main &&
+		git branch --set-upstream-to=upstream/main &&
+		git fetch upstream &&
+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
+		cat >expect <<-\EOF &&
+		refs/remotes/upstream/HEAD
+		refs/remotes/upstream/main
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success '--no-limited-fetch overrides the shallow-repository default' '
+	test_when_finished "rm -rf shallow-add" &&
+	git clone --no-local --depth=1 --branch main --single-branch \
+		one shallow-add &&
+	(
+		cd shallow-add &&
+		git remote add --no-limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetch &&
+		test_must_fail git config get remote.upstream.fetchRefmap &&
+		test_must_fail git config get remote.upstream.fetchUpstreams
+	)
+'
+
 test_expect_success '--limited-fetch works in a full repository too' '
 	test_when_finished "rm -rf full-add" &&
 	git clone --no-local one full-add &&
-- 
gitgitgadget
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads