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

[PATCH v6 3/4] remote: add "git remote add --limited-fetch"

From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 4, 2026, 08:31 UTC
Message-ID
<3abcc8915b4aa199d95a4eeb39a57de2fc491d3d.1791102684.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com>
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>.refmap 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              | 28 ++++++++++++++++++++++------
 t/t5505-remote.sh             | 16 ++++++++++++++++
 3 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc
index eaae30aa88..4255f8b3e6 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>)
@@ -70,6 +70,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>.refmap` 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.
++
 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 de989ea3ba..f036dd5d6f 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,22 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		int 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.refmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+		} 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..0168d5abfe 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -137,6 +137,22 @@ 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.refmap &&
+		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
Previous: Harald Nordgren via GitGitGadgetNext: Harald Nordgren via GitGitGadget
Message 42 of 55 in “fetch: add config to avoid fetching every branch in shallow repo”
  1. fetch: add config to avoid fetching every branch in shallow repoHarald Nordgren via GitGitGadget, Sep 19, 2026
  2. Phillip WoodSep 21, 2026
  3. Harald NordgrenSep 21, 2026
  4. Harald NordgrenSep 22, 2026
  5. Phillip WoodSep 22, 2026
  6. Harald NordgrenSep 22, 2026
  7. Phillip WoodSep 23, 2026
  8. Junio C HamanoSep 22, 2026
  9. Harald NordgrenSep 22, 2026
  10. Phillip WoodSep 23, 2026
  11. Junio C HamanoSep 23, 2026
  12. D. Ben KnobleSep 23, 2026
  13. Junio C HamanoSep 23, 2026
  14. D. Ben KnobleSep 24, 2026
  15. Junio C HamanoSep 24, 2026
  16. fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Sep 23, 2026
  17. Junio C HamanoSep 23, 2026
  18. 0/4 fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Sep 25, 2026
  19. 1/4 fetch: add remote.<name>.refmapHarald Nordgren via GitGitGadget, Sep 25, 2026
  20. Junio C HamanoSep 25, 2026
  21. 2/4 fetch: infer branches to fetch from a refmap-only remoteHarald Nordgren via GitGitGadget, Sep 25, 2026
  22. Junio C HamanoSep 25, 2026
  23. 3/4 remote: add "git remote add --limited-fetch"Harald Nordgren via GitGitGadget, Sep 25, 2026
  24. 4/4 remote: default to --limited-fetch in a shallow repositoryHarald Nordgren via GitGitGadget, Sep 25, 2026
  25. 0/4 fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Sep 29, 2026
  26. 1/4 fetch: add remote.<name>.refmapHarald Nordgren via GitGitGadget, Sep 29, 2026
  27. 2/4 fetch: infer branches to fetch from a refmap-only remoteHarald Nordgren via GitGitGadget, Sep 29, 2026
  28. Harald NordgrenSep 29, 2026
  29. Junio C HamanoSep 29, 2026
  30. 3/4 remote: add "git remote add --limited-fetch"Harald Nordgren via GitGitGadget, Sep 29, 2026
  31. 4/4 remote: default to --limited-fetch in a shallow repositoryHarald Nordgren via GitGitGadget, Sep 29, 2026
  32. Junio C HamanoSep 29, 2026
  33. 0/4 fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Oct 2, 2026
  34. 1/4 fetch: add remote.<name>.refmapHarald Nordgren via GitGitGadget, Oct 2, 2026
  35. 2/4 fetch: infer branches to fetch from a refmap-only remoteHarald Nordgren via GitGitGadget, Oct 2, 2026
  36. 3/4 remote: add "git remote add --limited-fetch"Harald Nordgren via GitGitGadget, Oct 2, 2026
  37. Junio C HamanoOct 2, 2026
  38. 4/4 remote: default to --limited-fetch in a shallow repositoryHarald Nordgren via GitGitGadget, Oct 2, 2026
  39. 0/4 fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Oct 4, 2026
  40. 1/4 fetch: add remote.<name>.refmapHarald Nordgren via GitGitGadget, Oct 4, 2026
  41. 2/4 fetch: infer branches to fetch from a refmap-only remoteHarald Nordgren via GitGitGadget, Oct 4, 2026
  42. 3/4 remote: add "git remote add --limited-fetch"Harald Nordgren via GitGitGadget, Oct 4, 2026
  43. 4/4 remote: default to --limited-fetch in a shallow repositoryHarald Nordgren via GitGitGadget, Oct 4, 2026
  44. Junio C HamanoOct 4, 2026
  45. Harald NordgrenOct 4, 2026
  46. Junio C HamanoOct 5, 2026
  47. Harald NordgrenOct 5, 2026
  48. 0/4 fetch: avoid fetching every branch of a new remote in a shallow repoHarald Nordgren via GitGitGadget, Oct 7, 2026
  49. 1/4 fetch: add remote.<name>.refmapHarald Nordgren via GitGitGadget, Oct 7, 2026
  50. 2/4 fetch: infer branches to fetch from a refmap-only remoteHarald Nordgren via GitGitGadget, Oct 7, 2026
  51. Junio C HamanoOct 8, 2026
  52. Harald NordgrenOct 9, 2026
  53. Harald NordgrenOct 9, 2026
  54. 3/4 remote: add "git remote add --limited-fetch"Harald Nordgren via GitGitGadget, Oct 7, 2026
  55. 4/4 remote: default to --limited-fetch in a shallow repositoryHarald Nordgren via GitGitGadget, Oct 7, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.