[PATCH 2/2] remote: allow a list of remotes in remote.pushDefault
- From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Oct 2, 2026, 07:17 UTC
- Message-ID
- <955709d4ef3aa43a6fb15cf5007daf094b76d427.1790925472.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2423.git.git.1790925472.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>
Someone who pushes to a personal fork in some repositories and to origin in others cannot set remote.pushDefault globally. Repositories without the named remote fail to push, because git takes the missing name for a URL.
Let remote.pushDefault hold a space separated list of remote names and push to the first one that is configured in the repository. When none of them is configured, fall back to branch.<name>.remote as if the variable was not set. A single name keeps working as before, and a value set in the repository still overrides one set globally.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- Documentation/config/remote.adoc | 7 +++++++ remote.c | 29 ++++++++++++++++++++++--- t/t5516-fetch-push.sh | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc index 3a20d0f752..6a67c97fe8 100644 --- a/Documentation/config/remote.adoc +++ b/Documentation/config/remote.adoc @@ -2,6 +2,13 @@ remote.pushDefault:: The remote to push to by default. Overrides `branch.<name>.remote` for all branches, and is overridden by `branch.<name>.pushRemote` for specific branches. ++ +The value may be a space separated list of remote names, in which case +the first one that is configured in the repository is used. If none of +them is configured, `branch.<name>.remote` is used as if +`remote.pushDefault` was not set. This allows setting a list such as +`fork origin` globally and have repositories without a `fork` remote +push to `origin`. remote.<name>.url:: The URL of a remote repository. See linkgit:git-fetch[1] or diff --git a/remote.c b/remote.c index 6567ec91cc..3cad0673b9 100644 --- a/remote.c +++ b/remote.c @@ -700,6 +700,25 @@ const char *remote_for_branch(struct branch *branch, int *explicit) explicit); } +static const char *pushdefault_candidate(struct remote_state *remote_state) +{ + const char *p = remote_state->pushremote_name; + + if (!strchr(p, ' ')) + return p; + + while (*p) { + size_t len = strcspn(p, " "); + struct remote *remote = find_remote(remote_state, p, len); + + if (remote && valid_remote(remote)) + return remote->name; + p += len; + p += strspn(p, " "); + } + return NULL; +} + static const char * remotes_pushremote_for_branch(struct remote_state *remote_state, struct branch *branch, int *explicit) @@ -710,9 +729,13 @@ remotes_pushremote_for_branch(struct remote_state *remote_state, return branch->pushremote_name; } if (remote_state->pushremote_name) { - if (explicit) - *explicit = 1; - return remote_state->pushremote_name; + const char *name = pushdefault_candidate(remote_state); + + if (name) { + if (explicit) + *explicit = 1; + return name; + } } return remotes_remote_for_branch(remote_state, branch, explicit); } diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index b982b209bf..7a0cdafd65 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -591,6 +591,42 @@ test_expect_success 'push with remote.pushdefault' ' check_push_result down_repo $the_commit heads/main ' +test_expect_success 'push with remote.pushdefault list picks first existing remote' ' + mk_test up_repo heads/main && + mk_test down_repo heads/main && + test_config remote.up.url up_repo && + test_config remote.down.url down_repo && + test_config branch.main.remote up && + test_config remote.pushdefault "missing down up" && + test_config push.default matching && + git push && + check_push_result up_repo $the_first_commit heads/main && + check_push_result down_repo $the_commit heads/main +' + +test_expect_success 'push with remote.pushdefault list of missing remotes' ' + mk_test up_repo heads/main && + test_config remote.up.url up_repo && + test_config branch.main.remote up && + test_config remote.pushdefault "missing also-missing" && + test_config push.default matching && + git push && + check_push_result up_repo $the_commit heads/main +' + +test_expect_success 'repository remote.pushdefault overrides global list' ' + mk_test up_repo heads/main && + mk_test down_repo heads/main && + test_config remote.up.url up_repo && + test_config remote.down.url down_repo && + test_config_global remote.pushdefault "down up" && + test_config remote.pushdefault up && + test_config push.default matching && + git push && + check_push_result up_repo $the_commit heads/main && + check_push_result down_repo $the_first_commit heads/main +' + test_expect_success 'push with config remote.*.pushurl' ' mk_test testrepo heads/main && git checkout main &&
-- gitgitgadget