All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Harald Nordgren <haraldnordgren@gmail.com>,
	Harald Nordgren <haraldnordgren@gmail.com>
Subject: [PATCH] branch: let inherit track a remote-tracking start point
Date: Fri, 16 Oct 2026 07:02:45 +0000	[thread overview]
Message-ID: <pull.2450.git.git.1792134165785.gitgitgadget@gmail.com> (raw)

From: Harald Nordgren <haraldnordgren@gmail.com>

With branch.autoSetupMerge=inherit, "git switch topic" creates a local
topic branch from origin/topic with no upstream, and warns that no
remote is set. "git switch -c topic origin/topic" and an explicit
"--track=inherit" do the same.

A remote-tracking branch has no upstream of its own to copy. Make
inherit use it as the upstream directly, as the default "true" does,
so that inherit sets up tracking in every case the default does.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    branch: let inherit track a remote-tracking start point
    
    With branch.autoSetupMerge=inherit, make git switch -c new_branch
    upstream/master work without a warning, and sets the remote branch
    upstream/master as the upstream.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2450%2FHaraldNordgren%2Fhn%2Fbranch-inherit-remote-tracking-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2450/HaraldNordgren/hn/branch-inherit-remote-tracking-v1
Pull-Request: https://github.com/git/git/pull/2450

 Documentation/config/branch.adoc |  2 +-
 Documentation/git-branch.adoc    |  4 +++-
 branch.c                         |  3 +++
 t/t2060-switch.sh                | 17 +++++++++++++++++
 4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc
index c2acd89c99..6d9fbe5353 100644
--- a/Documentation/config/branch.adoc
+++ b/Documentation/config/branch.adoc
@@ -10,7 +10,7 @@
 `always`;; automatic setup is done when the starting point is either a
 	local branch or remote-tracking branch
 `inherit`;; if the starting point has a tracking configuration, it is copied to the new
-	branch
+	branch. A remote-tracking starting point is tracked directly, as with `true`.
 `simple`;; automatic setup is done only when the starting point
 	is a remote-tracking branch and the new branch has the same name as the
 	remote branch.
diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc
index bfdf459329..238d99fd27 100644
--- a/Documentation/git-branch.adoc
+++ b/Documentation/git-branch.adoc
@@ -280,7 +280,9 @@ remove it anyway.
 The exact upstream branch is chosen depending on the optional argument:
 `-t`, `--track`, or `--track=direct` means to use the start-point branch
 itself as the upstream; `--track=inherit` means to copy the upstream
-configuration of the start-point branch.
+configuration of the start-point branch. A remote-tracking start-point
+has no upstream of its own, so `--track=inherit` uses it as the upstream
+directly.
 +
 The `branch.autoSetupMerge` configuration variable specifies how `git switch`,
 `git checkout` and `git branch` should behave when neither `--track` nor
diff --git a/branch.c b/branch.c
index 22f4f46b96..2c23179775 100644
--- a/branch.c
+++ b/branch.c
@@ -263,6 +263,9 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
 	if (!track)
 		BUG("asked to set up tracking, but tracking is disallowed");
 
+	if (track == BRANCH_TRACK_INHERIT && !starts_with(orig_ref, "refs/heads/"))
+		track = BRANCH_TRACK_REMOTE;
+
 	memset(&tracking, 0, sizeof(tracking));
 	tracking.spec.dst = (char *)orig_ref;
 	tracking.srcs = &tracking_srcs;
diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh
index cec2fe6876..da9d01ab84 100755
--- a/t/t2060-switch.sh
+++ b/t/t2060-switch.sh
@@ -146,6 +146,23 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
 	test_cmp_config "" --default "" branch.main2.merge
 '
 
+test_expect_success 'autoSetupMerge=inherit tracks a remote-tracking start point' '
+	git clone . downstream &&
+	(
+		cd downstream &&
+		git config branch.autoSetupMerge inherit &&
+		git switch -c new_branch origin/main &&
+		git status >../actual &&
+		cat >expect <<-EOF &&
+		On branch new_branch
+		Your branch is up to date with ${SQ}origin/main${SQ}.
+
+		nothing to commit, working tree clean
+		EOF
+		test_cmp expect ../actual
+	)
+'
+
 test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
 	test_when_finished "
 		git worktree remove wt1 ||:

base-commit: f57c9a73884400380bddd172acc88b569834f909
-- 
gitgitgadget

                 reply	other threads:[~2026-10-16  7:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2450.git.git.1792134165785.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=haraldnordgren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.