Thread View: linux.kernel.git
44 messages
44 total messages
Started by "Harald Nordgren via GitGitGadge...
Tue, 20 Oct 2026 13:42
[PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
217 lines
19657 bytes
19657 bytes
Avoid fetching every branch of a new remote in a shallow repo.
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 (5):
fetch: add remote.<name>.refmap
fetch: extract collect_upstream_from_remote() helper
fetch: infer branches to fetch from a refmap-only remote
remote: add "git remote add --limited-fetch"
remote: default to --limited-fetch in a shallow repository
Documentation/config/remote.adoc | 8 +++
Documentation/fetch-options.adoc | 5 ++
Documentation/git-remote.adoc | 14 +++-
builtin/fetch.c | 92 ++++++++++++++++++++----
builtin/remote.c | 31 ++++++--
remote.c | 51 ++++++++++++-
remote.h | 19 +++++
t/meson.build | 1 +
t/t5505-remote.sh | 76 ++++++++++++++++++++
t/t5510-fetch.sh | 63 ++++++++++++++++
t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++
11 files changed, 456 insertions(+), 23 deletions(-)
create mode 100755 t/t5586-fetch-refmap.sh
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v10
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v10
Pull-Request: https://github.com/git/git/pull/2412
Range-diff vs v9:
1: 2a48c21bc8 = 1: ba7f1354cd fetch: add remote.<name>.refmap
2: 50382bce7e ! 2: aa20604144 fetch: extract collect_upstream_from_remote() helper
@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
if (transport->remote->fetch.nr) {
refspec_ref_prefixes(&transport->remote->fetch,
@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
- if (follow_remote_head != FOLLOW_REMOTE_NEVER)
- do_set_head = 1;
+ else
+ follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
}
- if (branch && branch_has_merge_config(branch) &&
- !strcmp(branch->remote_name, transport->remote->name)) {
3: 9ad8ad3eb4 ! 3: 9fb7e77a86 fetch: infer branches to fetch from a refmap-only remote
@@ 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) {
- struct string_list tracked = STRING_LIST_INIT_DUP;
- struct string_list_item *item;
-
-- if (transport->remote->fetch.nr) {
-- refspec_ref_prefixes(&transport->remote->fetch,
-- &transport_ls_refs_options.ref_prefixes);
-- if (follow_remote_head != FOLLOW_REMOTE_NEVER)
-- do_set_head = 1;
-- }
++ struct string_list tracked = STRING_LIST_INIT_DUP;
++ struct string_list_item *item;
++
+ refspec_ref_prefixes(&transport->remote->fetch,
+ &transport_ls_refs_options.ref_prefixes);
-+ if (follow_remote_head != FOLLOW_REMOTE_NEVER)
-+ do_set_head = 1;
-
- /*
- * The configured refspec may not cover the current
-@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
- strvec_push(&transport_ls_refs_options.ref_prefixes,
- item->string);
- string_list_clear(&tracked, 0);
-+ } else {
++ /*
++ * 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;
++
++ /*
++ * 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, &tracked,
++ transport->remote, NULL);
++ for_each_string_list_item(item, &tracked)
++ strvec_push(&transport_ls_refs_options.ref_prefixes,
++ item->string);
++ string_list_clear(&tracked, 0);
+ } else {
+ /*
+ * The --refmap command line option, if given, takes
+ * precedence over remote.<name>.refmap.
+ */
+ struct refspec *effective_refmap = refmap.nr ? &refmap :
+ &transport->remote->refmap;
-+ struct string_list tracked = STRING_LIST_INIT_DUP;
-+ struct string_list_item *item;
-+
+ struct string_list tracked = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+
+- if (transport->remote->fetch.nr) {
+- refspec_ref_prefixes(&transport->remote->fetch,
+- &transport_ls_refs_options.ref_prefixes);
+ if (effective_refmap->nr) {
+ branches_tracking_remote(the_repository,
+ transport->remote, &tracked);
-+ if (follow_remote_head != FOLLOW_REMOTE_NEVER)
-+ do_set_head = 1;
+ /*
+ * See remote.c's handling of remote.<name>.followRemoteHEAD
+ * for the analogous, still-unresolved case.
+@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
+ follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
+ else
+ follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
+ } else {
+ collect_upstream_from_remote(the_repository, &tracked,
+ transport->remote, NULL);
-+ }
-+ for_each_string_list_item(item, &tracked)
-+ strvec_push(&transport_ls_refs_options.ref_prefixes,
-+ item->string);
-+ string_list_clear(&tracked, 0);
-
- /*
- * If there are no refs specified to fetch, then we just
+ }
+-
+- /*
+- * 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, &tracked,
+- transport->remote, NULL);
+ for_each_string_list_item(item, &tracked)
+ strvec_push(&transport_ls_refs_options.ref_prefixes,
+ item->string);
## remote.c ##
@@ remote.c: void collect_upstream_from_remote(struct repository *repo,
4: 75169f668e = 4: 80028d4f86 remote: add "git remote add --limited-fetch"
5: 9ee090d3d0 = 5: 552bf08971 remote: default to --limited-fetch in a shallow repository
--
gitgitgadget
[PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
223 lines
18866 bytes
18866 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
Add a per-remote config variable, remote.<name>.refmap, that provides
the default value for --refmap the same way remote.<name>.fetch
already provides the default refspecs to fetch. Like --refmap itself,
it only maps refs that are actually being fetched, so it has nothing
to do when there is nothing explicit to fetch, on the command line or
via remote.<name>.fetch.
A refspec-less fetch with a refmap configured but nothing else to say
what to fetch falls back to the same defaults as a remote with no
refspec or refmap at all. Only an explicit --refmap given on the
command line with no command-line refspec to go with it is still
rejected, since that combination is a plain mistake to type, unlike a
remote.<name>.refmap configured on its own. A remote.<name>.fetch
that is also configured is used as before.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
Documentation/config/remote.adoc | 5 +++
Documentation/fetch-options.adoc | 3 ++
builtin/fetch.c | 10 ++++-
remote.c | 12 +++++-
remote.h | 6 +++
t/t5510-fetch.sh | 63 ++++++++++++++++++++++++++++++++
6 files changed, 96 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
index 3a20d0f752..103eada406 100644
--- a/Documentation/config/remote.adoc
+++ b/Documentation/config/remote.adoc
@@ -33,6 +33,11 @@ remote.<name>.fetch::
The default set of "refspec" for linkgit:git-fetch[1]. See
linkgit:git-fetch[1].
+remote.<name>.refmap::
+ The default value of the `--refmap` option for linkgit:git-fetch[1].
+ Used to map remote refs being fetched to remote-tracking refs to
+ store. 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 47dea1de8e..c2101a7b39 100644
--- a/Documentation/fetch-options.adoc
+++ b/Documentation/fetch-options.adoc
@@ -244,6 +244,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>.refmap` 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 1f76ae6242..ffcb5ba29a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -518,6 +518,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>.refmap.
+ */
+ struct refspec *effective_refmap =
+ refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
/* opportunistically-updated references: */
struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -561,8 +567,8 @@ 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.
*/
- if (refmap.nr)
- fetch_refspec = &refmap;
+ if (effective_refmap && effective_refmap->nr)
+ fetch_refspec = effective_refmap;
else
fetch_refspec = &remote->fetch;
diff --git a/remote.c b/remote.c
index 49ee2ea318..c9252a90f4 100644
--- a/remote.c
+++ b/remote.c
@@ -152,6 +152,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);
@@ -176,6 +177,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);
@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,
return -1;
refspec_append(&remote->fetch, v);
free(v);
+ } else if (!strcmp(subkey, "refmap")) {
+ 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))
@@ -995,7 +1003,9 @@ 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);
+ if (remote->fetch.nr)
+ return refspec_find_match(&remote->fetch, refspec);
+ return refspec_find_match(&remote->refmap, refspec);
}
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 17c0be499c..ec5cd9dca6 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
)
'
+test_expect_success 'remote.*.refmap acts like --refmap on the command line' '
+ test_when_finished "git -C three config --unset remote.origin.refmap" &&
+ git branch -f side &&
+ git -C three config remote.origin.refmap \
+ "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
+}
+
+test_expect_success 'remote.<name>.refmap without tracking (baseline)' '
+ test_when_finished "rm -fr fetch-refmap-baseline fetch-refmap-upstream" &&
+ git init -b main fetch-refmap-upstream &&
+ test_commit -C fetch-refmap-upstream base &&
+ git -C fetch-refmap-upstream branch other &&
+ git init fetch-refmap-baseline &&
+ (
+ cd fetch-refmap-baseline &&
+ git remote add origin ../fetch-refmap-upstream &&
+
+ # Without fetch refspec, but with fetch refmap.
+ git config --unset-all remote.origin.fetch &&
+ git config remote.origin.refmap "+refs/heads/*:refs/remotes/origin/*" &&
+
+ # 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 &&
+ 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>.refmap plays no part in deciding what to
+ # fetch, only in remapping something that is already being
+ # fetched by name.
+ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
+ git fetch origin &&
+ check_fetched_refs <<-\EOF
+ refs/remotes/origin/HEAD
+ refs/remotes/origin/main
+ refs/remotes/origin/other
+ EOF
+ )
+'
+
test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
git branch -f side &&
(
--
gitgitgadget
[PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
112 lines
14575 bytes
14575 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
When a fetch has no refspec to work from, we fall back to guessing
what to fetch: the remote's configured fetch refspec if there is one,
and separately, whether the current branch's upstream lives on this
remote. The latter is independent of the former on purpose: a narrow
refspec (for example from "git remote add -t") can leave the current
branch's own upstream uncovered, and we still need to be able to fetch
it.
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 | 23 ++++++++++++++---------
remote.c | 15 +++++++++++++++
remote.h | 9 +++++++++
3 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index ffcb5ba29a..136a59154a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1979,7 +1979,8 @@ 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);
+ struct string_list tracked = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
if (transport->remote->fetch.nr) {
refspec_ref_prefixes(&transport->remote->fetch,
@@ -1995,14 +1996,18 @@ 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, &tracked,
+ transport->remote, NULL);
+ for_each_string_list_item(item, &tracked)
+ strvec_push(&transport_ls_refs_options.ref_prefixes,
+ item->string);
+ string_list_clear(&tracked, 0);
/*
* If there are no refs specified to fetch, then we just
diff --git a/remote.c b/remote.c
index c9252a90f4..965e42ff52 100644
--- a/remote.c
+++ b/remote.c
@@ -1891,6 +1891,21 @@ int branch_merge_matches(struct branch *branch,
return refname_match(branch->merge[i]->src, refname);
}
+void collect_upstream_from_remote(struct repository *repo,
+ struct string_list *tracked,
+ struct remote *remote,
+ const char *refname)
+{
+ 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++)
+ string_list_insert(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..7c86c529b6 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,
+ struct string_list *tracked,
+ struct remote *remote,
+ const char *refname);
+
/* list of the remote in a group as configured */
struct remote_group_data {
const char *name;
--
gitgitgadget
[PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
382 lines
23929 bytes
23929 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
Configuring remote.<name>.refmap without remote.<name>.fetch used to
make a refspec-less "git fetch <name>" fail with "--refmap option is
only meaningful with command-line refspec(s)", since a refmap only
says where to put fetched refs, not what to fetch.
Make that case infer what to fetch: the local branches whose
@{upstream} is already on that remote. This lets a remote be
configured to fetch only the branches actually in use, without
listing them by hand in remote.<name>.fetch, and without needing to
touch the command line every time.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
Documentation/config/remote.adoc | 5 +-
Documentation/fetch-options.adoc | 6 +-
builtin/fetch.c | 75 +++++++++++++++----
remote.c | 24 +++++++
remote.h | 4 ++
t/meson.build | 1 +
t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++
7 files changed, 219 insertions(+), 15 deletions(-)
create mode 100755 t/t5586-fetch-refmap.sh
diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
index 103eada406..7c52e443a5 100644
--- a/Documentation/config/remote.adoc
+++ b/Documentation/config/remote.adoc
@@ -36,7 +36,10 @@ remote.<name>.fetch::
remote.<name>.refmap::
The default value of the `--refmap` option for linkgit:git-fetch[1].
Used to map remote refs being fetched to remote-tracking refs to
- store. See the `--refmap` entry in linkgit:git-fetch[1].
+ store. If `remote.<name>.fetch` is not set either, a refspec-less
+ fetch infers what to fetch from local branches built on this
+ remote, instead of fetching every branch it has. See the
+ `--refmap` entry in linkgit:git-fetch[1].
remote.<name>.push::
The default set of "refspec" for linkgit:git-push[1]. See
diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
index c2101a7b39..90b9e13ac2 100644
--- a/Documentation/fetch-options.adoc
+++ b/Documentation/fetch-options.adoc
@@ -245,8 +245,10 @@ 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>.refmap`) but
+nothing to fetch is specified on the command line, nor is there a
+`remote.<name>.fetch`, branches from the remote that are used as the
+`@{upstream}` of our local branches are fetched.
`-t`::
`--tags`::
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 136a59154a..0fc1fb66d5 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -524,6 +524,8 @@ 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;
@@ -531,15 +533,32 @@ 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 (!rs->nr && remote && !remote->fetch.nr &&
+ effective_refmap && effective_refmap->nr) {
+ struct string_list tracked = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+
+ branches_tracking_remote(the_repository, remote, &tracked);
+ for_each_string_list_item(item, &tracked)
+ refspec_append(&inferred_rs, item->string);
+ string_list_clear(&tracked, 0);
+
+ rs = &inferred_rs;
+ infer_from_refmap = 1;
+ }
+
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;
}
@@ -574,6 +593,8 @@ 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 {
@@ -669,6 +690,7 @@ 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;
}
@@ -1978,13 +2000,47 @@ 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) {
+ struct string_list tracked = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+
+ 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;
+
+ /*
+ * 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, &tracked,
+ transport->remote, NULL);
+ for_each_string_list_item(item, &tracked)
+ strvec_push(&transport_ls_refs_options.ref_prefixes,
+ item->string);
+ string_list_clear(&tracked, 0);
} else {
+ /*
+ * The --refmap command line option, if given, takes
+ * precedence over remote.<name>.refmap.
+ */
+ struct refspec *effective_refmap = refmap.nr ? &refmap :
+ &transport->remote->refmap;
struct string_list tracked = STRING_LIST_INIT_DUP;
struct string_list_item *item;
- if (transport->remote->fetch.nr) {
- refspec_ref_prefixes(&transport->remote->fetch,
- &transport_ls_refs_options.ref_prefixes);
+ if (effective_refmap->nr) {
+ branches_tracking_remote(the_repository,
+ transport->remote, &tracked);
/*
* See remote.c's handling of remote.<name>.followRemoteHEAD
* for the analogous, still-unresolved case.
@@ -1995,15 +2051,10 @@ static int do_fetch(struct transport *transport,
follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
else
follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
+ } else {
+ collect_upstream_from_remote(the_repository, &tracked,
+ transport->remote, NULL);
}
-
- /*
- * 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, &tracked,
- transport->remote, NULL);
for_each_string_list_item(item, &tracked)
strvec_push(&transport_ls_refs_options.ref_prefixes,
item->string);
diff --git a/remote.c b/remote.c
index 965e42ff52..6467496a0f 100644
--- a/remote.c
+++ b/remote.c
@@ -1906,6 +1906,30 @@ void collect_upstream_from_remote(struct repository *repo,
string_list_insert(tracked, branch->merge[i]->src);
}
+struct branches_tracking_remote_cb_data {
+ struct repository *repo;
+ struct remote *remote;
+ struct string_list *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->tracked, data->remote,
+ ref->name);
+ return 0;
+}
+
+void branches_tracking_remote(struct repository *repo, struct remote *remote,
+ struct string_list *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, ...)
{
diff --git a/remote.h b/remote.h
index 7c86c529b6..62265346d3 100644
--- a/remote.h
+++ b/remote.h
@@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,
struct remote *remote,
const char *refname);
+/* fills tracked with the refname of every local branch's upstream on remote */
+void branches_tracking_remote(struct repository *repo, struct remote *remote,
+ struct string_list *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 11b3752f00..0953a4112f 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -736,6 +736,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..b81fc48cbe
--- /dev/null
+++ b/t/t5586-fetch-refmap.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+test_description='"git fetch" with a remote.<name>.refmap but no remote.<name>.fetch
+
+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.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+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.refmap \
+ "+refs/heads/*:refs/remotes/upstream/*"
+ )
+'
+
+test_expect_success 'a bare fetch needs nothing until a branch is tracked' '
+ (
+ cd client &&
+ git fetch upstream &&
+ git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
+ test_must_be_empty actual
+ )
+'
+
+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 &&
+ 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 &&
+ git rev-parse refs/remotes/upstream/side >actual-oid &&
+ git -C .. rev-parse side >expect-oid &&
+ test_cmp expect-oid actual-oid
+ )
+'
+
+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 &&
+ 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
+ )
+'
+
+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 &&
+ 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
+ )
+'
+
+test_expect_success 'git remote show does not choke on a refmap-only remote' '
+ (
+ cd client &&
+ git remote show upstream
+ )
+'
+
+test_done
--
gitgitgadget
[PATCH v10 4/5] remote: add "git remote add --limited-fetch"
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
139 lines
16308 bytes
16308 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>.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 315100409d..f8764394e7 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>.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 fb7e5b114f..a7c13946d1 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
[PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 20 Oct 2026 13:42
Date: Tue, 20 Oct 2026 13:42
146 lines
16399 bytes
16399 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
Adding a second remote to a shallow, single-branch 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 and single-branch in the first place, 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 | 8 ++++-
builtin/remote.c | 5 ++-
t/t5505-remote.sh | 60 +++++++++++++++++++++++++++++++++++
3 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc
index f8764394e7..f43d707c91 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,8 @@ 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.
+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 a7c13946d1..8c4497cccd 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) {
int 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 0168d5abfe..f3b5905e9e 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.refmap &&
+ 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 &&
+ 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.refmap
+ )
+'
+
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
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: Junio C Hamano
Date: Fri, 23 Oct 2026 23:13
Date: Fri, 23 Oct 2026 23:13
41 lines
14454 bytes
14454 bytes
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > Avoid fetching every branch of a new remote in a shallow repo. > > Changes in v10: > > * Resolve merge conflict after master integrated > ch/fetch-followremotehead-lazy-validation. > Harald Nordgren (5): > fetch: add remote.<name>.refmap > fetch: extract collect_upstream_from_remote() helper > fetch: infer branches to fetch from a refmap-only remote > remote: add "git remote add --limited-fetch" > remote: default to --limited-fetch in a shallow repository > > Documentation/config/remote.adoc | 8 +++ > Documentation/fetch-options.adoc | 5 ++ > Documentation/git-remote.adoc | 14 +++- > builtin/fetch.c | 92 ++++++++++++++++++++---- > builtin/remote.c | 31 ++++++-- > remote.c | 51 ++++++++++++- > remote.h | 19 +++++ > t/meson.build | 1 + > t/t5505-remote.sh | 76 ++++++++++++++++++++ > t/t5510-fetch.sh | 63 ++++++++++++++++ > t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++ > 11 files changed, 456 insertions(+), 23 deletions(-) > create mode 100755 t/t5586-fetch-refmap.sh This topic seeems fairly well done, without seeing any additional comments in the last two rounds, but I am highly disturbed that I was practically the only person who reviewd its later iterations. Because I am not a target audience of the feature, I would not be surprised if I am missing something large. I would very much appreciate if somebody else can give the series one more read and Ack them, so that we can move it forward. Thanks.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:08
Date: Sat, 24 Oct 2026 15:08
62 lines
14520 bytes
14520 bytes
On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Add a per-remote config variable, remote.<name>.refmap, that provides
> the default value for --refmap the same way remote.<name>.fetch
> already provides the default refspecs to fetch. Like --refmap itself,
> it only maps refs that are actually being fetched, so it has nothing
> to do when there is nothing explicit to fetch, on the command line or
> via remote.<name>.fetch.
>
> A refspec-less fetch with a refmap configured but nothing else to say
> what to fetch falls back to the same defaults as a remote with no
> refspec or refmap at all. Only an explicit --refmap given on the
> command line with no command-line refspec to go with it is still
> rejected, since that combination is a plain mistake to type, unlike a
> remote.<name>.refmap configured on its own. A remote.<name>.fetch
> that is also configured is used as before.
A bit wordy in the 2nd paragraph (and LLM-tasting), but… :shrug:
>
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
> int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
> {
> - return refspec_find_match(&remote->fetch, refspec);
> + if (remote->fetch.nr)
> + return refspec_find_match(&remote->fetch, refspec);
> + return refspec_find_match(&remote->refmap, refspec);
> }
I think this appeared in v2 a bit open-coded, then was refactored in
v3, but I haven't seen any comment on it? I might just be missing
some.
Anyway, let me think aloud about this. At first glance, we can't
fallback to refmap if fetch *fails* to find refspec. Should we or not?
I'm not sure.
remote.h says this procedure does "For the given remote, reads the
refspec's src and sets the other fields." Ok, and glancing at
refspec_find_match the idea is that "refspec" input is a query (with a
src or dst) and we find the matching entry and fill out the input's
fields.
So… I assume the usual idea would be that refmap is a "superset" of
the fetch refspec, since I might want to map refs that I don't
normally fetch. (Or it might be disjoint, if none of the mappings
apply to my usual fetches.) But that means that remote_find_tracking()
won't be able to find entries for the mapped refs that aren't part of
my "normal" fetches? Are there downstream consequences of that? I'm
not sure, I haven't looked at how else remote_find_tracking is used.
Could that affect, for example, --delete-merged?
--
D. Ben Knoble
Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:08
Date: Sat, 24 Oct 2026 15:08
198 lines
19900 bytes
19900 bytes
On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> When a fetch has no refspec to work from, we fall back to guessing
> what to fetch: the remote's configured fetch refspec if there is one,
> and separately, whether the current branch's upstream lives on this
> remote.
It took me a couple tries to parse this because the 2 halves ("[one],
and separately, [two]") are 2 different kinds of objects! I think what
you are trying to tell me is
…we fall back to guessing what to fetch: the remote's …
separately, the current branch's upstream if it lives on the fetched
remote.
That way [two] is also a "thing to fetch" in the same class of objects
as [one]? Or have I misunderstood?
> The latter is independent of the former on purpose: a narrow
> refspec (for example from "git remote add -t") can leave the current
> branch's own upstream uncovered, and we still need to be able to fetch
> it.
I don't really understand the meaning here. I can guess that you mean
something like "A remote with a narrow refspec, like what you get from
'git remote add -t', can omit a branch's upstream from the remote's
fetch refspec, and we still want to fetch that upstream." But it's
taken me a while to put that together (after puzzling out the above)
after first not knowing what "uncovered" meant.
I think the whole thing would be clearer with an example:
"""
Imagine a remote with a narrow refspec:
git remote add -t main colleague <remote>
and a branch whose upstream points at that remote, but isn't part of
the remote's refspec:
git fetch colleague work:refs/remotes/colleague/work
git switch -c work colleague/work
When we "git fetch colleague" without a refspec, we decide what to
fetch by combining the remote's refspec (main -> remote-tracking
branch, in this case) and the current branch's upstream, if it was
based on that remote (work -> remote-tracking branch, in this case).
"""
Note: we aren't "guessing" what to fetch!
> 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.
And then this maybe comes next. Personally I appreciate the extra
explanation, but if it exists elsewhere we could also refer to the
implementing commit?
I've also made some assumptions about how that "current branch" check
works, since I'm not sure how to make sense of it without this
manually created remote-tracking branch?
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
> builtin/fetch.c | 23 ++++++++++++++---------
> remote.c | 15 +++++++++++++++
> remote.h | 9 +++++++++
> 3 files changed, 38 insertions(+), 9 deletions(-)
>
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index ffcb5ba29a..136a59154a 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -1979,7 +1979,8 @@ 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);
> + struct string_list tracked = STRING_LIST_INIT_DUP;
> + struct string_list_item *item;
>
> if (transport->remote->fetch.nr) {
> refspec_ref_prefixes(&transport->remote->fetch,
> @@ -1995,14 +1996,18 @@ 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, &tracked,
> + transport->remote, NULL);
> + for_each_string_list_item(item, &tracked)
> + strvec_push(&transport_ls_refs_options.ref_prefixes,
> + item->string);
> + string_list_clear(&tracked, 0);
>
> /*
> * If there are no refs specified to fetch, then we just
> diff --git a/remote.c b/remote.c
> index c9252a90f4..965e42ff52 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -1891,6 +1891,21 @@ int branch_merge_matches(struct branch *branch,
> return refname_match(branch->merge[i]->src, refname);
> }
>
> +void collect_upstream_from_remote(struct repository *repo,
> + struct string_list *tracked,
> + struct remote *remote,
> + const char *refname)
I'm not sure if we have guidance about where in the parameter list
"out" parameters should go, but I would expect them to be last? Idk.
Should the "inputs" be const to better communicate that we aren't
going to modify them? I'm not really sure of the const semantics of C
anyway, and I know there have been topics in the recent past around
const-modifier lints and so on.
> +{
> + 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++)
> + string_list_insert(tracked, branch->merge[i]->src);
> +}
We do trade some performance now, right? We first collect a
string_list, then push them into a strvec, which has some overhead
relative to skipping the intermediate step. Plus, string_lists sort,
which the original didn't do. And the caller uses the DUP version of
string_list (and IIUC strvec_push strdup's as well), so we end up
copying twice?
If we used a strvec instead, we could use strvec_pushv, perhaps, being
a bit more efficient? (It would be nice if we had an ownership-taking
strvec_appendv or something?)
But stepping back, if the caller just provides a strvec instead of a
string_list, we can push to it directly, since that's what happens
anyway! We don't do any filtering or anything in the new caller.
> __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..7c86c529b6 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,
> + struct string_list *tracked,
> + struct remote *remote,
> + const char *refname);
> +
> /* list of the remote in a group as configured */
> struct remote_group_data {
> const char *name;
> --
> gitgitgadget
>
--
D. Ben Knoble
Re: [PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:09
Date: Sat, 24 Oct 2026 15:09
336 lines
26009 bytes
26009 bytes
On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Configuring remote.<name>.refmap without remote.<name>.fetch used to
> make a refspec-less "git fetch <name>" fail with "--refmap option is
> only meaningful with command-line refspec(s)", since a refmap only
> says where to put fetched refs, not what to fetch.
This makes this commit sound like a fixup of [1/5], but I think it's
actually one of the main goals of the series! Can we flip the
justification around to say something like "we want to allow refmaps
to do XYZ […] Incidentally, we also make sense of the case when refmap
is configured without fetch" ? That would make the purpose clearer to
me, at least.
> Make that case infer what to fetch: the local branches whose
> @{upstream} is already on that remote. This lets a remote be
> configured to fetch only the branches actually in use, without
> listing them by hand in remote.<name>.fetch, and without needing to
> touch the command line every time.
>
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
> Documentation/config/remote.adoc | 5 +-
> Documentation/fetch-options.adoc | 6 +-
> builtin/fetch.c | 75 +++++++++++++++----
> remote.c | 24 +++++++
> remote.h | 4 ++
> t/meson.build | 1 +
> t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++
> 7 files changed, 219 insertions(+), 15 deletions(-)
> create mode 100755 t/t5586-fetch-refmap.sh
>
> diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
> index 103eada406..7c52e443a5 100644
> --- a/Documentation/config/remote.adoc
> +++ b/Documentation/config/remote.adoc
> @@ -36,7 +36,10 @@ remote.<name>.fetch::
> remote.<name>.refmap::
> The default value of the `--refmap` option for linkgit:git-fetch[1].
> Used to map remote refs being fetched to remote-tracking refs to
> - store. See the `--refmap` entry in linkgit:git-fetch[1].
> + store. If `remote.<name>.fetch` is not set either, a refspec-less
> + fetch infers what to fetch from local branches built on this
> + remote, instead of fetching every branch it has. See the
> + `--refmap` entry in linkgit:git-fetch[1].
>
> remote.<name>.push::
> The default set of "refspec" for linkgit:git-push[1]. See
> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
> index c2101a7b39..90b9e13ac2 100644
> --- a/Documentation/fetch-options.adoc
> +++ b/Documentation/fetch-options.adoc
> @@ -245,8 +245,10 @@ 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>.refmap`) but
> +nothing to fetch is specified on the command line, nor is there a
> +`remote.<name>.fetch`, branches from the remote that are used as the
"remote-tracking branches" ??
> +`@{upstream}` of our local branches are fetched.
The clause here is complicated ':D I found "infer what to fetch from
local branches built on this remote" to be a bit clearer, but we
definitely still need to get to the point, which is that
remote-tracking branches from this remote that are also configured as
upstreams of local branches are fetched.
Now, I wonder a bit: the doc says "when a refmap is active" we do that
"fetch remote-tracking branches that are upstream of local branches"
[all scoped to the current remote]---but it doesn't really say how the
refmap affects that? I mean, I imagine it's used for mapping (duh),
but I don't know if it also narrows the scope, or if the scope is
deliberately all remote-tracking branches that meet the requirements,
or…?
IOW, it seems to beg the question "why does the presence of a refmap
control this if it has no effect on the set of selected branches," and
I think the answer is partly "because we designed it that way" and
possibly found in the mails leading up to this point, but we might
want to try to connect the dots for folks? Idk.
>
> `-t`::
> `--tags`::
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index 136a59154a..0fc1fb66d5 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -524,6 +524,8 @@ 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;
> @@ -531,15 +533,32 @@ 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 (!rs->nr && remote && !remote->fetch.nr &&
> + effective_refmap && effective_refmap->nr) {
> + struct string_list tracked = STRING_LIST_INIT_DUP;
> + struct string_list_item *item;
> +
> + branches_tracking_remote(the_repository, remote, &tracked);
> + for_each_string_list_item(item, &tracked)
> + refspec_append(&inferred_rs, item->string);
> + string_list_clear(&tracked, 0);
Interesting choice of string_list again ;) do we need the sorting
behavior? (In fact, since it could come from an "ordered" config,
perhaps we should use that order?) A strvec seems more appropriate
then…
> +
> + rs = &inferred_rs;
> + infer_from_refmap = 1;
> + }
> +
> 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;
> }
> @@ -574,6 +593,8 @@ 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 {
> @@ -669,6 +690,7 @@ 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;
> }
>
> @@ -1978,13 +2000,47 @@ 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) {
> + struct string_list tracked = STRING_LIST_INIT_DUP;
> + struct string_list_item *item;
> +
> + 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.
Is this a "we haven't resolved what to do" case, or a "some remote (or
something) hasn't resolved" case? I'm not sure what I should be
looking at here.
Oh, I see. It's a duplicate of comment and code from the
else{effective_refmap->nr} block below. Hrm… probably it would be nice
not to duplicate that? I can't spot any differences between the code
blocks.
> + */
> + 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;
> +
> + /*
> + * 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, &tracked,
> + transport->remote, NULL);
> + for_each_string_list_item(item, &tracked)
> + strvec_push(&transport_ls_refs_options.ref_prefixes,
> + item->string);
> + string_list_clear(&tracked, 0);
> } else {
> + /*
> + * The --refmap command line option, if given, takes
> + * precedence over remote.<name>.refmap.
> + */
> + struct refspec *effective_refmap = refmap.nr ? &refmap :
> + &transport->remote->refmap;
> struct string_list tracked = STRING_LIST_INIT_DUP;
> struct string_list_item *item;
>
> - if (transport->remote->fetch.nr) {
> - refspec_ref_prefixes(&transport->remote->fetch,
> - &transport_ls_refs_options.ref_prefixes);
> + if (effective_refmap->nr) {
> + branches_tracking_remote(the_repository,
> + transport->remote, &tracked);
> /*
> * See remote.c's handling of remote.<name>.followRemoteHEAD
> * for the analogous, still-unresolved case.
> @@ -1995,15 +2051,10 @@ static int do_fetch(struct transport *transport,
> follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
> else
> follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
> + } else {
> + collect_upstream_from_remote(the_repository, &tracked,
> + transport->remote, NULL);
> }
> -
> - /*
> - * 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, &tracked,
> - transport->remote, NULL);
> for_each_string_list_item(item, &tracked)
> strvec_push(&transport_ls_refs_options.ref_prefixes,
> item->string);
> diff --git a/remote.c b/remote.c
> index 965e42ff52..6467496a0f 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -1906,6 +1906,30 @@ void collect_upstream_from_remote(struct repository *repo,
> string_list_insert(tracked, branch->merge[i]->src);
> }
>
> +struct branches_tracking_remote_cb_data {
> + struct repository *repo;
> + struct remote *remote;
> + struct string_list *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->tracked, data->remote,
> + ref->name);
> + return 0;
> +}
> +
> +void branches_tracking_remote(struct repository *repo, struct remote *remote,
> + struct string_list *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, ...)
> {
> diff --git a/remote.h b/remote.h
> index 7c86c529b6..62265346d3 100644
> --- a/remote.h
> +++ b/remote.h
> @@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo,
> struct remote *remote,
> const char *refname);
>
> +/* fills tracked with the refname of every local branch's upstream on remote */
> +void branches_tracking_remote(struct repository *repo, struct remote *remote,
> + struct string_list *tracked);
> +
> /* list of the remote in a group as configured */
> struct remote_group_data {
> const char *name;
I stopped reading here, but it made a bit of sense up to this point. I
wonder if the remote.h comment on the new function could be more
clear, but :shrug:
--
D. Ben Knoble
Re: [PATCH v10 4/5] remote: add "git remote add --limited-fetch"
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:11
Date: Sat, 24 Oct 2026 15:11
145 lines
17558 bytes
17558 bytes
On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> 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>
> ---
> diff --git a/builtin/remote.c b/builtin/remote.c
> index fb7e5b114f..a7c13946d1 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"));
We need "== 1" because default to "-1" for unspecified, which is a bit
atypical of boolean flags unless we need to know about that case.
Reading ahead, 5/5 wants to know if limited-fetch was unspecified so
we can act on it. Ok.
>
> 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;
> +
:\ I don't have a better suggestion for formatting this line, but it's… odd.
> 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
Looks reasonable.
--
D. Ben Knoble
Re: [PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:13
Date: Sat, 24 Oct 2026 15:13
95 lines
15852 bytes
15852 bytes
On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Adding a second remote to a shallow, single-branch 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 and single-branch in the first place, and can
> make a plain "git fetch" on that remote hang or take a very long
> time on a repository with many branches.
First we focus on "single-branch shallow clones", but…
> 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.
…now we only care about shallow. I think that's reasonable, since we
don't have a way to track "was cloned with a single-branch" (and it's
not really the key criterion anyway), but the wording begged the
question ;)
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
> Documentation/git-remote.adoc | 8 ++++-
> builtin/remote.c | 5 ++-
> t/t5505-remote.sh | 60 +++++++++++++++++++++++++++++++++++
> 3 files changed, 71 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/git-remote.adoc b/Documentation/git-remote.adoc
> index f8764394e7..f43d707c91 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,8 @@ 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.
> +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 a7c13946d1..8c4497cccd 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) {
> int 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) {
I didn't read the tests, but the code looks short and sweet ;)
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: "D. Ben Knoble"
Date: Sat, 24 Oct 2026 15:15
Date: Sat, 24 Oct 2026 15:15
53 lines
14075 bytes
14075 bytes
On Fri, Oct 23, 2026 at 7:13 PM Junio C Hamano <gitster@pobox.com> wrote: > > "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > > > Avoid fetching every branch of a new remote in a shallow repo. > > > > Changes in v10: > > > > * Resolve merge conflict after master integrated > > ch/fetch-followremotehead-lazy-validation. > > > Harald Nordgren (5): > > fetch: add remote.<name>.refmap > > fetch: extract collect_upstream_from_remote() helper > > fetch: infer branches to fetch from a refmap-only remote > > remote: add "git remote add --limited-fetch" > > remote: default to --limited-fetch in a shallow repository > > > > Documentation/config/remote.adoc | 8 +++ > > Documentation/fetch-options.adoc | 5 ++ > > Documentation/git-remote.adoc | 14 +++- > > builtin/fetch.c | 92 ++++++++++++++++++++---- > > builtin/remote.c | 31 ++++++-- > > remote.c | 51 ++++++++++++- > > remote.h | 19 +++++ > > t/meson.build | 1 + > > t/t5505-remote.sh | 76 ++++++++++++++++++++ > > t/t5510-fetch.sh | 63 ++++++++++++++++ > > t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++ > > 11 files changed, 456 insertions(+), 23 deletions(-) > > create mode 100755 t/t5586-fetch-refmap.sh > > This topic seeems fairly well done, without seeing any additional > comments in the last two rounds, but I am highly disturbed that I > was practically the only person who reviewd its later iterations. > Because I am not a target audience of the feature, I would not be > surprised if I am missing something large. > > I would very much appreciate if somebody else can give the series > one more read and Ack them, so that we can move it forward. > > Thanks. I gave it a read; it's possible the things I wrote were already seen by you and not worth commenting on, but I'm not sure. As for the reviewing… well, it was nice to review when it felt like things had settled somewhat :) -- D. Ben Knoble
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Sat, 24 Oct 2026 17:18
Date: Sat, 24 Oct 2026 17:18
42 lines
13692 bytes
13692 bytes
> remote.h says this procedure does "For the given remote, reads the
> refspec's src and sets the other fields." Ok, and glancing at
> refspec_find_match the idea is that "refspec" input is a query (with a
> src or dst) and we find the matching entry and fill out the input's
> fields.
>
> So… I assume the usual idea would be that refmap is a "superset" of
> the fetch refspec, since I might want to map refs that I don't
> normally fetch. (Or it might be disjoint, if none of the mappings
> apply to my usual fetches.) But that means that remote_find_tracking()
> won't be able to find entries for the mapped refs that aren't part of
> my "normal" fetches? Are there downstream consequences of that? I'm
> not sure, I haven't looked at how else remote_find_tracking is used.
>
> Could that affect, for example, --delete-merged?
Good point, I will change it like this:
```
1: ba7f1354cd ! 16: 4603c84d68 fetch: add remote.<name>.refmap
@@ remote.c: 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);
-+ if (remote->fetch.nr)
-+ return refspec_find_match(&remote->fetch, refspec);
-+ return refspec_find_match(&remote->refmap, refspec);
++ if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))
++ return refspec_find_match(&remote->refmap, refspec);
++ return 0;
}
static struct ref *alloc_ref_with_prefix(const char *prefix,
size_t prefixlen,
```
Harald
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: "D. Ben Knoble"
Date: Sun, 25 Oct 2026 14:14
Date: Sun, 25 Oct 2026 14:14
52 lines
14348 bytes
14348 bytes
On Sat, Oct 24, 2026 at 1:19 PM Harald Nordgren
<haraldnordgren@gmail.com> wrote:
>
> > remote.h says this procedure does "For the given remote, reads the
> > refspec's src and sets the other fields." Ok, and glancing at
> > refspec_find_match the idea is that "refspec" input is a query (with a
> > src or dst) and we find the matching entry and fill out the input's
> > fields.
> >
> > So… I assume the usual idea would be that refmap is a "superset" of
> > the fetch refspec, since I might want to map refs that I don't
> > normally fetch. (Or it might be disjoint, if none of the mappings
> > apply to my usual fetches.) But that means that remote_find_tracking()
> > won't be able to find entries for the mapped refs that aren't part of
> > my "normal" fetches? Are there downstream consequences of that? I'm
> > not sure, I haven't looked at how else remote_find_tracking is used.
> >
> > Could that affect, for example, --delete-merged?
>
> Good point, I will change it like this:
>
> ```
> 1: ba7f1354cd ! 16: 4603c84d68 fetch: add remote.<name>.refmap
> @@ remote.c: 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);
> -+ if (remote->fetch.nr)
> -+ return refspec_find_match(&remote->fetch, refspec);
> -+ return refspec_find_match(&remote->refmap, refspec);
> ++ if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))
> ++ return refspec_find_match(&remote->refmap, refspec);
> ++ return 0;
> }
>
> static struct ref *alloc_ref_with_prefix(const char *prefix,
> size_t prefixlen,
> ```
That does look like it would fall back on the refmap, but to be honest
I'm not sure what is "correct" here :) I was hoping for more
discussion of the questions I raised to see some thought about what we
should or shouldn't do. I suppose falling back might be important for
the later step with --limited-fetch, though?
--
D. Ben Knoble
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Sun, 25 Oct 2026 16:47
Date: Sun, 25 Oct 2026 16:47
66 lines
14872 bytes
14872 bytes
On Sun, Oct 25, 2026 at 4:14 PM D. Ben Knoble <ben.knoble@gmail.com> wrote:
>
> On Sat, Oct 24, 2026 at 1:19 PM Harald Nordgren
> <haraldnordgren@gmail.com> wrote:
> >
> > > remote.h says this procedure does "For the given remote, reads the
> > > refspec's src and sets the other fields." Ok, and glancing at
> > > refspec_find_match the idea is that "refspec" input is a query (with a
> > > src or dst) and we find the matching entry and fill out the input's
> > > fields.
> > >
> > > So… I assume the usual idea would be that refmap is a "superset" of
> > > the fetch refspec, since I might want to map refs that I don't
> > > normally fetch. (Or it might be disjoint, if none of the mappings
> > > apply to my usual fetches.) But that means that remote_find_tracking()
> > > won't be able to find entries for the mapped refs that aren't part of
> > > my "normal" fetches? Are there downstream consequences of that? I'm
> > > not sure, I haven't looked at how else remote_find_tracking is used.
> > >
> > > Could that affect, for example, --delete-merged?
> >
> > Good point, I will change it like this:
> >
> > ```
> > 1: ba7f1354cd ! 16: 4603c84d68 fetch: add remote.<name>.refmap
> > @@ remote.c: 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);
> > -+ if (remote->fetch.nr)
> > -+ return refspec_find_match(&remote->fetch, refspec);
> > -+ return refspec_find_match(&remote->refmap, refspec);
> > ++ if (!remote->fetch.nr || refspec_find_match(&remote->fetch, refspec))
> > ++ return refspec_find_match(&remote->refmap, refspec);
> > ++ return 0;
> > }
> >
> > static struct ref *alloc_ref_with_prefix(const char *prefix,
> > size_t prefixlen,
> > ```
>
> That does look like it would fall back on the refmap, but to be honest
> I'm not sure what is "correct" here :) I was hoping for more
> discussion of the questions I raised to see some thought about what we
> should or shouldn't do. I suppose falling back might be important for
> the later step with --limited-fetch, though?
Without the fallback, @{upstream}, --delete-merged and "git branch
--track" fail for refs fetched through the refmap.
I'll add tests to lock down that such refs are tracked through the
refmap, and that refs excluded by a negative refspec in
remote.<name>.fetch are not.
--limited-fetch does not need the fallback.
Harald
Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper
Author: Harald Nordgren
Date: Sun, 25 Oct 2026 17:20
Date: Sun, 25 Oct 2026 17:20
99 lines
15879 bytes
15879 bytes
> > When a fetch has no refspec to work from, we fall back to guessing
> > what to fetch: the remote's configured fetch refspec if there is one,
> > and separately, whether the current branch's upstream lives on this
> > remote.
>
> It took me a couple tries to parse this because the 2 halves ("[one],
> and separately, [two]") are 2 different kinds of objects! I think what
> you are trying to tell me is
>
> …we fall back to guessing what to fetch: the remote's …
> separately, the current branch's upstream if it lives on the fetched
> remote.
>
> That way [two] is also a "thing to fetch" in the same class of objects
> as [one]? Or have I misunderstood?
I'll take a look!
> > The latter is independent of the former on purpose: a narrow
> > refspec (for example from "git remote add -t") can leave the current
> > branch's own upstream uncovered, and we still need to be able to fetch
> > it.
>
> I don't really understand the meaning here. I can guess that you mean
> something like "A remote with a narrow refspec, like what you get from
> 'git remote add -t', can omit a branch's upstream from the remote's
> fetch refspec, and we still want to fetch that upstream." But it's
> taken me a while to put that together (after puzzling out the above)
> after first not knowing what "uncovered" meant.
>
> I think the whole thing would be clearer with an example:
>
> """
> Imagine a remote with a narrow refspec:
>
> git remote add -t main colleague <remote>
>
> and a branch whose upstream points at that remote, but isn't part of
> the remote's refspec:
>
> git fetch colleague work:refs/remotes/colleague/work
> git switch -c work colleague/work
>
> When we "git fetch colleague" without a refspec, we decide what to
> fetch by combining the remote's refspec (main -> remote-tracking
> branch, in this case) and the current branch's upstream, if it was
> based on that remote (work -> remote-tracking branch, in this case).
> """
>
> Note: we aren't "guessing" what to fetch!
Thanks! Maybe this should be the example instead?
git clone --single-branch <url>
git switch -c work
git push -u origin work
> I'm not sure if we have guidance about where in the parameter list
> "out" parameters should go, but I would expect them to be last? Idk.
>
> Should the "inputs" be const to better communicate that we aren't
> going to modify them? I'm not really sure of the const semantics of C
> anyway, and I know there have been topics in the recent past around
> const-modifier lints and so on.
Yes.
> > +{
> > + 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++)
> > + string_list_insert(tracked, branch->merge[i]->src);
> > +}
>
> We do trade some performance now, right? We first collect a
> string_list, then push them into a strvec, which has some overhead
> relative to skipping the intermediate step. Plus, string_lists sort,
> which the original didn't do. And the caller uses the DUP version of
> string_list (and IIUC strvec_push strdup's as well), so we end up
> copying twice?
>
> If we used a strvec instead, we could use strvec_pushv, perhaps, being
> a bit more efficient? (It would be nice if we had an ownership-taking
> strvec_appendv or something?)
>
> But stepping back, if the caller just provides a strvec instead of a
> string_list, we can push to it directly, since that's what happens
> anyway! We don't do any filtering or anything in the new caller.
Yes, I'll handle this.
Harald
Re: [PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote
Author: Harald Nordgren
Date: Sun, 25 Oct 2026 17:27
Date: Sun, 25 Oct 2026 17:27
220 lines
21251 bytes
21251 bytes
> > Configuring remote.<name>.refmap without remote.<name>.fetch used to
> > make a refspec-less "git fetch <name>" fail with "--refmap option is
> > only meaningful with command-line refspec(s)", since a refmap only
> > says where to put fetched refs, not what to fetch.
>
> This makes this commit sound like a fixup of [1/5], but I think it's
> actually one of the main goals of the series! Can we flip the
> justification around to say something like "we want to allow refmaps
> to do XYZ […] Incidentally, we also make sense of the case when refmap
> is configured without fetch" ? That would make the purpose clearer to
> me, at least.
Good idea.
> > Make that case infer what to fetch: the local branches whose
> > @{upstream} is already on that remote. This lets a remote be
> > configured to fetch only the branches actually in use, without
> > listing them by hand in remote.<name>.fetch, and without needing to
> > touch the command line every time.
> >
> > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> > ---
> > Documentation/config/remote.adoc | 5 +-
> > Documentation/fetch-options.adoc | 6 +-
> > builtin/fetch.c | 75 +++++++++++++++----
> > remote.c | 24 +++++++
> > remote.h | 4 ++
> > t/meson.build | 1 +
> > t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++
> > 7 files changed, 219 insertions(+), 15 deletions(-)
> > create mode 100755 t/t5586-fetch-refmap.sh
> >
> > diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
> > index 103eada406..7c52e443a5 100644
> > --- a/Documentation/config/remote.adoc
> > +++ b/Documentation/config/remote.adoc
> > @@ -36,7 +36,10 @@ remote.<name>.fetch::
> > remote.<name>.refmap::
> > The default value of the `--refmap` option for linkgit:git-fetch[1].
> > Used to map remote refs being fetched to remote-tracking refs to
> > - store. See the `--refmap` entry in linkgit:git-fetch[1].
> > + store. If `remote.<name>.fetch` is not set either, a refspec-less
> > + fetch infers what to fetch from local branches built on this
> > + remote, instead of fetching every branch it has. See the
> > + `--refmap` entry in linkgit:git-fetch[1].
> >
> > remote.<name>.push::
> > The default set of "refspec" for linkgit:git-push[1]. See
> > diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
> > index c2101a7b39..90b9e13ac2 100644
> > --- a/Documentation/fetch-options.adoc
> > +++ b/Documentation/fetch-options.adoc
> > @@ -245,8 +245,10 @@ 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>.refmap`) but
> > +nothing to fetch is specified on the command line, nor is there a
> > +`remote.<name>.fetch`, branches from the remote that are used as the
>
> "remote-tracking branches" ??
Yes, the next version will say "remote-tracking branches from this
remote that are also configured as the @{upstream} of a local branch
are fetched".
> > +`@{upstream}` of our local branches are fetched.
>
> The clause here is complicated ':D I found "infer what to fetch from
> local branches built on this remote" to be a bit clearer, but we
> definitely still need to get to the point, which is that
> remote-tracking branches from this remote that are also configured as
> upstreams of local branches are fetched.
>
> Now, I wonder a bit: the doc says "when a refmap is active" we do that
> "fetch remote-tracking branches that are upstream of local branches"
> [all scoped to the current remote]---but it doesn't really say how the
> refmap affects that? I mean, I imagine it's used for mapping (duh),
> but I don't know if it also narrows the scope, or if the scope is
> deliberately all remote-tracking branches that meet the requirements,
> or…?
>
> IOW, it seems to beg the question "why does the presence of a refmap
> control this if it has no effect on the set of selected branches," and
> I think the answer is partly "because we designed it that way" and
> possibly found in the mails leading up to this point, but we might
> want to try to connect the dots for folks? Idk.
I'll take a look at improving this part.
> >
> > `-t`::
> > `--tags`::
> > diff --git a/builtin/fetch.c b/builtin/fetch.c
> > index 136a59154a..0fc1fb66d5 100644
> > --- a/builtin/fetch.c
> > +++ b/builtin/fetch.c
> > @@ -524,6 +524,8 @@ 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;
> > @@ -531,15 +533,32 @@ 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 (!rs->nr && remote && !remote->fetch.nr &&
> > + effective_refmap && effective_refmap->nr) {
> > + struct string_list tracked = STRING_LIST_INIT_DUP;
> > + struct string_list_item *item;
> > +
> > + branches_tracking_remote(the_repository, remote, &tracked);
> > + for_each_string_list_item(item, &tracked)
> > + refspec_append(&inferred_rs, item->string);
> > + string_list_clear(&tracked, 0);
>
> Interesting choice of string_list again ;) do we need the sorting
> behavior? (In fact, since it could come from an "ordered" config,
> perhaps we should use that order?) A strvec seems more appropriate
> then…
Agreed, the next version uses a strvec here too. Nothing is sorted
anymore: the branches come in refname order, and each branch's merge
entries in config order.
> > +
> > + rs = &inferred_rs;
> > + infer_from_refmap = 1;
> > + }
> > +
> > 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;
> > }
> > @@ -574,6 +593,8 @@ 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 {
> > @@ -669,6 +690,7 @@ 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;
> > }
> >
> > @@ -1978,13 +2000,47 @@ 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) {
> > + struct string_list tracked = STRING_LIST_INIT_DUP;
> > + struct string_list_item *item;
> > +
> > + 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.
>
> Is this a "we haven't resolved what to do" case, or a "some remote (or
> something) hasn't resolved" case? I'm not sure what I should be
> looking at here.
>
> Oh, I see. It's a duplicate of comment and code from the
> else{effective_refmap->nr} block below. Hrm… probably it would be nice
> not to duplicate that? I can't spot any differences between the code
> blocks.
Yes, good idea!
Harald
Re: [PATCH v10 5/5] remote: default to --limited-fetch in a shallow repository
Author: Harald Nordgren
Date: Sun, 25 Oct 2026 18:12
Date: Sun, 25 Oct 2026 18:12
27 lines
13234 bytes
13234 bytes
> > Adding a second remote to a shallow, single-branch 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 and single-branch in the first place, and can > > make a plain "git fetch" on that remote hang or take a very long > > time on a repository with many branches. > > First we focus on "single-branch shallow clones", but… > > > 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. > > …now we only care about shallow. I think that's reasonable, since we > don't have a way to track "was cloned with a single-branch" (and it's > not really the key criterion anyway), but the wording begged the > question ;) Good point, I'll improve. Harald
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Sun, 25 Oct 2026 22:15
Date: Sun, 25 Oct 2026 22:15
21 lines
13840 bytes
13840 bytes
"D. Ben Knoble" <ben.knoble@gmail.com> writes: > That does look like it would fall back on the refmap, but to be honest > I'm not sure what is "correct" here :) I was hoping for more > discussion of the questions I raised to see some thought about what we > should or shouldn't do. I suppose falling back might be important for > the later step with --limited-fetch, though? I just encountered a case where I wished to configure both refmap and fetch at the same time. From a second remote (let's call it "cabal"), store any branch fetched from them in refs/remotes/cabal/ but I want to fetch only their 'master', 'next' and 'seen'. I'd want to be able to write something like: [remote "cabal"] fetch = master next seen refmap = +refs/heads/*:refs/remotes/cabal/* And, no, this does not require any inferring based on what is checked out and what the checked out branch tracks.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Mon, 26 Oct 2026 08:21
Date: Mon, 26 Oct 2026 08:21
24 lines
13050 bytes
13050 bytes
> > That does look like it would fall back on the refmap, but to be honest > > I'm not sure what is "correct" here :) I was hoping for more > > discussion of the questions I raised to see some thought about what we > > should or shouldn't do. I suppose falling back might be important for > > the later step with --limited-fetch, though? > > I just encountered a case where I wished to configure both refmap > and fetch at the same time. From a second remote (let's call it > "cabal"), store any branch fetched from them in refs/remotes/cabal/ > but I want to fetch only their 'master', 'next' and 'seen'. I'd > want to be able to write something like: > > [remote "cabal"] > fetch = master next seen > refmap = +refs/heads/*:refs/remotes/cabal/* > > And, no, this does not require any inferring based on what is > checked out and what the checked out branch tracks. Yes, I think we can handle this as well with some extra code. Harald
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: Phillip Wood
Date: Mon, 26 Oct 2026 09:43
Date: Mon, 26 Oct 2026 09:43
57 lines
13811 bytes
13811 bytes
On 24/10/2026 00:13, Junio C Hamano wrote:
> "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> This topic seeems fairly well done, without seeing any additional
> comments in the last two rounds, but I am highly disturbed that I
> was practically the only person who reviewd its later iterations.
> Because I am not a target audience of the feature, I would not be
> surprised if I am missing something large.
>
> I would very much appreciate if somebody else can give the series
> one more read and Ack them, so that we can move it forward.
I've just read through the commit messages and documentation changes and
I think it is a mistake to use a single config variable to configure
where fetched refs are stored and which refs are fetched. The problem
this series tries to solve is that the default refspec fetches all the
branches from the remote and it's not possible to use that mapping and
also restrict which branches are fetched. To fix that it introduces a
new config setting to configure where the refs are stored which is good,
but it then uses the presence of that setting to implicitly configure
the default set of refs to fetch. To me that repeats the mistake of the
existing fetch config.
It would be much more flexible if we had a separate config setting
(maybe remote.<name>.fetchRefs) to describe which refs are fetched. That
setting could accept something like "*@{upstream}" to "mean fetch the
upstream branches of my local branches that live on this remote" which
would cover what this series wants. I think it would be useful to
support "+" and "^" prefixes on the refs to fetch as well, so that the
refmap config controls the mapping and the "what to fetch" config
handles negation and forced updates on a per-ref basis.
Setting
remote.<name>.fetchRefmap refs/heads/*:refs/remotes/<name>/*
remote.<name>.fetchRefs +refs/heads/*
would be equivalent to
remote.<name>.fetch +refs/heads/*:refs/remotes/<name>/*
Ideally when we're seeing which refs to fetch we'd use the last entry
that matches so that
remote.<name>.fetchRefs +refs/head/*
remote.<name>.fetchRefs refs/heads/master
would refuse non-fast-forward updates for master. Currently we use the
first refspec we find that matches which goes against the usual "last
one wins" behavior for config settings and makes it impossible to add
per-branch refspecs with "git config" because the default setting comes
first in the config file and matches all branches.
Thanks
Phillip
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Phillip Wood
Date: Mon, 26 Oct 2026 09:44
Date: Mon, 26 Oct 2026 09:44
223 lines
19749 bytes
19749 bytes
Hi Harald
On 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote:
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Add a per-remote config variable, remote.<name>.refmap, that provides
> the default value for --refmap the same way remote.<name>.fetch
> already provides the default refspecs to fetch.
The second half of this sentence seems redundant to me. Can we please
call the new config setting "remote.<name>.fetchRefmap" so that it is
clear it applies to fetches. That would also allow us to add a matching
"remote.<name>.pushRefmap" in the future to map the refnames of refs
that are pushed without setting up a default set of refs to push.
> Like --refmap itself,
> it only maps refs that are actually being fetched, so it has nothing
> to do when there is nothing explicit to fetch, on the command line or
> via remote.<name>.fetch.
So the default value behaves like the option - why wouldn't it?
> A refspec-less fetch with a refmap configured but nothing else to say
> what to fetch falls back to the same defaults as a remote with no
> refspec or refmap at all. Only an explicit --refmap given on the
> command line with no command-line refspec to go with it is still
> rejected, since that combination is a plain mistake to type, unlike a
> remote.<name>.refmap configured on its own. A remote.<name>.fetch
> that is also configured is used as before.
I'm afraid I cannot understand what this paragraph is trying to say.
> diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc
>
> +remote.<name>.refmap::
> + The default value of the `--refmap` option for linkgit:git-fetch[1].
> + Used to map remote refs being fetched to remote-tracking refs to
> + store. See the `--refmap` entry in linkgit:git-fetch[1].
I'm not sure we need to link to the git fetch documentation twice. It
would be useful to explain how this interacts with remote.<name>.fetch.
The documentation for "--refmap" says that if it is given the refspec
config is ignored - so does setting this mean remote.<name>.fetch is
ignored?
> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
> ++
> +`remote.<name>.refmap` provides the default value for this option, the
> +same way `remote.<name>.fetch` provides the default refspecs to fetch.
As above the second half of this sentence doesn't really add anything.
Thanks
Phillip
>
> `-t`::
> `--tags`::
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index 1f76ae6242..ffcb5ba29a 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -518,6 +518,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>.refmap.
> + */
> + struct refspec *effective_refmap =
> + refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
>
> /* opportunistically-updated references: */
> struct ref *orefs = NULL, **oref_tail = &orefs;
> @@ -561,8 +567,8 @@ 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.
> */
> - if (refmap.nr)
> - fetch_refspec = &refmap;
> + if (effective_refmap && effective_refmap->nr)
> + fetch_refspec = effective_refmap;
> else
> fetch_refspec = &remote->fetch;
>
> diff --git a/remote.c b/remote.c
> index 49ee2ea318..c9252a90f4 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -152,6 +152,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);
> @@ -176,6 +177,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);
> @@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,
> return -1;
> refspec_append(&remote->fetch, v);
> free(v);
> + } else if (!strcmp(subkey, "refmap")) {
> + 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))
> @@ -995,7 +1003,9 @@ 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);
> + if (remote->fetch.nr)
> + return refspec_find_match(&remote->fetch, refspec);
> + return refspec_find_match(&remote->refmap, refspec);
> }
>
> 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 17c0be499c..ec5cd9dca6 100755
> --- a/t/t5510-fetch.sh
> +++ b/t/t5510-fetch.sh
> @@ -927,6 +927,69 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
> )
> '
>
> +test_expect_success 'remote.*.refmap acts like --refmap on the command line' '
> + test_when_finished "git -C three config --unset remote.origin.refmap" &&
> + git branch -f side &&
> + git -C three config remote.origin.refmap \
> + "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
> +}
> +
> +test_expect_success 'remote.<name>.refmap without tracking (baseline)' '
> + test_when_finished "rm -fr fetch-refmap-baseline fetch-refmap-upstream" &&
> + git init -b main fetch-refmap-upstream &&
> + test_commit -C fetch-refmap-upstream base &&
> + git -C fetch-refmap-upstream branch other &&
> + git init fetch-refmap-baseline &&
> + (
> + cd fetch-refmap-baseline &&
> + git remote add origin ../fetch-refmap-upstream &&
> +
> + # Without fetch refspec, but with fetch refmap.
> + git config --unset-all remote.origin.fetch &&
> + git config remote.origin.refmap "+refs/heads/*:refs/remotes/origin/*" &&
> +
> + # 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 &&
> + 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>.refmap plays no part in deciding what to
> + # fetch, only in remapping something that is already being
> + # fetched by name.
> + git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
> + git fetch origin &&
> + check_fetched_refs <<-\EOF
> + refs/remotes/origin/HEAD
> + refs/remotes/origin/main
> + refs/remotes/origin/other
> + EOF
> + )
> +'
> +
> test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
> git branch -f side &&
> (
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Phillip Wood
Date: Mon, 26 Oct 2026 10:44
Date: Mon, 26 Oct 2026 10:44
26 lines
12289 bytes
12289 bytes
On 25/10/2026 22:15, Junio C Hamano wrote: > > I just encountered a case where I wished to configure both refmap > and fetch at the same time. From a second remote (let's call it > "cabal"), store any branch fetched from them in refs/remotes/cabal/ > but I want to fetch only their 'master', 'next' and 'seen'. I'd > want to be able to write something like: > > [remote "cabal"] > fetch = master next seen > refmap = +refs/heads/*:refs/remotes/cabal/* That's a coincidence, I sent a message earlier [1] arguing that we should have separate config settings for "what to fetch" and "where to store it" Thanks Phillip [1] https://lore.kernel.org/git/e6af5d1e-4ca1-48a5-8a84-5e0cefbdbc2b@gmail.com > > And, no, this does not require any inferring based on what is > checked out and what the checked out branch tracks.
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: Junio C Hamano
Date: Mon, 26 Oct 2026 15:44
Date: Mon, 26 Oct 2026 15:44
64 lines
15904 bytes
15904 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:
> I've just read through the commit messages and documentation changes and
> I think it is a mistake to use a single config variable to configure
> where fetched refs are stored and which refs are fetched. The problem
> this series tries to solve is that the default refspec fetches all the
> branches from the remote and it's not possible to use that mapping and
> also restrict which branches are fetched. To fix that it introduces a
> new config setting to configure where the refs are stored which is good,
> but it then uses the presence of that setting to implicitly configure
> the default set of refs to fetch. To me that repeats the mistake of the
> existing fetch config.
>
> It would be much more flexible if we had a separate config setting
> (maybe remote.<name>.fetchRefs) to describe which refs are fetched.
Good point.
When I wrote the refmap feature (without configuration variable)
long time ago, one of the goals was to allow me to say
$ git fetch cabal \
--refmap="+refs/heads/*:refs/remotes/cabal/*" \
maint master next
That is, "here is the place I grab from there are stored" and
"maint master next are the things I grab from there" are specified
separately. With the configuration variable added in this series,
$ git config set remote.cabal.refmap "+refs/heads/*:refs/remotes/cabal/*"
$ git fetch cabal master next seen
should become possible with the same behaviour. I am not sure
offhand if this series does that, but I would consider it a bug if
it does not.
In any case, this series, since it lacks the separate setting, takes
the lack of "what to fetch" on the command line (and perhaps lack of
remote.cabal.fetch???) as a sign to trigger this new feature to
guess what to fetch based on what is tracked by the local branches.
For now, as this "what is tracked by the local branches" is the only
kind of smart that was designed to decide what to fetch, it may be OK
to use "you gave me nothing to specify what to fetch, so I'll guess
using the smart". But when we introduce more kinds of smart later,
this becomes a problem. So I do agree with you that we should have
something to specifically say "when I do not explicitly give you
what to fetch, decide based on what local branches track".
> That
> setting could accept something like "*@{upstream}" to "mean fetch the
> upstream branches of my local branches that live on this remote" which
> would cover what this series wants. I think it would be useful to
> support "+" and "^" prefixes on the refs to fetch as well, so that the
> refmap config controls the mapping and the "what to fetch" config
> handles negation and forced updates on a per-ref basis.
The reasoning here is slightly wrong. "^" (not) may be good thing
to consider as it is to decide what to fetch, but "+" (overwrite)
should be on the refmap side, i.e., where to store. If you are not
storing, then "overwrite what I have with non-fast-forward" becomes
a nonsense request, and that is why it does not belong to "what to
fetch".
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Mon, 26 Oct 2026 15:48
Date: Mon, 26 Oct 2026 15:48
48 lines
14740 bytes
14740 bytes
Harald Nordgren <haraldnordgren@gmail.com> writes:
>> > That does look like it would fall back on the refmap, but to be honest
>> > I'm not sure what is "correct" here :) I was hoping for more
>> > discussion of the questions I raised to see some thought about what we
>> > should or shouldn't do. I suppose falling back might be important for
>> > the later step with --limited-fetch, though?
>>
>> I just encountered a case where I wished to configure both refmap
>> and fetch at the same time. From a second remote (let's call it
>> "cabal"), store any branch fetched from them in refs/remotes/cabal/
>> but I want to fetch only their 'master', 'next' and 'seen'. I'd
>> want to be able to write something like:
>>
>> [remote "cabal"]
>> fetch = master next seen
>> refmap = +refs/heads/*:refs/remotes/cabal/*
>>
>> And, no, this does not require any inferring based on what is
>> checked out and what the checked out branch tracks.
>
> Yes, I think we can handle this as well with some extra code.
I actually do not think we should have master next seen on the same
remote.cabal.fetch configuration variable. It is a multi-valued
variable, so it should read more like
[remote "cabal"]
fetch = refs/heads/master
fetch = refs/heads/next
fetch = refs/heads/seen
refmap = +refs/heads/*:refs/remotes/cabal/*
And without these remote.cabal.fetch configured, but with
remote.cabal.refmap configured, this command line
$ git fetch cabal next
should work just like
$ git fetch cabal \
--refmap="+refs/heads/*:refs/remotes/cabal/*" \
next
I think.
Thanks.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Mon, 26 Oct 2026 15:51
Date: Mon, 26 Oct 2026 15:51
27 lines
14138 bytes
14138 bytes
Phillip Wood <phillip.wood123@gmail.com> writes: > On 25/10/2026 22:15, Junio C Hamano wrote: >> >> I just encountered a case where I wished to configure both refmap >> and fetch at the same time. From a second remote (let's call it >> "cabal"), store any branch fetched from them in refs/remotes/cabal/ >> but I want to fetch only their 'master', 'next' and 'seen'. I'd >> want to be able to write something like: >> >> [remote "cabal"] >> fetch = master next seen >> refmap = +refs/heads/*:refs/remotes/cabal/* > > That's a coincidence, I sent a message earlier [1] arguing that we > should have separate config settings for "what to fetch" and "where to > store it" In other words, "what to fetch" and "where to store them" have separate configuration. They should be made to interact correctly and the current patchset may or may not work the way we want. What we need a separate setting is probably what should happen when we do not specify "what to fetch" at all. One heuristics is what this series proposes i.e., "what our local branches build on". But that does not have to stay the only option forever.
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: Harald Nordgren
Date: Tue, 27 Oct 2026 12:22
Date: Tue, 27 Oct 2026 12:22
41 lines
13575 bytes
13575 bytes
> > I've just read through the commit messages and documentation changes and > > I think it is a mistake to use a single config variable to configure > > where fetched refs are stored and which refs are fetched. The problem > > this series tries to solve is that the default refspec fetches all the > > branches from the remote and it's not possible to use that mapping and > > also restrict which branches are fetched. To fix that it introduces a > > new config setting to configure where the refs are stored which is good, > > but it then uses the presence of that setting to implicitly configure > > the default set of refs to fetch. To me that repeats the mistake of the > > existing fetch config. > > > > It would be much more flexible if we had a separate config setting > > (maybe remote.<name>.fetchRefs) to describe which refs are fetched. > > Good point. Ok. > When I wrote the refmap feature (without configuration variable) > long time ago, one of the goals was to allow me to say > > $ git fetch cabal \ > --refmap="+refs/heads/*:refs/remotes/cabal/*" \ > maint master next > > That is, "here is the place I grab from there are stored" and > "maint master next are the things I grab from there" are specified > separately. With the configuration variable added in this series, > > $ git config set remote.cabal.refmap "+refs/heads/*:refs/remotes/cabal/*" > $ git fetch cabal master next seen > > should become possible with the same behaviour. I am not sure > offhand if this series does that, but I would consider it a bug if > it does not. It will do that in my next version and I added a test specifically for this. Harald
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Phillip Wood
Date: Tue, 27 Oct 2026 17:03
Date: Tue, 27 Oct 2026 17:03
40 lines
12767 bytes
12767 bytes
On 26/10/2026 15:48, Junio C Hamano wrote: > > I actually do not think we should have master next seen on the same > remote.cabal.fetch configuration variable. It is a multi-valued > variable, so it should read more like > > [remote "cabal"] > fetch = refs/heads/master > fetch = refs/heads/next > fetch = refs/heads/seen > refmap = +refs/heads/*:refs/remotes/cabal/* Doesn't that cause older versions of git to try and update the local branch when fetching, or is it only when pushing that we'll fill in the right hand side of the refspec with a copy of the left hand side? If we use a different config setting for "what to fetch" then users that want the new behavior can unset remote.<name>.fetch and older versions of git will fail rather than overwriting local branches when there is no refspec on the commandline. Thanks Phillip > > And without these remote.cabal.fetch configured, but with > remote.cabal.refmap configured, this command line > > $ git fetch cabal next > > should work just like > > $ git fetch cabal \ > --refmap="+refs/heads/*:refs/remotes/cabal/*" \ > next > > I think. > > Thanks.
Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo
Author: Phillip Wood
Date: Tue, 27 Oct 2026 17:04
Date: Tue, 27 Oct 2026 17:04
45 lines
12947 bytes
12947 bytes
On 26/10/2026 15:44, Junio C Hamano wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
> When I wrote the refmap feature (without configuration variable)
> long time ago, one of the goals was to allow me to say
>
> $ git fetch cabal \
> --refmap="+refs/heads/*:refs/remotes/cabal/*" \
> maint master next
>
> That is, "here is the place I grab from there are stored" and
> "maint master next are the things I grab from there" are specified
> separately. With the configuration variable added in this series,
>
> $ git config set remote.cabal.refmap "+refs/heads/*:refs/remotes/cabal/*"
> $ git fetch cabal master next seen
>
> should become possible with the same behaviour.
That makes sense
>> That
>> setting could accept something like "*@{upstream}" to "mean fetch the
>> upstream branches of my local branches that live on this remote" which
>> would cover what this series wants. I think it would be useful to
>> support "+" and "^" prefixes on the refs to fetch as well, so that the
>> refmap config controls the mapping and the "what to fetch" config
>> handles negation and forced updates on a per-ref basis.
>
> The reasoning here is slightly wrong. "^" (not) may be good thing
> to consider as it is to decide what to fetch, but "+" (overwrite)
> should be on the refmap side, i.e., where to store. If you are not
> storing, then "overwrite what I have with non-fast-forward" becomes
> a nonsense request, and that is why it does not belong to "what to
> fetch".
I see what you're saying. It is unfortunate though that we then need
separate refspecs if want to reject non fast-forward updates of some
branches but map the refs in the same way for the fast-forward and
non-fast-forward cases.
Thanks
Phillip
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Tue, 27 Oct 2026 17:21
Date: Tue, 27 Oct 2026 17:21
49 lines
15087 bytes
15087 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:
> On 26/10/2026 15:48, Junio C Hamano wrote:
>>
>> I actually do not think we should have master next seen on the same
>> remote.cabal.fetch configuration variable. It is a multi-valued
>> variable, so it should read more like
>>
>> [remote "cabal"]
>> fetch = refs/heads/master
>> fetch = refs/heads/next
>> fetch = refs/heads/seen
>> refmap = +refs/heads/*:refs/remotes/cabal/*
>
> Doesn't that cause older versions of git to try and update the local
> branch when fetching, or is it only when pushing that we'll fill in the
> right hand side of the refspec with a copy of the left hand side?
"git fetch --help" is fairly clear on what should happen in its
description on <refspec> command line arguments, and
I meant in the above example that remote.cabal.fetch should be used
as if these three refnames are written on the fetch command line
when nothing is given from the command line. Now, <refspec> that
does not have :dst side for fetch stores the result nowhere but
FETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of
git), you will get them in FETCH_HEAD, so your "git pull" will still
work to create an octopus merge ;-)
I was hoping that with refmap, the users can tell the system where
to store them. As specifying a shorthand also works,
[remote "cabal"]
fetch = master
fetch = next
fetch = seen
refmap = +refs/heads/*:refs/remotes/cabal/*
would be an easy layout to allow adding different branches than
having to say
[remote "cabal"]
fetch = +refs/heads/master:refs/remotes/cabal/master
fetch = +refs/heads/next:refs/remotes/cabal/next
fetch = +refs/heads/seen:refs/remotes/cabal/seen
Thanks.
Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper
Author: "D. Ben Knoble"
Date: Tue, 27 Oct 2026 19:41
Date: Tue, 27 Oct 2026 19:41
55 lines
14379 bytes
14379 bytes
On Sun, Oct 25, 2026 at 1:20 PM Harald Nordgren <haraldnordgren@gmail.com> wrote: > > > > The latter is independent of the former on purpose: a narrow > > > refspec (for example from "git remote add -t") can leave the current > > > branch's own upstream uncovered, and we still need to be able to fetch > > > it. > > > > I don't really understand the meaning here. I can guess that you mean > > something like "A remote with a narrow refspec, like what you get from > > 'git remote add -t', can omit a branch's upstream from the remote's > > fetch refspec, and we still want to fetch that upstream." But it's > > taken me a while to put that together (after puzzling out the above) > > after first not knowing what "uncovered" meant. > > > > I think the whole thing would be clearer with an example: > > > > """ > > Imagine a remote with a narrow refspec: > > > > git remote add -t main colleague <remote> > > > > and a branch whose upstream points at that remote, but isn't part of > > the remote's refspec: > > > > git fetch colleague work:refs/remotes/colleague/work > > git switch -c work colleague/work > > > > When we "git fetch colleague" without a refspec, we decide what to > > fetch by combining the remote's refspec (main -> remote-tracking > > branch, in this case) and the current branch's upstream, if it was > > based on that remote (work -> remote-tracking branch, in this case). > > """ > > > > Note: we aren't "guessing" what to fetch! > > Thanks! Maybe this should be the example instead? > > git clone --single-branch <url> > git switch -c work > git push -u origin work > I'm not sure if that captures the same "narrow refspec" remote or not, to be honest, though it might be simpler? I'm not sure what the push has to do with it, though, and I don't know what you think about the rest of the proposed wording ;) [later] Reading the new version, I think I see what you're after. I personally would love to see an example adding a remote, since I tend to want "big" fetches from my primary remotes, but that's fine.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Phillip Wood
Date: Wed, 28 Oct 2026 16:16
Date: Wed, 28 Oct 2026 16:16
41 lines
13371 bytes
13371 bytes
On 27/10/2026 17:21, Junio C Hamano wrote: > Phillip Wood <phillip.wood123@gmail.com> writes: >> >> Doesn't that cause older versions of git to try and update the local >> branch when fetching, or is it only when pushing that we'll fill in the >> right hand side of the refspec with a copy of the left hand side? > > I meant in the above example that remote.cabal.fetch should be used > as if these three refnames are written on the fetch command line > when nothing is given from the command line. Now, <refspec> that > does not have :dst side for fetch stores the result nowhere but > FETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of > git), you will get them in FETCH_HEAD, so your "git pull" will still > work to create an octopus merge ;-) Thanks for clarifying that > I was hoping that with refmap, the users can tell the system where > to store them. As specifying a shorthand also works, > > [remote "cabal"] > fetch = master > fetch = next > fetch = seen > refmap = +refs/heads/*:refs/remotes/cabal/* > > would be an easy layout to allow adding different branches than > having to say > > [remote "cabal"] > fetch = +refs/heads/master:refs/remotes/cabal/master > fetch = +refs/heads/next:refs/remotes/cabal/next > fetch = +refs/heads/seen:refs/remotes/cabal/seen That makes sense. What should we do if "remote.cabal.refmap" exists and "remote.cabal.fetch" contains a colon? We could just ignore that entry but it might be better to error out. Thanks Phillip
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Wed, 28 Oct 2026 17:42
Date: Wed, 28 Oct 2026 17:42
40 lines
14546 bytes
14546 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:
>> I was hoping that with refmap, the users can tell the system where
>> to store them. As specifying a shorthand also works,
>>
>> [remote "cabal"]
>> fetch = master
>> fetch = next
>> fetch = seen
>> refmap = +refs/heads/*:refs/remotes/cabal/*
>>
>> would be an easy layout to allow adding different branches than
>> having to say
>>
>> [remote "cabal"]
>> fetch = +refs/heads/master:refs/remotes/cabal/master
>> fetch = +refs/heads/next:refs/remotes/cabal/next
>> fetch = +refs/heads/seen:refs/remotes/cabal/seen
>
> That makes sense. What should we do if "remote.cabal.refmap" exists and
> "remote.cabal.fetch" contains a colon? We could just ignore that entry
> but it might be better to error out.
Possible alternatives include
- giving fetch with dst take precedence over refmap
- storing the result in two locations
- error out not knowing what is the right thing to do
I think the command line version, i.e.
$ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \
refs/heads/master:refs/remotes/origin/master \
next
stores 'master' as alt/master and origin/master, and 'next' as
alt/next, i.e., takes the second option.
Thanks.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Wed, 28 Oct 2026 20:57
Date: Wed, 28 Oct 2026 20:57
64 lines
14915 bytes
14915 bytes
On Wed, Oct 28, 2026 at 7:42 PM Junio C Hamano <gitster@pobox.com> wrote: > > Phillip Wood <phillip.wood123@gmail.com> writes: > > >> I was hoping that with refmap, the users can tell the system where > >> to store them. As specifying a shorthand also works, > >> > >> [remote "cabal"] > >> fetch = master > >> fetch = next > >> fetch = seen > >> refmap = +refs/heads/*:refs/remotes/cabal/* > >> > >> would be an easy layout to allow adding different branches than > >> having to say > >> > >> [remote "cabal"] > >> fetch = +refs/heads/master:refs/remotes/cabal/master > >> fetch = +refs/heads/next:refs/remotes/cabal/next > >> fetch = +refs/heads/seen:refs/remotes/cabal/seen > > > > That makes sense. What should we do if "remote.cabal.refmap" exists and > > "remote.cabal.fetch" contains a colon? We could just ignore that entry > > but it might be better to error out. > > Possible alternatives include > > - giving fetch with dst take precedence over refmap > - storing the result in two locations > - error out not knowing what is the right thing to do The answer seems to be the second one, with my v12, and I wrote a test now to confirm it, I can include it in a v13 when it's time: ``` +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 && + ( + 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 fetch cabal && + check_fetched_refs <<-\EOF + refs/remotes/cabal/HEAD + refs/remotes/cabal/master + refs/remotes/custom/master + EOF + ) +' ``` Harald
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Ben Knoble
Date: Thu, 29 Oct 2026 14:30
Date: Thu, 29 Oct 2026 14:30
25 lines
11995 bytes
11995 bytes
> Le 26 oct. 2026 à 05:44, Phillip Wood <phillip.wood123@gmail.com> a écrit : > > Hi Harald > >> On 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote: >> From: Harald Nordgren <haraldnordgren@gmail.com> >> Add a per-remote config variable, remote.<name>.refmap, that provides >> the default value for --refmap the same way remote.<name>.fetch >> already provides the default refspecs to fetch. > > The second half of this sentence seems redundant to me. Can we please call the new config setting "remote.<name>.fetchRefmap" so that it is clear it applies to fetches. That would also allow us to add a matching "remote.<name>.pushRefmap" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push. Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think. I’m on mobile now, but if I remember I’ll try to spell out what I mean more concretely. Thanks for the reminder!
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Thu, 29 Oct 2026 14:50
Date: Thu, 29 Oct 2026 14:50
33 lines
13075 bytes
13075 bytes
On Thu, Oct 29, 2026 at 4:30 PM Ben Knoble <ben.knoble@gmail.com> wrote: > > > > Le 26 oct. 2026 à 05:44, Phillip Wood <phillip.wood123@gmail.com> a écrit : > > > > Hi Harald > > > >> On 20/10/2026 14:42, Harald Nordgren via GitGitGadget wrote: > >> From: Harald Nordgren <haraldnordgren@gmail.com> > >> Add a per-remote config variable, remote.<name>.refmap, that provides > >> the default value for --refmap the same way remote.<name>.fetch > >> already provides the default refspecs to fetch. > > > > The second half of this sentence seems redundant to me. Can we please call the new config setting "remote.<name>.fetchRefmap" so that it is clear it applies to fetches. That would also allow us to add a matching "remote.<name>.pushRefmap" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push. > > Yes, I would love to add pushRefmap atop later! It would > ease my working with notes, I think. > > I’m on mobile now, but if I remember I’ll try to spell out what I mean > more concretely. Thanks for the reminder! It has already been renamed to fetchRefmap in v12. Harald
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Phillip Wood
Date: Thu, 29 Oct 2026 16:26
Date: Thu, 29 Oct 2026 16:26
50 lines
13876 bytes
13876 bytes
On 28/10/2026 17:42, Junio C Hamano wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
>>
>> That makes sense. What should we do if "remote.cabal.refmap" exists and
>> "remote.cabal.fetch" contains a colon? We could just ignore that entry
>> but it might be better to error out.
>
> Possible alternatives include
>
> - giving fetch with dst take precedence over refmap
> - storing the result in two locations
> - error out not knowing what is the right thing to do
>
> I think the command line version, i.e.
>
> $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \
> refs/heads/master:refs/remotes/origin/master \
> next
>
> stores 'master' as alt/master and origin/master, and 'next' as
> alt/next, i.e., takes the second option.
That's surprising, I'd naively assumed a refspec on the command line
would override the refmap. It feels problematic if we cannot override
the fetchRefmap config with a refspec on the command line, but need to
use "--refmap" instead.
I think there are at least a couple of ways of thinking about the
interaction of "remote.<name>.fetchRefmap" and "remote.<name>.fetch".
The first is to consider that the "fetchRefmap" config is for "where to
store the fetched refs" and the "fetch" config is for "what to fetch".
In that case we'd reject values containing colons in the "fetch" config
as any mapping should be done by the "fetchRefmap" config. The other way
is to consider that the "fetchRefmap" config holds the default argument
for "--refmap" and "fetch" holds the default command line refspecs to
use when the user doesn't specify any. In that case we should accept
"fetch" values containing colons and behave as if those values had been
given as command line arguments. I'm leaning towards the latter, but if
we do decide to do that, I think we should make sure
git -c remote.cabal.fetchRefmap='refs/heads/*:refs/remotes/cabal/*' \
fetch cabal refs/heads/master:refs/remotes/origin/master
only updates origin/master and not cabal/master.
Thanks
Phillip
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Thu, 29 Oct 2026 22:18
Date: Thu, 29 Oct 2026 22:18
29 lines
14618 bytes
14618 bytes
Phillip Wood <phillip.wood123@gmail.com> writes: >> I think the command line version, i.e. >> >> $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \ >> refs/heads/master:refs/remotes/origin/master \ >> next >> >> stores 'master' as alt/master and origin/master, and 'next' as >> alt/next, i.e., takes the second option. > > That's surprising, I'd naively assumed a refspec on the command line > would override the refmap. It feels problematic if we cannot override > the fetchRefmap config with a refspec on the command line, but need to > use "--refmap" instead. Maybe, maybe not. The command line refmap was meant to primarily help use case where the "what to fetch" was to be specified without colon, so when fetching multiple branches, you do not have to repeat the refs/remotes/origin/ all over again. So if you are giving a refspec with a colon (like 'master' in the above example) to specify where to store, it is an unusual request to do something different. Something different could be "do more than what the refmap alone would do", which led to the behaviour implemented when refmap was introduced. But it could be "refrain from what the refmap alone would do and do something else", and if you take that position, you would get the "overriding" behaviour.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Junio C Hamano
Date: Thu, 29 Oct 2026 22:21
Date: Thu, 29 Oct 2026 22:21
14 lines
13060 bytes
13060 bytes
Ben Knoble <ben.knoble@gmail.com> writes: > Yes, I would love to add pushRefmap atop later! It would > ease my working with notes, I think. > Yes, I would love to add pushRefmap atop later! It would > ease my working with notes, I think. Tell us more when you are off mobile ;-). I do not think I planned to add '--refmap' support on the push side, even from the command line, but I can imagine there would be reasonable workflows that benefit from having one.
Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap
Author: Harald Nordgren
Date: Fri, 30 Oct 2026 10:14
Date: Fri, 30 Oct 2026 10:14
40 lines
13157 bytes
13157 bytes
On Fri, Oct 30, 2026 at 12:21 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Ben Knoble <ben.knoble@gmail.com> writes:
>
> > Yes, I would love to add pushRefmap atop later! It would
> > ease my working with notes, I think.
>
> > Yes, I would love to add pushRefmap atop later! It would
> > ease my working with notes, I think.
>
> Tell us more when you are off mobile ;-).
>
> I do not think I planned to add '--refmap' support on the push side,
> even from the command line, but I can imagine there would be
> reasonable workflows that benefit from having one.
Not sure if it's related to this specific conversation, but I realized
that @{push} is not being resolved here, so I plan on adding a small
fix in a v13:
```
--- a/remote.c
+++ b/remote.c
@@ -2018,6 +2018,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"),
```
Harald
Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)
Author: "D. Ben Knoble"
Date: Sat, 07 Nov 2026 20:20
Date: Sat, 07 Nov 2026 20:20
87 lines
15350 bytes
15350 bytes
+Cc Kristoffer Haugsbakk as a fellow notes user ;)
On Thu, Oct 29, 2026 at 6:21 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Ben Knoble <ben.knoble@gmail.com> writes:
>
> > Yes, I would love to add pushRefmap atop later! It would
> > ease my working with notes, I think.
>
> > Yes, I would love to add pushRefmap atop later! It would
> > ease my working with notes, I think.
>
> Tell us more when you are off mobile ;-).
>
> I do not think I planned to add '--refmap' support on the push side,
> even from the command line, but I can imagine there would be
> reasonable workflows that benefit from having one.
In my local copy of git.git, I want to have both Junio's amlog for
spelunking and my own notes for patches. I only want to push out my
notes + topics to my remote, though (I work on Git from different
machines and it's nice to have the notes sync'd on both). Here's the
hack I ended up with, and it has some downsides.
In particular, "git push" in this configuration does too much, so I
usually have to think about whether that's ok. A pushRefmap would let
me separate what to push by default (push.default!) from how to map
pushing when I want to push, say, notes or other things. I have to be
especially careful with a "git pf" (push, force-with-lease,
force-if-includes), since I usually do *not* want to forcibly
overwrite the remote notes---instead I should pull those down and
merge them†.
So, here's the relevant config (I have a remote "benknoble" and a
remote "origin", among others):
[core]
notesRef = refs/notes/benknoble/commits
[notes]
rewriteRef = refs/notes/benknoble/commits
displayRef = refs/notes/origin/amlog
[remote "origin"]
fetch = +refs/notes/amlog:refs/notes/origin/amlog
The "usual" stuff to make my notes important, but still show amlog
notes when browsing history. (The origin remote section is in addition
to the normal remote section with it's url and heads->origin/
mapping.)
[remote]
pushDefault = benknoble
Push to my remote.
[remote "benknoble"]
fetch = refs/notes/*:refs/notes/benknoble/*
push = refs/heads/*:refs/heads/*
push = refs/notes/benknoble/*:refs/notes/*
push = ^refs/notes/origin/amlog
In addition to the usual config added by "git remote add", fetch
remote notes into a local hierarchy, and push them out, but ignore
amlog, and don't forget to also be able to push heads. Without the
heads map, "git push" will not push branches!
Now, maybe I'm holding something in a way it wasn't intended and
that's made my life harder, I dunno.
†: There isn't a great story for that, either :/ When "git fetch"
fails to fetch notes because of divergence with local, I end up saying
git fetch benknoble refs/notes/commits
git range-diff refs/notes/benknoble/commits...FETCH_HEAD
git notes merge FETCH_HEAD
or something.
It's *almost* like we need remote-tracking branches for notes, ahaha…
at that point, Scott's git-meta work becomes possibly more appealing
to me, even if a part of me wants to "fix" notes so everyone can use
them easily.
--
D. Ben Knoble
Re: Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)
Author: "Kristoffer Haugsbakk"
Date: Fri, 20 Nov 2026 14:24
Date: Fri, 20 Nov 2026 14:24
131 lines
18147 bytes
18147 bytes
On Sat, Nov 7, 2026, at 21:20, D. Ben Knoble wrote:
>[snip]
>> Ben Knoble <ben.knoble@gmail.com> writes:
>>
>> > Yes, I would love to add pushRefmap atop later! It would
>> > ease my working with notes, I think.
>>
>> > Yes, I would love to add pushRefmap atop later! It would
>> > ease my working with notes, I think.
>>
>> Tell us more when you are off mobile ;-).
>>
>> I do not think I planned to add '--refmap' support on the push side,
>> even from the command line, but I can imagine there would be
>> reasonable workflows that benefit from having one.
>
> In my local copy of git.git, I want to have both Junio's amlog for
> spelunking and my own notes for patches. I only want to push out my
> notes + topics to my remote, though (I work on Git from different
> machines and it's nice to have the notes sync'd on both). Here's the
> hack I ended up with, and it has some downsides.
>
> In particular, "git push" in this configuration does too much, so I
> usually have to think about whether that's ok. A pushRefmap would let
> me separate what to push by default (push.default!) from how to map
> pushing when I want to push, say, notes or other things. I have to be
> especially careful with a "git pf" (push, force-with-lease,
> force-if-includes), since I usually do *not* want to forcibly
> overwrite the remote notes---instead I should pull those down and
> merge them†.
>
> So, here's the relevant config (I have a remote "benknoble" and a
> remote "origin", among others):
>
> [core]
> notesRef = refs/notes/benknoble/commits
> [notes]
> rewriteRef = refs/notes/benknoble/commits
> displayRef = refs/notes/origin/amlog
> [remote "origin"]
> fetch = +refs/notes/amlog:refs/notes/origin/amlog
I don’t use multiple computers right now. But I do have both private and
public Git notes. In the work repository I have this
[remote "o"]
[...]
# NOTE “tracking” idea came from the Git mailing list
# NOTE We cannot use a negative refspec to exclude branch things here
# (what I wanted) because neg. refspecs cannot have
# the destination part
fetch = +refs/*:refs/tracking/o/refs/*
This is from some email thread(s) about using a namespace for tracking
all remote refs instead of just branches. Now I have the state of the
remote notes in case there is a divergence.
(Realistically this would only check my own mistakes since no one else
seems to use these Notes.)
There’s even a comment there about negative refspecs. I think something
like that got mentioned here in this thread or some months ago? I didn’t
have the opportunity to engage then.
>
> The "usual" stuff to make my notes important, but still show amlog
> notes when browsing history. (The origin remote section is in addition
> to the normal remote section with it's url and heads->origin/
> mapping.)
>
> [remote]
> pushDefault = benknoble
>
> Push to my remote.
>
> [remote "benknoble"]
> fetch = refs/notes/*:refs/notes/benknoble/*
> push = refs/heads/*:refs/heads/*
> push = refs/notes/benknoble/*:refs/notes/*
> push = ^refs/notes/origin/amlog
>
> In addition to the usual config added by "git remote add", fetch
> remote notes into a local hierarchy, and push them out, but ignore
> amlog, and don't forget to also be able to push heads. Without the
> heads map, "git push" will not push branches!
>
> Now, maybe I'm holding something in a way it wasn't intended and
> that's made my life harder, I dunno.
I haven’t tried the above but it doesn’t look like you have made it
harder for yourself than you need to, considering your needs. ;)
>
> †: There isn't a great story for that, either :/ When "git fetch"
> fails to fetch notes because of divergence with local, I end up saying
>
> git fetch benknoble refs/notes/commits
> git range-diff refs/notes/benknoble/commits...FETCH_HEAD
> git notes merge FETCH_HEAD
>
> or something.
>
> It's *almost* like we need remote-tracking branches for notes, ahaha…
> at that point, Scott's git-meta work becomes possibly more appealing
> to me, even if a part of me wants to "fix" notes so everyone can use
> them easily.
I don’t know what git-meta is. But in any case, I haven’t seen a
forcing-use case for making Notes simpler to use. Right now they are
okay-enough for the simple needs that I have seen, which is
single-author (program or person) or automatic-merge (git-appraise(1)
uses json-lines so that cat-sort-uniq merge strategy works).
But imagine some kind of collaborative Notes usage, like multiple people
posting commentaries on commits. In that case one would need better
tooling, for sure. But collaborative metadata on commits is a bit like
the ideas like “history of history”.[1] It’s probably not going to
happen.
[1] https://lwn.net/Articles/914041/
Re: Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)
Author: "D. Ben Knoble"
Date: Mon, 30 Nov 2026 14:42
Date: Mon, 30 Nov 2026 14:42
114 lines
16618 bytes
16618 bytes
Hi Kristoffer, On Fri, Nov 20, 2026 at 9:25 AM Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com> wrote: > I don’t use multiple computers right now. But I do have both private and > public Git notes. In the work repository I have this > > [remote "o"] > [...] > # NOTE “tracking” idea came from the Git mailing list > # NOTE We cannot use a negative refspec to exclude branch things here > # (what I wanted) because neg. refspecs cannot have > # the destination part > fetch = +refs/*:refs/tracking/o/refs/* > > This is from some email thread(s) about using a namespace for tracking > all remote refs instead of just branches. Now I have the state of the > remote notes in case there is a divergence. Thanks, that's a neat idea. I think where I overcomplicate by trying to simplifying is fetching the remote notes into the same local ref that I write notes to ':D > > The "usual" stuff to make my notes important, but still show amlog > > notes when browsing history. (The origin remote section is in addition > > to the normal remote section with it's url and heads->origin/ > > mapping.) > > > > [remote] > > pushDefault = benknoble > > > > Push to my remote. > > > > [remote "benknoble"] > > fetch = refs/notes/*:refs/notes/benknoble/* > > push = refs/heads/*:refs/heads/* > > push = refs/notes/benknoble/*:refs/notes/* > > push = ^refs/notes/origin/amlog > > > > In addition to the usual config added by "git remote add", fetch > > remote notes into a local hierarchy, and push them out, but ignore > > amlog, and don't forget to also be able to push heads. Without the > > heads map, "git push" will not push branches! > > > > Now, maybe I'm holding something in a way it wasn't intended and > > that's made my life harder, I dunno. > > I haven’t tried the above but it doesn’t look like you have made it > harder for yourself than you need to, considering your needs. ;) What's painful is the extra reconfiguration of the push spec, hence my interest in refmaps :) If we omit the heads map, *bare* "git push" won't push a branch (but being explicit still works). With it, bare "git push" works a bit like push.default=matching, which is *also* not what I want :/ I usually want to push the current branch (push.default=current) + my notes. In most cases, there's no harm done, but I end up having to be more explicit than usual. With a refmap for pushes, I imagine I could say "when pushing, here's how to rewrite" without having to spell out what the default push refspec is manually---I might need some additional support to say "push notes in *addition* to the push.default stuff", but it would get me closer, I think. > > When "git fetch" > > fails to fetch notes because of divergence with local, I end up saying > > > > git fetch benknoble refs/notes/commits > > git range-diff refs/notes/benknoble/commits...FETCH_HEAD > > git notes merge FETCH_HEAD > > > > or something. > > > > It's *almost* like we need remote-tracking branches for notes, ahaha… > > at that point, Scott's git-meta work becomes possibly more appealing > > to me, even if a part of me wants to "fix" notes so everyone can use > > them easily. > > I don’t know what git-meta is. https://git-meta.com/ [LLM warning] > But in any case, I haven’t seen a > forcing-use case for making Notes simpler to use. Right now they are > okay-enough for the simple needs that I have seen, which is > single-author (program or person) or automatic-merge (git-appraise(1) > uses json-lines so that cat-sort-uniq merge strategy works). > > But imagine some kind of collaborative Notes usage, like multiple people > posting commentaries on commits. In that case one would need better > tooling, for sure. But collaborative metadata on commits is a bit like > the ideas like “history of history”.[1] It’s probably not going to > happen. > > [1] https://lwn.net/Articles/914041/ I think git-meta is intended for collaboration to be possible. My case is actually the single-author case, repeated: I'm interested in multiple author's notes but keep them separate. I read both but only write one :) -- D. Ben Knoble
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