* [PATCH] checkout: add --fetch to fetch remote before resolving start-point
@ 2026-04-24 10:03 Harald Nordgren via GitGitGadget
2026-04-24 13:48 ` Ramsay Jones
` (5 more replies)
0 siblings, 6 replies; 35+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-04-24 10:03 UTC (permalink / raw)
To: git; +Cc: Harald Nordgren, Harald Nordgren
From: Harald Nordgren <haraldnordgren@gmail.com>
Add a --fetch option to git checkout and git switch, plus a
checkout.autoFetch config to enable it by default. When set and the
start-point argument names a configured remote (either bare, like
"origin", or prefixed, like "origin/foo"), fetch that remote before
resolving the ref. Aborts the checkout if the fetch fails.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
checkout: add --fetch to fetch remote before resolving start-point
A workflow I run several times a day looks like:
git fetch origin
git checkout -b new_branch origin/some-branch
The first command exists purely to make the second one see an up-to-date
view of the remote. If I forget it, origin/some-branch points at a stale
commit, and I end up creating a local branch from the wrong starting
point.
This series teaches git checkout (and git switch) a new --fetch flag
that folds the two steps into one:
git checkout --fetch -b new_branch origin/some-branch
When the start-point argument names a configured remote — either bare
(origin, which resolves to the remote's default branch) or in / form —
git fetch is run before the start-point is resolved. If the fetch fails,
the checkout aborts and no local branch is created.
A new checkout.autoFetch config option enables the same behavior by
default, for users who always want it.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1
Pull-Request: https://github.com/git/git/pull/2281
builtin/checkout.c | 48 ++++++++++++++++++++++++++++++++++++++--
t/t7201-co.sh | 51 +++++++++++++++++++++++++++++++++++++++++++
t/t9902-completion.sh | 1 +
3 files changed, 98 insertions(+), 2 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index e031e61886..c8fbc4923b 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -30,7 +30,9 @@
#include "repo-settings.h"
#include "resolve-undo.h"
#include "revision.h"
+#include "run-command.h"
#include "setup.h"
+#include "strvec.h"
#include "submodule.h"
#include "symlinks.h"
#include "trace2.h"
@@ -61,6 +63,7 @@ struct checkout_opts {
int count_checkout_paths;
int overlay_mode;
int dwim_new_local_branch;
+ int auto_fetch;
int discard_changes;
int accept_ref;
int accept_pathspec;
@@ -112,6 +115,34 @@ struct branch_info {
char *checkout;
};
+static void fetch_remote_for_start_point(const char *arg)
+{
+ const char *slash;
+ char *remote_name;
+ struct remote *remote;
+ struct child_process cmd = CHILD_PROCESS_INIT;
+
+ if (!arg || !*arg)
+ return;
+
+ slash = strchr(arg, '/');
+ if (slash == arg)
+ return;
+ remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);
+
+ remote = remote_get(remote_name);
+ if (!remote || !remote_is_configured(remote, 1)) {
+ free(remote_name);
+ return;
+ }
+
+ strvec_pushl(&cmd.args, "fetch", remote_name, NULL);
+ cmd.git_cmd = 1;
+ free(remote_name);
+ if (run_command(&cmd))
+ die(_("failed to fetch start-point '%s'"), arg);
+}
+
static void branch_info_release(struct branch_info *info)
{
free(info->name);
@@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value,
opts->dwim_new_local_branch = git_config_bool(var, value);
return 0;
}
+ if (!strcmp(var, "checkout.autofetch")) {
+ opts->auto_fetch = git_config_bool(var, value);
+ return 0;
+ }
if (starts_with(var, "submodule."))
return git_default_submodule_config(var, value, NULL);
@@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
opts->dwim_new_local_branch &&
opts->track == BRANCH_TRACK_UNSPECIFIED &&
!opts->new_branch;
- int n = parse_branchname_arg(argc, argv, dwim_ok, which_command,
- &new_branch_info, opts, &rev);
+ int n;
+
+ if (opts->auto_fetch)
+ fetch_remote_for_start_point(argv[0]);
+
+ n = parse_branchname_arg(argc, argv, dwim_ok, which_command,
+ &new_branch_info, opts, &rev);
argv += n;
argc -= n;
} else if (!opts->accept_ref && opts->from_treeish) {
@@ -2052,6 +2092,8 @@ int cmd_checkout(int argc,
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
OPT_BOOL(0, "auto-advance", &opts.auto_advance,
N_("auto advance to the next file when selecting hunks interactively")),
+ OPT_BOOL(0, "fetch", &opts.auto_fetch,
+ N_("fetch from the remote first if <start-point> is a remote-tracking ref")),
OPT_END()
};
@@ -2102,6 +2144,8 @@ int cmd_switch(int argc,
N_("second guess 'git switch <no-such-branch>'")),
OPT_BOOL(0, "discard-changes", &opts.discard_changes,
N_("throw away local modifications")),
+ OPT_BOOL(0, "fetch", &opts.auto_fetch,
+ N_("fetch from the remote first if <start-point> is a remote-tracking ref")),
OPT_END()
};
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 9bcf7c0b40..60ddebd9c3 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
test_cmp_config "" --default "" branch.main2.merge
'
+test_expect_success 'setup upstream for --fetch tests' '
+ git checkout main &&
+ git init fetch_upstream &&
+ test_commit -C fetch_upstream u_main &&
+ git remote add fetch_upstream fetch_upstream &&
+ git fetch fetch_upstream &&
+ git -C fetch_upstream checkout -b fetch_new &&
+ test_commit -C fetch_upstream u_new
+'
+
+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' '
+ git checkout main &&
+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new &&
+ git checkout --fetch -b local_new fetch_upstream/fetch_new &&
+ test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD
+'
+
+test_expect_success 'checkout --fetch with bare remote name fetches the remote' '
+ git checkout main &&
+ git -C fetch_upstream checkout -b fetch_new2 &&
+ test_commit -C fetch_upstream u_new2 &&
+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 &&
+ git checkout --fetch -b local_from_remote fetch_upstream &&
+ git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2
+'
+
+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' '
+ git checkout main &&
+ test_might_fail git branch -D bogus &&
+ test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist &&
+ test_must_fail git rev-parse --verify refs/heads/bogus
+'
+
+test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' '
+ git checkout main &&
+ git -C fetch_upstream checkout -b fetch_cfg &&
+ test_commit -C fetch_upstream u_cfg &&
+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg &&
+ git -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg &&
+ test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD
+'
+
+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' '
+ git checkout main &&
+ git -C fetch_upstream checkout -b fetch_switch &&
+ test_commit -C fetch_upstream u_switch &&
+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch &&
+ git switch --fetch -c local_switch fetch_upstream/fetch_switch &&
+ test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD
+'
+
test_done
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 2f9a597ec7..dc1d63669f 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2602,6 +2602,7 @@ test_expect_success 'double dash "git checkout"' '
--ignore-other-worktrees Z
--recurse-submodules Z
--auto-advance Z
+ --fetch Z
--progress Z
--guess Z
--no-guess Z
base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0
--
gitgitgadget
^ permalink raw reply related [flat|nested] 35+ messages in thread* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget @ 2026-04-24 13:48 ` Ramsay Jones 2026-04-24 17:12 ` D. Ben Knoble ` (4 subsequent siblings) 5 siblings, 0 replies; 35+ messages in thread From: Ramsay Jones @ 2026-04-24 13:48 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget, git; +Cc: Harald Nordgren On 24/04/2026 11:03 am, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --fetch option to git checkout and git switch, plus a > checkout.autoFetch config to enable it by default. When set and the > start-point argument names a configured remote (either bare, like > "origin", or prefixed, like "origin/foo"), fetch that remote before > resolving the ref. Aborts the checkout if the fetch fails. > > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- [snip] > > diff --git a/t/t7201-co.sh b/t/t7201-co.sh > index 9bcf7c0b40..60ddebd9c3 100755 > --- a/t/t7201-co.sh > +++ b/t/t7201-co.sh > @@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' > test_cmp_config "" --default "" branch.main2.merge > ' > > +test_expect_success 'setup upstream for --fetch tests' ' > + git checkout main && > + git init fetch_upstream && > + test_commit -C fetch_upstream u_main && > + git remote add fetch_upstream fetch_upstream && > + git fetch fetch_upstream && > + git -C fetch_upstream checkout -b fetch_new && > + test_commit -C fetch_upstream u_new > +' > + > +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' > + git checkout main && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && > + git checkout --fetch -b local_new fetch_upstream/fetch_new && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD > +' > + > +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_new2 && > + test_commit -C fetch_upstream u_new2 && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && > + git checkout --fetch -b local_from_remote fetch_upstream && > + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 > +' > + > +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' > + git checkout main && > + test_might_fail git branch -D bogus && > + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && > + test_must_fail git rev-parse --verify refs/heads/bogus > +' > + > +test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_cfg && > + test_commit -C fetch_upstream u_cfg && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && > + git -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD > +' > + > +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_switch && > + test_commit -C fetch_upstream u_switch && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && > + git switch --fetch -c local_switch fetch_upstream/fetch_switch && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD > +' > + I was just skimming the list (so if this is not appropriate, please just ignore) and, although I think '--no-fetch' will probably countermand the autoFetch config, I do not see a test that confirms it. Thanks. ATB, Ramsay Jones ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 2026-04-24 13:48 ` Ramsay Jones @ 2026-04-24 17:12 ` D. Ben Knoble 2026-04-25 17:24 ` Comments on Phillip's review Harald Nordgren 2026-04-24 17:38 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Kristoffer Haugsbakk ` (3 subsequent siblings) 5 siblings, 1 reply; 35+ messages in thread From: D. Ben Knoble @ 2026-04-24 17:12 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren On Fri, Apr 24, 2026 at 6:08 AM Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com> wrote: > > From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --fetch option to git checkout and git switch, plus a > checkout.autoFetch config to enable it by default. When set and the > start-point argument names a configured remote (either bare, like > "origin", or prefixed, like "origin/foo"), fetch that remote before > resolving the ref. Aborts the checkout if the fetch fails. > > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- > checkout: add --fetch to fetch remote before resolving start-point > > A workflow I run several times a day looks like: > > git fetch origin > git checkout -b new_branch origin/some-branch > > > The first command exists purely to make the second one see an up-to-date > view of the remote. If I forget it, origin/some-branch points at a stale > commit, and I end up creating a local branch from the wrong starting > point. When you realize this, "git pull --rebase" should help correct it. > > This series teaches git checkout (and git switch) a new --fetch flag > that folds the two steps into one: > > git checkout --fetch -b new_branch origin/some-branch > > > When the start-point argument names a configured remote — either bare > (origin, which resolves to the remote's default branch) or in / form — > git fetch is run before the start-point is resolved. If the fetch fails, > the checkout aborts and no local branch is created. > > A new checkout.autoFetch config option enables the same behavior by > default, for users who always want it. I could certainly see this being convenient. (I don't have any comment on the code at this time.) ^ permalink raw reply [flat|nested] 35+ messages in thread
* Comments on Phillip's review 2026-04-24 17:12 ` D. Ben Knoble @ 2026-04-25 17:24 ` Harald Nordgren 2026-04-25 17:44 ` Wrong subject line Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:24 UTC (permalink / raw) To: ben.knoble; +Cc: git, gitgitgadget, haraldnordgren > When you realize this, "git pull --rebase" should help correct it. Sure. I always run with ``` git config --global pull.rebase=true ``` I love rebasing and recomdend it to all colleagues that will listen, but it still sucks to expose yourself to a possible merge conflict when you realize you worked hours on top of a stale main branch. > I could certainly see this being convenient. 🙌🏻 Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Wrong subject line 2026-04-25 17:24 ` Comments on Phillip's review Harald Nordgren @ 2026-04-25 17:44 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:44 UTC (permalink / raw) To: haraldnordgren; +Cc: ben.knoble, git, gitgitgadget I know you are not Ben. Forgot to change the subject line from a previous message. Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 2026-04-24 13:48 ` Ramsay Jones 2026-04-24 17:12 ` D. Ben Knoble @ 2026-04-24 17:38 ` Kristoffer Haugsbakk 2026-04-25 17:41 ` Comments on Phillip's review Harald Nordgren 2026-04-24 17:42 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Marc Branchaud ` (2 subsequent siblings) 5 siblings, 1 reply; 35+ messages in thread From: Kristoffer Haugsbakk @ 2026-04-24 17:38 UTC (permalink / raw) To: git, gitgitgadget; +Cc: Harald Nordgren On Fri, Apr 24, 2026, at 12:03, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --fetch option to git checkout and git switch, plus a > checkout.autoFetch config to enable it by default. When set and the Why is the config not `checkout.config`? So it’s named the same as the option (modulo snake case/camel case which is not relevant here). > start-point argument names a configured remote (either bare, like > "origin", or prefixed, like "origin/foo"), It’s great that it only fetches when you have a remote-tracking branch or alias for `<remote>/HEAD`. Doing a fetch on every <start-point> would have been bad. > fetch that remote before > resolving the ref. Aborts the checkout if the fetch fails. > > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- > checkout: add --fetch to fetch remote before resolving start-point > > A workflow I run several times a day looks like: > > git fetch origin > git checkout -b new_branch origin/some-branch > > > The first command exists purely to make the second one see an up-to-date > view of the remote. If I forget it, origin/some-branch points at a stale > commit, and I end up creating a local branch from the wrong starting > point. > > This series teaches git checkout (and git switch) a new --fetch flag > that folds the two steps into one: > > git checkout --fetch -b new_branch origin/some-branch The motivation for why this is being proposed maybe might as well go in the commit message. Maybe that’s just me. The commit message just says that “this thing is added”. Not why. > > > When the start-point argument names a configured remote — either bare > (origin, which resolves to the remote's default branch) or in / form — > git fetch is run before the start-point is resolved. If the fetch fails, > the checkout aborts and no local branch is created. > > A new checkout.autoFetch config option enables the same behavior by > default, for users who always want it. > > Published-As: > https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git > pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1 > Pull-Request: https://github.com/git/git/pull/2281 > > builtin/checkout.c | 48 ++++++++++++++++++++++++++++++++++++++-- > t/t7201-co.sh | 51 +++++++++++++++++++++++++++++++++++++++++++ > t/t9902-completion.sh | 1 + > 3 files changed, 98 insertions(+), 2 deletions(-) I guess a later version will have the changes to the documentation. > > diff --git a/builtin/checkout.c b/builtin/checkout.c >[snip] > argv += n; > argc -= n; > } else if (!opts->accept_ref && opts->from_treeish) { > @@ -2052,6 +2092,8 @@ int cmd_checkout(int argc, > OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode > (default)")), > OPT_BOOL(0, "auto-advance", &opts.auto_advance, > N_("auto advance to the next file when selecting hunks > interactively")), > + OPT_BOOL(0, "fetch", &opts.auto_fetch, > + N_("fetch from the remote first if <start-point> is a remote-tracking ref")), s/remote-tracking ref/remote-tracking branch/ ? git(1) doesn’t have a namespace for tracking refs in general. > OPT_END() > }; > > @@ -2102,6 +2144,8 @@ int cmd_switch(int argc, > N_("second guess 'git switch <no-such-branch>'")), > OPT_BOOL(0, "discard-changes", &opts.discard_changes, > N_("throw away local modifications")), > + OPT_BOOL(0, "fetch", &opts.auto_fetch, > + N_("fetch from the remote first if <start-point> is a remote-tracking ref")), Ditto. > OPT_END() > }; >[snip] ^ permalink raw reply [flat|nested] 35+ messages in thread
* Comments on Phillip's review 2026-04-24 17:38 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Kristoffer Haugsbakk @ 2026-04-25 17:41 ` Harald Nordgren 2026-04-25 17:44 ` Wrong subject line Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:41 UTC (permalink / raw) To: kristofferhaugsbakk; +Cc: git, gitgitgadget, haraldnordgren > > Add a --fetch option to git checkout and git switch, plus a > > checkout.autoFetch config to enable it by default. When set and the > > Why is the config not `checkout.config`? So it’s named the same as the > option (modulo snake case/camel case which is not relevant here). Will rename the config to 'checkout.fetch'. > The motivation for why this is being proposed maybe might as well go in > the commit message. Maybe that’s just me. > > The commit message just says that “this thing is added”. Not why. I will update it. > I guess a later version will have the changes to the documentation. I forgot that, will add it! > s/remote-tracking ref/remote-tracking branch/ ? > > git(1) doesn’t have a namespace for tracking refs in general. 👍 Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Wrong subject line 2026-04-25 17:41 ` Comments on Phillip's review Harald Nordgren @ 2026-04-25 17:44 ` Harald Nordgren 2026-04-26 7:07 ` Kristoffer Haugsbakk 0 siblings, 1 reply; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:44 UTC (permalink / raw) To: haraldnordgren; +Cc: git, gitgitgadget, kristofferhaugsbakk I know you are not Ben. Forgot to change the subject line from a previous message. Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: Wrong subject line 2026-04-25 17:44 ` Wrong subject line Harald Nordgren @ 2026-04-26 7:07 ` Kristoffer Haugsbakk 2026-04-26 15:15 ` [PATCH] remote: add --set-head option to 'git remote add' Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Kristoffer Haugsbakk @ 2026-04-26 7:07 UTC (permalink / raw) To: Harald Nordgren; +Cc: git, gitgitgadget On Sat, Apr 25, 2026, at 19:44, Harald Nordgren wrote: > I know you are not Ben. Forgot to change the subject line from a > previous message. You mean I’m not Phillip. ;) Subject: Comments on Phillip's review I don’t understand why you change the email subjects so often. Right now I had three “Wrong subject line” in my inbox with lost threading (webmail client) with the only way to distinguish them being that I was the the CC on this one. Most of the time whole 100-email threads like patch series never change the subject. And to me it is easier to keep track of those “RE: [PATCH v5] florb: drop glorb” than if someone changes the subject to e.g. “Regarding memory leaks” because someone found a memory leak in a review. Because that was a reply to an email from two days ago, but I’ve been a away for a week so I think it’s a new thread about something else. That’s just my experience. My amateur webmail setup doesn’t really matter here since I just dip in/interrupt threads when I feel like it. ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] remote: add --set-head option to 'git remote add' 2026-04-26 7:07 ` Kristoffer Haugsbakk @ 2026-04-26 15:15 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-04-26 15:15 UTC (permalink / raw) To: kristofferhaugsbakk; +Cc: git, gitgitgadget, haraldnordgren > I don’t understand why you change the email subjects so often. Right now > I had three “Wrong subject line” in my inbox with lost threading > (webmail client) with the only way to distinguish them being that I was > the the CC on this one. > > Most of the time whole 100-email threads like patch series never change > the subject. And to me it is easier to keep track of those “RE: [PATCH > v5] florb: drop glorb” than if someone changes the subject to e.g. > “Regarding memory leaks” because someone found a memory leak in a > review. Because that was a reply to an email from two days ago, but I’ve > been a away for a week so I think it’s a new thread about > something else. I got some feedback before, not from you, that subject lines should be more varied, probably there is a golden middle that I need to find. And I fundamentally agree with you. Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget ` (2 preceding siblings ...) 2026-04-24 17:38 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Kristoffer Haugsbakk @ 2026-04-24 17:42 ` Marc Branchaud 2026-04-25 17:48 ` Wrong subject line Harald Nordgren 2026-04-24 22:21 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Junio C Hamano 2026-04-25 18:12 ` [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 5 siblings, 1 reply; 35+ messages in thread From: Marc Branchaud @ 2026-04-24 17:42 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget, git; +Cc: Harald Nordgren On 2026-04-24 04:03, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --fetch option to git checkout and git switch, plus a > checkout.autoFetch config to enable it by default. When set and the > start-point argument names a configured remote (either bare, like > "origin", or prefixed, like "origin/foo"), fetch that remote before > resolving the ref. Aborts the checkout if the fetch fails. Why tie the behaviour to the nature of the start-point? That seems over-designed and prone to tripping people up. Are you trying to cater to users who have multiple remotes? I can imagine people who just want to do a checkout of anything after fetching -- maybe they want to checkout a new tag, or some other detached HEAD, or just an already-existing local branch. They see that checkout has this nifty --fetch option so they think they can combine git fetch; git checkout into a single command ... but no, only if they checkout something in a remote's namespace. I don't personally feel the need for this new option, but I think you'll have a much easier time implementing and maintaining it if you just make --fetch do a plain fetch without caring about what the starting-point is. M. > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- > checkout: add --fetch to fetch remote before resolving start-point > > A workflow I run several times a day looks like: > > git fetch origin > git checkout -b new_branch origin/some-branch > > > The first command exists purely to make the second one see an up-to-date > view of the remote. If I forget it, origin/some-branch points at a stale > commit, and I end up creating a local branch from the wrong starting > point. > > This series teaches git checkout (and git switch) a new --fetch flag > that folds the two steps into one: > > git checkout --fetch -b new_branch origin/some-branch > > > When the start-point argument names a configured remote — either bare > (origin, which resolves to the remote's default branch) or in / form — > git fetch is run before the start-point is resolved. If the fetch fails, > the checkout aborts and no local branch is created. > > A new checkout.autoFetch config option enables the same behavior by > default, for users who always want it. > > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v1 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v1 > Pull-Request: https://github.com/git/git/pull/2281 > > builtin/checkout.c | 48 ++++++++++++++++++++++++++++++++++++++-- > t/t7201-co.sh | 51 +++++++++++++++++++++++++++++++++++++++++++ > t/t9902-completion.sh | 1 + > 3 files changed, 98 insertions(+), 2 deletions(-) > > diff --git a/builtin/checkout.c b/builtin/checkout.c > index e031e61886..c8fbc4923b 100644 > --- a/builtin/checkout.c > +++ b/builtin/checkout.c > @@ -30,7 +30,9 @@ > #include "repo-settings.h" > #include "resolve-undo.h" > #include "revision.h" > +#include "run-command.h" > #include "setup.h" > +#include "strvec.h" > #include "submodule.h" > #include "symlinks.h" > #include "trace2.h" > @@ -61,6 +63,7 @@ struct checkout_opts { > int count_checkout_paths; > int overlay_mode; > int dwim_new_local_branch; > + int auto_fetch; > int discard_changes; > int accept_ref; > int accept_pathspec; > @@ -112,6 +115,34 @@ struct branch_info { > char *checkout; > }; > > +static void fetch_remote_for_start_point(const char *arg) > +{ > + const char *slash; > + char *remote_name; > + struct remote *remote; > + struct child_process cmd = CHILD_PROCESS_INIT; > + > + if (!arg || !*arg) > + return; > + > + slash = strchr(arg, '/'); > + if (slash == arg) > + return; > + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); > + > + remote = remote_get(remote_name); > + if (!remote || !remote_is_configured(remote, 1)) { > + free(remote_name); > + return; > + } > + > + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); > + cmd.git_cmd = 1; > + free(remote_name); > + if (run_command(&cmd)) > + die(_("failed to fetch start-point '%s'"), arg); > +} > + > static void branch_info_release(struct branch_info *info) > { > free(info->name); > @@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value, > opts->dwim_new_local_branch = git_config_bool(var, value); > return 0; > } > + if (!strcmp(var, "checkout.autofetch")) { > + opts->auto_fetch = git_config_bool(var, value); > + return 0; > + } > > if (starts_with(var, "submodule.")) > return git_default_submodule_config(var, value, NULL); > @@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, > opts->dwim_new_local_branch && > opts->track == BRANCH_TRACK_UNSPECIFIED && > !opts->new_branch; > - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, > - &new_branch_info, opts, &rev); > + int n; > + > + if (opts->auto_fetch) > + fetch_remote_for_start_point(argv[0]); > + > + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, > + &new_branch_info, opts, &rev); > argv += n; > argc -= n; > } else if (!opts->accept_ref && opts->from_treeish) { > @@ -2052,6 +2092,8 @@ int cmd_checkout(int argc, > OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), > OPT_BOOL(0, "auto-advance", &opts.auto_advance, > N_("auto advance to the next file when selecting hunks interactively")), > + OPT_BOOL(0, "fetch", &opts.auto_fetch, > + N_("fetch from the remote first if <start-point> is a remote-tracking ref")), > OPT_END() > }; > > @@ -2102,6 +2144,8 @@ int cmd_switch(int argc, > N_("second guess 'git switch <no-such-branch>'")), > OPT_BOOL(0, "discard-changes", &opts.discard_changes, > N_("throw away local modifications")), > + OPT_BOOL(0, "fetch", &opts.auto_fetch, > + N_("fetch from the remote first if <start-point> is a remote-tracking ref")), > OPT_END() > }; > > diff --git a/t/t7201-co.sh b/t/t7201-co.sh > index 9bcf7c0b40..60ddebd9c3 100755 > --- a/t/t7201-co.sh > +++ b/t/t7201-co.sh > @@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' > test_cmp_config "" --default "" branch.main2.merge > ' > > +test_expect_success 'setup upstream for --fetch tests' ' > + git checkout main && > + git init fetch_upstream && > + test_commit -C fetch_upstream u_main && > + git remote add fetch_upstream fetch_upstream && > + git fetch fetch_upstream && > + git -C fetch_upstream checkout -b fetch_new && > + test_commit -C fetch_upstream u_new > +' > + > +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' > + git checkout main && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && > + git checkout --fetch -b local_new fetch_upstream/fetch_new && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD > +' > + > +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_new2 && > + test_commit -C fetch_upstream u_new2 && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && > + git checkout --fetch -b local_from_remote fetch_upstream && > + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 > +' > + > +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' > + git checkout main && > + test_might_fail git branch -D bogus && > + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && > + test_must_fail git rev-parse --verify refs/heads/bogus > +' > + > +test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_cfg && > + test_commit -C fetch_upstream u_cfg && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && > + git -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD > +' > + > +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_switch && > + test_commit -C fetch_upstream u_switch && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && > + git switch --fetch -c local_switch fetch_upstream/fetch_switch && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD > +' > + > test_done > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh > index 2f9a597ec7..dc1d63669f 100755 > --- a/t/t9902-completion.sh > +++ b/t/t9902-completion.sh > @@ -2602,6 +2602,7 @@ test_expect_success 'double dash "git checkout"' ' > --ignore-other-worktrees Z > --recurse-submodules Z > --auto-advance Z > + --fetch Z > --progress Z > --guess Z > --no-guess Z > > base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 ^ permalink raw reply [flat|nested] 35+ messages in thread
* Wrong subject line 2026-04-24 17:42 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Marc Branchaud @ 2026-04-25 17:48 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:48 UTC (permalink / raw) To: marcnarc; +Cc: git, gitgitgadget, haraldnordgren I know you are not Ben. Forgot to change the subject line from a previous message. Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget ` (3 preceding siblings ...) 2026-04-24 17:42 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Marc Branchaud @ 2026-04-24 22:21 ` Junio C Hamano 2026-04-25 2:54 ` Junio C Hamano 2026-04-25 18:12 ` [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 5 siblings, 1 reply; 35+ messages in thread From: Junio C Hamano @ 2026-04-24 22:21 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --fetch option to git checkout and git switch, plus a > checkout.autoFetch config to enable it by default. When set and the > start-point argument names a configured remote (either bare, like > "origin", or prefixed, like "origin/foo"), fetch that remote before > resolving the ref. Aborts the checkout if the fetch fails. > > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- It is true that "checkout" does funny things to special case the remote-tracking branches, like setting up the branch.<name>.merge configuration or even inferring the name of the local branch to be created. But I have to say that this one, especially the configuration variable, goes way too far. The usual uses of remote-tracking branch names, e.g., git log -1 origin/master git grep frotz origin/master git rev-list --count origin/maint..origin/master to name a specific object all assume and rely on the stability of them. Should the configuration cause a fetch to happen before any of these uses of remote-tracking branches for consistency? ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 22:21 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Junio C Hamano @ 2026-04-25 2:54 ` Junio C Hamano 2026-04-25 17:58 ` Multiple remotes Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Junio C Hamano @ 2026-04-25 2:54 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren Junio C Hamano <gitster@pobox.com> writes: > "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > >> From: Harald Nordgren <haraldnordgren@gmail.com> >> >> Add a --fetch option to git checkout and git switch, plus a >> checkout.autoFetch config to enable it by default. When set and the >> start-point argument names a configured remote (either bare, like >> "origin", or prefixed, like "origin/foo"), fetch that remote before >> resolving the ref. Aborts the checkout if the fetch fails. >> >> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> >> --- > > It is true that "checkout" does funny things to special case the > remote-tracking branches, like setting up the branch.<name>.merge > configuration or even inferring the name of the local branch to be > created. > ... > ... Should the configuration cause a fetch to happen before any > of these uses of remote-tracking branches for consistency? The last one was a rhetorical question. I do not want to see such a configuration variable to implicitly trigger fetching at all. I am somewhat sympathetic to the desire "I want to be sure that I start the new branch in a state as fresh as possible". It is tied to the "--track" option of "git checkout -b topic --track origin/main". If you are merely starting at a single arbitrary commit, instead of anticipating to having to repeatedly sync with the remote-tracking branch that will subsequently move, there is no point jumping to a "freshest" commit that you haven't even seen let alone inspected (i.e., you do not even know if it is a good base to build on). So instead of introducing a totally new option that can only be used only when "--track" is given, it might make more sense to introduce this as a variant of "--track", perhaps "--track=fetch,[in]direct" or something like that. And extend branch.autosetup{Merge,Rebase} that controls what happens when a branch is created with "checkout -t -b" or "branch --track" so that the remote-tracking branch gets updated, perhaps. As to "git checkout origin/main" (nothing else on the command line), it has "magic" compared to "git checkout origin/main~0" already by treating the parameter not just as a SHA-1 expression that names a commit object but as a remote-tracking branch (this is necessary for "-t"). So I am not fundamentally opposed to the idea to give an option to treat that form specifically. Having said all that, quite honestly, I prefer not to see any of the above changes, including the original patch. It leaves too many usability questions unaddressed. For a starter, if you interact with a repository with two or more branches, should $ git checkout --track=fetch -b topic origin/main update an unrelated remote-tracking branch origin/maint from the same remote? As I already said, most Git tools _depend_ on the stability of remote-tracking branches---the desire to update the origin/main when a new branch that builds on origin/main is created may be a valid one, but it is unclear if that warrants updating other remote-tracking branches only because they come from the same remote repository. There may be a dozen other UI/usability issues that will be introduced if we start to "fetch from remote" automatically, but I won't even try to be exhaustive while I am still on a leave ;-) ^ permalink raw reply [flat|nested] 35+ messages in thread
* Multiple remotes 2026-04-25 2:54 ` Junio C Hamano @ 2026-04-25 17:58 ` Harald Nordgren 2026-04-25 21:57 ` Ben Knoble 0 siblings, 1 reply; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 17:58 UTC (permalink / raw) To: gitster; +Cc: git, gitgitgadget, haraldnordgren > The last one was a rhetorical question. I do not want to see such a > configuration variable to implicitly trigger fetching at all. 🤣 Good to clarify that when working with me so that I don't go ahead and implement that! > If you are merely starting at a single arbitrary > commit, instead of anticipating to having to repeatedly sync with > the remote-tracking branch that will subsequently move, there is no > point jumping to a "freshest" commit that you haven't even seen let > alone inspected (i.e., you do not even know if it is a good base to > build on). Not sure I understand this sentiment. For better or worse, the latest commit will decide what you have to work with -- unless we expect it to be reverted or forced pushed over. What better starting point is there? > For a starter, if you interact > with a repository with two or more branches, should > > $ git checkout --track=fetch -b topic origin/main > > update an unrelated remote-tracking branch origin/maint from the > same remote? As I already said, most Git tools _depend_ on the > stability of remote-tracking branches This is an interesting question, and it's very likely that I am missing some nuance here. However, with that said what option does the developer have, you have to accept that the upstream changes constantly when others are working on it. What good does it do to keep the "head in the sand" any longer than necessary? I'm not sure there is a way to fetch only 'origin/main' and avoid 'origin/maint'? Maybe, maybe, if that exists it could be useful here. > still on a leave Enjoy your vacation! I don't expect any response from you until you're back! Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: Multiple remotes 2026-04-25 17:58 ` Multiple remotes Harald Nordgren @ 2026-04-25 21:57 ` Ben Knoble 2026-04-25 22:54 ` gh Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Ben Knoble @ 2026-04-25 21:57 UTC (permalink / raw) To: Harald Nordgren; +Cc: gitster, git, gitgitgadget, haraldnordgren > Le 25 avr. 2026 à 13:58, Harald Nordgren <haraldnordgren@gmail.com> a écrit : > > >> >> The last one was a rhetorical question. I do not want to see such a >> configuration variable to implicitly trigger fetching at all. > > 🤣 > > Good to clarify that when working with me so that I don't go ahead and > implement that! > >> If you are merely starting at a single arbitrary >> commit, instead of anticipating to having to repeatedly sync with >> the remote-tracking branch that will subsequently move, there is no >> point jumping to a "freshest" commit that you haven't even seen let >> alone inspected (i.e., you do not even know if it is a good base to >> build on). > > Not sure I understand this sentiment. For better or worse, the latest > commit will decide what you have to work with -- unless we expect it to be > reverted or forced pushed over. > > What better starting point is there? > >> For a starter, if you interact >> with a repository with two or more branches, should >> >> $ git checkout --track=fetch -b topic origin/main >> >> update an unrelated remote-tracking branch origin/maint from the >> same remote? As I already said, most Git tools _depend_ on the >> stability of remote-tracking branches > > This is an interesting question, and it's very likely that I am missing > some nuance here. However, with that said what option does the developer > have, you have to accept that the upstream changes constantly when others > are working on it. What good does it do to keep the "head in the sand" any > longer than necessary? > > I'm not sure there is a way to fetch only 'origin/main' and avoid > 'origin/maint'? Maybe, maybe, if that exists it could be useful here. Isn’t that exactly what git fetch origin main does? (Might need to expand the refspec.) > >> still on a leave > > Enjoy your vacation! I don't expect any response from you until you're back! > > > Harald > ^ permalink raw reply [flat|nested] 35+ messages in thread
* gh 2026-04-25 21:57 ` Ben Knoble @ 2026-04-25 22:54 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-04-25 22:54 UTC (permalink / raw) To: ben.knoble; +Cc: git, gitgitgadget, gitster, haraldnordgren > Isn’t that exactly what > > git fetch origin main > > does? (Might need to expand the refspec.) Very good point, I will update it! Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget ` (4 preceding siblings ...) 2026-04-24 22:21 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Junio C Hamano @ 2026-04-25 18:12 ` Harald Nordgren via GitGitGadget 2026-04-26 7:24 ` [PATCH v3] " Harald Nordgren via GitGitGadget 5 siblings, 1 reply; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-04-25 18:12 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> A common workflow is: git fetch origin git checkout -b new_branch origin/some-branch The first command exists purely so the second sees an up-to-date view of the remote. If it is forgotten, origin/some-branch points at a stale commit and the new local branch is created from the wrong start point. Teach checkout (and switch) a --fetch flag that folds the two steps into one: git checkout --fetch -b new_branch origin/some-branch When --fetch is given and <start-point> names a configured remote (either bare, like "origin", or prefixed, like "origin/foo"), fetch that remote before resolving the ref. Abort the checkout if the fetch fails. Also add a checkout.fetch config to enable this by default. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point * Rename the config from checkout.autoFetch to checkout.fetch, so it matches the --fetch option name. * Rename the internal struct field from auto_fetch to fetch for consistency with the option and config names. * Reword the commit message to lead with the problem (forgetting 'git fetch' and ending up with a stale start-point) before describing the solution. * Document --fetch / --no-fetch in git-checkout and git-switch, and document checkout.fetch in the config reference. * Use "remote-tracking branch" instead of "remote-tracking ref" in the option help text. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v2 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v1: 1: e2fa50ff40 ! 1: 13074c9fea checkout: add --fetch to fetch remote before resolving start-point @@ Metadata ## Commit message ## checkout: add --fetch to fetch remote before resolving start-point - Add a --fetch option to git checkout and git switch, plus a - checkout.autoFetch config to enable it by default. When set and the - start-point argument names a configured remote (either bare, like - "origin", or prefixed, like "origin/foo"), fetch that remote before - resolving the ref. Aborts the checkout if the fetch fails. + A common workflow is: + + git fetch origin + git checkout -b new_branch origin/some-branch + + The first command exists purely so the second sees an up-to-date view + of the remote. If it is forgotten, origin/some-branch points at a stale + commit and the new local branch is created from the wrong start point. + + Teach checkout (and switch) a --fetch flag that folds the two steps + into one: + + git checkout --fetch -b new_branch origin/some-branch + + When --fetch is given and <start-point> names a configured remote + (either bare, like "origin", or prefixed, like "origin/foo"), fetch + that remote before resolving the ref. Abort the checkout if the fetch + fails. + + Also add a checkout.fetch config to enable this by default. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> + ## Documentation/config/checkout.adoc ## +@@ Documentation/config/checkout.adoc: commands or functionality in the future. + option in `git checkout` and `git switch`. See + linkgit:git-switch[1] and linkgit:git-checkout[1]. + ++`checkout.fetch`:: ++ Provides the default value for the `--fetch` or `--no-fetch` ++ option in `git checkout` and `git switch`. See ++ linkgit:git-switch[1] and linkgit:git-checkout[1]. ++ + `checkout.workers`:: + The number of parallel workers to use when updating the working tree. + The default is one, i.e. sequential execution. If set to a value less + + ## Documentation/git-checkout.adoc ## +@@ Documentation/git-checkout.adoc: linkgit:git-config[1]. + The default behavior can be set via the `checkout.guess` configuration + variable. + ++`--fetch`:: ++`--no-fetch`:: ++ If _<start-point>_ names a configured remote -- either bare, ++ like `origin` (which resolves to the remote's default branch), ++ or in _<remote>/<branch>_ form -- run `git fetch` on that ++ remote before resolving _<start-point>_. If the fetch fails, ++ the checkout is aborted and no local branch is created. +++ ++The default behavior can be set via the `checkout.fetch` configuration ++variable. ++ + `-l`:: + Create the new branch's reflog; see linkgit:git-branch[1] for + details. + + ## Documentation/git-switch.adoc ## +@@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See also + The default behavior can be set via the `checkout.guess` configuration + variable. + ++`--fetch`:: ++`--no-fetch`:: ++ If _<start-point>_ names a configured remote -- either bare, ++ like `origin` (which resolves to the remote's default branch), ++ or in _<remote>/<branch>_ form -- run `git fetch` on that ++ remote before resolving _<start-point>_. If the fetch fails, ++ the switch is aborted and no local branch is created. +++ ++The default behavior can be set via the `checkout.fetch` configuration ++variable. ++ + `-f`:: + `--force`:: + An alias for `--discard-changes`. + ## builtin/checkout.c ## @@ #include "repo-settings.h" @@ builtin/checkout.c: struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; -+ int auto_fetch; ++ int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ builtin/checkout.c: static int git_checkout_config(const char *var, const char * opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } -+ if (!strcmp(var, "checkout.autofetch")) { -+ opts->auto_fetch = git_config_bool(var, value); ++ if (!strcmp(var, "checkout.fetch")) { ++ opts->fetch = git_config_bool(var, value); + return 0; + } @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const - &new_branch_info, opts, &rev); + int n; + -+ if (opts->auto_fetch) ++ if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, @@ builtin/checkout.c: int cmd_checkout(int argc, OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), OPT_BOOL(0, "auto-advance", &opts.auto_advance, N_("auto advance to the next file when selecting hunks interactively")), -+ OPT_BOOL(0, "fetch", &opts.auto_fetch, -+ N_("fetch from the remote first if <start-point> is a remote-tracking ref")), ++ OPT_BOOL(0, "fetch", &opts.fetch, ++ N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; @@ builtin/checkout.c: int cmd_switch(int argc, N_("second guess 'git switch <no-such-branch>'")), OPT_BOOL(0, "discard-changes", &opts.discard_changes, N_("throw away local modifications")), -+ OPT_BOOL(0, "fetch", &opts.auto_fetch, -+ N_("fetch from the remote first if <start-point> is a remote-tracking ref")), ++ OPT_BOOL(0, "fetch", &opts.fetch, ++ N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_must_fail git rev-parse --verify refs/heads/bogus +' + -+test_expect_success 'checkout.autoFetch=true enables fetching without --fetch' ' ++test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_cfg && + test_commit -C fetch_upstream u_cfg && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && -+ git -c checkout.autoFetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && ++ git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD +' + Documentation/config/checkout.adoc | 5 +++ Documentation/git-checkout.adoc | 11 +++++++ Documentation/git-switch.adoc | 11 +++++++ builtin/checkout.c | 48 ++++++++++++++++++++++++++-- t/t7201-co.sh | 51 ++++++++++++++++++++++++++++++ t/t9902-completion.sh | 1 + 6 files changed, 125 insertions(+), 2 deletions(-) diff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc index e35d212969..c95f72b38e 100644 --- a/Documentation/config/checkout.adoc +++ b/Documentation/config/checkout.adoc @@ -22,6 +22,11 @@ commands or functionality in the future. option in `git checkout` and `git switch`. See linkgit:git-switch[1] and linkgit:git-checkout[1]. +`checkout.fetch`:: + Provides the default value for the `--fetch` or `--no-fetch` + option in `git checkout` and `git switch`. See + linkgit:git-switch[1] and linkgit:git-checkout[1]. + `checkout.workers`:: The number of parallel workers to use when updating the working tree. The default is one, i.e. sequential execution. If set to a value less diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..f20e2f4c8c 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -201,6 +201,17 @@ linkgit:git-config[1]. The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ names a configured remote -- either bare, + like `origin` (which resolves to the remote's default branch), + or in _<remote>/<branch>_ form -- run `git fetch` on that + remote before resolving _<start-point>_. If the fetch fails, + the checkout is aborted and no local branch is created. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-l`:: Create the new branch's reflog; see linkgit:git-branch[1] for details. diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..3826ed9066 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ names a configured remote -- either bare, + like `origin` (which resolves to the remote's default branch), + or in _<remote>/<branch>_ form -- run `git fetch` on that + remote before resolving _<start-point>_. If the fetch fails, + the switch is aborted and no local branch is created. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-f`:: `--force`:: An alias for `--discard-changes`. diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..b2a34f0f00 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -30,7 +30,9 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +63,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +115,34 @@ struct branch_info { char *checkout; }; +static void fetch_remote_for_start_point(const char *arg) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (!arg || !*arg) + return; + + slash = strchr(arg, '/'); + if (slash == arg) + return; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,6 +1268,10 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } + if (!strcmp(var, "checkout.fetch")) { + opts->fetch = git_config_bool(var, value); + return 0; + } if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1942,8 +1977,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { @@ -2052,6 +2092,8 @@ int cmd_checkout(int argc, OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), OPT_BOOL(0, "auto-advance", &opts.auto_advance, N_("auto advance to the next file when selecting hunks interactively")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; @@ -2102,6 +2144,8 @@ int cmd_switch(int argc, N_("second guess 'git switch <no-such-branch>'")), OPT_BOOL(0, "discard-changes", &opts.discard_changes, N_("throw away local modifications")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..f5729f0831 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,55 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD +' + +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && + git checkout --fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_cfg && + test_commit -C fetch_upstream u_cfg && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && + git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD +' + +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 2f9a597ec7..dc1d63669f 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2602,6 +2602,7 @@ test_expect_success 'double dash "git checkout"' ' --ignore-other-worktrees Z --recurse-submodules Z --auto-advance Z + --fetch Z --progress Z --guess Z --no-guess Z base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* [PATCH v3] checkout: add --fetch to fetch remote before resolving start-point 2026-04-25 18:12 ` [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget @ 2026-04-26 7:24 ` Harald Nordgren via GitGitGadget 2026-04-26 15:54 ` Ramsay Jones 2026-04-26 18:32 ` [PATCH v4] " Harald Nordgren via GitGitGadget 0 siblings, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-04-26 7:24 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> A common workflow is: git fetch origin git checkout -b new_branch origin/some-branch The first command exists purely so the second sees an up-to-date view of the remote. If it is forgotten, origin/some-branch points at a stale commit and the new local branch is created from the wrong start point. Teach checkout (and switch) a --fetch flag that folds the two steps into one: git checkout --fetch -b new_branch origin/some-branch When --fetch is given and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref. This narrows the fetch to the requested branch so that other remote-tracking branches are left untouched -- many tools rely on the stability of remote-tracking refs between explicit fetches. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Also add a checkout.fetch config to enable this by default. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point When <start-point> is in <remote>/<branch> form, only fetch that one branch instead of the whole remote, so unrelated remote-tracking branches stay stable. The bare-remote form (e.g. "origin") still fetches everything. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v3 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v2: 1: 13074c9fea ! 1: df7b63862c checkout: add --fetch to fetch remote before resolving start-point @@ Commit message git checkout --fetch -b new_branch origin/some-branch - When --fetch is given and <start-point> names a configured remote - (either bare, like "origin", or prefixed, like "origin/foo"), fetch - that remote before resolving the ref. Abort the checkout if the fetch - fails. + When --fetch is given and <start-point> is in <remote>/<branch> form, + run "git fetch <remote> <branch>" before resolving the ref. This + narrows the fetch to the requested branch so that other + remote-tracking branches are left untouched -- many tools rely on the + stability of remote-tracking refs between explicit fetches. If + <start-point> is a bare remote name like "origin" (which resolves to + that remote's default branch), "git fetch <remote>" is run instead, + since the target branch is not known up front. Abort the checkout if + the fetch fails. Also add a checkout.fetch config to enable this by default. @@ Documentation/git-checkout.adoc: linkgit:git-config[1]. +`--fetch`:: +`--no-fetch`:: -+ If _<start-point>_ names a configured remote -- either bare, -+ like `origin` (which resolves to the remote's default branch), -+ or in _<remote>/<branch>_ form -- run `git fetch` on that -+ remote before resolving _<start-point>_. If the fetch fails, -+ the checkout is aborted and no local branch is created. ++ If _<start-point>_ refers to a remote-tracking branch, fetch ++ from that remote before resolving it. When _<start-point>_ is ++ in _<remote>/<branch>_ form, only that branch is updated; when ++ it is a bare remote name (e.g. `origin`), the whole remote is ++ fetched. If the fetch fails, the checkout is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. @@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See +`--fetch`:: +`--no-fetch`:: -+ If _<start-point>_ names a configured remote -- either bare, -+ like `origin` (which resolves to the remote's default branch), -+ or in _<remote>/<branch>_ form -- run `git fetch` on that -+ remote before resolving _<start-point>_. If the fetch fails, -+ the switch is aborted and no local branch is created. ++ If _<start-point>_ refers to a remote-tracking branch, fetch ++ from that remote before resolving it. When _<start-point>_ is ++ in _<remote>/<branch>_ form, only that branch is updated; when ++ it is a bare remote name (e.g. `origin`), the whole remote is ++ fetched. If the fetch fails, the switch is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. @@ builtin/checkout.c: struct branch_info { + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); ++ if (slash && slash[1]) ++ strvec_push(&cmd.args, slash + 1); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD +' + ++test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_target && ++ test_commit -C fetch_upstream u_target_pre && ++ git -C fetch_upstream checkout -b fetch_other && ++ test_commit -C fetch_upstream u_other_pre && ++ git fetch fetch_upstream && ++ other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && ++ git -C fetch_upstream checkout fetch_target && ++ test_commit -C fetch_upstream u_target_post && ++ git -C fetch_upstream checkout fetch_other && ++ test_commit -C fetch_upstream u_other_post && ++ git checkout --fetch -b local_target fetch_upstream/fetch_target && ++ test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && ++ test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" ++' ++ +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && Documentation/config/checkout.adoc | 5 +++ Documentation/git-checkout.adoc | 11 +++++ Documentation/git-switch.adoc | 11 +++++ builtin/checkout.c | 50 +++++++++++++++++++++- t/t7201-co.sh | 68 ++++++++++++++++++++++++++++++ t/t9902-completion.sh | 1 + 6 files changed, 144 insertions(+), 2 deletions(-) diff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc index e35d212969..c95f72b38e 100644 --- a/Documentation/config/checkout.adoc +++ b/Documentation/config/checkout.adoc @@ -22,6 +22,11 @@ commands or functionality in the future. option in `git checkout` and `git switch`. See linkgit:git-switch[1] and linkgit:git-checkout[1]. +`checkout.fetch`:: + Provides the default value for the `--fetch` or `--no-fetch` + option in `git checkout` and `git switch`. See + linkgit:git-switch[1] and linkgit:git-checkout[1]. + `checkout.workers`:: The number of parallel workers to use when updating the working tree. The default is one, i.e. sequential execution. If set to a value less diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..f5cc1ced74 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -201,6 +201,17 @@ linkgit:git-config[1]. The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ refers to a remote-tracking branch, fetch + from that remote before resolving it. When _<start-point>_ is + in _<remote>/<branch>_ form, only that branch is updated; when + it is a bare remote name (e.g. `origin`), the whole remote is + fetched. If the fetch fails, the checkout is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-l`:: Create the new branch's reflog; see linkgit:git-branch[1] for details. diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..29743bafea 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ refers to a remote-tracking branch, fetch + from that remote before resolving it. When _<start-point>_ is + in _<remote>/<branch>_ form, only that branch is updated; when + it is a bare remote name (e.g. `origin`), the whole remote is + fetched. If the fetch fails, the switch is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-f`:: `--force`:: An alias for `--discard-changes`. diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..8d810fe2fa 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -30,7 +30,9 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +63,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +115,36 @@ struct branch_info { char *checkout; }; +static void fetch_remote_for_start_point(const char *arg) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (!arg || !*arg) + return; + + slash = strchr(arg, '/'); + if (slash == arg) + return; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (slash && slash[1]) + strvec_push(&cmd.args, slash + 1); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,6 +1270,10 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } + if (!strcmp(var, "checkout.fetch")) { + opts->fetch = git_config_bool(var, value); + return 0; + } if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1942,8 +1979,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { @@ -2052,6 +2094,8 @@ int cmd_checkout(int argc, OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), OPT_BOOL(0, "auto-advance", &opts.auto_advance, N_("auto advance to the next file when selecting hunks interactively")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; @@ -2102,6 +2146,8 @@ int cmd_switch(int argc, N_("second guess 'git switch <no-such-branch>'")), OPT_BOOL(0, "discard-changes", &opts.discard_changes, N_("throw away local modifications")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..cf2ceb4052 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,72 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD +' + +test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && + git checkout --fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_cfg && + test_commit -C fetch_upstream u_cfg && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && + git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD +' + +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 2f9a597ec7..dc1d63669f 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2602,6 +2602,7 @@ test_expect_success 'double dash "git checkout"' ' --ignore-other-worktrees Z --recurse-submodules Z --auto-advance Z + --fetch Z --progress Z --guess Z --no-guess Z base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* Re: [PATCH v3] checkout: add --fetch to fetch remote before resolving start-point 2026-04-26 7:24 ` [PATCH v3] " Harald Nordgren via GitGitGadget @ 2026-04-26 15:54 ` Ramsay Jones 2026-04-26 18:32 ` [PATCH v4] " Harald Nordgren via GitGitGadget 1 sibling, 0 replies; 35+ messages in thread From: Ramsay Jones @ 2026-04-26 15:54 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget, git Cc: D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren On 26/04/2026 8:24 am, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren <haraldnordgren@gmail.com> > [snip] > diff --git a/t/t7201-co.sh b/t/t7201-co.sh > index 9bcf7c0b40..cf2ceb4052 100755 > --- a/t/t7201-co.sh > +++ b/t/t7201-co.sh > @@ -801,4 +801,72 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' > test_cmp_config "" --default "" branch.main2.merge > ' > > +test_expect_success 'setup upstream for --fetch tests' ' > + git checkout main && > + git init fetch_upstream && > + test_commit -C fetch_upstream u_main && > + git remote add fetch_upstream fetch_upstream && > + git fetch fetch_upstream && > + git -C fetch_upstream checkout -b fetch_new && > + test_commit -C fetch_upstream u_new > +' > + > +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' > + git checkout main && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && > + git checkout --fetch -b local_new fetch_upstream/fetch_new && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD > +' > + > +test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_target && > + test_commit -C fetch_upstream u_target_pre && > + git -C fetch_upstream checkout -b fetch_other && > + test_commit -C fetch_upstream u_other_pre && > + git fetch fetch_upstream && > + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && > + git -C fetch_upstream checkout fetch_target && > + test_commit -C fetch_upstream u_target_post && > + git -C fetch_upstream checkout fetch_other && > + test_commit -C fetch_upstream u_other_post && > + git checkout --fetch -b local_target fetch_upstream/fetch_target && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && > + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" > +' > + > +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_new2 && > + test_commit -C fetch_upstream u_new2 && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && > + git checkout --fetch -b local_from_remote fetch_upstream && > + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 > +' > + > +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' > + git checkout main && > + test_might_fail git branch -D bogus && > + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && > + test_must_fail git rev-parse --verify refs/heads/bogus > +' > + > +test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_cfg && > + test_commit -C fetch_upstream u_cfg && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && > + git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD > +' > + > +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_switch && > + test_commit -C fetch_upstream u_switch && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && > + git switch --fetch -c local_switch fetch_upstream/fetch_switch && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD > +' > + > test_done Still just skimming the list, so ... ;) I seem to have missed v2 (but I guess the config name changed in v2), but it seems that there is still no test that confirms '--no-fetch' can countermand the config variable (or, indeed, an earlier command-line '--fetch' etc,.). [I would have no use for this facility (I have _never_ used git-pull for similar reasons) since I would find it odd to 'mash' git-fetch into git-checkout/switch! :) ] ATB, Ramsay Jones ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v4] checkout: add --fetch to fetch remote before resolving start-point 2026-04-26 7:24 ` [PATCH v3] " Harald Nordgren via GitGitGadget 2026-04-26 15:54 ` Ramsay Jones @ 2026-04-26 18:32 ` Harald Nordgren via GitGitGadget 2026-04-28 1:47 ` Junio C Hamano 2026-04-28 9:03 ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-04-26 18:32 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> A common workflow is: git fetch origin git checkout -b new_branch origin/some-branch The first command exists purely so the second sees an up-to-date view of the remote. If it is forgotten, origin/some-branch points at a stale commit and the new local branch is created from the wrong start point. Teach checkout (and switch) a --fetch flag that folds the two steps into one: git checkout --fetch -b new_branch origin/some-branch When --fetch is given and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref. This narrows the fetch to the requested branch so that other remote-tracking branches are left untouched -- many tools rely on the stability of remote-tracking refs between explicit fetches. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Also add a checkout.fetch config to enable this by default. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point Adding tests to confirm that '--no-fetch' can countermand the config. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v4 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v4 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v3: 1: df7b63862c ! 1: 150ccbb621 checkout: add --fetch to fetch remote before resolving start-point @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD +' + ++test_expect_success '--no-fetch overrides checkout.fetch=true' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_nofetch && ++ test_commit -C fetch_upstream u_nofetch && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && ++ test_must_fail git -c checkout.fetch=true checkout --no-fetch \ ++ -b local_nofetch fetch_upstream/fetch_nofetch && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && ++ test_must_fail git rev-parse --verify refs/heads/local_nofetch ++' ++ ++test_expect_success '--no-fetch overrides earlier --fetch on command line' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_override && ++ test_commit -C fetch_upstream u_override && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && ++ test_must_fail git checkout --fetch --no-fetch \ ++ -b local_override fetch_upstream/fetch_override && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && ++ test_must_fail git rev-parse --verify refs/heads/local_override ++' ++ +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && Documentation/config/checkout.adoc | 5 ++ Documentation/git-checkout.adoc | 11 ++++ Documentation/git-switch.adoc | 11 ++++ builtin/checkout.c | 50 ++++++++++++++++- t/t7201-co.sh | 90 ++++++++++++++++++++++++++++++ t/t9902-completion.sh | 1 + 6 files changed, 166 insertions(+), 2 deletions(-) diff --git a/Documentation/config/checkout.adoc b/Documentation/config/checkout.adoc index e35d212969..c95f72b38e 100644 --- a/Documentation/config/checkout.adoc +++ b/Documentation/config/checkout.adoc @@ -22,6 +22,11 @@ commands or functionality in the future. option in `git checkout` and `git switch`. See linkgit:git-switch[1] and linkgit:git-checkout[1]. +`checkout.fetch`:: + Provides the default value for the `--fetch` or `--no-fetch` + option in `git checkout` and `git switch`. See + linkgit:git-switch[1] and linkgit:git-checkout[1]. + `checkout.workers`:: The number of parallel workers to use when updating the working tree. The default is one, i.e. sequential execution. If set to a value less diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..f5cc1ced74 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -201,6 +201,17 @@ linkgit:git-config[1]. The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ refers to a remote-tracking branch, fetch + from that remote before resolving it. When _<start-point>_ is + in _<remote>/<branch>_ form, only that branch is updated; when + it is a bare remote name (e.g. `origin`), the whole remote is + fetched. If the fetch fails, the checkout is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-l`:: Create the new branch's reflog; see linkgit:git-branch[1] for details. diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..29743bafea 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -110,6 +110,17 @@ ambiguous but exists on the 'origin' remote. See also The default behavior can be set via the `checkout.guess` configuration variable. +`--fetch`:: +`--no-fetch`:: + If _<start-point>_ refers to a remote-tracking branch, fetch + from that remote before resolving it. When _<start-point>_ is + in _<remote>/<branch>_ form, only that branch is updated; when + it is a bare remote name (e.g. `origin`), the whole remote is + fetched. If the fetch fails, the switch is aborted. ++ +The default behavior can be set via the `checkout.fetch` configuration +variable. + `-f`:: `--force`:: An alias for `--discard-changes`. diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..8d810fe2fa 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -30,7 +30,9 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +63,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +115,36 @@ struct branch_info { char *checkout; }; +static void fetch_remote_for_start_point(const char *arg) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (!arg || !*arg) + return; + + slash = strchr(arg, '/'); + if (slash == arg) + return; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (slash && slash[1]) + strvec_push(&cmd.args, slash + 1); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,6 +1270,10 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } + if (!strcmp(var, "checkout.fetch")) { + opts->fetch = git_config_bool(var, value); + return 0; + } if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1942,8 +1979,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { @@ -2052,6 +2094,8 @@ int cmd_checkout(int argc, OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), OPT_BOOL(0, "auto-advance", &opts.auto_advance, N_("auto advance to the next file when selecting hunks interactively")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; @@ -2102,6 +2146,8 @@ int cmd_switch(int argc, N_("second guess 'git switch <no-such-branch>'")), OPT_BOOL(0, "discard-changes", &opts.discard_changes, N_("throw away local modifications")), + OPT_BOOL(0, "fetch", &opts.fetch, + N_("fetch from the remote first if <start-point> is a remote-tracking branch")), OPT_END() }; diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..731be2680a 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,94 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD +' + +test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && + git checkout --fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + +test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_cfg && + test_commit -C fetch_upstream u_cfg && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && + git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && + test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD +' + +test_expect_success '--no-fetch overrides checkout.fetch=true' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_nofetch && + test_commit -C fetch_upstream u_nofetch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && + test_must_fail git -c checkout.fetch=true checkout --no-fetch \ + -b local_nofetch fetch_upstream/fetch_nofetch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && + test_must_fail git rev-parse --verify refs/heads/local_nofetch +' + +test_expect_success '--no-fetch overrides earlier --fetch on command line' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_override && + test_commit -C fetch_upstream u_override && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && + test_must_fail git checkout --fetch --no-fetch \ + -b local_override fetch_upstream/fetch_override && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && + test_must_fail git rev-parse --verify refs/heads/local_override +' + +test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 2f9a597ec7..dc1d63669f 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -2602,6 +2602,7 @@ test_expect_success 'double dash "git checkout"' ' --ignore-other-worktrees Z --recurse-submodules Z --auto-advance Z + --fetch Z --progress Z --guess Z --no-guess Z base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* Re: [PATCH v4] checkout: add --fetch to fetch remote before resolving start-point 2026-04-26 18:32 ` [PATCH v4] " Harald Nordgren via GitGitGadget @ 2026-04-28 1:47 ` Junio C Hamano 2026-04-28 8:44 ` [PATCH] " Harald Nordgren 2026-04-28 9:03 ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 1 reply; 35+ messages in thread From: Junio C Hamano @ 2026-04-28 1:47 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget Cc: git, Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > A common workflow is: > > git fetch origin > git checkout -b new_branch origin/some-branch > > The first command exists purely so the second sees an up-to-date view > of the remote. This is only half true, isn't it? Git is among projects that encourage forking only from a well-known point in history (like the latest released version), not at a random "tip of the day" commit from the upstream. Such projects also tend to discourage people from constantly pulling updated upstream into their unfinished topic branch, or rebase their unfinished topic branch on top of updated upstream, only to "catch up", and instead encourage them to make a trial merge to notice when the base got too stale to cause eventual merge to conflict too much, and when it happens, make such a back-merge, but otherwise keep working on the stable base and avoid such "catching up". And when working with such a project, what users who do the above is forgetting is to inspect origin/master between the two steps to see if it is a good commit to start your topic at. > If it is forgotten, origin/some-branch points at a stale commit > and the new local branch is created from the wrong start point. So this part is not quite true. What makes your topic begin at a wrong starting point is not that you forget to fetch, but you forget to verify what you fetched and think if it is a good starting point. And for that verification to happen, you do not want "checkout" and "fetch" mixed into one. On the other hand, if you are allowed to fork at anywhere (as opposed to a latest release), then not fetching and building on top of slightly older codebase is not such a huge deal, as you're likely to be making the "catch up" changes on top of your unfinished work later anyway. So as I already said before, I am fairly negative on this topic. It feels more like a knob to allow and actively encourage people to be more sloppy than anything else. I may have already pointed this out (but I do not remember), but this option would not make any sense when --track is not in effect, so instead of adding a brand new option, making it an extension to the existing --track option might make it slightly more palatable. ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-04-28 1:47 ` Junio C Hamano @ 2026-04-28 8:44 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-04-28 8:44 UTC (permalink / raw) To: gitster Cc: ben.knoble, git, gitgitgadget, haraldnordgren, kristofferhaugsbakk, marcnarc, ramsay I hope you had a good leave and is back with renewed energy! ☀️ > Git is among projects that encourage forking only from a well-known > point in history (like the latest released version), not at a random > "tip of the day" commit from the upstream. Are you talking about the Git project that we are working on right now, or talking about how people use Git "in the wild"? Because how people use Git in the wild can be a bit different, and merge conflicts arguably the worst part of collaborating with a team using Git. In my early days as a professional coder, snubbed my toe countless times on forgetting to pull in the latest changes, before starting to work on something. I respect that things work differently in a neatly ordered project like Git itself, where you do a great work of organizing, but all other projects are not like that. My advice to a junior developer is to pull in the latest changes when starting and to rebase obsessively to prevent a large merge conflict down the road. > So instead of introducing a totally new option that can only be used > only when "--track" is given, it might make more sense to introduce > this as a variant of "--track", perhaps "--track=fetch,[in]direct" > or something like that. > I may have already pointed this out (but I do not remember), but > this option would not make any sense when --track is not in effect, > so instead of adding a brand new option, making it an extension to > the existing --track option might make it slightly more palatable. Fair enough. You did point it out and I will give that a try! Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point 2026-04-26 18:32 ` [PATCH v4] " Harald Nordgren via GitGitGadget 2026-04-28 1:47 ` Junio C Hamano @ 2026-04-28 9:03 ` Harald Nordgren via GitGitGadget 2026-05-03 20:59 ` Junio C Hamano 2026-05-03 22:31 ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-04-28 9:03 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> A common workflow is: git fetch origin git checkout -b new_branch --track origin/some-branch The first command exists so the second sees an up-to-date view of the remote. If it is forgotten, origin/some-branch points at a stale commit and the new local branch is created from the wrong start point. This only matters when the user is setting up tracking and expects the new branch to start at the freshest tip; for a one-off checkout of an arbitrary commit there is no reason to "freshen" the start-point. Tie the new behavior to --track for that reason: extend its argument to take a comma-separated list, where "fetch" can be combined with the existing "direct" (default) and "inherit" modes. Examples: git checkout --track=fetch -b new_branch origin/some-branch git checkout --track=fetch,inherit -b new_branch some_local_branch git switch --track=fetch -c new_branch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref. This narrows the fetch to the requested branch so that other remote-tracking branches are left untouched -- many tools rely on the stability of remote-tracking refs between explicit fetches. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point * Folded --fetch into --track. The standalone --fetch/--no-fetch flags are gone; the same behavior is now requested via --track=fetch (combinable as --track=fetch,inherit). * Removed the checkout.fetch config. Since fetching is tied to --track, there's no separate config knob anymore. * Docs reworked accordingly. --track's syntax is now (direct|inherit|fetch)[,...] in both git-checkout and git-switch man pages, with the fetch behavior described under it. The old --fetch and checkout.fetch sections are deleted. * New parser callback in checkout.c. A small parse_opt_checkout_track splits the comma-separated argument with string_list_split and sets opts->track and opts->fetch together. * Tests updated and trimmed. All test invocations switched from --fetch to --track=fetch. Dropped the checkout.fetch=true and --no-fetch override tests (those features no longer exist). Added a --track=fetch,inherit test, a --track=bogus error test, and stronger config-assertion checks on the basic test. Two redundant tests (fetch,direct and order-insensitivity) were removed. * Completion list cleaned up. --fetch removed from the expected git checkout option list. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v5 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v5 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v4: 1: 150ccbb621 ! 1: 8ebc2f94b9 checkout: add --fetch to fetch remote before resolving start-point @@ Metadata Author: Harald Nordgren <haraldnordgren@gmail.com> ## Commit message ## - checkout: add --fetch to fetch remote before resolving start-point + checkout: extend --track with a "fetch" mode to refresh start-point A common workflow is: git fetch origin - git checkout -b new_branch origin/some-branch + git checkout -b new_branch --track origin/some-branch - The first command exists purely so the second sees an up-to-date view - of the remote. If it is forgotten, origin/some-branch points at a stale - commit and the new local branch is created from the wrong start point. + The first command exists so the second sees an up-to-date view of the + remote. If it is forgotten, origin/some-branch points at a stale + commit and the new local branch is created from the wrong start + point. This only matters when the user is setting up tracking and + expects the new branch to start at the freshest tip; for a one-off + checkout of an arbitrary commit there is no reason to "freshen" the + start-point. - Teach checkout (and switch) a --fetch flag that folds the two steps - into one: + Tie the new behavior to --track for that reason: extend its argument + to take a comma-separated list, where "fetch" can be combined with the + existing "direct" (default) and "inherit" modes. Examples: - git checkout --fetch -b new_branch origin/some-branch + git checkout --track=fetch -b new_branch origin/some-branch + git checkout --track=fetch,inherit -b new_branch some_local_branch + git switch --track=fetch -c new_branch origin/some-branch - When --fetch is given and <start-point> is in <remote>/<branch> form, - run "git fetch <remote> <branch>" before resolving the ref. This + When "fetch" is requested and <start-point> is in <remote>/<branch> + form, run "git fetch <remote> <branch>" before resolving the ref. This narrows the fetch to the requested branch so that other remote-tracking branches are left untouched -- many tools rely on the stability of remote-tracking refs between explicit fetches. If @@ Commit message since the target branch is not known up front. Abort the checkout if the fetch fails. - Also add a checkout.fetch config to enable this by default. - Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> - ## Documentation/config/checkout.adoc ## -@@ Documentation/config/checkout.adoc: commands or functionality in the future. - option in `git checkout` and `git switch`. See - linkgit:git-switch[1] and linkgit:git-checkout[1]. - -+`checkout.fetch`:: -+ Provides the default value for the `--fetch` or `--no-fetch` -+ option in `git checkout` and `git switch`. See -+ linkgit:git-switch[1] and linkgit:git-checkout[1]. -+ - `checkout.workers`:: - The number of parallel workers to use when updating the working tree. - The default is one, i.e. sequential execution. If set to a value less - ## Documentation/git-checkout.adoc ## -@@ Documentation/git-checkout.adoc: linkgit:git-config[1]. - The default behavior can be set via the `checkout.guess` configuration - variable. +@@ Documentation/git-checkout.adoc: of it"). + resets _<branch>_ to the start point instead of failing. -+`--fetch`:: -+`--no-fetch`:: -+ If _<start-point>_ refers to a remote-tracking branch, fetch -+ from that remote before resolving it. When _<start-point>_ is -+ in _<remote>/<branch>_ form, only that branch is updated; when -+ it is a bare remote name (e.g. `origin`), the whole remote is -+ fetched. If the fetch fails, the checkout is aborted. + `-t`:: +-`--track[=(direct|inherit)]`:: ++`--track[=(direct|inherit|fetch)[,...]]`:: + When creating a new branch, set up "upstream" configuration. See + `--track` in linkgit:git-branch[1] for details. As a convenience, + --track without -b implies branch creation. + + ++The argument is a comma-separated list. `direct` (the default) and ++`inherit` select the tracking mode. Adding `fetch` requests that the ++remote be fetched before _<start-point>_ is resolved, so the new branch ++starts from a fresh tip: when _<start-point>_ is in ++_<remote>/<branch>_ form, only that branch is updated; when it is a ++bare remote name (e.g. `origin`), the whole remote is fetched. If the ++fetch fails, the checkout is aborted. ++ -+The default behavior can be set via the `checkout.fetch` configuration -+variable. -+ - `-l`:: - Create the new branch's reflog; see linkgit:git-branch[1] for - details. + If no `-b` option is given, the name of the new branch will be + derived from the remote-tracking branch, by looking at the local part of + the refspec configured for the corresponding remote, and then stripping ## Documentation/git-switch.adoc ## -@@ Documentation/git-switch.adoc: ambiguous but exists on the 'origin' remote. See also - The default behavior can be set via the `checkout.guess` configuration - variable. +@@ Documentation/git-switch.adoc: should result in deletion of the path). + attached to a terminal, regardless of `--quiet`. -+`--fetch`:: -+`--no-fetch`:: -+ If _<start-point>_ refers to a remote-tracking branch, fetch -+ from that remote before resolving it. When _<start-point>_ is -+ in _<remote>/<branch>_ form, only that branch is updated; when -+ it is a bare remote name (e.g. `origin`), the whole remote is -+ fetched. If the fetch fails, the switch is aborted. + `-t`:: +-`--track[ (direct|inherit)]`:: ++`--track[=(direct|inherit|fetch)[,...]]`:: + When creating a new branch, set up "upstream" configuration. + `-c` is implied. See `--track` in linkgit:git-branch[1] for + details. + + ++The argument is a comma-separated list. `direct` (the default) and ++`inherit` select the tracking mode. Adding `fetch` requests that the ++remote be fetched before _<start-point>_ is resolved, so the new branch ++starts from a fresh tip: when _<start-point>_ is in ++_<remote>/<branch>_ form, only that branch is updated; when it is a ++bare remote name (e.g. `origin`), the whole remote is fetched. If the ++fetch fails, the switch is aborted. ++ -+The default behavior can be set via the `checkout.fetch` configuration -+variable. -+ - `-f`:: - `--force`:: - An alias for `--discard-changes`. + If no `-c` option is given, the name of the new branch will be derived + from the remote-tracking branch, by looking at the local part of the + refspec configured for the corresponding remote, and then stripping ## builtin/checkout.c ## @@ @@ builtin/checkout.c: struct branch_info { + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} ++ ++static int parse_opt_checkout_track(const struct option *opt, ++ const char *arg, int unset) ++{ ++ struct checkout_opts *opts = opt->value; ++ struct string_list tokens = STRING_LIST_INIT_DUP; ++ struct string_list_item *item; ++ int ret = 0; ++ ++ if (unset) { ++ opts->track = BRANCH_TRACK_NEVER; ++ opts->fetch = 0; ++ return 0; ++ } ++ ++ opts->track = BRANCH_TRACK_EXPLICIT; ++ if (!arg) ++ return 0; ++ ++ string_list_split(&tokens, arg, ",", -1); ++ for_each_string_list_item(item, &tokens) { ++ if (!strcmp(item->string, "fetch")) { ++ opts->fetch = 1; ++ } else if (!strcmp(item->string, "direct")) { ++ opts->track = BRANCH_TRACK_EXPLICIT; ++ } else if (!strcmp(item->string, "inherit")) { ++ opts->track = BRANCH_TRACK_INHERIT; ++ } else { ++ ret = error(_("option `%s' expects \"%s\", \"%s\", " ++ "or \"%s\""), ++ "--track", "direct", "inherit", "fetch"); ++ break; ++ } ++ } ++ ++ string_list_clear(&tokens, 0); ++ return ret; ++} + static void branch_info_release(struct branch_info *info) { @@ builtin/checkout.c: static int git_checkout_config(const char *var, const char * opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } -+ if (!strcmp(var, "checkout.fetch")) { -+ opts->fetch = git_config_bool(var, value); -+ return 0; -+ } - +- if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); + +@@ builtin/checkout.c: static struct option *add_common_switch_branch_options( + { + struct option options[] = { + OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), +- OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", ++ OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", + N_("set branch tracking configuration"), + PARSE_OPT_OPTARG, +- parse_opt_tracking_mode), ++ parse_opt_checkout_track), + OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), + PARSE_OPT_NOCOMPLETE), + OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { -@@ builtin/checkout.c: int cmd_checkout(int argc, - OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), - OPT_BOOL(0, "auto-advance", &opts.auto_advance, - N_("auto advance to the next file when selecting hunks interactively")), -+ OPT_BOOL(0, "fetch", &opts.fetch, -+ N_("fetch from the remote first if <start-point> is a remote-tracking branch")), - OPT_END() - }; - -@@ builtin/checkout.c: int cmd_switch(int argc, - N_("second guess 'git switch <no-such-branch>'")), - OPT_BOOL(0, "discard-changes", &opts.discard_changes, - N_("throw away local modifications")), -+ OPT_BOOL(0, "fetch", &opts.fetch, -+ N_("fetch from the remote first if <start-point> is a remote-tracking branch")), - OPT_END() - }; - ## t/t7201-co.sh ## @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' -+test_expect_success 'setup upstream for --fetch tests' ' ++test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_commit -C fetch_upstream u_new +' + -+test_expect_success 'checkout --fetch -b picks up branch created upstream after clone' ' ++test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && -+ git checkout --fetch -b local_new fetch_upstream/fetch_new && -+ test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD ++ git checkout --track=fetch -b local_new fetch_upstream/fetch_new && ++ test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && ++ test_cmp_config fetch_upstream branch.local_new.remote && ++ test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + -+test_expect_success 'checkout --fetch <remote>/<branch> leaves other tracking branches untouched' ' ++test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && -+ git checkout --fetch -b local_target fetch_upstream/fetch_target && ++ git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + -+test_expect_success 'checkout --fetch with bare remote name fetches the remote' ' ++test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && -+ git checkout --fetch -b local_from_remote fetch_upstream && ++ git checkout --track=fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + -+test_expect_success 'checkout --fetch aborts and does not create branch on fetch failure' ' ++test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && -+ test_must_fail git checkout --fetch -b bogus fetch_upstream/does_not_exist && ++ test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + -+test_expect_success 'checkout.fetch=true enables fetching without --fetch' ' ++test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && -+ git -C fetch_upstream checkout -b fetch_cfg && -+ test_commit -C fetch_upstream u_cfg && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_cfg && -+ git -c checkout.fetch=true checkout -b local_cfg fetch_upstream/fetch_cfg && -+ test_cmp_rev refs/remotes/fetch_upstream/fetch_cfg HEAD -+' -+ -+test_expect_success '--no-fetch overrides checkout.fetch=true' ' ++ git -C fetch_upstream checkout -b fetch_inherit && ++ test_commit -C fetch_upstream u_inherit && ++ git fetch fetch_upstream fetch_inherit && ++ git checkout -b base_inherit fetch_upstream/fetch_inherit && ++ test_commit -C fetch_upstream u_inherit2 && + git checkout main && -+ git -C fetch_upstream checkout -b fetch_nofetch && -+ test_commit -C fetch_upstream u_nofetch && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && -+ test_must_fail git -c checkout.fetch=true checkout --no-fetch \ -+ -b local_nofetch fetch_upstream/fetch_nofetch && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_nofetch && -+ test_must_fail git rev-parse --verify refs/heads/local_nofetch ++ git checkout --track=fetch,inherit -b local_inherit base_inherit && ++ test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && ++ test_cmp_config fetch_upstream branch.local_inherit.remote && ++ test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + -+test_expect_success '--no-fetch overrides earlier --fetch on command line' ' ++test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && -+ git -C fetch_upstream checkout -b fetch_override && -+ test_commit -C fetch_upstream u_override && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && -+ test_must_fail git checkout --fetch --no-fetch \ -+ -b local_override fetch_upstream/fetch_override && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_override && -+ test_must_fail git rev-parse --verify refs/heads/local_override ++ test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && ++ test_grep "expects" err +' + -+test_expect_success 'switch --fetch -c picks up branch created upstream after clone' ' ++test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && -+ git switch --fetch -c local_switch fetch_upstream/fetch_switch && ++ git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done - - ## t/t9902-completion.sh ## -@@ t/t9902-completion.sh: test_expect_success 'double dash "git checkout"' ' - --ignore-other-worktrees Z - --recurse-submodules Z - --auto-advance Z -+ --fetch Z - --progress Z - --guess Z - --no-guess Z Documentation/git-checkout.adoc | 10 +++- Documentation/git-switch.adoc | 10 +++- builtin/checkout.c | 85 +++++++++++++++++++++++++++++++-- t/t7201-co.sh | 81 +++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 7 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..3b8292612d 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -158,11 +158,19 @@ of it"). resets _<branch>_ to the start point instead of failing. `-t`:: -`--track[=(direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. See `--track` in linkgit:git-branch[1] for details. As a convenience, --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode. Adding `fetch` requests that the +remote be fetched before _<start-point>_ is resolved, so the new branch +starts from a fresh tip: when _<start-point>_ is in +_<remote>/<branch>_ form, only that branch is updated; when it is a +bare remote name (e.g. `origin`), the whole remote is fetched. If the +fetch fails, the checkout is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..35a03e8a52 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -154,11 +154,19 @@ should result in deletion of the path). attached to a terminal, regardless of `--quiet`. `-t`:: -`--track[ (direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. `-c` is implied. See `--track` in linkgit:git-branch[1] for details. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode. Adding `fetch` requests that the +remote be fetched before _<start-point>_ is resolved, so the new branch +starts from a fresh tip: when _<start-point>_ is in +_<remote>/<branch>_ form, only that branch is updated; when it is a +bare remote name (e.g. `origin`), the whole remote is fetched. If the +fetch fails, the switch is aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..de4d7c00c7 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -30,7 +30,9 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +63,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +115,74 @@ struct branch_info { char *checkout; }; +static void fetch_remote_for_start_point(const char *arg) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (!arg || !*arg) + return; + + slash = strchr(arg, '/'); + if (slash == arg) + return; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (slash && slash[1]) + strvec_push(&cmd.args, slash + 1); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} + +static int parse_opt_checkout_track(const struct option *opt, + const char *arg, int unset) +{ + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; + int ret = 0; + + if (unset) { + opts->track = BRANCH_TRACK_NEVER; + opts->fetch = 0; + return 0; + } + + opts->track = BRANCH_TRACK_EXPLICIT; + if (!arg) + return 0; + + string_list_split(&tokens, arg, ",", -1); + for_each_string_list_item(item, &tokens) { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); + break; + } + } + + string_list_clear(&tokens, 0); + return ret; +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,7 +1308,6 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } - if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1734,10 +1804,10 @@ static struct option *add_common_switch_branch_options( { struct option options[] = { OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", N_("set branch tracking configuration"), PARSE_OPT_OPTARG, - parse_opt_tracking_mode), + parse_opt_checkout_track), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), PARSE_OPT_NOCOMPLETE), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ -1942,8 +2012,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..39236dca12 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,85 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && + test_cmp_config fetch_upstream branch.local_new.remote && + test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && + git checkout --track=fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + +test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && + test_commit -C fetch_upstream u_inherit && + git fetch fetch_upstream fetch_inherit && + git checkout -b base_inherit fetch_upstream/fetch_inherit && + test_commit -C fetch_upstream u_inherit2 && + git checkout main && + git checkout --track=fetch,inherit -b local_inherit base_inherit && + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && + test_cmp_config fetch_upstream branch.local_inherit.remote && + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + +test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && + test_grep "expects" err +' + +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* Re: [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point 2026-04-28 9:03 ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget @ 2026-05-03 20:59 ` Junio C Hamano 2026-05-03 22:32 ` [PATCH] checkout: add --autostash option for branch switching Harald Nordgren 2026-05-03 22:31 ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 1 reply; 35+ messages in thread From: Junio C Hamano @ 2026-05-03 20:59 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget Cc: git, Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > > A common workflow is: > > git fetch origin > git checkout -b new_branch --track origin/some-branch > > The first command exists so the second sees an up-to-date view of the > remote. If it is forgotten, origin/some-branch points at a stale > commit and the new local branch is created from the wrong start > point. As I pointed out multiple times, I prefer not to see this called "wrong". Even if you did not "forget", somebody may be pushing after you fetched and you may end up forking from a "stale" commit. So not fetching is not inherently "wrong", simply because that is how real world works. Multiple people working in a distributed environment does not give you absolute garantee that you will be "up to date", ever, which makes it wrong to call anything that is not "up to date" a "wrong starting point". > This only matters when the user is setting up tracking and > expects the new branch to start at the freshest tip; for a one-off > checkout of an arbitrary commit there is no reason to "freshen" the > start-point. I do not think "arbitrary" fits in this workflow description. If anything, "I'd take anything that the remote repository happens to have at the tip, even without having a chance to sanity checke if that is a good starting point" is more appropriate workflow to be described with a word "arbitrary commit". If you are checking out without forking from there, you'd more likely be checking out the "latest" you have fetched from the other side, often knowing exactly what it is after checking it with "git show origin/$topic". > Tie the new behavior to --track for that reason: Notice that the reader hasn't heard what "the new behaviour" is up to this point yet? How about rewriting everything up to and including this "Tie the new ..." line perhaps like so: If you want to fork your topic branch from the very latest of the tip of a branch your remote has, you would do: git fetch origin some-branch git checkout -b new_branch --track origin/some-branch Extend the "--track" option of "git checkout" and allow users to write git checkout --track=fetch -b new_branch origin/some_branch to (1) fetch 'some-branch' from the remote 'origin', updating the remote-tracking branch 'origin/some-branch', (2) arrange subsequent 'git pull' on 'new_branch' to interact with 'origin/some_branch' and (3) fork 'new_branch' from it. In the value of the '--track' option, 'fetch' can be combined with ... ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH] checkout: add --autostash option for branch switching 2026-05-03 20:59 ` Junio C Hamano @ 2026-05-03 22:32 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-05-03 22:32 UTC (permalink / raw) To: gitster Cc: ben.knoble, git, gitgitgadget, haraldnordgren, kristofferhaugsbakk, marcnarc, ramsay > How about rewriting everything up to and including this "Tie the new > ..." line perhaps like so: Done! Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point 2026-04-28 9:03 ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-03 20:59 ` Junio C Hamano @ 2026-05-03 22:31 ` Harald Nordgren via GitGitGadget 2026-05-07 20:12 ` Harald Nordgren 2026-05-08 22:52 ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-05-03 22:31 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> If you want to fork your topic branch from the very latest of the tip of a branch your remote has, you would do: git fetch origin some-branch git checkout -b new_branch --track origin/some-branch Extend the "--track" option of "git checkout" and allow users to write git checkout -b new_branch --track=fetch origin/some-branch to (1) fetch 'some-branch' from the remote 'origin', updating the remote-tracking branch 'origin/some-branch', (2) arrange subsequent 'git pull' on 'new_branch' to interact with 'origin/some-branch' and (3) fork 'new_branch' from it. In the value of the '--track' option, 'fetch' can be combined with the existing 'direct' (default) and 'inherit' modes via a comma-separated list. Examples: git checkout -b new_branch --track=fetch,inherit some_local_branch git switch -c new_branch --track=fetch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref, so that other remote-tracking branches are left untouched. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point Commit message only, no code/doc/test changes. Restructured the opening around the user-visible workflow before introducing '--track=fetch', reordered all example invocations to ' -b/-c --track[=...] ', dropped the "wrong/stale start-point" and "arbitrary commit" framings, and trimmed the over-explanation of the narrowed fetch. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v6 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v6 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v5: 1: 8ebc2f94b9 ! 1: 1b42c648b9 checkout: extend --track with a "fetch" mode to refresh start-point @@ Metadata ## Commit message ## checkout: extend --track with a "fetch" mode to refresh start-point - A common workflow is: + If you want to fork your topic branch from the very latest of the + tip of a branch your remote has, you would do: - git fetch origin + git fetch origin some-branch git checkout -b new_branch --track origin/some-branch - The first command exists so the second sees an up-to-date view of the - remote. If it is forgotten, origin/some-branch points at a stale - commit and the new local branch is created from the wrong start - point. This only matters when the user is setting up tracking and - expects the new branch to start at the freshest tip; for a one-off - checkout of an arbitrary commit there is no reason to "freshen" the - start-point. + Extend the "--track" option of "git checkout" and allow users to + write - Tie the new behavior to --track for that reason: extend its argument - to take a comma-separated list, where "fetch" can be combined with the - existing "direct" (default) and "inherit" modes. Examples: + git checkout -b new_branch --track=fetch origin/some-branch - git checkout --track=fetch -b new_branch origin/some-branch - git checkout --track=fetch,inherit -b new_branch some_local_branch - git switch --track=fetch -c new_branch origin/some-branch + to (1) fetch 'some-branch' from the remote 'origin', updating the + remote-tracking branch 'origin/some-branch', (2) arrange subsequent + 'git pull' on 'new_branch' to interact with 'origin/some-branch' and + (3) fork 'new_branch' from it. + + In the value of the '--track' option, 'fetch' can be combined with + the existing 'direct' (default) and 'inherit' modes via a + comma-separated list. Examples: + + git checkout -b new_branch --track=fetch,inherit some_local_branch + git switch -c new_branch --track=fetch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> - form, run "git fetch <remote> <branch>" before resolving the ref. This - narrows the fetch to the requested branch so that other - remote-tracking branches are left untouched -- many tools rely on the - stability of remote-tracking refs between explicit fetches. If + form, run "git fetch <remote> <branch>" before resolving the ref, so + that other remote-tracking branches are left untouched. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if Documentation/git-checkout.adoc | 10 +++- Documentation/git-switch.adoc | 10 +++- builtin/checkout.c | 85 +++++++++++++++++++++++++++++++-- t/t7201-co.sh | 81 +++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 7 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..3b8292612d 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -158,11 +158,19 @@ of it"). resets _<branch>_ to the start point instead of failing. `-t`:: -`--track[=(direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. See `--track` in linkgit:git-branch[1] for details. As a convenience, --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode. Adding `fetch` requests that the +remote be fetched before _<start-point>_ is resolved, so the new branch +starts from a fresh tip: when _<start-point>_ is in +_<remote>/<branch>_ form, only that branch is updated; when it is a +bare remote name (e.g. `origin`), the whole remote is fetched. If the +fetch fails, the checkout is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..35a03e8a52 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -154,11 +154,19 @@ should result in deletion of the path). attached to a terminal, regardless of `--quiet`. `-t`:: -`--track[ (direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. `-c` is implied. See `--track` in linkgit:git-branch[1] for details. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode. Adding `fetch` requests that the +remote be fetched before _<start-point>_ is resolved, so the new branch +starts from a fresh tip: when _<start-point>_ is in +_<remote>/<branch>_ form, only that branch is updated; when it is a +bare remote name (e.g. `origin`), the whole remote is fetched. If the +fetch fails, the switch is aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..de4d7c00c7 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -30,7 +30,9 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +63,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +115,74 @@ struct branch_info { char *checkout; }; +static void fetch_remote_for_start_point(const char *arg) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (!arg || !*arg) + return; + + slash = strchr(arg, '/'); + if (slash == arg) + return; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (slash && slash[1]) + strvec_push(&cmd.args, slash + 1); + cmd.git_cmd = 1; + free(remote_name); + if (run_command(&cmd)) + die(_("failed to fetch start-point '%s'"), arg); +} + +static int parse_opt_checkout_track(const struct option *opt, + const char *arg, int unset) +{ + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; + int ret = 0; + + if (unset) { + opts->track = BRANCH_TRACK_NEVER; + opts->fetch = 0; + return 0; + } + + opts->track = BRANCH_TRACK_EXPLICIT; + if (!arg) + return 0; + + string_list_split(&tokens, arg, ",", -1); + for_each_string_list_item(item, &tokens) { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); + break; + } + } + + string_list_clear(&tokens, 0); + return ret; +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,7 +1308,6 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } - if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1734,10 +1804,10 @@ static struct option *add_common_switch_branch_options( { struct option options[] = { OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", N_("set branch tracking configuration"), PARSE_OPT_OPTARG, - parse_opt_tracking_mode), + parse_opt_checkout_track), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), PARSE_OPT_NOCOMPLETE), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ -1942,8 +2012,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..39236dca12 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,85 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && + test_cmp_config fetch_upstream branch.local_new.remote && + test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_new2 && + test_commit -C fetch_upstream u_new2 && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && + git checkout --track=fetch -b local_from_remote fetch_upstream && + git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 +' + +test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && + test_commit -C fetch_upstream u_inherit && + git fetch fetch_upstream fetch_inherit && + git checkout -b base_inherit fetch_upstream/fetch_inherit && + test_commit -C fetch_upstream u_inherit2 && + git checkout main && + git checkout --track=fetch,inherit -b local_inherit base_inherit && + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && + test_cmp_config fetch_upstream branch.local_inherit.remote && + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + +test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && + test_grep "expects" err +' + +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-03 22:31 ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget @ 2026-05-07 20:12 ` Harald Nordgren 2026-05-08 13:15 ` Phillip Wood 2026-05-08 22:52 ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 1 sibling, 1 reply; 35+ messages in thread From: Harald Nordgren @ 2026-05-07 20:12 UTC (permalink / raw) To: gitgitgadget Cc: ben.knoble, git, haraldnordgren, kristofferhaugsbakk, marcnarc, ramsay Is this ready to move to next? Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-07 20:12 ` Harald Nordgren @ 2026-05-08 13:15 ` Phillip Wood 2026-05-08 22:40 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren 0 siblings, 1 reply; 35+ messages in thread From: Phillip Wood @ 2026-05-08 13:15 UTC (permalink / raw) To: Harald Nordgren, gitgitgadget Cc: ben.knoble, git, kristofferhaugsbakk, marcnarc, ramsay Hi Harald On 07/05/2026 21:12, Harald Nordgren wrote: > Is this ready to move to next? I'm not particularly enthusiastic one way or the other about adding this, but so long as we only try to fetch when the user explicitly asks for it I don't particularly object. However having had a quick scan of the implementation I have a few comments * "--track=inherit,direct" is nonsense and should be rejected * currently "--track" has "last one wins" behavior so "--track=inherit --track=direct" behaves like "--track=direct". We should probably keep that so that "--track=fetch --track=direct" behaves like "--track=direct", not "--track=fetch,direct" * if "git fetch" fails and the remote tracking ref already exists then we should print a warning and carry on rather than dying which is more convenient if the user or remote server are offline. * "git checkout --track=fetch origin/branch" should respect remote.origin.fetch so that we fetch the ref that we're going to checkout. I wonder if we can share this logic with the code that sets the upstream branch. * "git checkout --track=fetch origin" should only fetch the remote ref that we're going to checkout, not all the refs from origin. i.e. it should read origin/HEAD to work out what to fetch. Thanks Phillip ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH] checkout: add --fetch to fetch remote before resolving start-point 2026-05-08 13:15 ` Phillip Wood @ 2026-05-08 22:40 ` Harald Nordgren 0 siblings, 0 replies; 35+ messages in thread From: Harald Nordgren @ 2026-05-08 22:40 UTC (permalink / raw) To: phillip.wood123 Cc: ben.knoble, git, gitgitgadget, haraldnordgren, kristofferhaugsbakk, marcnarc, ramsay > * "--track=inherit,direct" is nonsense and should be rejected > > * currently "--track" has "last one wins" behavior so > "--track=inherit --track=direct" behaves like "--track=direct". We > should probably keep that so that "--track=fetch --track=direct" > behaves like "--track=direct", not "--track=fetch,direct" > > * if "git fetch" fails and the remote tracking ref already exists then > we should print a warning and carry on rather than dying which is more > convenient if the user or remote server are offline. > > * "git checkout --track=fetch origin/branch" should respect > remote.origin.fetch so that we fetch the ref that we're going to > checkout. I wonder if we can share this logic with the code that > sets the upstream branch. > > * "git checkout --track=fetch origin" should only fetch the remote > ref that we're going to checkout, not all the refs from origin. i.e. > it should read origin/HEAD to work out what to fetch. Good points! Harald ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-03 22:31 ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-07 20:12 ` Harald Nordgren @ 2026-05-08 22:52 ` Harald Nordgren via GitGitGadget 2026-05-11 13:16 ` Phillip Wood 2026-05-11 13:47 ` [PATCH v8] " Harald Nordgren via GitGitGadget 1 sibling, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-05-08 22:52 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Phillip Wood, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> If you want to fork your topic branch from the very latest of the tip of a branch your remote has, you would do: git fetch origin some-branch git checkout -b new_branch --track origin/some-branch Extend the "--track" option of "git checkout" and allow users to write git checkout -b new_branch --track=fetch origin/some-branch to (1) fetch 'some-branch' from the remote 'origin', updating the remote-tracking branch 'origin/some-branch', (2) arrange subsequent 'git pull' on 'new_branch' to interact with 'origin/some-branch' and (3) fork 'new_branch' from it. In the value of the '--track' option, 'fetch' can be combined with the existing 'direct' (default) and 'inherit' modes via a comma-separated list. Examples: git checkout -b new_branch --track=fetch,inherit some_local_branch git switch -c new_branch --track=fetch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref, so that other remote-tracking branches are left untouched. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: add --fetch to fetch remote before resolving start-point * Reject --track=inherit,direct as mutually exclusive modes. * Make repeated --track= last-one-wins: --track=fetch --track=direct behaves like --track=direct. * On fetch failure, warn and proceed from the existing remote-tracking ref instead of aborting (friendlier when offline); only abort when there is no existing ref. * For --track=fetch <remote>/<branch>, resolve the source ref through the configured remote.<name>.fetch refspec so custom refspecs fetch the correct ref. * For --track=fetch <remote>, read <remote>/HEAD and fetch only that branch instead of the whole remote. * Tests and docs updated accordingly. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v7 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v7 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v6: 1: 1b42c648b9 ! 1: de375d55f1 checkout: extend --track with a "fetch" mode to refresh start-point @@ Documentation/git-checkout.adoc: of it"). --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and -+`inherit` select the tracking mode. Adding `fetch` requests that the -+remote be fetched before _<start-point>_ is resolved, so the new branch -+starts from a fresh tip: when _<start-point>_ is in -+_<remote>/<branch>_ form, only that branch is updated; when it is a -+bare remote name (e.g. `origin`), the whole remote is fetched. If the -+fetch fails, the checkout is aborted. ++`inherit` select the tracking mode and are mutually exclusive. Adding ++`fetch` requests that the remote be fetched before _<start-point>_ is ++resolved, so the new branch starts from a fresh tip: when ++_<start-point>_ is in _<remote>/<branch>_ form, only that branch is ++updated; when _<start-point>_ is a bare remote name (e.g. `origin`), ++only the remote's default branch is updated. If the fetch fails and the ++corresponding remote-tracking ref already exists, a warning is printed ++and the checkout proceeds from the existing tip; otherwise the checkout ++is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of @@ Documentation/git-switch.adoc: should result in deletion of the path). details. + +The argument is a comma-separated list. `direct` (the default) and -+`inherit` select the tracking mode. Adding `fetch` requests that the -+remote be fetched before _<start-point>_ is resolved, so the new branch -+starts from a fresh tip: when _<start-point>_ is in -+_<remote>/<branch>_ form, only that branch is updated; when it is a -+bare remote name (e.g. `origin`), the whole remote is fetched. If the -+fetch fails, the switch is aborted. ++`inherit` select the tracking mode and are mutually exclusive. Adding ++`fetch` requests that the remote be fetched before _<start-point>_ is ++resolved, so the new branch starts from a fresh tip: when ++_<start-point>_ is in _<remote>/<branch>_ form, only that branch is ++updated; when _<start-point>_ is a bare remote name (e.g. `origin`), ++only the remote's default branch is updated. If the fetch fails and the ++corresponding remote-tracking ref already exists, a warning is printed ++and the switch proceeds from the existing tip; otherwise the switch is ++aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the @@ Documentation/git-switch.adoc: should result in deletion of the path). ## builtin/checkout.c ## @@ + #include "preload-index.h" + #include "read-cache.h" + #include "refs.h" ++#include "refspec.h" + #include "remote.h" #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" @@ builtin/checkout.c: struct branch_info { char *checkout; }; -+static void fetch_remote_for_start_point(const char *arg) ++static int resolve_fetch_target(const char *arg, char **remote_out, ++ char **src_ref_out) +{ + const char *slash; + char *remote_name; + struct remote *remote; -+ struct child_process cmd = CHILD_PROCESS_INIT; ++ struct refspec_item query = { 0 }; ++ struct strbuf dst = STRBUF_INIT; ++ const char *rest; ++ ++ *remote_out = NULL; ++ *src_ref_out = NULL; + + if (!arg || !*arg) -+ return; ++ return -1; + + slash = strchr(arg, '/'); + if (slash == arg) -+ return; ++ return -1; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); ++ return -1; ++ } ++ ++ rest = (slash && slash[1]) ? slash + 1 : NULL; ++ if (!rest) { ++ struct object_id oid; ++ const char *head_target; ++ const char *short_target; ++ ++ strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); ++ head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), ++ dst.buf, ++ RESOLVE_REF_READING | ++ RESOLVE_REF_NO_RECURSE, ++ &oid, NULL); ++ strbuf_reset(&dst); ++ if (head_target && ++ skip_prefix(head_target, "refs/remotes/", &short_target) && ++ skip_prefix(short_target, remote_name, &short_target) && ++ *short_target == '/') ++ rest = short_target + 1; ++ } ++ ++ if (rest) { ++ strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest); ++ query.dst = dst.buf; ++ if (!remote_find_tracking(remote, &query) && query.src) { ++ *src_ref_out = xstrdup(query.src); ++ free(query.src); ++ } else { ++ *src_ref_out = xstrdup(rest); ++ } ++ } ++ ++ strbuf_release(&dst); ++ *remote_out = remote_name; ++ return 0; ++} ++ ++static void fetch_remote_for_start_point(const char *arg) ++{ ++ char *remote_name = NULL; ++ char *src_ref = NULL; ++ struct child_process cmd = CHILD_PROCESS_INIT; ++ struct strbuf dst_ref = STRBUF_INIT; ++ int have_existing_ref = 0; ++ ++ if (resolve_fetch_target(arg, &remote_name, &src_ref)) + return; ++ ++ if (src_ref) { ++ const char *short_src = src_ref; ++ struct object_id oid; ++ ++ skip_prefix(short_src, "refs/heads/", &short_src); ++ strbuf_addf(&dst_ref, "refs/remotes/%s/%s", remote_name, short_src); ++ if (!refs_read_ref(get_main_ref_store(the_repository), ++ dst_ref.buf, &oid)) ++ have_existing_ref = 1; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); -+ if (slash && slash[1]) -+ strvec_push(&cmd.args, slash + 1); ++ if (src_ref) ++ strvec_push(&cmd.args, src_ref); + cmd.git_cmd = 1; ++ if (run_command(&cmd)) { ++ if (have_existing_ref) ++ warning(_("failed to fetch start-point '%s'; " ++ "using existing '%s'"), ++ arg, dst_ref.buf); ++ else ++ die(_("failed to fetch start-point '%s'"), arg); ++ } ++ + free(remote_name); -+ if (run_command(&cmd)) -+ die(_("failed to fetch start-point '%s'"), arg); ++ free(src_ref); ++ strbuf_release(&dst_ref); +} + +static int parse_opt_checkout_track(const struct option *opt, @@ builtin/checkout.c: struct branch_info { + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; ++ int saw_direct = 0, saw_inherit = 0; + int ret = 0; + ++ opts->fetch = 0; ++ + if (unset) { + opts->track = BRANCH_TRACK_NEVER; -+ opts->fetch = 0; + return 0; + } + @@ builtin/checkout.c: struct branch_info { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { ++ saw_direct = 1; + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { ++ saw_inherit = 1; + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); -+ break; ++ goto out; + } + } + ++ if (saw_direct && saw_inherit) ++ ret = error(_("option `%s' cannot combine \"%s\" and \"%s\""), ++ "--track", "direct", "inherit"); ++ ++out: + string_list_clear(&tokens, 0); + return ret; +} @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + -+test_expect_success 'checkout --track=fetch with bare remote name fetches the remote' ' ++test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' ' + git checkout main && -+ git -C fetch_upstream checkout -b fetch_new2 && -+ test_commit -C fetch_upstream u_new2 && -+ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 && ++ git -C fetch_upstream checkout main && ++ git remote set-head fetch_upstream main && ++ git -C fetch_upstream checkout -b fetch_unrelated && ++ test_commit -C fetch_upstream u_unrelated_pre && ++ git fetch fetch_upstream fetch_unrelated && ++ unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) && ++ git -C fetch_upstream checkout main && ++ test_commit -C fetch_upstream u_main_post && ++ git -C fetch_upstream checkout fetch_unrelated && ++ test_commit -C fetch_upstream u_unrelated_post && + git checkout --track=fetch -b local_from_remote fetch_upstream && -+ git rev-parse --verify refs/remotes/fetch_upstream/fetch_new2 ++ test_cmp_rev refs/remotes/fetch_upstream/main HEAD && ++ test "$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)" = "$unrelated_before" +' + -+test_expect_success 'checkout --track=fetch aborts and does not create branch on fetch failure' ' ++test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + ++test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_offline && ++ test_commit -C fetch_upstream u_offline && ++ git fetch fetch_upstream fetch_offline && ++ saved_url=$(git config remote.fetch_upstream.url) && ++ test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" && ++ git config remote.fetch_upstream.url ./does-not-exist && ++ git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err && ++ test_grep "failed to fetch" err && ++ test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD ++' ++ ++test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_refspec && ++ test_commit -C fetch_upstream u_refspec && ++ git fetch fetch_upstream fetch_refspec && ++ git remote add fetch_custom ./fetch_upstream && ++ test_when_finished "git remote remove fetch_custom" && ++ git config --replace-all remote.fetch_custom.fetch \ ++ "+refs/heads/*:refs/remotes/custom-ns/*" && ++ git fetch fetch_custom && ++ test_commit -C fetch_upstream u_refspec_post && ++ git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec && ++ test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD ++' ++ ++test_expect_success 'checkout --track=inherit,direct is rejected' ' ++ test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && ++ test_grep "cannot combine" err ++' ++ ++test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_lastwin && ++ test_commit -C fetch_upstream u_lastwin && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin && ++ test_must_fail git checkout --track=fetch --track=direct \ ++ -b local_lastwin fetch_upstream/fetch_lastwin && ++ test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin ++' ++ +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && Documentation/git-checkout.adoc | 13 ++- Documentation/git-switch.adoc | 13 ++- builtin/checkout.c | 168 +++++++++++++++++++++++++++++++- t/t7201-co.sh | 132 +++++++++++++++++++++++++ 4 files changed, 319 insertions(+), 7 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..28f17f427e 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -158,11 +158,22 @@ of it"). resets _<branch>_ to the start point instead of failing. `-t`:: -`--track[=(direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. See `--track` in linkgit:git-branch[1] for details. As a convenience, --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the checkout proceeds from the existing tip; otherwise the checkout +is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..3f54cf39e9 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -154,11 +154,22 @@ should result in deletion of the path). attached to a terminal, regardless of `--quiet`. `-t`:: -`--track[ (direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. `-c` is implied. See `--track` in linkgit:git-branch[1] for details. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the switch proceeds from the existing tip; otherwise the switch is +aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/builtin/checkout.c b/builtin/checkout.c index e031e61886..8f8d1ecffe 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -26,11 +26,14 @@ #include "preload-index.h" #include "read-cache.h" #include "refs.h" +#include "refspec.h" #include "remote.h" #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" +#include "strvec.h" #include "submodule.h" #include "symlinks.h" #include "trace2.h" @@ -61,6 +64,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -112,6 +116,156 @@ struct branch_info { char *checkout; }; +static int resolve_fetch_target(const char *arg, char **remote_out, + char **src_ref_out) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct refspec_item query = { 0 }; + struct strbuf dst = STRBUF_INIT; + const char *rest; + + *remote_out = NULL; + *src_ref_out = NULL; + + if (!arg || !*arg) + return -1; + + slash = strchr(arg, '/'); + if (slash == arg) + return -1; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return -1; + } + + rest = (slash && slash[1]) ? slash + 1 : NULL; + if (!rest) { + struct object_id oid; + const char *head_target; + const char *short_target; + + strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); + head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + dst.buf, + RESOLVE_REF_READING | + RESOLVE_REF_NO_RECURSE, + &oid, NULL); + strbuf_reset(&dst); + if (head_target && + skip_prefix(head_target, "refs/remotes/", &short_target) && + skip_prefix(short_target, remote_name, &short_target) && + *short_target == '/') + rest = short_target + 1; + } + + if (rest) { + strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest); + query.dst = dst.buf; + if (!remote_find_tracking(remote, &query) && query.src) { + *src_ref_out = xstrdup(query.src); + free(query.src); + } else { + *src_ref_out = xstrdup(rest); + } + } + + strbuf_release(&dst); + *remote_out = remote_name; + return 0; +} + +static void fetch_remote_for_start_point(const char *arg) +{ + char *remote_name = NULL; + char *src_ref = NULL; + struct child_process cmd = CHILD_PROCESS_INIT; + struct strbuf dst_ref = STRBUF_INIT; + int have_existing_ref = 0; + + if (resolve_fetch_target(arg, &remote_name, &src_ref)) + return; + + if (src_ref) { + const char *short_src = src_ref; + struct object_id oid; + + skip_prefix(short_src, "refs/heads/", &short_src); + strbuf_addf(&dst_ref, "refs/remotes/%s/%s", remote_name, short_src); + if (!refs_read_ref(get_main_ref_store(the_repository), + dst_ref.buf, &oid)) + have_existing_ref = 1; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (src_ref) + strvec_push(&cmd.args, src_ref); + cmd.git_cmd = 1; + if (run_command(&cmd)) { + if (have_existing_ref) + warning(_("failed to fetch start-point '%s'; " + "using existing '%s'"), + arg, dst_ref.buf); + else + die(_("failed to fetch start-point '%s'"), arg); + } + + free(remote_name); + free(src_ref); + strbuf_release(&dst_ref); +} + +static int parse_opt_checkout_track(const struct option *opt, + const char *arg, int unset) +{ + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; + int saw_direct = 0, saw_inherit = 0; + int ret = 0; + + opts->fetch = 0; + + if (unset) { + opts->track = BRANCH_TRACK_NEVER; + return 0; + } + + opts->track = BRANCH_TRACK_EXPLICIT; + if (!arg) + return 0; + + string_list_split(&tokens, arg, ",", -1); + for_each_string_list_item(item, &tokens) { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { + saw_direct = 1; + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { + saw_inherit = 1; + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); + goto out; + } + } + + if (saw_direct && saw_inherit) + ret = error(_("option `%s' cannot combine \"%s\" and \"%s\""), + "--track", "direct", "inherit"); + +out: + string_list_clear(&tokens, 0); + return ret; +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1237,7 +1391,6 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } - if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1734,10 +1887,10 @@ static struct option *add_common_switch_branch_options( { struct option options[] = { OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", N_("set branch tracking configuration"), PARSE_OPT_OPTARG, - parse_opt_tracking_mode), + parse_opt_checkout_track), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), PARSE_OPT_NOCOMPLETE), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ -1942,8 +2095,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..19ac6a1a2e 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && + test_cmp_config fetch_upstream branch.local_new.remote && + test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' ' + git checkout main && + git -C fetch_upstream checkout main && + git remote set-head fetch_upstream main && + git -C fetch_upstream checkout -b fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_pre && + git fetch fetch_upstream fetch_unrelated && + unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) && + git -C fetch_upstream checkout main && + test_commit -C fetch_upstream u_main_post && + git -C fetch_upstream checkout fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_post && + git checkout --track=fetch -b local_from_remote fetch_upstream && + test_cmp_rev refs/remotes/fetch_upstream/main HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)" = "$unrelated_before" +' + +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_offline && + test_commit -C fetch_upstream u_offline && + git fetch fetch_upstream fetch_offline && + saved_url=$(git config remote.fetch_upstream.url) && + test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" && + git config remote.fetch_upstream.url ./does-not-exist && + git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err && + test_grep "failed to fetch" err && + test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD +' + +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_refspec && + test_commit -C fetch_upstream u_refspec && + git fetch fetch_upstream fetch_refspec && + git remote add fetch_custom ./fetch_upstream && + test_when_finished "git remote remove fetch_custom" && + git config --replace-all remote.fetch_custom.fetch \ + "+refs/heads/*:refs/remotes/custom-ns/*" && + git fetch fetch_custom && + test_commit -C fetch_upstream u_refspec_post && + git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec && + test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD +' + +test_expect_success 'checkout --track=inherit,direct is rejected' ' + test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && + test_grep "cannot combine" err +' + +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_lastwin && + test_commit -C fetch_upstream u_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin && + test_must_fail git checkout --track=fetch --track=direct \ + -b local_lastwin fetch_upstream/fetch_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin +' + +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && + test_commit -C fetch_upstream u_inherit && + git fetch fetch_upstream fetch_inherit && + git checkout -b base_inherit fetch_upstream/fetch_inherit && + test_commit -C fetch_upstream u_inherit2 && + git checkout main && + git checkout --track=fetch,inherit -b local_inherit base_inherit && + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && + test_cmp_config fetch_upstream branch.local_inherit.remote && + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + +test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && + test_grep "expects" err +' + +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* Re: [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-08 22:52 ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget @ 2026-05-11 13:16 ` Phillip Wood 2026-05-11 13:47 ` [PATCH v8] " Harald Nordgren via GitGitGadget 1 sibling, 0 replies; 35+ messages in thread From: Phillip Wood @ 2026-05-11 13:16 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget, git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Harald Nordgren Hi Harald On 08/05/2026 23:52, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren <haraldnordgren@gmail.com> > > +static void fetch_remote_for_start_point(const char *arg) > +{ > + char *remote_name = NULL; > + char *src_ref = NULL; > + struct child_process cmd = CHILD_PROCESS_INIT; > + struct strbuf dst_ref = STRBUF_INIT; > + int have_existing_ref = 0; > + > + if (resolve_fetch_target(arg, &remote_name, &src_ref)) > + return; > + > + if (src_ref) { > + const char *short_src = src_ref; > + struct object_id oid; > + > + skip_prefix(short_src, "refs/heads/", &short_src); > + strbuf_addf(&dst_ref, "refs/remotes/%s/%s", remote_name, short_src); > + if (!refs_read_ref(get_main_ref_store(the_repository), > + dst_ref.buf, &oid)) > + have_existing_ref = 1; src_ref is the name of the branch on the remote server, not the name of the remote tracking ref which is given by arg. If arg is a remote name then we need to resolve refs/remotes/$arg/HEAD to find the branch to check, otherwise we should be checking refs/remotes/$arg I've only given this version a quick scan through, but I didn't notice any other issues. Thanks Phillip > + } > + > + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); > + if (src_ref) > + strvec_push(&cmd.args, src_ref); > + cmd.git_cmd = 1; > + if (run_command(&cmd)) { > + if (have_existing_ref) > + warning(_("failed to fetch start-point '%s'; " > + "using existing '%s'"), > + arg, dst_ref.buf); > + else > + die(_("failed to fetch start-point '%s'"), arg); > + } > + > + free(remote_name); > + free(src_ref); > + strbuf_release(&dst_ref); > +} > + > +static int parse_opt_checkout_track(const struct option *opt, > + const char *arg, int unset) > +{ > + struct checkout_opts *opts = opt->value; > + struct string_list tokens = STRING_LIST_INIT_DUP; > + struct string_list_item *item; > + int saw_direct = 0, saw_inherit = 0; > + int ret = 0; > + > + opts->fetch = 0; > + > + if (unset) { > + opts->track = BRANCH_TRACK_NEVER; > + return 0; > + } > + > + opts->track = BRANCH_TRACK_EXPLICIT; > + if (!arg) > + return 0; > + > + string_list_split(&tokens, arg, ",", -1); > + for_each_string_list_item(item, &tokens) { > + if (!strcmp(item->string, "fetch")) { > + opts->fetch = 1; > + } else if (!strcmp(item->string, "direct")) { > + saw_direct = 1; > + opts->track = BRANCH_TRACK_EXPLICIT; > + } else if (!strcmp(item->string, "inherit")) { > + saw_inherit = 1; > + opts->track = BRANCH_TRACK_INHERIT; > + } else { > + ret = error(_("option `%s' expects \"%s\", \"%s\", " > + "or \"%s\""), > + "--track", "direct", "inherit", "fetch"); > + goto out; > + } > + } > + > + if (saw_direct && saw_inherit) > + ret = error(_("option `%s' cannot combine \"%s\" and \"%s\""), > + "--track", "direct", "inherit"); > + > +out: > + string_list_clear(&tokens, 0); > + return ret; > +} > + > static void branch_info_release(struct branch_info *info) > { > free(info->name); > @@ -1237,7 +1391,6 @@ static int git_checkout_config(const char *var, const char *value, > opts->dwim_new_local_branch = git_config_bool(var, value); > return 0; > } > - > if (starts_with(var, "submodule.")) > return git_default_submodule_config(var, value, NULL); > > @@ -1734,10 +1887,10 @@ static struct option *add_common_switch_branch_options( > { > struct option options[] = { > OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), > - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", > + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", > N_("set branch tracking configuration"), > PARSE_OPT_OPTARG, > - parse_opt_tracking_mode), > + parse_opt_checkout_track), > OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), > PARSE_OPT_NOCOMPLETE), > OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), > @@ -1942,8 +2095,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, > opts->dwim_new_local_branch && > opts->track == BRANCH_TRACK_UNSPECIFIED && > !opts->new_branch; > - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, > - &new_branch_info, opts, &rev); > + int n; > + > + if (opts->fetch) > + fetch_remote_for_start_point(argv[0]); > + > + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, > + &new_branch_info, opts, &rev); > argv += n; > argc -= n; > } else if (!opts->accept_ref && opts->from_treeish) { > diff --git a/t/t7201-co.sh b/t/t7201-co.sh > index 9bcf7c0b40..19ac6a1a2e 100755 > --- a/t/t7201-co.sh > +++ b/t/t7201-co.sh > @@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' > test_cmp_config "" --default "" branch.main2.merge > ' > > +test_expect_success 'setup upstream for --track=fetch tests' ' > + git checkout main && > + git init fetch_upstream && > + test_commit -C fetch_upstream u_main && > + git remote add fetch_upstream fetch_upstream && > + git fetch fetch_upstream && > + git -C fetch_upstream checkout -b fetch_new && > + test_commit -C fetch_upstream u_new > +' > + > +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' > + git checkout main && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && > + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && > + test_cmp_config fetch_upstream branch.local_new.remote && > + test_cmp_config refs/heads/fetch_new branch.local_new.merge > +' > + > +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_target && > + test_commit -C fetch_upstream u_target_pre && > + git -C fetch_upstream checkout -b fetch_other && > + test_commit -C fetch_upstream u_other_pre && > + git fetch fetch_upstream && > + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && > + git -C fetch_upstream checkout fetch_target && > + test_commit -C fetch_upstream u_target_post && > + git -C fetch_upstream checkout fetch_other && > + test_commit -C fetch_upstream u_other_post && > + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && > + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" > +' > + > +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' ' > + git checkout main && > + git -C fetch_upstream checkout main && > + git remote set-head fetch_upstream main && > + git -C fetch_upstream checkout -b fetch_unrelated && > + test_commit -C fetch_upstream u_unrelated_pre && > + git fetch fetch_upstream fetch_unrelated && > + unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) && > + git -C fetch_upstream checkout main && > + test_commit -C fetch_upstream u_main_post && > + git -C fetch_upstream checkout fetch_unrelated && > + test_commit -C fetch_upstream u_unrelated_post && > + git checkout --track=fetch -b local_from_remote fetch_upstream && > + test_cmp_rev refs/remotes/fetch_upstream/main HEAD && > + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)" = "$unrelated_before" > +' > + > +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' ' > + git checkout main && > + test_might_fail git branch -D bogus && > + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && > + test_must_fail git rev-parse --verify refs/heads/bogus > +' > + > +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_offline && > + test_commit -C fetch_upstream u_offline && > + git fetch fetch_upstream fetch_offline && > + saved_url=$(git config remote.fetch_upstream.url) && > + test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" && > + git config remote.fetch_upstream.url ./does-not-exist && > + git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err && > + test_grep "failed to fetch" err && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD > +' > + > +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_refspec && > + test_commit -C fetch_upstream u_refspec && > + git fetch fetch_upstream fetch_refspec && > + git remote add fetch_custom ./fetch_upstream && > + test_when_finished "git remote remove fetch_custom" && > + git config --replace-all remote.fetch_custom.fetch \ > + "+refs/heads/*:refs/remotes/custom-ns/*" && > + git fetch fetch_custom && > + test_commit -C fetch_upstream u_refspec_post && > + git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec && > + test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD > +' > + > +test_expect_success 'checkout --track=inherit,direct is rejected' ' > + test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && > + test_grep "cannot combine" err > +' > + > +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_lastwin && > + test_commit -C fetch_upstream u_lastwin && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin && > + test_must_fail git checkout --track=fetch --track=direct \ > + -b local_lastwin fetch_upstream/fetch_lastwin && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin > +' > + > +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_inherit && > + test_commit -C fetch_upstream u_inherit && > + git fetch fetch_upstream fetch_inherit && > + git checkout -b base_inherit fetch_upstream/fetch_inherit && > + test_commit -C fetch_upstream u_inherit2 && > + git checkout main && > + git checkout --track=fetch,inherit -b local_inherit base_inherit && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && > + test_cmp_config fetch_upstream branch.local_inherit.remote && > + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge > +' > + > +test_expect_success 'checkout --track=bogus reports an error' ' > + git checkout main && > + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && > + test_grep "expects" err > +' > + > +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' > + git checkout main && > + git -C fetch_upstream checkout -b fetch_switch && > + test_commit -C fetch_upstream u_switch && > + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && > + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && > + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD > +' > + > test_done > > base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v8] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-08 22:52 ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-11 13:16 ` Phillip Wood @ 2026-05-11 13:47 ` Harald Nordgren via GitGitGadget 2026-05-12 0:32 ` Junio C Hamano 2026-05-12 10:55 ` [PATCH v9] " Harald Nordgren via GitGitGadget 1 sibling, 2 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-05-11 13:47 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Phillip Wood, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> If you want to fork your topic branch from the very latest of the tip of a branch your remote has, you would do: git fetch origin some-branch git checkout -b new_branch --track origin/some-branch Extend the "--track" option of "git checkout" and allow users to write git checkout -b new_branch --track=fetch origin/some-branch to (1) fetch 'some-branch' from the remote 'origin', updating the remote-tracking branch 'origin/some-branch', (2) arrange subsequent 'git pull' on 'new_branch' to interact with 'origin/some-branch' and (3) fork 'new_branch' from it. In the value of the '--track' option, 'fetch' can be combined with the existing 'direct' (default) and 'inherit' modes via a comma-separated list. Examples: git checkout -b new_branch --track=fetch,inherit some_local_branch git switch -c new_branch --track=fetch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref, so that other remote-tracking branches are left untouched. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: --track=fetch Check the fallback ref using arg directly instead of reconstructing it. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v8 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v8 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v7: 1: de375d55f1 ! 1: 61c2199fd5 checkout: extend --track with a "fetch" mode to refresh start-point @@ builtin/checkout.c #include "revision.h" +#include "run-command.h" #include "setup.h" -+#include "strvec.h" + #include "strvec.h" #include "submodule.h" - #include "symlinks.h" - #include "trace2.h" @@ builtin/checkout.c: struct checkout_opts { int count_checkout_paths; int overlay_mode; @@ builtin/checkout.c: struct branch_info { + if (resolve_fetch_target(arg, &remote_name, &src_ref)) + return; + -+ if (src_ref) { -+ const char *short_src = src_ref; ++ { + struct object_id oid; + -+ skip_prefix(short_src, "refs/heads/", &short_src); -+ strbuf_addf(&dst_ref, "refs/remotes/%s/%s", remote_name, short_src); ++ if (strchr(arg, '/')) ++ strbuf_addf(&dst_ref, "refs/remotes/%s", arg); ++ else ++ strbuf_addf(&dst_ref, "refs/remotes/%s/HEAD", arg); + if (!refs_read_ref(get_main_ref_store(the_repository), + dst_ref.buf, &oid)) + have_existing_ref = 1; Documentation/git-checkout.adoc | 13 ++- Documentation/git-switch.adoc | 13 ++- builtin/checkout.c | 168 +++++++++++++++++++++++++++++++- t/t7201-co.sh | 132 +++++++++++++++++++++++++ 4 files changed, 319 insertions(+), 7 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..28f17f427e 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -158,11 +158,22 @@ of it"). resets _<branch>_ to the start point instead of failing. `-t`:: -`--track[=(direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. See `--track` in linkgit:git-branch[1] for details. As a convenience, --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the checkout proceeds from the existing tip; otherwise the checkout +is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..3f54cf39e9 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -154,11 +154,22 @@ should result in deletion of the path). attached to a terminal, regardless of `--quiet`. `-t`:: -`--track[ (direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. `-c` is implied. See `--track` in linkgit:git-branch[1] for details. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the switch proceeds from the existing tip; otherwise the switch is +aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/builtin/checkout.c b/builtin/checkout.c index ac0186a33e..157242bc9f 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -26,10 +26,12 @@ #include "preload-index.h" #include "read-cache.h" #include "refs.h" +#include "refspec.h" #include "remote.h" #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" #include "strvec.h" #include "submodule.h" @@ -62,6 +64,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -113,6 +116,157 @@ struct branch_info { char *checkout; }; +static int resolve_fetch_target(const char *arg, char **remote_out, + char **src_ref_out) +{ + const char *slash; + char *remote_name; + struct remote *remote; + struct refspec_item query = { 0 }; + struct strbuf dst = STRBUF_INIT; + const char *rest; + + *remote_out = NULL; + *src_ref_out = NULL; + + if (!arg || !*arg) + return -1; + + slash = strchr(arg, '/'); + if (slash == arg) + return -1; + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + + remote = remote_get(remote_name); + if (!remote || !remote_is_configured(remote, 1)) { + free(remote_name); + return -1; + } + + rest = (slash && slash[1]) ? slash + 1 : NULL; + if (!rest) { + struct object_id oid; + const char *head_target; + const char *short_target; + + strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); + head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + dst.buf, + RESOLVE_REF_READING | + RESOLVE_REF_NO_RECURSE, + &oid, NULL); + strbuf_reset(&dst); + if (head_target && + skip_prefix(head_target, "refs/remotes/", &short_target) && + skip_prefix(short_target, remote_name, &short_target) && + *short_target == '/') + rest = short_target + 1; + } + + if (rest) { + strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest); + query.dst = dst.buf; + if (!remote_find_tracking(remote, &query) && query.src) { + *src_ref_out = xstrdup(query.src); + free(query.src); + } else { + *src_ref_out = xstrdup(rest); + } + } + + strbuf_release(&dst); + *remote_out = remote_name; + return 0; +} + +static void fetch_remote_for_start_point(const char *arg) +{ + char *remote_name = NULL; + char *src_ref = NULL; + struct child_process cmd = CHILD_PROCESS_INIT; + struct strbuf dst_ref = STRBUF_INIT; + int have_existing_ref = 0; + + if (resolve_fetch_target(arg, &remote_name, &src_ref)) + return; + + { + struct object_id oid; + + if (strchr(arg, '/')) + strbuf_addf(&dst_ref, "refs/remotes/%s", arg); + else + strbuf_addf(&dst_ref, "refs/remotes/%s/HEAD", arg); + if (!refs_read_ref(get_main_ref_store(the_repository), + dst_ref.buf, &oid)) + have_existing_ref = 1; + } + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (src_ref) + strvec_push(&cmd.args, src_ref); + cmd.git_cmd = 1; + if (run_command(&cmd)) { + if (have_existing_ref) + warning(_("failed to fetch start-point '%s'; " + "using existing '%s'"), + arg, dst_ref.buf); + else + die(_("failed to fetch start-point '%s'"), arg); + } + + free(remote_name); + free(src_ref); + strbuf_release(&dst_ref); +} + +static int parse_opt_checkout_track(const struct option *opt, + const char *arg, int unset) +{ + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; + int saw_direct = 0, saw_inherit = 0; + int ret = 0; + + opts->fetch = 0; + + if (unset) { + opts->track = BRANCH_TRACK_NEVER; + return 0; + } + + opts->track = BRANCH_TRACK_EXPLICIT; + if (!arg) + return 0; + + string_list_split(&tokens, arg, ",", -1); + for_each_string_list_item(item, &tokens) { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { + saw_direct = 1; + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { + saw_inherit = 1; + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); + goto out; + } + } + + if (saw_direct && saw_inherit) + ret = error(_("option `%s' cannot combine \"%s\" and \"%s\""), + "--track", "direct", "inherit"); + +out: + string_list_clear(&tokens, 0); + return ret; +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value, opts->dwim_new_local_branch = git_config_bool(var, value); return 0; } - if (starts_with(var, "submodule.")) return git_default_submodule_config(var, value, NULL); @@ -1741,10 +1894,10 @@ static struct option *add_common_switch_branch_options( { struct option options[] = { OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", N_("set branch tracking configuration"), PARSE_OPT_OPTARG, - parse_opt_tracking_mode), + parse_opt_checkout_track), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), PARSE_OPT_NOCOMPLETE), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ -1949,8 +2102,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..19ac6a1a2e 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,136 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && + test_cmp_config fetch_upstream branch.local_new.remote && + test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' ' + git checkout main && + git -C fetch_upstream checkout main && + git remote set-head fetch_upstream main && + git -C fetch_upstream checkout -b fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_pre && + git fetch fetch_upstream fetch_unrelated && + unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) && + git -C fetch_upstream checkout main && + test_commit -C fetch_upstream u_main_post && + git -C fetch_upstream checkout fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_post && + git checkout --track=fetch -b local_from_remote fetch_upstream && + test_cmp_rev refs/remotes/fetch_upstream/main HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)" = "$unrelated_before" +' + +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_offline && + test_commit -C fetch_upstream u_offline && + git fetch fetch_upstream fetch_offline && + saved_url=$(git config remote.fetch_upstream.url) && + test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" && + git config remote.fetch_upstream.url ./does-not-exist && + git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err && + test_grep "failed to fetch" err && + test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD +' + +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_refspec && + test_commit -C fetch_upstream u_refspec && + git fetch fetch_upstream fetch_refspec && + git remote add fetch_custom ./fetch_upstream && + test_when_finished "git remote remove fetch_custom" && + git config --replace-all remote.fetch_custom.fetch \ + "+refs/heads/*:refs/remotes/custom-ns/*" && + git fetch fetch_custom && + test_commit -C fetch_upstream u_refspec_post && + git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec && + test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD +' + +test_expect_success 'checkout --track=inherit,direct is rejected' ' + test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && + test_grep "cannot combine" err +' + +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_lastwin && + test_commit -C fetch_upstream u_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin && + test_must_fail git checkout --track=fetch --track=direct \ + -b local_lastwin fetch_upstream/fetch_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin +' + +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && + test_commit -C fetch_upstream u_inherit && + git fetch fetch_upstream fetch_inherit && + git checkout -b base_inherit fetch_upstream/fetch_inherit && + test_commit -C fetch_upstream u_inherit2 && + git checkout main && + git checkout --track=fetch,inherit -b local_inherit base_inherit && + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && + test_cmp_config fetch_upstream branch.local_inherit.remote && + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + +test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && + test_grep "expects" err +' + +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done base-commit: 7760f83b59750c27df653c5c46d0f80e44cfe02c -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
* Re: [PATCH v8] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-11 13:47 ` [PATCH v8] " Harald Nordgren via GitGitGadget @ 2026-05-12 0:32 ` Junio C Hamano 2026-05-12 10:55 ` [PATCH v9] " Harald Nordgren via GitGitGadget 1 sibling, 0 replies; 35+ messages in thread From: Junio C Hamano @ 2026-05-12 0:32 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget Cc: git, Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Phillip Wood, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc > index 43ccf47cf6..28f17f427e 100644 > --- a/Documentation/git-checkout.adoc > +++ b/Documentation/git-checkout.adoc > @@ -158,11 +158,22 @@ of it"). > resets _<branch>_ to the start point instead of failing. > > `-t`:: > -`--track[=(direct|inherit)]`:: > +`--track[=(direct|inherit|fetch)[,...]]`:: > When creating a new branch, set up "upstream" configuration. See > `--track` in linkgit:git-branch[1] for details. As a convenience, > --track without -b implies branch creation. > + > +The argument is a comma-separated list. `direct` (the default) and > +`inherit` select the tracking mode and are mutually exclusive. Adding > +`fetch` requests that the remote be fetched before _<start-point>_ is > +resolved, so the new branch starts from a fresh tip: when > +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is > +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), > +only the remote's default branch is updated. The latter is because "checkout -t -b new remote/origin" makes "new" track "remote/origin/HEAD", which makes sense. And of course this fetch can fail. > +static int resolve_fetch_target(const char *arg, char **remote_out, > + char **src_ref_out) > +{ > + const char *slash; > + char *remote_name; > + struct remote *remote; > + struct refspec_item query = { 0 }; > + struct strbuf dst = STRBUF_INIT; > + const char *rest; > + > + *remote_out = NULL; > + *src_ref_out = NULL; > + > + if (!arg || !*arg) > + return -1; > + > + slash = strchr(arg, '/'); > + if (slash == arg) > + return -1; > + remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); > + > + remote = remote_get(remote_name); > + if (!remote || !remote_is_configured(remote, 1)) { > + free(remote_name); > + return -1; > + } > + > + rest = (slash && slash[1]) ? slash + 1 : NULL; There is no slash when asking for "origin", and the control goes into the "if (!rest)" block. > + if (!rest) { > + struct object_id oid; > + const char *head_target; > + const char *short_target; > + > + strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); > + head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), > + dst.buf, > + RESOLVE_REF_READING | > + RESOLVE_REF_NO_RECURSE, > + &oid, NULL); > + strbuf_reset(&dst); > + if (head_target && > + skip_prefix(head_target, "refs/remotes/", &short_target) && > + skip_prefix(short_target, remote_name, &short_target) && > + *short_target == '/') > + rest = short_target + 1; > + } If refs/remotes/origin/HEAD points at refs/remotes/origin/main, rest gets "main". Otherwise (i.e. unexpected contents in HEAD or lacking HEAD), rest remains NULL. And then we have the "if (rest)" block. > + if (rest) { > + strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest); > + query.dst = dst.buf; > + if (!remote_find_tracking(remote, &query) && query.src) { > + *src_ref_out = xstrdup(query.src); > + free(query.src); > + } else { > + *src_ref_out = xstrdup(rest); > + } > + } > + > + strbuf_release(&dst); > + *remote_out = remote_name; > + return 0; > +} It is not a new problem but I do not remember how we explicitly forbid "hierarchical" remote names. refs/remotes/a/b/c might be their branch "b/c" at remote we call "a", or branch "c" at remote "a/b". Unless we forbid slashes in remote names, the "there is no slash so we got only the name of the remote to mean its HEAD" logic would not work well, so we may want to double check. > +static void fetch_remote_for_start_point(const char *arg) > +{ > + char *remote_name = NULL; > + char *src_ref = NULL; > + struct child_process cmd = CHILD_PROCESS_INIT; > + struct strbuf dst_ref = STRBUF_INIT; > + int have_existing_ref = 0; > + > + if (resolve_fetch_target(arg, &remote_name, &src_ref)) > + return; > + > + { > + struct object_id oid; > + > + if (strchr(arg, '/')) > + strbuf_addf(&dst_ref, "refs/remotes/%s", arg); > + else > + strbuf_addf(&dst_ref, "refs/remotes/%s/HEAD", arg); > + if (!refs_read_ref(get_main_ref_store(the_repository), > + dst_ref.buf, &oid)) > + have_existing_ref = 1; > + } I do not quite see the point of this extra block. Can we do without it (and move the def of oid up near the beginning of the function, of course)? Even better, as resolve_fetch_target() already looks at "arg" and poked at the remote-tracking ref hierarchy, wouldn't it make more sense to make that helper function responsible for finding out if there already is a usable, albeit potentially stale, ref? > + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); > + if (src_ref) > + strvec_push(&cmd.args, src_ref); > + cmd.git_cmd = 1; > + if (run_command(&cmd)) { > + if (have_existing_ref) > + warning(_("failed to fetch start-point '%s'; " > + "using existing '%s'"), > + arg, dst_ref.buf); > + else > + die(_("failed to fetch start-point '%s'"), arg); > + } > + > + free(remote_name); > + free(src_ref); > + strbuf_release(&dst_ref); > +} > @@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value, > opts->dwim_new_local_branch = git_config_bool(var, value); > return 0; > } > - > if (starts_with(var, "submodule.")) > return git_default_submodule_config(var, value, NULL); Unrelated patch noise? ^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v9] checkout: extend --track with a "fetch" mode to refresh start-point 2026-05-11 13:47 ` [PATCH v8] " Harald Nordgren via GitGitGadget 2026-05-12 0:32 ` Junio C Hamano @ 2026-05-12 10:55 ` Harald Nordgren via GitGitGadget 1 sibling, 0 replies; 35+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-05-12 10:55 UTC (permalink / raw) To: git Cc: Ramsay Jones, D. Ben Knoble, Kristoffer Haugsbakk, Marc Branchaud, Phillip Wood, Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> If you want to fork your topic branch from the very latest of the tip of a branch your remote has, you would do: git fetch origin some-branch git checkout -b new_branch --track origin/some-branch Extend the "--track" option of "git checkout" and allow users to write git checkout -b new_branch --track=fetch origin/some-branch to (1) fetch 'some-branch' from the remote 'origin', updating the remote-tracking branch 'origin/some-branch', (2) arrange subsequent 'git pull' on 'new_branch' to interact with 'origin/some-branch' and (3) fork 'new_branch' from it. In the value of the '--track' option, 'fetch' can be combined with the existing 'direct' (default) and 'inherit' modes via a comma-separated list. Examples: git checkout -b new_branch --track=fetch,inherit some_local_branch git switch -c new_branch --track=fetch origin/some-branch When "fetch" is requested and <start-point> is in <remote>/<branch> form, run "git fetch <remote> <branch>" before resolving the ref, so that other remote-tracking branches are left untouched. If <start-point> is a bare remote name like "origin" (which resolves to that remote's default branch), "git fetch <remote>" is run instead, since the target branch is not known up front. Abort the checkout if the fetch fails. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- checkout: --track=fetch * Support hierarchical remote names (e.g. nested/remote) by trying the longest prefix first. * Fold the existing-ref lookup into resolve_fetch_target() so it returns the ref name directly. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2281%2FHaraldNordgren%2Fcheckout-fetch-start-point-v9 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2281/HaraldNordgren/checkout-fetch-start-point-v9 Pull-Request: https://github.com/git/git/pull/2281 Range-diff vs v8: 1: 61c2199fd5 ! 1: 021375e4cc checkout: extend --track with a "fetch" mode to refresh start-point @@ builtin/checkout.c: struct branch_info { }; +static int resolve_fetch_target(const char *arg, char **remote_out, -+ char **src_ref_out) ++ char **src_ref_out, char **existing_ref_out) +{ + const char *slash; -+ char *remote_name; -+ struct remote *remote; ++ char *remote_name = NULL; ++ struct remote *remote = NULL; + struct refspec_item query = { 0 }; + struct strbuf dst = STRBUF_INIT; -+ const char *rest; ++ struct object_id oid; ++ const char *rest = NULL; ++ const char *head_target = NULL; ++ const char *short_target; + + *remote_out = NULL; + *src_ref_out = NULL; ++ *existing_ref_out = NULL; + -+ if (!arg || !*arg) -+ return -1; -+ -+ slash = strchr(arg, '/'); -+ if (slash == arg) ++ if (!arg || !*arg || *arg == '/') + return -1; -+ remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg); + -+ remote = remote_get(remote_name); -+ if (!remote || !remote_is_configured(remote, 1)) { ++ slash = arg + strlen(arg); ++ while (1) { + free(remote_name); -+ return -1; ++ remote_name = xstrndup(arg, slash - arg); ++ remote = remote_get(remote_name); ++ if (remote && remote_is_configured(remote, 1)) ++ break; ++ while (slash > arg && *--slash != '/') ++ ; ++ if (slash == arg) { ++ free(remote_name); ++ return -1; ++ } + } + -+ rest = (slash && slash[1]) ? slash + 1 : NULL; ++ if (*slash == '/' && slash[1]) ++ rest = slash + 1; + if (!rest) { -+ struct object_id oid; -+ const char *head_target; -+ const char *short_target; -+ + strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); + head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + dst.buf, + RESOLVE_REF_READING | + RESOLVE_REF_NO_RECURSE, + &oid, NULL); ++ if (head_target) { ++ *existing_ref_out = xstrdup(dst.buf); ++ if (skip_prefix(head_target, "refs/remotes/", &short_target) && ++ skip_prefix(short_target, remote_name, &short_target) && ++ *short_target == '/') ++ rest = short_target + 1; ++ } + strbuf_reset(&dst); -+ if (head_target && -+ skip_prefix(head_target, "refs/remotes/", &short_target) && -+ skip_prefix(short_target, remote_name, &short_target) && -+ *short_target == '/') -+ rest = short_target + 1; + } + + if (rest) { @@ builtin/checkout.c: struct branch_info { + } else { + *src_ref_out = xstrdup(rest); + } ++ if (!*existing_ref_out) { ++ strbuf_reset(&dst); ++ strbuf_addf(&dst, "refs/remotes/%s", arg); ++ if (!refs_read_ref(get_main_ref_store(the_repository), ++ dst.buf, &oid)) ++ *existing_ref_out = xstrdup(dst.buf); ++ } + } + + strbuf_release(&dst); @@ builtin/checkout.c: struct branch_info { +{ + char *remote_name = NULL; + char *src_ref = NULL; ++ char *existing_ref = NULL; + struct child_process cmd = CHILD_PROCESS_INIT; -+ struct strbuf dst_ref = STRBUF_INIT; -+ int have_existing_ref = 0; + -+ if (resolve_fetch_target(arg, &remote_name, &src_ref)) ++ if (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref)) + return; + -+ { -+ struct object_id oid; -+ -+ if (strchr(arg, '/')) -+ strbuf_addf(&dst_ref, "refs/remotes/%s", arg); -+ else -+ strbuf_addf(&dst_ref, "refs/remotes/%s/HEAD", arg); -+ if (!refs_read_ref(get_main_ref_store(the_repository), -+ dst_ref.buf, &oid)) -+ have_existing_ref = 1; -+ } -+ + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (src_ref) + strvec_push(&cmd.args, src_ref); + cmd.git_cmd = 1; + if (run_command(&cmd)) { -+ if (have_existing_ref) ++ if (existing_ref) + warning(_("failed to fetch start-point '%s'; " + "using existing '%s'"), -+ arg, dst_ref.buf); ++ arg, existing_ref); + else + die(_("failed to fetch start-point '%s'"), arg); + } + + free(remote_name); + free(src_ref); -+ strbuf_release(&dst_ref); ++ free(existing_ref); +} + +static int parse_opt_checkout_track(const struct option *opt, @@ builtin/checkout.c: struct branch_info { static void branch_info_release(struct branch_info *info) { free(info->name); -@@ builtin/checkout.c: static int git_checkout_config(const char *var, const char *value, - opts->dwim_new_local_branch = git_config_bool(var, value); - return 0; - } -- - if (starts_with(var, "submodule.")) - return git_default_submodule_config(var, value, NULL); - @@ builtin/checkout.c: static struct option *add_common_switch_branch_options( { struct option options[] = { @@ t/t7201-co.sh: test_expect_success 'tracking info copied with autoSetupMerge=inh + test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD +' + ++test_expect_success 'checkout --track=fetch handles hierarchical remote name' ' ++ git checkout main && ++ git -C fetch_upstream checkout -b fetch_hier && ++ test_commit -C fetch_upstream u_hier && ++ git remote add nested/remote ./fetch_upstream && ++ test_when_finished "git remote remove nested/remote" && ++ git fetch nested/remote fetch_hier && ++ test_commit -C fetch_upstream u_hier_post && ++ git checkout --track=fetch -b local_hier nested/remote/fetch_hier && ++ test_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD ++' ++ +test_expect_success 'checkout --track=inherit,direct is rejected' ' + test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && + test_grep "cannot combine" err Documentation/git-checkout.adoc | 13 ++- Documentation/git-switch.adoc | 13 ++- builtin/checkout.c | 168 +++++++++++++++++++++++++++++++- t/t7201-co.sh | 144 +++++++++++++++++++++++++++ 4 files changed, 332 insertions(+), 6 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..28f17f427e 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -158,11 +158,22 @@ of it"). resets _<branch>_ to the start point instead of failing. `-t`:: -`--track[=(direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. See `--track` in linkgit:git-branch[1] for details. As a convenience, --track without -b implies branch creation. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the checkout proceeds from the existing tip; otherwise the checkout +is aborted. ++ If no `-b` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..3f54cf39e9 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -154,11 +154,22 @@ should result in deletion of the path). attached to a terminal, regardless of `--quiet`. `-t`:: -`--track[ (direct|inherit)]`:: +`--track[=(direct|inherit|fetch)[,...]]`:: When creating a new branch, set up "upstream" configuration. `-c` is implied. See `--track` in linkgit:git-branch[1] for details. + +The argument is a comma-separated list. `direct` (the default) and +`inherit` select the tracking mode and are mutually exclusive. Adding +`fetch` requests that the remote be fetched before _<start-point>_ is +resolved, so the new branch starts from a fresh tip: when +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is +updated; when _<start-point>_ is a bare remote name (e.g. `origin`), +only the remote's default branch is updated. If the fetch fails and the +corresponding remote-tracking ref already exists, a warning is printed +and the switch proceeds from the existing tip; otherwise the switch is +aborted. ++ If no `-c` option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping diff --git a/builtin/checkout.c b/builtin/checkout.c index ac0186a33e..aff442c526 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -26,10 +26,12 @@ #include "preload-index.h" #include "read-cache.h" #include "refs.h" +#include "refspec.h" #include "remote.h" #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "run-command.h" #include "setup.h" #include "strvec.h" #include "submodule.h" @@ -62,6 +64,7 @@ struct checkout_opts { int count_checkout_paths; int overlay_mode; int dwim_new_local_branch; + int fetch; int discard_changes; int accept_ref; int accept_pathspec; @@ -113,6 +116,158 @@ struct branch_info { char *checkout; }; +static int resolve_fetch_target(const char *arg, char **remote_out, + char **src_ref_out, char **existing_ref_out) +{ + const char *slash; + char *remote_name = NULL; + struct remote *remote = NULL; + struct refspec_item query = { 0 }; + struct strbuf dst = STRBUF_INIT; + struct object_id oid; + const char *rest = NULL; + const char *head_target = NULL; + const char *short_target; + + *remote_out = NULL; + *src_ref_out = NULL; + *existing_ref_out = NULL; + + if (!arg || !*arg || *arg == '/') + return -1; + + slash = arg + strlen(arg); + while (1) { + free(remote_name); + remote_name = xstrndup(arg, slash - arg); + remote = remote_get(remote_name); + if (remote && remote_is_configured(remote, 1)) + break; + while (slash > arg && *--slash != '/') + ; + if (slash == arg) { + free(remote_name); + return -1; + } + } + + if (*slash == '/' && slash[1]) + rest = slash + 1; + if (!rest) { + strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name); + head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + dst.buf, + RESOLVE_REF_READING | + RESOLVE_REF_NO_RECURSE, + &oid, NULL); + if (head_target) { + *existing_ref_out = xstrdup(dst.buf); + if (skip_prefix(head_target, "refs/remotes/", &short_target) && + skip_prefix(short_target, remote_name, &short_target) && + *short_target == '/') + rest = short_target + 1; + } + strbuf_reset(&dst); + } + + if (rest) { + strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest); + query.dst = dst.buf; + if (!remote_find_tracking(remote, &query) && query.src) { + *src_ref_out = xstrdup(query.src); + free(query.src); + } else { + *src_ref_out = xstrdup(rest); + } + if (!*existing_ref_out) { + strbuf_reset(&dst); + strbuf_addf(&dst, "refs/remotes/%s", arg); + if (!refs_read_ref(get_main_ref_store(the_repository), + dst.buf, &oid)) + *existing_ref_out = xstrdup(dst.buf); + } + } + + strbuf_release(&dst); + *remote_out = remote_name; + return 0; +} + +static void fetch_remote_for_start_point(const char *arg) +{ + char *remote_name = NULL; + char *src_ref = NULL; + char *existing_ref = NULL; + struct child_process cmd = CHILD_PROCESS_INIT; + + if (resolve_fetch_target(arg, &remote_name, &src_ref, &existing_ref)) + return; + + strvec_pushl(&cmd.args, "fetch", remote_name, NULL); + if (src_ref) + strvec_push(&cmd.args, src_ref); + cmd.git_cmd = 1; + if (run_command(&cmd)) { + if (existing_ref) + warning(_("failed to fetch start-point '%s'; " + "using existing '%s'"), + arg, existing_ref); + else + die(_("failed to fetch start-point '%s'"), arg); + } + + free(remote_name); + free(src_ref); + free(existing_ref); +} + +static int parse_opt_checkout_track(const struct option *opt, + const char *arg, int unset) +{ + struct checkout_opts *opts = opt->value; + struct string_list tokens = STRING_LIST_INIT_DUP; + struct string_list_item *item; + int saw_direct = 0, saw_inherit = 0; + int ret = 0; + + opts->fetch = 0; + + if (unset) { + opts->track = BRANCH_TRACK_NEVER; + return 0; + } + + opts->track = BRANCH_TRACK_EXPLICIT; + if (!arg) + return 0; + + string_list_split(&tokens, arg, ",", -1); + for_each_string_list_item(item, &tokens) { + if (!strcmp(item->string, "fetch")) { + opts->fetch = 1; + } else if (!strcmp(item->string, "direct")) { + saw_direct = 1; + opts->track = BRANCH_TRACK_EXPLICIT; + } else if (!strcmp(item->string, "inherit")) { + saw_inherit = 1; + opts->track = BRANCH_TRACK_INHERIT; + } else { + ret = error(_("option `%s' expects \"%s\", \"%s\", " + "or \"%s\""), + "--track", "direct", "inherit", "fetch"); + goto out; + } + } + + if (saw_direct && saw_inherit) + ret = error(_("option `%s' cannot combine \"%s\" and \"%s\""), + "--track", "direct", "inherit"); + +out: + string_list_clear(&tokens, 0); + return ret; +} + static void branch_info_release(struct branch_info *info) { free(info->name); @@ -1741,10 +1896,10 @@ static struct option *add_common_switch_branch_options( { struct option options[] = { OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), - OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", + OPT_CALLBACK_F('t', "track", opts, "(direct|inherit|fetch)[,...]", N_("set branch tracking configuration"), PARSE_OPT_OPTARG, - parse_opt_tracking_mode), + parse_opt_checkout_track), OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), PARSE_OPT_NOCOMPLETE), OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), @@ -1949,8 +2104,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track == BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; - int n = parse_branchname_arg(argc, argv, dwim_ok, which_command, - &new_branch_info, opts, &rev); + int n; + + if (opts->fetch) + fetch_remote_for_start_point(argv[0]); + + n = parse_branchname_arg(argc, argv, dwim_ok, which_command, + &new_branch_info, opts, &rev); argv += n; argc -= n; } else if (!opts->accept_ref && opts->from_treeish) { diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..6dfe9ec931 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -801,4 +801,148 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'setup upstream for --track=fetch tests' ' + git checkout main && + git init fetch_upstream && + test_commit -C fetch_upstream u_main && + git remote add fetch_upstream fetch_upstream && + git fetch fetch_upstream && + git -C fetch_upstream checkout -b fetch_new && + test_commit -C fetch_upstream u_new +' + +test_expect_success 'checkout --track=fetch -b picks up branch created upstream after clone' ' + git checkout main && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_new && + git checkout --track=fetch -b local_new fetch_upstream/fetch_new && + test_cmp_rev refs/remotes/fetch_upstream/fetch_new HEAD && + test_cmp_config fetch_upstream branch.local_new.remote && + test_cmp_config refs/heads/fetch_new branch.local_new.merge +' + +test_expect_success 'checkout --track=fetch <remote>/<branch> leaves other tracking branches untouched' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_target && + test_commit -C fetch_upstream u_target_pre && + git -C fetch_upstream checkout -b fetch_other && + test_commit -C fetch_upstream u_other_pre && + git fetch fetch_upstream && + other_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_other) && + git -C fetch_upstream checkout fetch_target && + test_commit -C fetch_upstream u_target_post && + git -C fetch_upstream checkout fetch_other && + test_commit -C fetch_upstream u_other_post && + git checkout --track=fetch -b local_target fetch_upstream/fetch_target && + test_cmp_rev refs/remotes/fetch_upstream/fetch_target HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_other)" = "$other_before" +' + +test_expect_success 'checkout --track=fetch with bare remote name fetches only <remote>/HEAD target' ' + git checkout main && + git -C fetch_upstream checkout main && + git remote set-head fetch_upstream main && + git -C fetch_upstream checkout -b fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_pre && + git fetch fetch_upstream fetch_unrelated && + unrelated_before=$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated) && + git -C fetch_upstream checkout main && + test_commit -C fetch_upstream u_main_post && + git -C fetch_upstream checkout fetch_unrelated && + test_commit -C fetch_upstream u_unrelated_post && + git checkout --track=fetch -b local_from_remote fetch_upstream && + test_cmp_rev refs/remotes/fetch_upstream/main HEAD && + test "$(git rev-parse refs/remotes/fetch_upstream/fetch_unrelated)" = "$unrelated_before" +' + +test_expect_success 'checkout --track=fetch aborts and does not create branch when no existing ref' ' + git checkout main && + test_might_fail git branch -D bogus && + test_must_fail git checkout --track=fetch -b bogus fetch_upstream/does_not_exist && + test_must_fail git rev-parse --verify refs/heads/bogus +' + +test_expect_success 'checkout --track=fetch warns and proceeds when fetch fails but ref exists' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_offline && + test_commit -C fetch_upstream u_offline && + git fetch fetch_upstream fetch_offline && + saved_url=$(git config remote.fetch_upstream.url) && + test_when_finished "git config remote.fetch_upstream.url \"$saved_url\"" && + git config remote.fetch_upstream.url ./does-not-exist && + git checkout --track=fetch -b local_offline fetch_upstream/fetch_offline 2>err && + test_grep "failed to fetch" err && + test_cmp_rev refs/remotes/fetch_upstream/fetch_offline HEAD +' + +test_expect_success 'checkout --track=fetch resolves through configured fetch refspec' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_refspec && + test_commit -C fetch_upstream u_refspec && + git fetch fetch_upstream fetch_refspec && + git remote add fetch_custom ./fetch_upstream && + test_when_finished "git remote remove fetch_custom" && + git config --replace-all remote.fetch_custom.fetch \ + "+refs/heads/*:refs/remotes/custom-ns/*" && + git fetch fetch_custom && + test_commit -C fetch_upstream u_refspec_post && + git checkout --track=fetch -b local_refspec custom-ns/fetch_refspec && + test_cmp_rev refs/remotes/custom-ns/fetch_refspec HEAD +' + +test_expect_success 'checkout --track=fetch handles hierarchical remote name' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_hier && + test_commit -C fetch_upstream u_hier && + git remote add nested/remote ./fetch_upstream && + test_when_finished "git remote remove nested/remote" && + git fetch nested/remote fetch_hier && + test_commit -C fetch_upstream u_hier_post && + git checkout --track=fetch -b local_hier nested/remote/fetch_hier && + test_cmp_rev refs/remotes/nested/remote/fetch_hier HEAD +' + +test_expect_success 'checkout --track=inherit,direct is rejected' ' + test_must_fail git checkout --track=inherit,direct -b bad fetch_upstream/fetch_new 2>err && + test_grep "cannot combine" err +' + +test_expect_success 'checkout --track=fetch then --track=direct drops fetch (last-one-wins)' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_lastwin && + test_commit -C fetch_upstream u_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin && + test_must_fail git checkout --track=fetch --track=direct \ + -b local_lastwin fetch_upstream/fetch_lastwin && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_lastwin +' + +test_expect_success 'checkout --track=fetch,inherit fetches and inherits' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_inherit && + test_commit -C fetch_upstream u_inherit && + git fetch fetch_upstream fetch_inherit && + git checkout -b base_inherit fetch_upstream/fetch_inherit && + test_commit -C fetch_upstream u_inherit2 && + git checkout main && + git checkout --track=fetch,inherit -b local_inherit base_inherit && + test_cmp_rev refs/remotes/fetch_upstream/fetch_inherit HEAD && + test_cmp_config fetch_upstream branch.local_inherit.remote && + test_cmp_config refs/heads/fetch_inherit branch.local_inherit.merge +' + +test_expect_success 'checkout --track=bogus reports an error' ' + git checkout main && + test_must_fail git checkout --track=bogus -b bogus_branch fetch_upstream/fetch_new 2>err && + test_grep "expects" err +' + +test_expect_success 'switch --track=fetch -c picks up branch created upstream after clone' ' + git checkout main && + git -C fetch_upstream checkout -b fetch_switch && + test_commit -C fetch_upstream u_switch && + test_must_fail git rev-parse --verify refs/remotes/fetch_upstream/fetch_switch && + git switch --track=fetch -c local_switch fetch_upstream/fetch_switch && + test_cmp_rev refs/remotes/fetch_upstream/fetch_switch HEAD +' + test_done base-commit: 29bd7ed5127255713c1ac2f43b7c6f257d7b4594 -- gitgitgadget ^ permalink raw reply related [flat|nested] 35+ messages in thread
end of thread, other threads:[~2026-05-12 10:55 UTC | newest] Thread overview: 35+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 2026-04-24 13:48 ` Ramsay Jones 2026-04-24 17:12 ` D. Ben Knoble 2026-04-25 17:24 ` Comments on Phillip's review Harald Nordgren 2026-04-25 17:44 ` Wrong subject line Harald Nordgren 2026-04-24 17:38 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Kristoffer Haugsbakk 2026-04-25 17:41 ` Comments on Phillip's review Harald Nordgren 2026-04-25 17:44 ` Wrong subject line Harald Nordgren 2026-04-26 7:07 ` Kristoffer Haugsbakk 2026-04-26 15:15 ` [PATCH] remote: add --set-head option to 'git remote add' Harald Nordgren 2026-04-24 17:42 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Marc Branchaud 2026-04-25 17:48 ` Wrong subject line Harald Nordgren 2026-04-24 22:21 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Junio C Hamano 2026-04-25 2:54 ` Junio C Hamano 2026-04-25 17:58 ` Multiple remotes Harald Nordgren 2026-04-25 21:57 ` Ben Knoble 2026-04-25 22:54 ` gh Harald Nordgren 2026-04-25 18:12 ` [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget 2026-04-26 7:24 ` [PATCH v3] " Harald Nordgren via GitGitGadget 2026-04-26 15:54 ` Ramsay Jones 2026-04-26 18:32 ` [PATCH v4] " Harald Nordgren via GitGitGadget 2026-04-28 1:47 ` Junio C Hamano 2026-04-28 8:44 ` [PATCH] " Harald Nordgren 2026-04-28 9:03 ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-03 20:59 ` Junio C Hamano 2026-05-03 22:32 ` [PATCH] checkout: add --autostash option for branch switching Harald Nordgren 2026-05-03 22:31 ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-07 20:12 ` Harald Nordgren 2026-05-08 13:15 ` Phillip Wood 2026-05-08 22:40 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren 2026-05-08 22:52 ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget 2026-05-11 13:16 ` Phillip Wood 2026-05-11 13:47 ` [PATCH v8] " Harald Nordgren via GitGitGadget 2026-05-12 0:32 ` Junio C Hamano 2026-05-12 10:55 ` [PATCH v9] " Harald Nordgren via GitGitGadget
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.