threads / patch / 64668

patchstatus: show default branch comparison when tracking non-default branch

Subject: [PATCH] status: show default branch comparison when tracking non-default branch

## tl;dr

259 messages between Dec 23, 2025 and Mar 9, 2026. Diffs are folded; open one to read it.

replies: 258people: 11as markdown or json

Harald Nordgren via GitGitGadget· Dec 23, 2025, 00:53 UTC · lore
From: Harald Nordgren <haraldnordgren@gmail.com>

When a branch tracks a non-default remote branch (e.g., origin/feature), git status now also displays how the branch compares to the default branch (origin/main or upstream/main). This helps users understand if their branch has drifted from the main development line even when it's in sync with its tracking branch.

The comparison is shown as a separate line after the tracking
branch status:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The default branch is determined dynamically by checking:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    status: show default branch comparison when tracking non-default branch
    
    When a branch tracks a non-default remote branch (e.g., origin/feature),
    git status now also displays how the branch compares to the default
    branch (origin/main or upstream/main). This helps users understand if
    their branch has drifted from the main development line even when it's
    in sync with its tracking branch.
    
    The comparison is shown as a separate line after the tracking branch
    status:
    
     * "Ahead of 'origin/main' by N commits" when purely ahead
     * "Behind 'origin/main' by N commits" when purely behind
     * "Diverged from 'origin/main' by N commits" when diverged
    
    The default branch is determined dynamically by checking:
    
     1. refs/remotes/upstream/HEAD (if upstream remote exists)
     2. refs/remotes/origin/HEAD (fallback)
    
    This works with any default branch name (main, master, develop, etc.) as
    long as the symbolic ref is configured. The comparison is also shown
    when the branch is up-to-date with its tracking branch but differs from
    the default branch.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v1
Pull-Request: https://github.com/git/git/pull/2138
 remote.c                 | 101 ++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 347 insertions(+)
Show changes to 2 files +347 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..9be5e5aa22 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,95 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static const char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	const char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	const char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2337,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2348,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2362,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2380,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2393,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2409,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done

base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
-- 
gitgitgadget
Junio C Hamano· Dec 23, 2025, 05:32 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 5 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> When a branch tracks a non-default remote branch (e.g.,
> origin/feature), git status now also displays how the branch
> compares to the default branch (origin/main or upstream/main).
"now" meaning what"?
The usual way to compose a log message of this project is to
 - Give an observation on how the current system works in the
   present tense (so no need to say "Currently X is Y", or
   "Previously X was Y" to describe the state before your change;
   just "X is Y" is enough), and discuss what you perceive as a
   problem in it.
 - Propose a solution (optional---often, problem description
   trivially leads to an obvious solution in reader's minds).
 - Give commands to somebody editing the codebase to "make it so",
   instead of saying "This commit does X".
in this order.

So if you are following the convention, "now also displays" ought to be about what the current code without this patch does, but I am sensing that it probably is not the case.

Start your explanation by decribing what the users see in "git status" output without this patch. Perhaps like

    "git status" on a branch that follows a remote branch compares
    commits on the current branch and the remote-tracking branch it
    builds upon, to show "ahead" (i.e. you have built new history,
    while others are not touching it), "behind" (i.e. you haven't
    added any work since you were in-sync, while others have added
    their work on the branch), "diverged" (i.e. you have commits
    that you haven't pushed out, while others have added commits).

That is the "giving an observation" part. And then describe why that comparison with a single remote branch may be insufficient to learn the current status. Your reasoning might be something like

    When you fork a branch 'feature' from the 'main' branch of the
    remote, but then create 'feature' branch at the remote and push
    there, while you still occasionally pull from or rebase onto
    their 'main', you'd _also_ want to know how much you have
    diverged from 'main', in addition to how your 'feature' and
    their 'feature' compares.  Currently the comparison with 'main'
    is not given.
That's the "discuss your problem with the status quo" part.
Only after that, propose to show two sets of comparison.
> This helps users understand if their branch has drifted from the
> main development line even when it's in sync with its tracking
> branch.

Describe what does it help to know that after that sentence, like "... to get the feel of when to start thinking about rebasing", or something.

Show 5 quoted lines
> The comparison is shown as a separate line after the tracking
> branch status:
> - "Ahead of 'origin/main' by N commits" when purely ahead
> - "Behind 'origin/main' by N commits" when purely behind
> - "Diverged from 'origin/main' by N commits" when diverged

In other words, exactly the same way as what we show with the tracking branch?

The triangular workflow involves two remote things. One is where you pull from to catch up. After building on top, you push to somewhere else to publish your work. This may be a different branch in the same repository you pull from, or a branch in a completely different repository. What you pushed out may be processed by others and may come back in the branch you pull from eventually to complete the triangle.

In such a triangular workflow, comparison with these two remote things may be needed. One with the branch you forked your work from to know how much work _other_ people added to the branch to learn when to start thinking about catching up, and with the branch you are pushing your work to to know how much work you are holding locally without pushing out.

I am not sure what you mean by the word "default" here, though.

You seem to be using the "what would a new user get when they clone the remote (by virtue of their HEAD pointing at that branch)", but I am not sure if that is a good way to determine the other remote thing to compare with.

Even if one remote branch you pull from (but not push to) has a name that is not one of those usual ones like 'main', 'master', 'trunk', 'default', you would want to compare with it in addition to where you are pushing to. So branch.<name>.merge + branch.<name>.remote that defines where you pull from is one thing to compare with. To learn the other, the destination of a push of this branch, would involve poking at remote.pushdefault, branch.<name>.pushRemote, branch.<name>.remote to find out which remote repository it goes, and then remote.<remote>.push to find out where this branch goes, but the helper functions to learn all that are already available.

So, I think the topic addresses a good problem, its presentation needs a bit more work, and its design (not the implementation) of how to figure out the other thing to compare I am not sure about.

Thanks.
Harald Nordgren· Dec 23, 2025, 10:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Show 17 quoted lines
> In other words, exactly the same way as what we show with the
> tracking branch?
> 
> The triangular workflow involves two remote things.  One is where
> you pull from to catch up.  After building on top, you push to
> somewhere else to publish your work.  This may be a different branch
> in the same repository you pull from, or a branch in a completely
> different repository.  What you pushed out may be processed by
> others and may come back in the branch you pull from eventually to
> complete the triangle.
> 
> In such a triangular workflow, comparison with these two remote
> things may be needed. One with the branch you forked your work from
> to know how much work _other_ people added to the branch to learn
> when to start thinking about catching up, and with the branch you
> are pushing your work to to know how much work you are holding
> locally without pushing out.
Yes, it's the same as when tracking master/main, but with far less complexity for the end-user. For many years I have had the habit of running these on my feature branches:
	git checkout -b feature_branch
	git branch --set-upstream-to origin/$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
	# To merge in other's code early
	git pull --rebase origin $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
	# To push to correct branch, because I'm tracking default instead "feature_branch"
	git push origin $(git rev-parse --abbrev-ref HEAD)
I have found this very hard to explain the benefits of this to other developers. Git is already scary to many, they are afraid of losing work, so they would rather not mess with the tracking branches and break the regular "git push" functionality.
For me, I feel blind of I can't see how my branch compares the master/main at all times.
Show 10 quoted lines
> I am not sure what you mean by the word "default" here, though.
> 
> You seem to be using the "what would a new user get when they clone
> the remote (by virtue of their HEAD pointing at that branch)", but
> I am not sure if that is a good way to determine the other remote
> thing to compare with.
>
> Even if one remote branch you pull from (but not push to) has a name
> that is not one of those usual ones like 'main', 'master', 'trunk',
> 'default'
Agreed, this should be as agnostic as possible. "Default" might be me using GitHub terminology. However, it seems that 'git symbolic-ref refs/remotes/upstream/HEAD' always produces the desires result, so for the sake of discussion we can call it "upstream/HEAD" instead of "default".
Harald
Harald Nordgren· Dec 23, 2025, 11:36 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

(Adding another comment to my own thread to reply to Junio C Hamano)
Show 7 quoted lines
> branch.<name>.merge + branch.<name>.remote
> that defines where you pull from is one thing to compare with.  To
> learn the other, the destination of a push of this branch, would
> involve poking at remote.pushdefault, branch.<name>.pushRemote,
> branch.<name>.remote to find out which remote repository it goes,
> and then remote.<remote>.push to find out where this branch goes,
> but the helper functions to learn all that are already available.
When a new branch is created it has no push settings:
	git checkout -b ahead_of_main_status__tmp2
	git push
	fatal: The current branch ahead_of_main_status__tmp2 has no upstream branch.
	To push the current branch and set the remote as upstream, use
    	git push --set-upstream origin ahead_of_main_status__tmp2
	To have this happen automatically for branches without a tracking
	upstream, see 'push.autoSetupRemote' in 'git help config'.
Once the users runs that suggested command
	git push --set-upstream origin ahead_of_main_status__tmp2
then the 'branch.<name>.merge' and 'branch.<name>.remote' no longer hold the reference to "upstream/HEAD".
For sure, it would be great to re-use previous logic for this, but can it really be done without new logic?
Harald
Chris Torek· Dec 23, 2025, 12:23 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

On Tue, Dec 23, 2025 at 3:36 AM Harald Nordgren <haraldnordgren@gmail.com> wrote:

Show 5 quoted lines
> Once the users runs that suggested command
>
>         git push --set-upstream origin ahead_of_main_status__tmp2
>
> then the 'branch.<name>.merge' and 'branch.<name>.remote' no longer hold the reference to "upstream/HEAD".
Right.
And, as Junio noted, to:
>> learn the other, the destination of a push of this branch, would
>> involve poking at remote.pushdefault, branch.<name>.pushRemote,
>> branch.<name>.remote to find out which remote repository it goes,
>> and then remote.<remote>.push to find out where this branch goes,

That is, there are some separate configuration items that can change where `git push` goes.

Using `git push --set-upstream` sets ones that affect `git pull`, `git status`, and `git push`, but it's possible to set ones that affect only `git push`.

That still leaves `git status` with the problem you (Harald) have observed, so perhaps the path forward is to have `git status` check things like branch.<name>.pushRemote to see if they exist and differ from branch.<name>.remote.

Chris
Junio C Hamano· Dec 23, 2025, 13:32 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 14 quoted lines
>> You seem to be using the "what would a new user get when they clone
>> the remote (by virtue of their HEAD pointing at that branch)", but
>> I am not sure if that is a good way to determine the other remote
>> thing to compare with.
>>
>> Even if one remote branch you pull from (but not push to) has a name
>> that is not one of those usual ones like 'main', 'master', 'trunk',
>> 'default'
>
> Agreed, this should be as agnostic as possible. "Default" might be
> me using GitHub terminology. However, it seems that 'git
> symbolic-ref refs/remotes/upstream/HEAD' always produces the
> desires result, so for the sake of discussion we can call it
> "upstream/HEAD" instead of "default".

I didn't exactly question the terminology, but was wondering more about the wisdom of using remotes/*/HEAD.

If a project uses the same remote repository to maintain its maintenance and development tracks, their HEAD might point at the 'main' (used for development), but some of your branches you used to work on fixes that can later be merged to the maintenance track, it is likely that you'll fork from their 'maint', and while you keep polishing your fixes, you may push your 'fix' branch to their 'fix' branch. Comparing your 'fix' with their 'fix' is what we already do, and it gives two thirds of what you need, but the missing comparison is with their 'maint', not with their HEAD that points at their 'main'.

Thanks.
Harald Nordgren· Dec 23, 2025, 14:09 UTC · re: Junio C Hamano · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Show 10 quoted lines
> If a project uses the same remote repository to maintain its
> maintenance and development tracks, their HEAD might point at the
> 'main' (used for development), but some of your branches you used to
> work on fixes that can later be merged to the maintenance track, it
> is likely that you'll fork from their 'maint', and while you keep
> polishing your fixes, you may push your 'fix' branch to their 'fix'
> branch.  Comparing your 'fix' with their 'fix' is what we already
> do, and it gives two thirds of what you need, but the missing
> comparison is with their 'maint', not with their HEAD that points at
> their 'main'.

That's a fair point. I am used to trunk-based development, but I recognize that there are other paradigms out there.

One possibly solution could be to make this adjustable in repo-wide config (maybe 'repo.settings.defaultBranch=maint') and when unset it compares to 'refs/remotes/upstream/HEAD'?

Harald Nordgren· Dec 23, 2025, 14:18 UTC · re: Chris Torek · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

> That still leaves `git status` with the problem you (Harald) have observed,
> so perhaps the path forward is to have `git status` check things like
> branch.<name>.pushRemote to see if they exist and differ from
> branch.<name>.remote.

I played around with pushRemote and I'm not getting it to do something that's useful for me here. Granted I'm not an expert there so happy to take some hints on how to use it more specifically.

I checked some of my other projects and it seems pushRemote doesn't get set, even when working with one fork and one upstream repo:

	$ git remote -v
	HaraldNordgren	git@github.com:HaraldNordgren/brew.git (fetch)
	HaraldNordgren	git@github.com:HaraldNordgren/brew.git (push)
	origin	git@github.com:Homebrew/brew.git (fetch)
	origin	git@github.com:Homebrew/brew.git (push)
Harald
Chris Torek· Dec 23, 2025, 14:22 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

On Tue, Dec 23, 2025 at 6:18 AM Harald Nordgren <haraldnordgren@gmail.com> wrote:

> I played around with pushRemote and I'm not getting it to do something
> that's useful for me here. Granted I'm not an expert there so happy to take
> some hints on how to use it more specifically.

I've never actually used it myself. You do have to set it manually (with `git config` for instance).

I've never been completely convinced that Git's triangular workflow setups are The Right Way, or even a Good Way. I've always just done everything manually. But they're documented, so they ought to work.

Chris
Harald Nordgren via GitGitGadget· Dec 23, 2025, 22:54 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v2 0/2] status: show default branch comparison when tracking non-default branch

cc: Chris Torek chris.torek@gmail.com
Harald Nordgren (2):
  status: show comparison with upstream default branch
  Simplify default branch comparison logic
 remote.c                 |  83 +++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 329 insertions(+)
base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v2
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v1:
 1:  c27a806dc9 ! 1:  a3800aed18 status: show default branch comparison when tracking non-default branch
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    status: show default branch comparison when tracking non-default branch
     +    status: show comparison with upstream default branch
      
     -    When a branch tracks a non-default remote branch (e.g.,
     -    origin/feature), git status now also displays how the branch
     -    compares to the default branch (origin/main or upstream/main).
     -    This helps users understand if their branch has drifted from the
     -    main development line even when it's in sync with its tracking
     -    branch.
     +    "git status" on a branch that follows a remote branch compares
     +    commits on the current branch and the remote-tracking branch it
     +    builds upon, to show "ahead" (i.e. you have built new history,
     +    while others are not touching it), "behind" (i.e. you haven't
     +    added any work since you were in-sync, while others have added
     +    their work on the branch), "diverged" (i.e. you have commits
     +    that you haven't pushed out, while others have added commits).
      
     -    The comparison is shown as a separate line after the tracking
     -    branch status:
     +    When you fork a branch 'feature' from the 'main' branch of the
     +    remote, but then create 'feature' branch at the remote and push
     +    there, while you still occasionally pull from or rebase onto
     +    their 'main', you'd also want to know how much you have diverged
     +    from 'main', in addition to how your 'feature' and their
     +    'feature' compares. Currently the comparison with 'main' is not
     +    given, making it hard to know when to start thinking about
     +    rebasing onto the upstream default branch.
     +
     +    Show two sets of comparison: one with the tracking branch (as
     +    before), and another with the upstream's default branch (what
     +    their HEAD points to, typically 'main' or 'master'). The latter
     +    comparison appears on a separate line after the tracking branch
     +    status, using the same format:
          - "Ahead of 'origin/main' by N commits" when purely ahead
          - "Behind 'origin/main' by N commits" when purely behind
          - "Diverged from 'origin/main' by N commits" when diverged
      
          Example output when tracking a feature branch:
     -
              On branch feature
              Your branch is ahead of 'origin/feature' by 2 commits.
                (use "git push" to publish your local commits)
      
              Ahead of 'origin/main' by 5 commits.
      
     -    The default branch is determined dynamically by checking:
     +    The upstream default branch is determined by checking symbolic refs:
          1. refs/remotes/upstream/HEAD (if upstream remote exists)
          2. refs/remotes/origin/HEAD (fallback)
      
          This works with any default branch name (main, master, develop,
          etc.) as long as the symbolic ref is configured. The comparison
          is also shown when the branch is up-to-date with its tracking
     -    branch but differs from the default branch.
     +    branch but differs from the upstream default branch.
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
       
     -+static const char *get_default_remote_ref(char **full_ref_out)
     ++static char *get_default_remote_ref(char **full_ref_out)
      +{
      +	int flag;
      +	const char *resolved;
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +static int is_default_remote_branch(const char *name)
      +{
      +	char *default_full = NULL;
     -+	const char *default_short;
     ++	char *default_short;
      +	int result = 0;
      +
      +	default_short = get_default_remote_ref(&default_full);
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +
      +	result = !strcmp(name, default_short);
      +
     ++	free(default_short);
      +	free(default_full);
      +	return result;
      +}
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +{
      +	int default_ours = 0, default_theirs = 0;
      +	char *default_full = NULL;
     -+	const char *default_short;
     ++	char *default_short;
      +
      +	default_short = get_default_remote_ref(&default_full);
      +	if (!default_short)
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +
      +	if (stat_branch_pair(branch_refname, default_full,
      +			     &default_ours, &default_theirs, abf) <= 0) {
     ++		free(default_short);
      +		free(default_full);
      +		return;
      +	}
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +			default_short, default_ours + default_theirs);
      +	}
      +
     ++	free(default_short);
      +	free(default_full);
      +}
      +
 -:  ---------- > 2:  417f2075fb Simplify default branch comparison logic
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 23, 2025, 22:54 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v2 1/2] status: show comparison with upstream default branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The upstream default branch is determined by checking symbolic refs:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the upstream default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 104 +++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 350 insertions(+)
Show changes to 2 files +350 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..b2a1e980b1 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,98 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_short);
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_short);
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_short);
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2340,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2351,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2365,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2383,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2396,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2412,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 23, 2025, 22:54 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v2 2/2] Simplify default branch comparison logic

From: Harald Nordgren <haraldnordgren@gmail.com>

This maintains the same functionality while reducing ref resolution calls from multiple to one, and eliminating unnecessary memory allocations.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 59 ++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)
Show changes to remote.c +19 −40
diff --git a/remote.c b/remote.c
index b2a1e980b1..f3831ef3be 100644
--- a/remote.c
+++ b/remote.c
@@ -2267,41 +2267,17 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return NULL;
 }
 
-static int is_default_remote_branch(const char *name)
-{
-	char *default_full = NULL;
-	char *default_short;
-	int result = 0;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return 0;
-
-	result = !strcmp(name, default_short);
-
-	free(default_short);
-	free(default_full);
-	return result;
-}
-
 static void format_default_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
+					     const char *default_full,
+					     const char *default_short,
 					     enum ahead_behind_flags abf)
 {
 	int default_ours = 0, default_theirs = 0;
-	char *default_full = NULL;
-	char *default_short;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return;
 
 	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0) {
-		free(default_short);
-		free(default_full);
+			     &default_ours, &default_theirs, abf) <= 0)
 		return;
-	}
 
 	strbuf_addstr(sb, "\n");
 
@@ -2324,9 +2300,6 @@ static void format_default_branch_comparison(struct strbuf *sb,
 			   default_ours + default_theirs),
 			default_short, default_ours + default_theirs);
 	}
-
-	free(default_short);
-	free(default_full);
 }
 
 /*
@@ -2340,7 +2313,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	int show_default_branch_comparison;
+	char *default_full = NULL;
+	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2352,7 +2326,13 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	show_default_branch_comparison = !is_default_remote_branch(base);
+	default_short = get_default_remote_ref(&default_full);
+	if (default_short && !strcmp(base, default_short)) {
+		free(default_short);
+		free(default_full);
+		default_short = NULL;
+		default_full = NULL;
+	}
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
@@ -2365,8 +2345,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2383,8 +2361,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2396,8 +2372,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2412,10 +2386,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	}
+
+	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
+		format_default_branch_comparison(sb, branch->refname, default_full,
+						 default_short, abf);
+
 	free(base);
+	free(default_short);
+	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Yee Cheng Chin· Dec 23, 2025, 23:11 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

> The default branch is determined dynamically by checking:
> 1. refs/remotes/upstream/HEAD (if upstream remote exists)
> 2. refs/remotes/origin/HEAD (fallback)

I feel like this is making a lot of assumptions regarding remotes. "origin" and "upstream" are not inherently special names for remotes. I personally have different Git repositories where they could mean slightly different things, and I don't use the "upstream" wording myself (I sometimes use "official" for the upstream branch, and/or "ychin" for my own fork's remote). Feels like we should not be imposing such a hard-coded value when nothing else in Git enforces it.

Also, when there are multiple remotes, it's not always clear which one the user actually cares about. It's not always clear if they care about the upstream or the downstream remote, of a third one that actually matters more.

This would also work poorly with detached branches (e.g. the popular 'gh-pages' branches in a lot of repositories), or permanently foked branches like fixed versions (e.g. v2.x legacy branch when the software main branch moved to v3.0). Seems like for this to work well it would need to be configurable per branch. Even on a repository level there would likely be lots of edge cases with each branch having its unique circumstances.

Harald Nordgren· Dec 23, 2025, 23:59 UTC · re: Yee Cheng Chin · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

These are very fair points, maybe a reason not to have this feature on by default then.
For me, I work against clear origin/master, origin/master, origin/develop branches at my dayjob, and when I do open-source there is a default branch to work against. The GitHub 'gh' tool defaults to upstream and origin repos, which is why I chose those. But I agree it might not be right for everyone.
Maybe what I wrote in a previous message about making this configurable via 'repo.settings.defaultBranch' (maybe call it 'repo.settings.statusGoalBranch') would be useful, but having it off by default instead of on by default as I originally suggested.
I feel strongly that it should be able to be set be repo-specific (and globally). Having it only per branch defeats a big part of it. Should be straightforward git config to have a repo-wide rule but still allow disabling it for e.g. 'gh-pages', I hope?
Harald
Harald Nordgren· Dec 24, 2025, 00:00 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

By the way, I'm using my own version of git while working on this, and I'm loving it 😅
	$ /Users/Harald/git-repos/github.com/git/git/git status
	On branch ahead_of_main_status
	Your branch and 'origin/ahead_of_main_status' have diverged,
	and have 1 and 1 different commits each, respectively.
	  (use "git pull" if you want to integrate the remote branch with yours)
	Ahead of 'upstream/master' by 2 commits.
	nothing to commit, working tree clean
Junio C Hamano· Dec 24, 2025, 00:38 UTC · re: Yee Cheng Chin · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Yee Cheng Chin <ychin.macvim@gmail.com> writes:
Show 6 quoted lines
>> The default branch is determined dynamically by checking:
>> 1. refs/remotes/upstream/HEAD (if upstream remote exists)
>> 2. refs/remotes/origin/HEAD (fallback)
>
> I feel like this is making a lot of assumptions regarding remotes.
> "origin" and "upstream" are not inherently special names for remotes.

Good point. There is a mechanism to determine where a branch would be pushed to with "git push", and where the new material to update the branch would come from with "git pull", and these places need to be considered when doing comparisons. This series seems to punt on determining both repository and branch and instead uses a hardcoded "upstream" (or "origin") and "HEAD", which is not satisfactory.

Show 18 quoted lines
> I personally have different Git repositories where they could mean
> slightly different things, and I don't use the "upstream" wording
> myself (I sometimes use "official" for the upstream branch, and/or
> "ychin" for my own fork's remote). Feels like we should not be
> imposing such a hard-coded value when nothing else in Git enforces it.
>
> Also, when there are multiple remotes, it's not always clear which one
> the user actually cares about. It's not always clear if they care
> about the upstream or the downstream remote, of a third one that
> actually matters more.
>
> This would also work poorly with detached branches (e.g. the popular
> 'gh-pages' branches in a lot of repositories), or permanently foked
> branches like fixed versions (e.g. v2.x legacy branch when the
> software main branch moved to v3.0). Seems like for this to work well
> it would need to be configurable per branch. Even on a repository
> level there would likely be lots of edge cases with each branch having
> its unique circumstances.

Yes, unrelated branches like gh-pages gives us a very good example to think about.

Thanks.
Yee Cheng Chin· Dec 24, 2025, 00:49 UTC · re: Junio C Hamano · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

On Tue, Dec 23, 2025 at 4:38 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 6 quoted lines
> Good point.  There is a mechanism to determine where a branch would
> be pushed to with "git push", and where the new material to update
> the branch would come from with "git pull", and these places need to
> be considered when doing comparisons.  This series seems to punt on
> determining both repository and branch and instead uses a hardcoded
> "upstream" (or "origin") and "HEAD", which is not satisfactory.

I may be speaking for the author here, but I think the reason why the upstream discovery (or alternatively an additional configuration in the repo) feature was suggested is that not everyone uses the mechanism you described (which I think is the "pushRemote" config). It's true that if you *do* indeed have separate push/pull remotes for a branch, then both should ideally be shown in `git status`. However, oftentimes when you set up a feature branch it may not be correct / desirable to set up separate push/pull remotes for those, especially if you need to collaborate with people on the feature and therefore do need to pull from the same feature's branch. In this case, I think the author still wants `git status` to be able to show the diversion from the original upstream master/main.

Personally I wonder if this feature gets added, people will start asking for a list of "upstream branches" to be able to be compared to, rather than just a fixed upstream/HEAD or origin/HEAD. But maybe I'm thinking too far ahead.

But for example, I maintain MacVim, which is a downstream fork of Vim. As such, "upstream" is a fluid concept for me depending on what I'm doing. It could mean deviation between ychin/macvim vs macvim-dev/macvim, or macvim-dev/macvim vs vim/vim. For complicated forks, it's feasible someone may ask for the ability to see the branch comparison against multiple branches concurrently.

Yee Cheng Chin· Dec 24, 2025, 00:55 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

> I feel strongly that it should be able to be set be repo-specific (and globally). Having it only per branch defeats a big part of it. Should be straightforward git config to have a repo-wide rule but still allow disabling it for e.g. 'gh-pages', I hope?

I don't have that much of an opinion on per-repo / per-branch configuration but I just wanted to point out that some branches would benefit from the ability to turn this off. Perhaps it could be a two layered setting that could be both configured per-repo / per-branch but I don't know if this makes it too complicated.

Harald Nordgren· Dec 24, 2025, 01:12 UTC · re: Junio C Hamano · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

I see push and pull remotes mentioned a lot here. And I want to find out how much of the problem that can solve. Let’s say you have two remotes ’upstream’ and ’fork’.
Can push and pull remotes be set up in a way so the user can run these bare commands without and additional args or flags:
    git push # push to fork
    git pull # pull from upstream
And if so, will ’git status’ also show the status against upstream?
brian m. carlson· Dec 24, 2025, 01:30 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v2 1/2] status: show comparison with upstream default branch

On 2025-12-23 at 22:54:07, Harald Nordgren via GitGitGadget wrote:
Show 5 quoted lines
> +static char *get_default_remote_ref(char **full_ref_out)
> +{
> +	int flag;
> +	const char *resolved;
> +	static const char *remotes[] = { "upstream", "origin", NULL };

This should definitely be configurable, not hard-coded. For instance, I have this in my gitconfig:

    [clone]
        defaultRemoteName = def

so my default remote name is `def` (short for "default", as in the default place to push) and this code will never work.

Another reason to have this be configurable is that some projects (like a company I used to work at) have tooling that demands a specific remote naming convention for tooling to work properly. In this particular case, `upstream` would have been the right choice, but I also worked at a company where English was the common language. Many people may prefer to use names suitable to their local language.

-- 
brian m. carlson (they/them)
Toronto, Ontario, CA
Junio C Hamano· Dec 24, 2025, 01:44 UTC · re: Yee Cheng Chin · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Yee Cheng Chin <ychin.macvim@gmail.com> writes:
> I may be speaking for the author here, but I think the reason why the
> upstream discovery (or alternatively an additional configuration in
> the repo) feature was suggested is that not everyone uses the
> mechanism you described (which I think is the "pushRemote" config).

The mention of pushRemote by Harald is a red-herring. My point is that branch.<name>.remote tells what remote we interact with (and pushRemote is used as a fallback), so there is no need to, and it is actively wrong to, assume "upstream" or "origin". Even if you push back to the same repository as you pull from, you may push to a different branch there, in which case you do not need pushRemote, but you'd need to learn what configured remote you are using anyway.

Junio C Hamano· Dec 24, 2025, 01:46 UTC · re: brian m. carlson · lore

Re: [PATCH v2 1/2] status: show comparison with upstream default branch

"brian m. carlson" <sandals@crustytoothpaste.net> writes:
Show 15 quoted lines
> On 2025-12-23 at 22:54:07, Harald Nordgren via GitGitGadget wrote:
>> +static char *get_default_remote_ref(char **full_ref_out)
>> +{
>> +	int flag;
>> +	const char *resolved;
>> +	static const char *remotes[] = { "upstream", "origin", NULL };
>
> This should definitely be configurable, not hard-coded.  For instance, I
> have this in my gitconfig:
>
>     [clone]
>         defaultRemoteName = def
>
> so my default remote name is `def` (short for "default", as in the
> default place to push) and this code will never work.

But there does not need any new configuration. "git pull" and "git push" on the branch is equipped to figure out where they pull from and push to, so the new feature should be able to figure out these (often the same, but not necessarily) repositories the same way.

Harald Nordgren via GitGitGadget· Dec 24, 2025, 09:31 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v3 0/3] status: show default branch comparison when tracking non-default branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net

Harald Nordgren (3):
  status: show comparison with upstream default branch
  Simplify default branch comparison logic
  Use repo.settings.statusGoalBranch config for status comparison
 remote.c                 |  93 ++++++++++++
 t/t6040-tracking-info.sh | 317 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 410 insertions(+)
base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v3
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v2:
 1:  a3800aed18 = 1:  a3800aed18 status: show comparison with upstream default branch
 2:  417f2075fb = 2:  417f2075fb Simplify default branch comparison logic
 -:  ---------- > 3:  c9ec5d9610 Use repo.settings.statusGoalBranch config for status comparison
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 09:31 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v3 1/3] status: show comparison with upstream default branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The upstream default branch is determined by checking symbolic refs:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the upstream default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 104 +++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 350 insertions(+)
Show changes to 2 files +350 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..b2a1e980b1 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,98 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_short);
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_short);
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_short);
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2340,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2351,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2365,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2383,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2396,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2412,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 09:31 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v3 2/3] Simplify default branch comparison logic

From: Harald Nordgren <haraldnordgren@gmail.com>

This maintains the same functionality while reducing ref resolution calls from multiple to one, and eliminating unnecessary memory allocations.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 59 ++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)
Show changes to remote.c +19 −40
diff --git a/remote.c b/remote.c
index b2a1e980b1..f3831ef3be 100644
--- a/remote.c
+++ b/remote.c
@@ -2267,41 +2267,17 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return NULL;
 }
 
-static int is_default_remote_branch(const char *name)
-{
-	char *default_full = NULL;
-	char *default_short;
-	int result = 0;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return 0;
-
-	result = !strcmp(name, default_short);
-
-	free(default_short);
-	free(default_full);
-	return result;
-}
-
 static void format_default_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
+					     const char *default_full,
+					     const char *default_short,
 					     enum ahead_behind_flags abf)
 {
 	int default_ours = 0, default_theirs = 0;
-	char *default_full = NULL;
-	char *default_short;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return;
 
 	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0) {
-		free(default_short);
-		free(default_full);
+			     &default_ours, &default_theirs, abf) <= 0)
 		return;
-	}
 
 	strbuf_addstr(sb, "\n");
 
@@ -2324,9 +2300,6 @@ static void format_default_branch_comparison(struct strbuf *sb,
 			   default_ours + default_theirs),
 			default_short, default_ours + default_theirs);
 	}
-
-	free(default_short);
-	free(default_full);
 }
 
 /*
@@ -2340,7 +2313,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	int show_default_branch_comparison;
+	char *default_full = NULL;
+	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2352,7 +2326,13 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	show_default_branch_comparison = !is_default_remote_branch(base);
+	default_short = get_default_remote_ref(&default_full);
+	if (default_short && !strcmp(base, default_short)) {
+		free(default_short);
+		free(default_full);
+		default_short = NULL;
+		default_full = NULL;
+	}
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
@@ -2365,8 +2345,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2383,8 +2361,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2396,8 +2372,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2412,10 +2386,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	}
+
+	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
+		format_default_branch_comparison(sb, branch->refname, default_full,
+						 default_short, abf);
+
 	free(base);
+	free(default_short);
+	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 09:31 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v3 3/3] Use repo.settings.statusGoalBranch config for status comparison

From: Harald Nordgren <haraldnordgren@gmail.com>

Replace hardcoded upstream/origin preference with configurable repo.settings.statusGoalBranch setting. When unset, skip the default branch comparison entirely.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 48 ++++++++++++++++-----------
 t/t6040-tracking-info.sh | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 19 deletions(-)
Show changes to 2 files +100 −19

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index f3831ef3be..edb6d374e7 100644
--- a/remote.c
+++ b/remote.c
@@ -2239,32 +2239,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 
 static char *get_default_remote_ref(char **full_ref_out)
 {
-	int flag;
+	const char *config_value;
 	const char *resolved;
-	static const char *remotes[] = { "upstream", "origin", NULL };
-	int i;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
 
-	for (i = 0; remotes[i]; i++) {
-		struct strbuf head_ref = STRBUF_INIT;
-		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+	if (repo_config_get_value(the_repository, "repo.settings.statusGoalBranch", &config_value))
+		return NULL;
 
-		resolved = refs_resolve_ref_unsafe(
-			get_main_ref_store(the_repository),
-			head_ref.buf,
-			RESOLVE_REF_READING,
-			NULL, &flag);
+	if (!config_value || !*config_value)
+		return NULL;
 
-		strbuf_release(&head_ref);
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+		return NULL;
 
-		if (resolved && (flag & REF_ISSYMREF)) {
-			if (full_ref_out)
-				*full_ref_out = xstrdup(resolved);
-			return refs_shorten_unambiguous_ref(
-				get_main_ref_store(the_repository), resolved, 0);
-		}
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
 	}
 
-	return NULL;
+	strbuf_release(&ref_buf);
+	return ret;
 }
 
 static void format_default_branch_comparison(struct strbuf *sb,
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index e2bd48f858..00dadc03e7 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -309,6 +309,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -325,6 +326,7 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 	(
 		cd test &&
 		git checkout main >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -364,6 +366,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 	(
 		cd test &&
 		git checkout work2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -393,6 +396,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 	(
 		cd test &&
 		git checkout work2b >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -6
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -425,6 +429,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
 		git checkout work3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -450,6 +455,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -477,6 +483,7 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 	(
 		cd test &&
 		git checkout synced_feature >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -504,6 +511,7 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 	(
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -527,6 +535,7 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -538,4 +547,66 @@ EOF
 	test_cmp expect actual
 '
 
+test_expect_success 'status with repo.settings.statusGoalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/feature &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/nonexistent &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:19 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v4 0/4] status: show default branch comparison when tracking non-default branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net

Harald Nordgren (4):
  status: show comparison with upstream default branch
  Simplify default branch comparison logic
  Use repo.settings.statusGoalBranch config for status comparison
  Rename default_remote to goal_branch
 remote.c                 |  92 ++++++++++++
 t/t6040-tracking-info.sh | 317 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 409 insertions(+)
base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v4
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v3:
 1:  a3800aed18 = 1:  a3800aed18 status: show comparison with upstream default branch
 2:  417f2075fb = 2:  417f2075fb Simplify default branch comparison logic
 3:  c9ec5d9610 = 3:  c9ec5d9610 Use repo.settings.statusGoalBranch config for status comparison
 -:  ---------- > 4:  0e308141da Rename default_remote to goal_branch
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:19 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v4 1/4] status: show comparison with upstream default branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The upstream default branch is determined by checking symbolic refs:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the upstream default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 104 +++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 350 insertions(+)
Show changes to 2 files +350 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..b2a1e980b1 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,98 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_short);
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_short);
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_short);
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2340,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2351,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2365,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2383,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2396,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2412,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:19 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v4 2/4] Simplify default branch comparison logic

From: Harald Nordgren <haraldnordgren@gmail.com>

This maintains the same functionality while reducing ref resolution calls from multiple to one, and eliminating unnecessary memory allocations.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 59 ++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)
Show changes to remote.c +19 −40
diff --git a/remote.c b/remote.c
index b2a1e980b1..f3831ef3be 100644
--- a/remote.c
+++ b/remote.c
@@ -2267,41 +2267,17 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return NULL;
 }
 
-static int is_default_remote_branch(const char *name)
-{
-	char *default_full = NULL;
-	char *default_short;
-	int result = 0;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return 0;
-
-	result = !strcmp(name, default_short);
-
-	free(default_short);
-	free(default_full);
-	return result;
-}
-
 static void format_default_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
+					     const char *default_full,
+					     const char *default_short,
 					     enum ahead_behind_flags abf)
 {
 	int default_ours = 0, default_theirs = 0;
-	char *default_full = NULL;
-	char *default_short;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return;
 
 	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0) {
-		free(default_short);
-		free(default_full);
+			     &default_ours, &default_theirs, abf) <= 0)
 		return;
-	}
 
 	strbuf_addstr(sb, "\n");
 
@@ -2324,9 +2300,6 @@ static void format_default_branch_comparison(struct strbuf *sb,
 			   default_ours + default_theirs),
 			default_short, default_ours + default_theirs);
 	}
-
-	free(default_short);
-	free(default_full);
 }
 
 /*
@@ -2340,7 +2313,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	int show_default_branch_comparison;
+	char *default_full = NULL;
+	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2352,7 +2326,13 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	show_default_branch_comparison = !is_default_remote_branch(base);
+	default_short = get_default_remote_ref(&default_full);
+	if (default_short && !strcmp(base, default_short)) {
+		free(default_short);
+		free(default_full);
+		default_short = NULL;
+		default_full = NULL;
+	}
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
@@ -2365,8 +2345,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2383,8 +2361,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2396,8 +2372,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2412,10 +2386,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	}
+
+	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
+		format_default_branch_comparison(sb, branch->refname, default_full,
+						 default_short, abf);
+
 	free(base);
+	free(default_short);
+	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:19 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v4 3/4] Use repo.settings.statusGoalBranch config for status comparison

From: Harald Nordgren <haraldnordgren@gmail.com>

Replace hardcoded upstream/origin preference with configurable repo.settings.statusGoalBranch setting. When unset, skip the default branch comparison entirely.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 48 ++++++++++++++++-----------
 t/t6040-tracking-info.sh | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 19 deletions(-)
Show changes to 2 files +100 −19

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index f3831ef3be..edb6d374e7 100644
--- a/remote.c
+++ b/remote.c
@@ -2239,32 +2239,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 
 static char *get_default_remote_ref(char **full_ref_out)
 {
-	int flag;
+	const char *config_value;
 	const char *resolved;
-	static const char *remotes[] = { "upstream", "origin", NULL };
-	int i;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
 
-	for (i = 0; remotes[i]; i++) {
-		struct strbuf head_ref = STRBUF_INIT;
-		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+	if (repo_config_get_value(the_repository, "repo.settings.statusGoalBranch", &config_value))
+		return NULL;
 
-		resolved = refs_resolve_ref_unsafe(
-			get_main_ref_store(the_repository),
-			head_ref.buf,
-			RESOLVE_REF_READING,
-			NULL, &flag);
+	if (!config_value || !*config_value)
+		return NULL;
 
-		strbuf_release(&head_ref);
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+		return NULL;
 
-		if (resolved && (flag & REF_ISSYMREF)) {
-			if (full_ref_out)
-				*full_ref_out = xstrdup(resolved);
-			return refs_shorten_unambiguous_ref(
-				get_main_ref_store(the_repository), resolved, 0);
-		}
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
 	}
 
-	return NULL;
+	strbuf_release(&ref_buf);
+	return ret;
 }
 
 static void format_default_branch_comparison(struct strbuf *sb,
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index e2bd48f858..00dadc03e7 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -309,6 +309,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -325,6 +326,7 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 	(
 		cd test &&
 		git checkout main >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -364,6 +366,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 	(
 		cd test &&
 		git checkout work2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -393,6 +396,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 	(
 		cd test &&
 		git checkout work2b >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -6
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -425,6 +429,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
 		git checkout work3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -450,6 +455,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -477,6 +483,7 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 	(
 		cd test &&
 		git checkout synced_feature >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -504,6 +511,7 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 	(
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -527,6 +535,7 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -538,4 +547,66 @@ EOF
 	test_cmp expect actual
 '
 
+test_expect_success 'status with repo.settings.statusGoalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/feature &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/nonexistent &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:19 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v4 4/4] Rename default_remote to goal_branch

From: Harald Nordgren <haraldnordgren@gmail.com>

Rename variables and functions from 'default' to 'goal' to better reflect that they represent the goal branch from config rather than a default remote. Also move the goal branch lookup to where it's used.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 63 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 32 deletions(-)
Show changes to remote.c +31 −32
diff --git a/remote.c b/remote.c
index edb6d374e7..99c0e18df4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,7 +2237,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-static char *get_default_remote_ref(char **full_ref_out)
+static char *get_goal_branch_ref(char **full_ref_out)
 {
 	const char *config_value;
 	const char *resolved;
@@ -2253,8 +2253,11 @@ static char *get_default_remote_ref(char **full_ref_out)
 		return NULL;
 
 	slash_pos = strchr(config_value, '/');
-	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for repo.settings.statusGoalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
 		return NULL;
+	}
 
 	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
 		    (int)(slash_pos - config_value), config_value,
@@ -2277,38 +2280,38 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return ret;
 }
 
-static void format_default_branch_comparison(struct strbuf *sb,
+static void format_goal_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
-					     const char *default_full,
-					     const char *default_short,
+					     const char *goal_full,
+					     const char *goal_short,
 					     enum ahead_behind_flags abf)
 {
-	int default_ours = 0, default_theirs = 0;
+	int goal_ahead = 0, goal_behind = 0;
 
-	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0)
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
 		return;
 
 	strbuf_addstr(sb, "\n");
 
-	if (default_ours > 0 && default_theirs == 0) {
+	if (goal_ahead > 0 && goal_behind == 0) {
 		strbuf_addf(sb,
 			Q_("Ahead of '%s' by %d commit.\n",
 			   "Ahead of '%s' by %d commits.\n",
-			   default_ours),
-			default_short, default_ours);
-	} else if (default_theirs > 0 && default_ours == 0) {
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Behind '%s' by %d commit.\n",
 			   "Behind '%s' by %d commits.\n",
-			   default_theirs),
-			default_short, default_theirs);
-	} else if (default_ours > 0 && default_theirs > 0) {
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
 		strbuf_addf(sb,
 			Q_("Diverged from '%s' by %d commit.\n",
 			   "Diverged from '%s' by %d commits.\n",
-			   default_ours + default_theirs),
-			default_short, default_ours + default_theirs);
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
 	}
 }
 
@@ -2323,8 +2326,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	char *default_full = NULL;
-	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2336,14 +2337,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	default_short = get_default_remote_ref(&default_full);
-	if (default_short && !strcmp(base, default_short)) {
-		free(default_short);
-		free(default_full);
-		default_short = NULL;
-		default_full = NULL;
-	}
-
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2398,13 +2391,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
 
-	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
-		format_default_branch_comparison(sb, branch->refname, default_full,
-						 default_short, abf);
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
 
 	free(base);
-	free(default_short);
-	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren· Dec 24, 2025, 10:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

> This series seems to punt on
> determining both repository and branch and instead uses a hardcoded
> "upstream" (or "origin") and "HEAD", which is not satisfactory.
I have made a change to the code to not assume "upstream" or "origin" as remote names. Now there is a config setting 'repo.settings.statusGoalBranch' to enable this the check.
I side-stepped the discussion about pushRemotes since what Yee Cheng Chin brought up seems to kill the possibility of determining comparison branch dynamically. Let me know what you think of the latest code and I can handle it if needed 🤗
Show 5 quoted lines
> I don't have that much of an opinion on per-repo / per-branch
> configuration but I just wanted to point out that some branches would
> benefit from the ability to turn this off. Perhaps it could be a two
> layered setting that could be both configured per-repo / per-branch
> but I don't know if this makes it too complicated.
I skipped this for now because I couldn't find a precedent in the sourc code for config settings that are "on repo-wide, but turned off for some branches". I could dig into this if we need it. I would like to hear Junio C Hamano's thoughts on this specific thing too 🤗
Harald
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 0/5] status: show default branch comparison when tracking non-default branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net

Harald Nordgren (5):
  status: show comparison with upstream default branch
  Simplify default branch comparison logic
  Use repo.settings.statusGoalBranch config for status comparison
  Rename default_remote to goal_branch
  Add warning for malformed statusGoalBranch config
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v5
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v4:
 1:  a3800aed18 = 1:  a3800aed18 status: show comparison with upstream default branch
 2:  417f2075fb = 2:  417f2075fb Simplify default branch comparison logic
 3:  c9ec5d9610 = 3:  c9ec5d9610 Use repo.settings.statusGoalBranch config for status comparison
 4:  0e308141da = 4:  0e308141da Rename default_remote to goal_branch
 -:  ---------- > 5:  441678939f Add warning for malformed statusGoalBranch config
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 1/5] status: show comparison with upstream default branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The upstream default branch is determined by checking symbolic refs:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the upstream default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 104 +++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 350 insertions(+)
Show changes to 2 files +350 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..b2a1e980b1 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,98 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_short);
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_short);
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_short);
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2340,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2351,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2365,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2383,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2396,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2412,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 2/5] Simplify default branch comparison logic

From: Harald Nordgren <haraldnordgren@gmail.com>

This maintains the same functionality while reducing ref resolution calls from multiple to one, and eliminating unnecessary memory allocations.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 59 ++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)
Show changes to remote.c +19 −40
diff --git a/remote.c b/remote.c
index b2a1e980b1..f3831ef3be 100644
--- a/remote.c
+++ b/remote.c
@@ -2267,41 +2267,17 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return NULL;
 }
 
-static int is_default_remote_branch(const char *name)
-{
-	char *default_full = NULL;
-	char *default_short;
-	int result = 0;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return 0;
-
-	result = !strcmp(name, default_short);
-
-	free(default_short);
-	free(default_full);
-	return result;
-}
-
 static void format_default_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
+					     const char *default_full,
+					     const char *default_short,
 					     enum ahead_behind_flags abf)
 {
 	int default_ours = 0, default_theirs = 0;
-	char *default_full = NULL;
-	char *default_short;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return;
 
 	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0) {
-		free(default_short);
-		free(default_full);
+			     &default_ours, &default_theirs, abf) <= 0)
 		return;
-	}
 
 	strbuf_addstr(sb, "\n");
 
@@ -2324,9 +2300,6 @@ static void format_default_branch_comparison(struct strbuf *sb,
 			   default_ours + default_theirs),
 			default_short, default_ours + default_theirs);
 	}
-
-	free(default_short);
-	free(default_full);
 }
 
 /*
@@ -2340,7 +2313,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	int show_default_branch_comparison;
+	char *default_full = NULL;
+	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2352,7 +2326,13 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	show_default_branch_comparison = !is_default_remote_branch(base);
+	default_short = get_default_remote_ref(&default_full);
+	if (default_short && !strcmp(base, default_short)) {
+		free(default_short);
+		free(default_full);
+		default_short = NULL;
+		default_full = NULL;
+	}
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
@@ -2365,8 +2345,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2383,8 +2361,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2396,8 +2372,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2412,10 +2386,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	}
+
+	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
+		format_default_branch_comparison(sb, branch->refname, default_full,
+						 default_short, abf);
+
 	free(base);
+	free(default_short);
+	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 3/5] Use repo.settings.statusGoalBranch config for status comparison

From: Harald Nordgren <haraldnordgren@gmail.com>

Replace hardcoded upstream/origin preference with configurable repo.settings.statusGoalBranch setting. When unset, skip the default branch comparison entirely.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 48 ++++++++++++++++-----------
 t/t6040-tracking-info.sh | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 19 deletions(-)
Show changes to 2 files +100 −19

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index f3831ef3be..edb6d374e7 100644
--- a/remote.c
+++ b/remote.c
@@ -2239,32 +2239,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 
 static char *get_default_remote_ref(char **full_ref_out)
 {
-	int flag;
+	const char *config_value;
 	const char *resolved;
-	static const char *remotes[] = { "upstream", "origin", NULL };
-	int i;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
 
-	for (i = 0; remotes[i]; i++) {
-		struct strbuf head_ref = STRBUF_INIT;
-		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+	if (repo_config_get_value(the_repository, "repo.settings.statusGoalBranch", &config_value))
+		return NULL;
 
-		resolved = refs_resolve_ref_unsafe(
-			get_main_ref_store(the_repository),
-			head_ref.buf,
-			RESOLVE_REF_READING,
-			NULL, &flag);
+	if (!config_value || !*config_value)
+		return NULL;
 
-		strbuf_release(&head_ref);
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+		return NULL;
 
-		if (resolved && (flag & REF_ISSYMREF)) {
-			if (full_ref_out)
-				*full_ref_out = xstrdup(resolved);
-			return refs_shorten_unambiguous_ref(
-				get_main_ref_store(the_repository), resolved, 0);
-		}
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
 	}
 
-	return NULL;
+	strbuf_release(&ref_buf);
+	return ret;
 }
 
 static void format_default_branch_comparison(struct strbuf *sb,
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index e2bd48f858..00dadc03e7 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -309,6 +309,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -325,6 +326,7 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 	(
 		cd test &&
 		git checkout main >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -364,6 +366,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 	(
 		cd test &&
 		git checkout work2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -393,6 +396,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 	(
 		cd test &&
 		git checkout work2b >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -6
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -425,6 +429,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
 		git checkout work3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -450,6 +455,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -477,6 +483,7 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 	(
 		cd test &&
 		git checkout synced_feature >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -504,6 +511,7 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 	(
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -527,6 +535,7 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -538,4 +547,66 @@ EOF
 	test_cmp expect actual
 '
 
+test_expect_success 'status with repo.settings.statusGoalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/feature &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/nonexistent &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 4/5] Rename default_remote to goal_branch

From: Harald Nordgren <haraldnordgren@gmail.com>

Rename variables and functions from 'default' to 'goal' to better reflect that they represent the goal branch from config rather than a default remote. Also move the goal branch lookup to where it's used.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 63 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 32 deletions(-)
Show changes to remote.c +31 −32
diff --git a/remote.c b/remote.c
index edb6d374e7..99c0e18df4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,7 +2237,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-static char *get_default_remote_ref(char **full_ref_out)
+static char *get_goal_branch_ref(char **full_ref_out)
 {
 	const char *config_value;
 	const char *resolved;
@@ -2253,8 +2253,11 @@ static char *get_default_remote_ref(char **full_ref_out)
 		return NULL;
 
 	slash_pos = strchr(config_value, '/');
-	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for repo.settings.statusGoalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
 		return NULL;
+	}
 
 	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
 		    (int)(slash_pos - config_value), config_value,
@@ -2277,38 +2280,38 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return ret;
 }
 
-static void format_default_branch_comparison(struct strbuf *sb,
+static void format_goal_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
-					     const char *default_full,
-					     const char *default_short,
+					     const char *goal_full,
+					     const char *goal_short,
 					     enum ahead_behind_flags abf)
 {
-	int default_ours = 0, default_theirs = 0;
+	int goal_ahead = 0, goal_behind = 0;
 
-	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0)
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
 		return;
 
 	strbuf_addstr(sb, "\n");
 
-	if (default_ours > 0 && default_theirs == 0) {
+	if (goal_ahead > 0 && goal_behind == 0) {
 		strbuf_addf(sb,
 			Q_("Ahead of '%s' by %d commit.\n",
 			   "Ahead of '%s' by %d commits.\n",
-			   default_ours),
-			default_short, default_ours);
-	} else if (default_theirs > 0 && default_ours == 0) {
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Behind '%s' by %d commit.\n",
 			   "Behind '%s' by %d commits.\n",
-			   default_theirs),
-			default_short, default_theirs);
-	} else if (default_ours > 0 && default_theirs > 0) {
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
 		strbuf_addf(sb,
 			Q_("Diverged from '%s' by %d commit.\n",
 			   "Diverged from '%s' by %d commits.\n",
-			   default_ours + default_theirs),
-			default_short, default_ours + default_theirs);
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
 	}
 }
 
@@ -2323,8 +2326,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	char *default_full = NULL;
-	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2336,14 +2337,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	default_short = get_default_remote_ref(&default_full);
-	if (default_short && !strcmp(base, default_short)) {
-		free(default_short);
-		free(default_full);
-		default_short = NULL;
-		default_full = NULL;
-	}
-
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2398,13 +2391,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
 
-	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
-		format_default_branch_comparison(sb, branch->refname, default_full,
-						 default_short, abf);
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
 
 	free(base);
-	free(default_short);
-	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 10:38 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v5 5/5] Add warning for malformed statusGoalBranch config

From: Harald Nordgren <haraldnordgren@gmail.com>

Warn when repo.settings.statusGoalBranch has invalid format and skip the goal branch comparison. Also update test to compare full checkout output instead of grepping.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 t/t6040-tracking-info.sh | 49 +++++++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 13 deletions(-)
Show changes to t/t6040-tracking-info.sh +36 −13
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 00dadc03e7..598dd89483 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -310,7 +310,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
@@ -318,6 +318,8 @@ Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
 Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -327,11 +329,13 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 		cd test &&
 		git checkout main >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+		git checkout work 2>&1
 	) >actual &&
 	cat >expect <<-\EOF &&
 Switched to branch '\''work'\''
 Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
 Ahead of '\''origin/main'\'' by 3 commits.
 EOF
 	test_cmp expect actual
@@ -367,7 +371,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 		cd test &&
 		git checkout work2 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work2
@@ -375,6 +379,8 @@ Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
   (use "git push" to publish your local commits)
 
 Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -397,7 +403,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 		cd test &&
 		git checkout work2b >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -6
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work2b
@@ -406,6 +412,8 @@ and have 1 and 1 different commits each, respectively.
   (use "git pull" if you want to integrate the remote branch with yours)
 
 Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -430,7 +438,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 		cd test &&
 		git checkout work3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work3
@@ -438,6 +446,8 @@ Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forw
   (use "git pull" to update your local branch)
 
 Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -456,7 +466,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch upstream/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
@@ -464,6 +474,8 @@ Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
 Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -484,13 +496,15 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 		cd test &&
 		git checkout synced_feature >/dev/null &&
 		git config repo.settings.statusGoalBranch upstream/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -512,13 +526,15 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature2
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -536,13 +552,15 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -552,12 +570,13 @@ test_expect_success 'status with repo.settings.statusGoalBranch unset shows no d
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
-		git status --long -b | head -3
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -567,13 +586,15 @@ test_expect_success 'status with repo.settings.statusGoalBranch set uses configu
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -583,13 +604,14 @@ test_expect_success 'status with repo.settings.statusGoalBranch set to different
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/feature &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
 Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -599,12 +621,13 @@ test_expect_success 'status with repo.settings.statusGoalBranch set to non-exist
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/nonexistent &&
-		git status --long -b | head -3
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 0/6] status: show default branch comparison when tracking non-default branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net

Harald Nordgren (6):
  status: show comparison with upstream default branch
  Simplify default branch comparison logic
  Use repo.settings.statusGoalBranch config for status comparison
  Rename default_remote to goal_branch
  Add warning for malformed statusGoalBranch config
  Change config key to status.compareBranch
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v6
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v5:
 1:  a3800aed18 = 1:  a3800aed18 status: show comparison with upstream default branch
 2:  417f2075fb = 2:  417f2075fb Simplify default branch comparison logic
 3:  c9ec5d9610 = 3:  c9ec5d9610 Use repo.settings.statusGoalBranch config for status comparison
 4:  0e308141da = 4:  0e308141da Rename default_remote to goal_branch
 5:  441678939f = 5:  441678939f Add warning for malformed statusGoalBranch config
 -:  ---------- > 6:  242dbbae44 Change config key to status.compareBranch
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 1/6] status: show comparison with upstream default branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
The upstream default branch is determined by checking symbolic refs:
1. refs/remotes/upstream/HEAD (if upstream remote exists)
2. refs/remotes/origin/HEAD (fallback)

This works with any default branch name (main, master, develop, etc.) as long as the symbolic ref is configured. The comparison is also shown when the branch is up-to-date with its tracking branch but differs from the upstream default branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 104 +++++++++++++++++
 t/t6040-tracking-info.sh | 246 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 350 insertions(+)
Show changes to 2 files +350 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..b2a1e980b1 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,98 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_default_remote_ref(char **full_ref_out)
+{
+	int flag;
+	const char *resolved;
+	static const char *remotes[] = { "upstream", "origin", NULL };
+	int i;
+
+	for (i = 0; remotes[i]; i++) {
+		struct strbuf head_ref = STRBUF_INIT;
+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+
+		resolved = refs_resolve_ref_unsafe(
+			get_main_ref_store(the_repository),
+			head_ref.buf,
+			RESOLVE_REF_READING,
+			NULL, &flag);
+
+		strbuf_release(&head_ref);
+
+		if (resolved && (flag & REF_ISSYMREF)) {
+			if (full_ref_out)
+				*full_ref_out = xstrdup(resolved);
+			return refs_shorten_unambiguous_ref(
+				get_main_ref_store(the_repository), resolved, 0);
+		}
+	}
+
+	return NULL;
+}
+
+static int is_default_remote_branch(const char *name)
+{
+	char *default_full = NULL;
+	char *default_short;
+	int result = 0;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return 0;
+
+	result = !strcmp(name, default_short);
+
+	free(default_short);
+	free(default_full);
+	return result;
+}
+
+static void format_default_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     enum ahead_behind_flags abf)
+{
+	int default_ours = 0, default_theirs = 0;
+	char *default_full = NULL;
+	char *default_short;
+
+	default_short = get_default_remote_ref(&default_full);
+	if (!default_short)
+		return;
+
+	if (stat_branch_pair(branch_refname, default_full,
+			     &default_ours, &default_theirs, abf) <= 0) {
+		free(default_short);
+		free(default_full);
+		return;
+	}
+
+	strbuf_addstr(sb, "\n");
+
+	if (default_ours > 0 && default_theirs == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   default_ours),
+			default_short, default_ours);
+	} else if (default_theirs > 0 && default_ours == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   default_theirs),
+			default_short, default_theirs);
+	} else if (default_ours > 0 && default_theirs > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   default_ours + default_theirs),
+			default_short, default_ours + default_theirs);
+	}
+
+	free(default_short);
+	free(default_full);
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2248,6 +2340,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int show_default_branch_comparison;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2258,6 +2351,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
+	show_default_branch_comparison = !is_default_remote_branch(base);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2269,6 +2365,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2285,6 +2383,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2296,6 +2396,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2310,6 +2412,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		if (show_default_branch_comparison)
+			format_default_branch_comparison(sb, branch->refname, abf);
 	}
 	free(base);
 	return 1;
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..e2bd48f858 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,249 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git status --long -b | head -6
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git status --long -b | head -5
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 2/6] Simplify default branch comparison logic

From: Harald Nordgren <haraldnordgren@gmail.com>

This maintains the same functionality while reducing ref resolution calls from multiple to one, and eliminating unnecessary memory allocations.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 59 ++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)
Show changes to remote.c +19 −40
diff --git a/remote.c b/remote.c
index b2a1e980b1..f3831ef3be 100644
--- a/remote.c
+++ b/remote.c
@@ -2267,41 +2267,17 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return NULL;
 }
 
-static int is_default_remote_branch(const char *name)
-{
-	char *default_full = NULL;
-	char *default_short;
-	int result = 0;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return 0;
-
-	result = !strcmp(name, default_short);
-
-	free(default_short);
-	free(default_full);
-	return result;
-}
-
 static void format_default_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
+					     const char *default_full,
+					     const char *default_short,
 					     enum ahead_behind_flags abf)
 {
 	int default_ours = 0, default_theirs = 0;
-	char *default_full = NULL;
-	char *default_short;
-
-	default_short = get_default_remote_ref(&default_full);
-	if (!default_short)
-		return;
 
 	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0) {
-		free(default_short);
-		free(default_full);
+			     &default_ours, &default_theirs, abf) <= 0)
 		return;
-	}
 
 	strbuf_addstr(sb, "\n");
 
@@ -2324,9 +2300,6 @@ static void format_default_branch_comparison(struct strbuf *sb,
 			   default_ours + default_theirs),
 			default_short, default_ours + default_theirs);
 	}
-
-	free(default_short);
-	free(default_full);
 }
 
 /*
@@ -2340,7 +2313,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	int show_default_branch_comparison;
+	char *default_full = NULL;
+	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2352,7 +2326,13 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	show_default_branch_comparison = !is_default_remote_branch(base);
+	default_short = get_default_remote_ref(&default_full);
+	if (default_short && !strcmp(base, default_short)) {
+		free(default_short);
+		free(default_full);
+		default_short = NULL;
+		default_full = NULL;
+	}
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
@@ -2365,8 +2345,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
@@ -2383,8 +2361,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else if (!ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
@@ -2396,8 +2372,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	} else {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
@@ -2412,10 +2386,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
-		if (show_default_branch_comparison)
-			format_default_branch_comparison(sb, branch->refname, abf);
 	}
+
+	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
+		format_default_branch_comparison(sb, branch->refname, default_full,
+						 default_short, abf);
+
 	free(base);
+	free(default_short);
+	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 3/6] Use repo.settings.statusGoalBranch config for status comparison

From: Harald Nordgren <haraldnordgren@gmail.com>

Replace hardcoded upstream/origin preference with configurable repo.settings.statusGoalBranch setting. When unset, skip the default branch comparison entirely.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 48 ++++++++++++++++-----------
 t/t6040-tracking-info.sh | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 19 deletions(-)
Show changes to 2 files +100 −19

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index f3831ef3be..edb6d374e7 100644
--- a/remote.c
+++ b/remote.c
@@ -2239,32 +2239,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 
 static char *get_default_remote_ref(char **full_ref_out)
 {
-	int flag;
+	const char *config_value;
 	const char *resolved;
-	static const char *remotes[] = { "upstream", "origin", NULL };
-	int i;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
 
-	for (i = 0; remotes[i]; i++) {
-		struct strbuf head_ref = STRBUF_INIT;
-		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
+	if (repo_config_get_value(the_repository, "repo.settings.statusGoalBranch", &config_value))
+		return NULL;
 
-		resolved = refs_resolve_ref_unsafe(
-			get_main_ref_store(the_repository),
-			head_ref.buf,
-			RESOLVE_REF_READING,
-			NULL, &flag);
+	if (!config_value || !*config_value)
+		return NULL;
 
-		strbuf_release(&head_ref);
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+		return NULL;
 
-		if (resolved && (flag & REF_ISSYMREF)) {
-			if (full_ref_out)
-				*full_ref_out = xstrdup(resolved);
-			return refs_shorten_unambiguous_ref(
-				get_main_ref_store(the_repository), resolved, 0);
-		}
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
 	}
 
-	return NULL;
+	strbuf_release(&ref_buf);
+	return ret;
 }
 
 static void format_default_branch_comparison(struct strbuf *sb,
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index e2bd48f858..00dadc03e7 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -309,6 +309,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -325,6 +326,7 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 	(
 		cd test &&
 		git checkout main >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -364,6 +366,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 	(
 		cd test &&
 		git checkout work2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -393,6 +396,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 	(
 		cd test &&
 		git checkout work2b >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -6
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -425,6 +429,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
 		git checkout work3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -450,6 +455,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -5
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -477,6 +483,7 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 	(
 		cd test &&
 		git checkout synced_feature >/dev/null &&
+		git config repo.settings.statusGoalBranch upstream/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -504,6 +511,7 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 	(
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -527,6 +535,7 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
 		git status --long -b | head -4
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -538,4 +547,66 @@ EOF
 	test_cmp expect actual
 '
 
+test_expect_success 'status with repo.settings.statusGoalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/main &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/feature &&
+		git status --long -b | head -4
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with repo.settings.statusGoalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config repo.settings.statusGoalBranch origin/nonexistent &&
+		git status --long -b | head -3
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 4/6] Rename default_remote to goal_branch

From: Harald Nordgren <haraldnordgren@gmail.com>

Rename variables and functions from 'default' to 'goal' to better reflect that they represent the goal branch from config rather than a default remote. Also move the goal branch lookup to where it's used.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 63 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 32 deletions(-)
Show changes to remote.c +31 −32
diff --git a/remote.c b/remote.c
index edb6d374e7..99c0e18df4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,7 +2237,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-static char *get_default_remote_ref(char **full_ref_out)
+static char *get_goal_branch_ref(char **full_ref_out)
 {
 	const char *config_value;
 	const char *resolved;
@@ -2253,8 +2253,11 @@ static char *get_default_remote_ref(char **full_ref_out)
 		return NULL;
 
 	slash_pos = strchr(config_value, '/');
-	if (!slash_pos || slash_pos == config_value || !slash_pos[1])
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for repo.settings.statusGoalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
 		return NULL;
+	}
 
 	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
 		    (int)(slash_pos - config_value), config_value,
@@ -2277,38 +2280,38 @@ static char *get_default_remote_ref(char **full_ref_out)
 	return ret;
 }
 
-static void format_default_branch_comparison(struct strbuf *sb,
+static void format_goal_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
-					     const char *default_full,
-					     const char *default_short,
+					     const char *goal_full,
+					     const char *goal_short,
 					     enum ahead_behind_flags abf)
 {
-	int default_ours = 0, default_theirs = 0;
+	int goal_ahead = 0, goal_behind = 0;
 
-	if (stat_branch_pair(branch_refname, default_full,
-			     &default_ours, &default_theirs, abf) <= 0)
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
 		return;
 
 	strbuf_addstr(sb, "\n");
 
-	if (default_ours > 0 && default_theirs == 0) {
+	if (goal_ahead > 0 && goal_behind == 0) {
 		strbuf_addf(sb,
 			Q_("Ahead of '%s' by %d commit.\n",
 			   "Ahead of '%s' by %d commits.\n",
-			   default_ours),
-			default_short, default_ours);
-	} else if (default_theirs > 0 && default_ours == 0) {
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Behind '%s' by %d commit.\n",
 			   "Behind '%s' by %d commits.\n",
-			   default_theirs),
-			default_short, default_theirs);
-	} else if (default_ours > 0 && default_theirs > 0) {
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
 		strbuf_addf(sb,
 			Q_("Diverged from '%s' by %d commit.\n",
 			   "Diverged from '%s' by %d commits.\n",
-			   default_ours + default_theirs),
-			default_short, default_ours + default_theirs);
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
 	}
 }
 
@@ -2323,8 +2326,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
-	char *default_full = NULL;
-	char *default_short = NULL;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2336,14 +2337,6 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
-	default_short = get_default_remote_ref(&default_full);
-	if (default_short && !strcmp(base, default_short)) {
-		free(default_short);
-		free(default_full);
-		default_short = NULL;
-		default_full = NULL;
-	}
-
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2398,13 +2391,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
 
-	if (default_short && !upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK)
-		format_default_branch_comparison(sb, branch->refname, default_full,
-						 default_short, abf);
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
 
 	free(base);
-	free(default_short);
-	free(default_full);
 	return 1;
 }
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 5/6] Add warning for malformed statusGoalBranch config

From: Harald Nordgren <haraldnordgren@gmail.com>

Warn when repo.settings.statusGoalBranch has invalid format and skip the goal branch comparison. Also update test to compare full checkout output instead of grepping.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 t/t6040-tracking-info.sh | 49 +++++++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 13 deletions(-)
Show changes to t/t6040-tracking-info.sh +36 −13
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 00dadc03e7..598dd89483 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -310,7 +310,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
@@ -318,6 +318,8 @@ Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
 Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -327,11 +329,13 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 		cd test &&
 		git checkout main >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
+		git checkout work 2>&1
 	) >actual &&
 	cat >expect <<-\EOF &&
 Switched to branch '\''work'\''
 Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
 Ahead of '\''origin/main'\'' by 3 commits.
 EOF
 	test_cmp expect actual
@@ -367,7 +371,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 		cd test &&
 		git checkout work2 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work2
@@ -375,6 +379,8 @@ Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
   (use "git push" to publish your local commits)
 
 Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -397,7 +403,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 		cd test &&
 		git checkout work2b >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -6
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work2b
@@ -406,6 +412,8 @@ and have 1 and 1 different commits each, respectively.
   (use "git pull" if you want to integrate the remote branch with yours)
 
 Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -430,7 +438,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 		cd test &&
 		git checkout work3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work3
@@ -438,6 +446,8 @@ Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forw
   (use "git pull" to update your local branch)
 
 Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -456,7 +466,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch upstream/main &&
-		git status --long -b | head -5
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
@@ -464,6 +474,8 @@ Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
 Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -484,13 +496,15 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 		cd test &&
 		git checkout synced_feature >/dev/null &&
 		git config repo.settings.statusGoalBranch upstream/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -512,13 +526,15 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature2
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -536,13 +552,15 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -552,12 +570,13 @@ test_expect_success 'status with repo.settings.statusGoalBranch unset shows no d
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
-		git status --long -b | head -3
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -567,13 +586,15 @@ test_expect_success 'status with repo.settings.statusGoalBranch set uses configu
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/main &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
 Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -583,13 +604,14 @@ test_expect_success 'status with repo.settings.statusGoalBranch set to different
 		cd test &&
 		git checkout work >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/feature &&
-		git status --long -b | head -4
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch work
 Your branch is ahead of '\''origin/feature'\'' by 2 commits.
   (use "git push" to publish your local commits)
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
@@ -599,12 +621,13 @@ test_expect_success 'status with repo.settings.statusGoalBranch set to non-exist
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
 		git config repo.settings.statusGoalBranch origin/nonexistent &&
-		git status --long -b | head -3
+		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
 On branch synced_feature3
 Your branch is up to date with '\''origin/feature'\''.
 
+nothing to commit, working tree clean
 EOF
 	test_cmp expect actual
 '
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 24, 2025, 23:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v6 6/6] Change config key to status.compareBranch

From: Harald Nordgren <haraldnordgren@gmail.com>

Update from repo.settings.statusGoalBranch to follow standard git config pattern status.compareBranch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  4 ++--
 t/t6040-tracking-info.sh | 34 +++++++++++++++++-----------------
 2 files changed, 19 insertions(+), 19 deletions(-)
Show changes to 2 files +19 −19

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 99c0e18df4..7e13c027b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2246,7 +2246,7 @@ static char *get_goal_branch_ref(char **full_ref_out)
 	char *slash_pos;
 	char *ret = NULL;
 
-	if (repo_config_get_value(the_repository, "repo.settings.statusGoalBranch", &config_value))
+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
 		return NULL;
 
 	if (!config_value || !*config_value)
@@ -2254,7 +2254,7 @@ static char *get_goal_branch_ref(char **full_ref_out)
 
 	slash_pos = strchr(config_value, '/');
 	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
-		warning(_("invalid value for repo.settings.statusGoalBranch: '%s' (expected format: remote/branch)"),
+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
 			config_value);
 		return NULL;
 	}
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 598dd89483..fe34ddf0ab 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -309,7 +309,7 @@ test_expect_success 'status shows ahead of both tracked branch and origin/main'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -328,7 +328,7 @@ test_expect_success 'checkout shows ahead of both tracked branch and origin/main
 	(
 		cd test &&
 		git checkout main >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git checkout work 2>&1
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -370,7 +370,7 @@ test_expect_success 'status shows ahead of tracked and diverged from origin/main
 	(
 		cd test &&
 		git checkout work2 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -402,7 +402,7 @@ test_expect_success 'status shows diverged from tracked and behind origin/main'
 	(
 		cd test &&
 		git checkout work2b >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -437,7 +437,7 @@ test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
 		git checkout work3 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -465,7 +465,7 @@ test_expect_success 'status prefers upstream remote over origin for comparison'
 	(
 		cd test &&
 		git checkout work >/dev/null &&
-		git config repo.settings.statusGoalBranch upstream/main &&
+		git config status.goalBranch upstream/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -495,7 +495,7 @@ test_expect_success 'status shows up to date with tracked but diverged from defa
 	(
 		cd test &&
 		git checkout synced_feature >/dev/null &&
-		git config repo.settings.statusGoalBranch upstream/main &&
+		git config status.goalBranch upstream/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -525,7 +525,7 @@ test_expect_success 'status shows up to date with tracked but diverged from orig
 	(
 		cd test &&
 		git checkout synced_feature2 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -551,7 +551,7 @@ test_expect_success 'status shows up to date with tracked but shows default bran
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -565,11 +565,11 @@ EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with repo.settings.statusGoalBranch unset shows no default comparison' '
+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
-		git config --unset repo.settings.statusGoalBranch 2>/dev/null || true &&
+		git config --unset status.goalBranch 2>/dev/null || true &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -581,11 +581,11 @@ EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with repo.settings.statusGoalBranch set uses configured branch' '
+test_expect_success 'status with status.goalBranch set uses configured branch' '
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/main &&
+		git config status.goalBranch origin/main &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -599,11 +599,11 @@ EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with repo.settings.statusGoalBranch set to different remote/branch' '
+test_expect_success 'status with status.goalBranch set to different remote/branch' '
 	(
 		cd test &&
 		git checkout work >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/feature &&
+		git config status.goalBranch origin/feature &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
@@ -616,11 +616,11 @@ EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with repo.settings.statusGoalBranch set to non-existent branch' '
+test_expect_success 'status with status.goalBranch set to non-existent branch' '
 	(
 		cd test &&
 		git checkout synced_feature3 >/dev/null &&
-		git config repo.settings.statusGoalBranch origin/nonexistent &&
+		git config status.goalBranch origin/nonexistent &&
 		git status --long -b
 	) >actual &&
 	cat >expect <<-\EOF &&
-- 
gitgitgadget
Junio C Hamano· Dec 25, 2025, 08:00 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v6 0/6] status: show default branch comparison when tracking non-default branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 10 quoted lines
> cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
> ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net
>
> Harald Nordgren (6):
>   status: show comparison with upstream default branch
>   Simplify default branch comparison logic
>   Use repo.settings.statusGoalBranch config for status comparison
>   Rename default_remote to goal_branch
>   Add warning for malformed statusGoalBranch config
>   Change config key to status.compareBranch

It seems that [v6 6/6] smells like an "oops, what I did in [v6 3/6] was wrong, and this is an incremental fix on top of it".

Please don't.

When presenting your topic to the list, rather, after you finish a series and the end result reaches a satisfactory state, please look back and polish patches to hide such mistakes in the middle, pretend as if you are a perfect developer who wrote a logical progression of patches that goes straight to the goal without stumbling around, taking detours, and making mistakes you need to correct in a later step. Detours may have been taken when you initially wrote the series, and it may show the "true history" from your point of view, but to others (and the most importantly, to those who read "git log -p" later in order to extend your work to suit their needs better), they are merely distracting.

The titles in [2-6/6] by the way do not seem to follow the established convention, like [1/6] does, i.e. "status: show comparison...", use an area prefix "<area>:", followed by a short-summary that is not capitalized.

Thanks.
Harald Nordgren via GitGitGadget· Dec 25, 2025, 09:45 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v7] status: additional comparison with goal branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead" (i.e. you have built new history, while others are not touching it), "behind" (i.e. you haven't added any work since you were in-sync, while others have added their work on the branch), "diverged" (i.e. you have commits that you haven't pushed out, while others have added commits).

When you fork a branch 'feature' from the 'main' branch of the remote, but then create 'feature' branch at the remote and push there, while you still occasionally pull from or rebase onto their 'main', you'd also want to know how much you have diverged from 'main', in addition to how your 'feature' and their 'feature' compares. Currently the comparison with 'main' is not given, making it hard to know when to start thinking about rebasing onto the upstream default branch.

Show two sets of comparison: one with the tracking branch (as
before), and another with the upstream's default branch (what
their HEAD points to, typically 'main' or 'master'). The latter
comparison appears on a separate line after the tracking branch
status, using the same format:
- "Ahead of 'origin/main' by N commits" when purely ahead
- "Behind 'origin/main' by N commits" when purely behind
- "Diverged from 'origin/main' by N commits" when diverged
Example output when tracking a feature branch:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'origin/main' by 5 commits.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    status: show default branch comparison when tracking non-default branch
    
    cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
    ychin.macvim@gmail.com cc: "brian m. carlson"
    sandals@crustytoothpaste.net
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v7
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v7
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v6:
 1:  a3800aed18 ! 1:  04e2fb76ee status: show comparison with upstream default branch
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    status: show comparison with upstream default branch
     +    status: additional comparison with goal branch
      
          "git status" on a branch that follows a remote branch compares
          commits on the current branch and the remote-tracking branch it
     @@ Commit message
      
              Ahead of 'origin/main' by 5 commits.
      
     -    The upstream default branch is determined by checking symbolic refs:
     -    1. refs/remotes/upstream/HEAD (if upstream remote exists)
     -    2. refs/remotes/origin/HEAD (fallback)
     -
     -    This works with any default branch name (main, master, develop,
     -    etc.) as long as the symbolic ref is configured. The comparison
     -    is also shown when the branch is up-to-date with its tracking
     -    branch but differs from the upstream default branch.
     -
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## remote.c ##
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
       
     -+static char *get_default_remote_ref(char **full_ref_out)
     ++static char *get_goal_branch_ref(char **full_ref_out)
      +{
     -+	int flag;
     ++	const char *config_value;
      +	const char *resolved;
     -+	static const char *remotes[] = { "upstream", "origin", NULL };
     -+	int i;
     -+
     -+	for (i = 0; remotes[i]; i++) {
     -+		struct strbuf head_ref = STRBUF_INIT;
     -+		strbuf_addf(&head_ref, "refs/remotes/%s/HEAD", remotes[i]);
     -+
     -+		resolved = refs_resolve_ref_unsafe(
     -+			get_main_ref_store(the_repository),
     -+			head_ref.buf,
     -+			RESOLVE_REF_READING,
     -+			NULL, &flag);
     -+
     -+		strbuf_release(&head_ref);
     -+
     -+		if (resolved && (flag & REF_ISSYMREF)) {
     -+			if (full_ref_out)
     -+				*full_ref_out = xstrdup(resolved);
     -+			return refs_shorten_unambiguous_ref(
     -+				get_main_ref_store(the_repository), resolved, 0);
     -+		}
     -+	}
     ++	int flag;
     ++	struct strbuf ref_buf = STRBUF_INIT;
     ++	char *slash_pos;
     ++	char *ret = NULL;
      +
     -+	return NULL;
     -+}
     ++	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
     ++		return NULL;
      +
     -+static int is_default_remote_branch(const char *name)
     -+{
     -+	char *default_full = NULL;
     -+	char *default_short;
     -+	int result = 0;
     ++	if (!config_value || !*config_value)
     ++		return NULL;
      +
     -+	default_short = get_default_remote_ref(&default_full);
     -+	if (!default_short)
     -+		return 0;
     ++	slash_pos = strchr(config_value, '/');
     ++	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
     ++		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
     ++			config_value);
     ++		return NULL;
     ++	}
      +
     -+	result = !strcmp(name, default_short);
     ++	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
     ++		    (int)(slash_pos - config_value), config_value,
     ++		    slash_pos + 1);
     ++
     ++	resolved = refs_resolve_ref_unsafe(
     ++		get_main_ref_store(the_repository),
     ++		ref_buf.buf,
     ++		RESOLVE_REF_READING,
     ++		NULL, &flag);
     ++
     ++	if (resolved) {
     ++		if (full_ref_out)
     ++			*full_ref_out = xstrdup(resolved);
     ++		ret = refs_shorten_unambiguous_ref(
     ++			get_main_ref_store(the_repository), resolved, 0);
     ++	}
      +
     -+	free(default_short);
     -+	free(default_full);
     -+	return result;
     ++	strbuf_release(&ref_buf);
     ++	return ret;
      +}
      +
     -+static void format_default_branch_comparison(struct strbuf *sb,
     ++static void format_goal_branch_comparison(struct strbuf *sb,
      +					     const char *branch_refname,
     ++					     const char *goal_full,
     ++					     const char *goal_short,
      +					     enum ahead_behind_flags abf)
      +{
     -+	int default_ours = 0, default_theirs = 0;
     -+	char *default_full = NULL;
     -+	char *default_short;
     -+
     -+	default_short = get_default_remote_ref(&default_full);
     -+	if (!default_short)
     -+		return;
     ++	int goal_ahead = 0, goal_behind = 0;
      +
     -+	if (stat_branch_pair(branch_refname, default_full,
     -+			     &default_ours, &default_theirs, abf) <= 0) {
     -+		free(default_short);
     -+		free(default_full);
     ++	if (stat_branch_pair(branch_refname, goal_full,
     ++			     &goal_ahead, &goal_behind, abf) <= 0)
      +		return;
     -+	}
      +
      +	strbuf_addstr(sb, "\n");
      +
     -+	if (default_ours > 0 && default_theirs == 0) {
     ++	if (goal_ahead > 0 && goal_behind == 0) {
      +		strbuf_addf(sb,
      +			Q_("Ahead of '%s' by %d commit.\n",
      +			   "Ahead of '%s' by %d commits.\n",
     -+			   default_ours),
     -+			default_short, default_ours);
     -+	} else if (default_theirs > 0 && default_ours == 0) {
     ++			   goal_ahead),
     ++			goal_short, goal_ahead);
     ++	} else if (goal_behind > 0 && goal_ahead == 0) {
      +		strbuf_addf(sb,
      +			Q_("Behind '%s' by %d commit.\n",
      +			   "Behind '%s' by %d commits.\n",
     -+			   default_theirs),
     -+			default_short, default_theirs);
     -+	} else if (default_ours > 0 && default_theirs > 0) {
     ++			   goal_behind),
     ++			goal_short, goal_behind);
     ++	} else if (goal_ahead > 0 && goal_behind > 0) {
      +		strbuf_addf(sb,
      +			Q_("Diverged from '%s' by %d commit.\n",
      +			   "Diverged from '%s' by %d commits.\n",
     -+			   default_ours + default_theirs),
     -+			default_short, default_ours + default_theirs);
     ++			   goal_ahead + goal_behind),
     ++			goal_short, goal_ahead + goal_behind);
      +	}
     -+
     -+	free(default_short);
     -+	free(default_full);
      +}
      +
       /*
        * Return true when there is anything to report, otherwise false.
        */
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 	const char *full_base;
     - 	char *base;
     - 	int upstream_is_gone = 0;
     -+	int show_default_branch_comparison;
     - 
     - 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     - 	if (sti < 0) {
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       
       	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
       					    full_base, 0);
     -+
     -+	show_default_branch_comparison = !is_default_remote_branch(base);
      +
       	if (upstream_is_gone) {
       		strbuf_addf(sb,
       			_("Your branch is based on '%s', but the upstream is gone.\n"),
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 		strbuf_addf(sb,
     - 			_("Your branch is up to date with '%s'.\n"),
     - 			base);
     -+		if (show_default_branch_comparison)
     -+			format_default_branch_comparison(sb, branch->refname, abf);
     - 	} else if (abf == AHEAD_BEHIND_QUICK) {
     - 		strbuf_addf(sb,
     - 			    _("Your branch and '%s' refer to different commits.\n"),
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 		if (advice_enabled(ADVICE_STATUS_HINTS))
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git push\" to publish your local commits)\n"));
     -+		if (show_default_branch_comparison)
     -+			format_default_branch_comparison(sb, branch->refname, abf);
     - 	} else if (!ours) {
     - 		strbuf_addf(sb,
     - 			Q_("Your branch is behind '%s' by %d commit, "
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 		if (advice_enabled(ADVICE_STATUS_HINTS))
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git pull\" to update your local branch)\n"));
     -+		if (show_default_branch_comparison)
     -+			format_default_branch_comparison(sb, branch->refname, abf);
     - 	} else {
     - 		strbuf_addf(sb,
     - 			Q_("Your branch and '%s' have diverged,\n"
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 		    advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
     -+		if (show_default_branch_comparison)
     -+			format_default_branch_comparison(sb, branch->refname, abf);
       	}
     ++
     ++	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
     ++		char *goal_full = NULL;
     ++		char *goal_short = get_goal_branch_ref(&goal_full);
     ++
     ++		if (goal_short && strcmp(base, goal_short))
     ++			format_goal_branch_comparison(sb, branch->refname, goal_full,
     ++						     goal_short, abf);
     ++
     ++		free(goal_short);
     ++		free(goal_full);
     ++	}
     ++
       	free(base);
       	return 1;
     + }
      
       ## t/t6040-tracking-info.sh ##
      @@ t/t6040-tracking-info.sh: test_expect_success setup '
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout work >/dev/null &&
     -+		git status --long -b | head -5
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch work
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +  (use "git push" to publish your local commits)
      +
      +Ahead of '\''origin/main'\'' by 3 commits.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout main >/dev/null &&
     -+		git checkout work 2>&1 | grep -E "(Switched|Your branch|Ahead of)" | head -3
     ++		git config status.goalBranch origin/main &&
     ++		git checkout work 2>&1
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +Switched to branch '\''work'\''
      +Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     ++  (use "git push" to publish your local commits)
     ++
      +Ahead of '\''origin/main'\'' by 3 commits.
      +EOF
      +	test_cmp expect actual
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout work2 >/dev/null &&
     -+		git status --long -b | head -5
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch work2
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +  (use "git push" to publish your local commits)
      +
      +Diverged from '\''origin/main'\'' by 3 commits.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout work2b >/dev/null &&
     -+		git status --long -b | head -6
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch work2b
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +  (use "git pull" if you want to integrate the remote branch with yours)
      +
      +Behind '\''origin/main'\'' by 1 commit.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout work3 >/dev/null &&
     -+		git status --long -b | head -5
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch work3
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +  (use "git pull" to update your local branch)
      +
      +Ahead of '\''origin/main'\'' by 1 commit.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout work >/dev/null &&
     -+		git status --long -b | head -5
     ++		git config status.goalBranch upstream/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch work
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +  (use "git push" to publish your local commits)
      +
      +Diverged from '\''upstream/main'\'' by 5 commits.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout synced_feature >/dev/null &&
     -+		git status --long -b | head -4
     ++		git config status.goalBranch upstream/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch synced_feature
      +Your branch is up to date with '\''origin/feature'\''.
      +
      +Diverged from '\''upstream/main'\'' by 3 commits.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout synced_feature2 >/dev/null &&
     -+		git status --long -b | head -4
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch synced_feature2
      +Your branch is up to date with '\''origin/feature'\''.
      +
      +Diverged from '\''origin/main'\'' by 5 commits.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git checkout synced_feature3 >/dev/null &&
     -+		git status --long -b | head -4
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
     ++	) >actual &&
     ++	cat >expect <<-\EOF &&
     ++On branch synced_feature3
     ++Your branch is up to date with '\''origin/feature'\''.
     ++
     ++Diverged from '\''origin/main'\'' by 5 commits.
     ++
     ++nothing to commit, working tree clean
     ++EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status with status.goalBranch unset shows no default comparison' '
     ++	(
     ++		cd test &&
     ++		git checkout synced_feature3 >/dev/null &&
     ++		git config --unset status.goalBranch 2>/dev/null || true &&
     ++		git status --long -b
     ++	) >actual &&
     ++	cat >expect <<-\EOF &&
     ++On branch synced_feature3
     ++Your branch is up to date with '\''origin/feature'\''.
     ++
     ++nothing to commit, working tree clean
     ++EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status with status.goalBranch set uses configured branch' '
     ++	(
     ++		cd test &&
     ++		git checkout synced_feature3 >/dev/null &&
     ++		git config status.goalBranch origin/main &&
     ++		git status --long -b
      +	) >actual &&
      +	cat >expect <<-\EOF &&
      +On branch synced_feature3
      +Your branch is up to date with '\''origin/feature'\''.
      +
      +Diverged from '\''origin/main'\'' by 5 commits.
     ++
     ++nothing to commit, working tree clean
     ++EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status with status.goalBranch set to different remote/branch' '
     ++	(
     ++		cd test &&
     ++		git checkout work >/dev/null &&
     ++		git config status.goalBranch origin/feature &&
     ++		git status --long -b
     ++	) >actual &&
     ++	cat >expect <<-\EOF &&
     ++On branch work
     ++Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     ++  (use "git push" to publish your local commits)
     ++
     ++nothing to commit, working tree clean
     ++EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status with status.goalBranch set to non-existent branch' '
     ++	(
     ++		cd test &&
     ++		git checkout synced_feature3 >/dev/null &&
     ++		git config status.goalBranch origin/nonexistent &&
     ++		git status --long -b
     ++	) >actual &&
     ++	cat >expect <<-\EOF &&
     ++On branch synced_feature3
     ++Your branch is up to date with '\''origin/feature'\''.
     ++
     ++nothing to commit, working tree clean
      +EOF
      +	test_cmp expect actual
      +'
 2:  417f2075fb < -:  ---------- Simplify default branch comparison logic
 3:  c9ec5d9610 < -:  ---------- Use repo.settings.statusGoalBranch config for status comparison
 4:  0e308141da < -:  ---------- Rename default_remote to goal_branch
 5:  441678939f < -:  ---------- Add warning for malformed statusGoalBranch config
 6:  242dbbae44 < -:  ---------- Change config key to status.compareBranch
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
Show changes to 2 files +432 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..7e13c027b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,84 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_goal_branch_ref(char **full_ref_out)
+{
+	const char *config_value;
+	const char *resolved;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
+
+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
+		return NULL;
+
+	if (!config_value || !*config_value)
+		return NULL;
+
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
+		return NULL;
+	}
+
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
+static void format_goal_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     const char *goal_full,
+					     const char *goal_short,
+					     enum ahead_behind_flags abf)
+{
+	int goal_ahead = 0, goal_behind = 0;
+
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
+		return;
+
+	strbuf_addstr(sb, "\n");
+
+	if (goal_ahead > 0 && goal_behind == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
+	}
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2258,6 +2336,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2311,6 +2390,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..fe34ddf0ab 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,343 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git checkout work 2>&1
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset status.goalBranch 2>/dev/null || true &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/feature &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/nonexistent &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
 test_done

base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 25, 2025, 12:33 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v8] status: show comparison with configured goal branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to a goal branch (e.g., upstream/main), you can configure status.goalBranch to show an additional comparison.

Add support for status.goalBranch configuration option that specifies
a remote/branch to compare against. When set, git status shows both
the comparison with the tracking branch (as before) and an additional
comparison with the configured goal branch, if it differs from the
tracking branch. The goal branch comparison appears on a separate
line after the tracking branch status, using the same format:
- "Ahead of 'upstream/main' by N commits" when purely ahead
- "Behind 'upstream/main' by N commits" when purely behind
- "Diverged from 'upstream/main' by N commits" when diverged
Example output when tracking a feature branch with status.goalBranch
set to upstream/main:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'upstream/main' by 5 commits.

The comparison is only shown when status.goalBranch is configured and the goal branch differs from the tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    status: show comparison with configured goal branch
    
    cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
    ychin.macvim@gmail.com cc: "brian m. carlson"
    sandals@crustytoothpaste.net
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v8
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v8
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v7:
 1:  04e2fb76ee ! 1:  7e2574d5ae status: additional comparison with goal branch
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    status: additional comparison with goal branch
     +    status: show comparison with configured goal branch
      
          "git status" on a branch that follows a remote branch compares
          commits on the current branch and the remote-tracking branch it
     -    builds upon, to show "ahead" (i.e. you have built new history,
     -    while others are not touching it), "behind" (i.e. you haven't
     -    added any work since you were in-sync, while others have added
     -    their work on the branch), "diverged" (i.e. you have commits
     -    that you haven't pushed out, while others have added commits).
     +    builds upon, to show "ahead", "behind", or "diverged" status.
      
     -    When you fork a branch 'feature' from the 'main' branch of the
     -    remote, but then create 'feature' branch at the remote and push
     -    there, while you still occasionally pull from or rebase onto
     -    their 'main', you'd also want to know how much you have diverged
     -    from 'main', in addition to how your 'feature' and their
     -    'feature' compares. Currently the comparison with 'main' is not
     -    given, making it hard to know when to start thinking about
     -    rebasing onto the upstream default branch.
     +    When working on a feature branch that tracks a remote feature branch,
     +    but you also want to track progress relative to a goal branch (e.g.,
     +    upstream/main), you can configure status.goalBranch to show an
     +    additional comparison.
      
     -    Show two sets of comparison: one with the tracking branch (as
     -    before), and another with the upstream's default branch (what
     -    their HEAD points to, typically 'main' or 'master'). The latter
     -    comparison appears on a separate line after the tracking branch
     -    status, using the same format:
     -    - "Ahead of 'origin/main' by N commits" when purely ahead
     -    - "Behind 'origin/main' by N commits" when purely behind
     -    - "Diverged from 'origin/main' by N commits" when diverged
     +    Add support for status.goalBranch configuration option that specifies
     +    a remote/branch to compare against. When set, git status shows both
     +    the comparison with the tracking branch (as before) and an additional
     +    comparison with the configured goal branch, if it differs from the
     +    tracking branch. The goal branch comparison appears on a separate
     +    line after the tracking branch status, using the same format:
     +    - "Ahead of 'upstream/main' by N commits" when purely ahead
     +    - "Behind 'upstream/main' by N commits" when purely behind
     +    - "Diverged from 'upstream/main' by N commits" when diverged
      
     -    Example output when tracking a feature branch:
     +    Example output when tracking a feature branch with status.goalBranch
     +    set to upstream/main:
              On branch feature
              Your branch is ahead of 'origin/feature' by 2 commits.
                (use "git push" to publish your local commits)
      
     -        Ahead of 'origin/main' by 5 commits.
     +        Ahead of 'upstream/main' by 5 commits.
     +
     +    The comparison is only shown when status.goalBranch is configured
     +    and the goal branch differs from the tracking branch.
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
Show changes to 2 files +432 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..7e13c027b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,84 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_goal_branch_ref(char **full_ref_out)
+{
+	const char *config_value;
+	const char *resolved;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
+
+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
+		return NULL;
+
+	if (!config_value || !*config_value)
+		return NULL;
+
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
+		return NULL;
+	}
+
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
+static void format_goal_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     const char *goal_full,
+					     const char *goal_short,
+					     enum ahead_behind_flags abf)
+{
+	int goal_ahead = 0, goal_behind = 0;
+
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
+		return;
+
+	strbuf_addstr(sb, "\n");
+
+	if (goal_ahead > 0 && goal_behind == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
+	}
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2258,6 +2336,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2311,6 +2390,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..fe34ddf0ab 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,343 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git checkout work 2>&1
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset status.goalBranch 2>/dev/null || true &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/feature &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/nonexistent &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
 test_done

base-commit: c4a0c8845e2426375ad257b6c221a3a7d92ecfda
-- 
gitgitgadget
Junio C Hamano· Dec 26, 2025, 01:59 UTC · re: Harald Nordgren · lore

Re: [PATCH] status: show default branch comparison when tracking non-default branch

Harald Nordgren <haraldnordgren@gmail.com> writes:
> Got it! I'd rather just squash all the commits then, the total diff is
> quite small still.

Well, that is quite different from what I meant, but let's see what others may say.

Happy holidays.
Junio C Hamano· Dec 28, 2025, 11:46 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v8] status: show comparison with configured goal branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
> +test_expect_success 'status shows ahead of both tracked branch and origin/main' '
> +	(
> +		cd test &&
> +		git checkout work >/dev/null &&
What is this redirecction for?
> +		git config status.goalBranch origin/main &&
> +		git status --long -b
> +	) >actual &&

Instead of redirecting the whole thing, if you are grabbing the output from "git status", do it more like this, probably:

	(
		cd test &&
		... &&
		git status --long --branch >../actual
	)
> +	cat >expect <<-\EOF &&

Looking up what "<<-\EOF" means, it makes little sense to have these lines ...

Show 8 quoted lines
> +On branch work
> +Your branch is ahead of '\''origin/feature'\'' by 2 commits.
> +  (use "git push" to publish your local commits)
> +
> +Ahead of '\''origin/main'\'' by 3 commits.
> +
> +nothing to commit, working tree clean
> +EOF

... abut the left edge of the page. Unlike <<\EOF, the dash sign tells the shell that it should remove the leading tab from the line before feeding "cat", and the point of using that construct "<<-\EOF" to begin with is so that you can indent the here doc to the same level as the command text. IOW, you use "<<-\EOF" only because you want to avoid these ugly lines that are sticking to the left, like the above. Instead you can do this:

	cat >expect <<-\EOF &&
	On branch work
	Your branch is ...
	  (use "git push" ...
	...
	EOF
and the shell strips the leading tabs from these lines.
> +	test_cmp expect actual
> +'
Show 6 quoted lines
> +test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
> +	(
> +		cd test &&
> +		git checkout main >/dev/null &&
> +		git config status.goalBranch origin/main &&
> +		git checkout work 2>&1
Likewise.
Show 8 quoted lines
> +	) >actual &&
> +	cat >expect <<-\EOF &&
> +Switched to branch '\''work'\''
> +Your branch is ahead of '\''origin/feature'\'' by 2 commits.
> +  (use "git push" to publish your local commits)
> +
> +Ahead of '\''origin/main'\'' by 3 commits.
> +EOF
Likewise.
Also, doesn't $SQ work here, i.e.
	cat >expect <<-EOF &&
	Switched to branch ${SQ}work${SQ}
	Your branch is ahead of ${SQ}...${SQ} by 2 commits.
	...
	EOF

As you want interpolation if you go this route, we lose quote from the end of here-doc token and write "<<-EOF" here, instead of "<<-\EOF".

Show 8 quoted lines
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 >/dev/null &&
> +		git status --long -b
Likewise.
> +	) >actual &&
> +	test_grep "ahead of .origin/main. by 2 commits" actual &&
> +	test_grep ! "Ahead of" actual
> +'
I'll stop here.
Harald Nordgren via GitGitGadget· Dec 28, 2025, 15:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v9 0/2] status: show comparison with configured goal branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net

Harald Nordgren (2):
  status: show comparison with configured goal branch
  improve tests
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 339 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 431 insertions(+)
base-commit: 7c7698a654a7a0031f65b0ab0c1c4e438e95df60
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v9
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v9
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v8:
 1:  7e2574d5ae = 1:  ecfe122585 status: show comparison with configured goal branch
 -:  ---------- > 2:  53bab23737 improve tests
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 28, 2025, 15:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v9 1/2] status: show comparison with configured goal branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to a goal branch (e.g., upstream/main), you can configure status.goalBranch to show an additional comparison.

Add support for status.goalBranch configuration option that specifies
a remote/branch to compare against. When set, git status shows both
the comparison with the tracking branch (as before) and an additional
comparison with the configured goal branch, if it differs from the
tracking branch. The goal branch comparison appears on a separate
line after the tracking branch status, using the same format:
- "Ahead of 'upstream/main' by N commits" when purely ahead
- "Behind 'upstream/main' by N commits" when purely behind
- "Diverged from 'upstream/main' by N commits" when diverged
Example output when tracking a feature branch with status.goalBranch
set to upstream/main:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'upstream/main' by 5 commits.

The comparison is only shown when status.goalBranch is configured and the goal branch differs from the tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
Show changes to 2 files +432 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..7e13c027b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,84 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_goal_branch_ref(char **full_ref_out)
+{
+	const char *config_value;
+	const char *resolved;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
+
+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
+		return NULL;
+
+	if (!config_value || !*config_value)
+		return NULL;
+
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
+		return NULL;
+	}
+
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
+static void format_goal_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     const char *goal_full,
+					     const char *goal_short,
+					     enum ahead_behind_flags abf)
+{
+	int goal_ahead = 0, goal_behind = 0;
+
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
+		return;
+
+	strbuf_addstr(sb, "\n");
+
+	if (goal_ahead > 0 && goal_behind == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
+	}
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2258,6 +2336,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2311,6 +2390,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..fe34ddf0ab 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,343 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git checkout work 2>&1
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset status.goalBranch 2>/dev/null || true &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/feature &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/nonexistent &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 28, 2025, 15:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v9 2/2] improve tests

From: Harald Nordgren <haraldnordgren@gmail.com>
Simplify tests based on feedback.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 t/t6040-tracking-info.sh | 249 +++++++++++++++++++--------------------
 1 file changed, 124 insertions(+), 125 deletions(-)
Show changes to t/t6040-tracking-info.sh +124 −125
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index fe34ddf0ab..a875b4c73b 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -308,45 +308,44 @@ test_expect_success 'setup for ahead of non-main tracking branch' '
 test_expect_success 'status shows ahead of both tracked branch and origin/main' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Ahead of '\''origin/main'\'' by 3 commits.
+	Ahead of ${SQ}origin/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
 	(
 		cd test &&
-		git checkout main >/dev/null &&
+		git checkout main &&
 		git config status.goalBranch origin/main &&
-		git checkout work 2>&1
-	) >actual &&
-	cat >expect <<-\EOF &&
-Switched to branch '\''work'\''
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git checkout work >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Ahead of '\''origin/main'\'' by 3 commits.
-EOF
+	Ahead of ${SQ}origin/main${SQ} by 3 commits.
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status tracking origin/main shows only main' '
 	(
 		cd test &&
-		git checkout b4 >/dev/null &&
-		git status --long -b
-	) >actual &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
 	test_grep "ahead of .origin/main. by 2 commits" actual &&
 	test_grep ! "Ahead of" actual
 '
@@ -369,19 +368,19 @@ test_expect_success 'setup for ahead of tracked but diverged from main' '
 test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
 	(
 		cd test &&
-		git checkout work2 >/dev/null &&
+		git checkout work2 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work2
-Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work2
+	Your branch is ahead of ${SQ}origin/oldfeature${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
 
-Diverged from '\''origin/main'\'' by 3 commits.
+	Diverged from ${SQ}origin/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -401,20 +400,20 @@ test_expect_success 'setup for diverged from tracked but behind main' '
 test_expect_success 'status shows diverged from tracked and behind origin/main' '
 	(
 		cd test &&
-		git checkout work2b >/dev/null &&
+		git checkout work2b &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work2b
-Your branch and '\''origin/oldfeature'\'' have diverged,
-and have 1 and 1 different commits each, respectively.
-  (use "git pull" if you want to integrate the remote branch with yours)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work2b
+	Your branch and ${SQ}origin/oldfeature${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
 
-Behind '\''origin/main'\'' by 1 commit.
+	Behind ${SQ}origin/main${SQ} by 1 commit.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -436,19 +435,19 @@ test_expect_success 'setup for behind tracked but ahead of main' '
 test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
-		git checkout work3 >/dev/null &&
+		git checkout work3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work3
-Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
-  (use "git pull" to update your local branch)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work3
+	Your branch is behind ${SQ}origin/feature3${SQ} by 2 commits, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
 
-Ahead of '\''origin/main'\'' by 1 commit.
+	Ahead of ${SQ}origin/main${SQ} by 1 commit.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -464,19 +463,19 @@ test_expect_success 'setup upstream remote preference' '
 test_expect_success 'status prefers upstream remote over origin for comparison' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch upstream/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Diverged from '\''upstream/main'\'' by 5 commits.
+	Diverged from ${SQ}upstream/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -494,18 +493,18 @@ test_expect_success 'setup for up to date with tracked but ahead of default' '
 test_expect_success 'status shows up to date with tracked but diverged from default' '
 	(
 		cd test &&
-		git checkout synced_feature >/dev/null &&
+		git checkout synced_feature &&
 		git config status.goalBranch upstream/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''upstream/main'\'' by 3 commits.
+	Diverged from ${SQ}upstream/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -524,18 +523,18 @@ test_expect_success 'setup for up to date with tracked but ahead of origin/main'
 test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
 	(
 		cd test &&
-		git checkout synced_feature2 >/dev/null &&
+		git checkout synced_feature2 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature2
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature2
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -550,85 +549,85 @@ test_expect_success 'setup for up to date with tracked but purely ahead of origi
 test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch unset shows no default comparison' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
-		git config --unset status.goalBranch 2>/dev/null || true &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git checkout synced_feature3 &&
+		git config --unset status.goalBranch || true &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set uses configured branch' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set to different remote/branch' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch origin/feature &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set to non-existent branch' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/nonexistent &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
-- 
gitgitgadget
Junio C Hamano· Dec 28, 2025, 19:37 UTC · re: Harald Nordgren · lore

Re: Code review?

Harald Nordgren <haraldnordgren@gmail.com> writes:
> Could I get some code review on this? Maybe it's ready to be merged?
At least not from me at the code level, not yet for now.

I still do not quite get why you need a new configuration variable, so the problem I had were still at the design level.

A happy new year.
Harald Nordgren· Dec 28, 2025, 20:16 UTC · re: Junio C Hamano · lore

Code review?

Hi!
The config variable solves to problem of finding which ”goal branch” to compare to, which I otherwise find unsolvable. I took it from the previous discussion that trying to extract the default branch from the remote is not a good idea.
Is the solution through using the remote/pushRemote?
If that’s the solution they are set per branch (as I understand it), so each time checking out a new branch this ”goal branch” comparison would be lost and has to be configured again. That ruins the feature.
Looking at the git repositories I have on my machine, none of them have pushRemote set up. And the remote setting is pointing to my fork, never to upstream, I think most people have it like that.
I appreciate all the help so far! Happy new year!
Harald
Ben Knoble· Dec 28, 2025, 23:09 UTC · re: Harald Nordgren · lore

Re: Code review?

Show 18 quoted lines
> 
> Le 28 déc. 2025 à 15:16, Harald Nordgren <haraldnordgren@gmail.com> a écrit :
> 
> Hi!
> 
> The config variable solves to problem of finding which ”goal branch” to compare to, which I otherwise find unsolvable. I took it from the previous discussion that trying to extract the default branch from the remote is not a good idea.
> 
> Is the solution through using the remote/pushRemote?
> 
> If that’s the solution they are set per branch (as I understand it), so each time checking out a new branch this ”goal branch” comparison would be lost and has to be configured again. That ruins the feature.
> 
> Looking at the git repositories I have on my machine, none of them have pushRemote set up. And the remote setting is pointing to my fork, never to upstream, I think most people have it like that.
> 
> I appreciate all the help so far! Happy new year!
> 
> 
> Harald
> 
When I get to it, I intend to send a short email describing how I configure Git for triangular workflows. 
Harald Nordgren via GitGitGadget· Dec 30, 2025, 16:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v10 0/3] status: show additional comparison with push branch when different from tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com

Harald Nordgren (3):
  status: show comparison with configured goal branch
  improve tests
  use pushRemote and tracking branch
 remote.c                 |  89 +++++++++++++++++++++
 t/t6040-tracking-info.sh | 167 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 256 insertions(+)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v10
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v10
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v9:
 1:  ecfe122585 = 1:  43a75944fb status: show comparison with configured goal branch
 2:  53bab23737 = 2:  e6d24b8b6a improve tests
 -:  ---------- > 3:  13c2a03b0a use pushRemote and tracking branch
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 30, 2025, 16:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v10 1/3] status: show comparison with configured goal branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to a goal branch (e.g., upstream/main), you can configure status.goalBranch to show an additional comparison.

Add support for status.goalBranch configuration option that specifies
a remote/branch to compare against. When set, git status shows both
the comparison with the tracking branch (as before) and an additional
comparison with the configured goal branch, if it differs from the
tracking branch. The goal branch comparison appears on a separate
line after the tracking branch status, using the same format:
- "Ahead of 'upstream/main' by N commits" when purely ahead
- "Behind 'upstream/main' by N commits" when purely behind
- "Diverged from 'upstream/main' by N commits" when diverged
Example output when tracking a feature branch with status.goalBranch
set to upstream/main:
    On branch feature
    Your branch is ahead of 'origin/feature' by 2 commits.
      (use "git push" to publish your local commits)
    Ahead of 'upstream/main' by 5 commits.

The comparison is only shown when status.goalBranch is configured and the goal branch differs from the tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  92 +++++++++++
 t/t6040-tracking-info.sh | 340 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 432 insertions(+)
Show changes to 2 files +432 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..7e13c027b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,84 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_goal_branch_ref(char **full_ref_out)
+{
+	const char *config_value;
+	const char *resolved;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *slash_pos;
+	char *ret = NULL;
+
+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
+		return NULL;
+
+	if (!config_value || !*config_value)
+		return NULL;
+
+	slash_pos = strchr(config_value, '/');
+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
+			config_value);
+		return NULL;
+	}
+
+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
+		    (int)(slash_pos - config_value), config_value,
+		    slash_pos + 1);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
+static void format_goal_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     const char *goal_full,
+					     const char *goal_short,
+					     enum ahead_behind_flags abf)
+{
+	int goal_ahead = 0, goal_behind = 0;
+
+	if (stat_branch_pair(branch_refname, goal_full,
+			     &goal_ahead, &goal_behind, abf) <= 0)
+		return;
+
+	strbuf_addstr(sb, "\n");
+
+	if (goal_ahead > 0 && goal_behind == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   goal_ahead),
+			goal_short, goal_ahead);
+	} else if (goal_behind > 0 && goal_ahead == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   goal_behind),
+			goal_short, goal_behind);
+	} else if (goal_ahead > 0 && goal_behind > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   goal_ahead + goal_behind),
+			goal_short, goal_ahead + goal_behind);
+	}
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2258,6 +2336,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2311,6 +2390,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *goal_full = NULL;
+		char *goal_short = get_goal_branch_ref(&goal_full);
+
+		if (goal_short && strcmp(base, goal_short))
+			format_goal_branch_comparison(sb, branch->refname, goal_full,
+						     goal_short, abf);
+
+		free(goal_short);
+		free(goal_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..fe34ddf0ab 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,6 +21,7 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -292,4 +293,343 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for ahead of non-main tracking branch' '
+	(
+		cd test &&
+		git checkout -b feature origin/main &&
+		advance feature1 &&
+		git push origin feature &&
+		git checkout -b work --track origin/feature &&
+		advance work1 &&
+		advance work2
+	)
+'
+
+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
+	(
+		cd test &&
+		git checkout main >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git checkout work 2>&1
+	) >actual &&
+	cat >expect <<-\EOF &&
+Switched to branch '\''work'\''
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Ahead of '\''origin/main'\'' by 3 commits.
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 >/dev/null &&
+		git status --long -b
+	) >actual &&
+	test_grep "ahead of .origin/main. by 2 commits" actual &&
+	test_grep ! "Ahead of" actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout origin/main &&
+		git checkout -b oldfeature &&
+		advance oldfeature1 &&
+		git push origin oldfeature &&
+		git checkout origin/main &&
+		advance main_newer &&
+		git push origin HEAD:main &&
+		git checkout -b work2 --track origin/oldfeature &&
+		advance work2_commit
+	)
+'
+
+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+	(
+		cd test &&
+		git checkout work2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2
+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''origin/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for diverged from tracked but behind main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b work2b &&
+		git branch --set-upstream-to=origin/oldfeature &&
+		git checkout origin/main &&
+		advance main_extra &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows diverged from tracked and behind origin/main' '
+	(
+		cd test &&
+		git checkout work2b >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work2b
+Your branch and '\''origin/oldfeature'\'' have diverged,
+and have 1 and 1 different commits each, respectively.
+  (use "git pull" if you want to integrate the remote branch with yours)
+
+Behind '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for behind tracked but ahead of main' '
+	(
+		cd test &&
+		git fetch origin &&
+		git checkout origin/main &&
+		git checkout -b feature3 &&
+		advance feature3_1 &&
+		advance feature3_2 &&
+		advance feature3_3 &&
+		git push origin feature3 &&
+		git checkout -b work3 --track origin/feature3 &&
+		git reset --hard HEAD~2
+	)
+'
+
+test_expect_success 'status shows behind tracked and ahead of origin/main' '
+	(
+		cd test &&
+		git checkout work3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work3
+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
+  (use "git pull" to update your local branch)
+
+Ahead of '\''origin/main'\'' by 1 commit.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote preference' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+	)
+'
+
+test_expect_success 'status prefers upstream remote over origin for comparison' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+Diverged from '\''upstream/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of default' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from default' '
+	(
+		cd test &&
+		git checkout synced_feature >/dev/null &&
+		git config status.goalBranch upstream/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''upstream/main'\'' by 3 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
+	(
+		cd test &&
+		git remote remove upstream &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature2 --track origin/feature &&
+		git checkout origin/main &&
+		advance main_ahead2 &&
+		git push origin HEAD:main
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
+	(
+		cd test &&
+		git checkout synced_feature2 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature2
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
+	(
+		cd test &&
+		git checkout origin/feature &&
+		git checkout -b synced_feature3 --track origin/feature
+	)
+'
+
+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config --unset status.goalBranch 2>/dev/null || true &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set uses configured branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/main &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+Diverged from '\''origin/main'\'' by 5 commits.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to different remote/branch' '
+	(
+		cd test &&
+		git checkout work >/dev/null &&
+		git config status.goalBranch origin/feature &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch work
+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
+  (use "git push" to publish your local commits)
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with status.goalBranch set to non-existent branch' '
+	(
+		cd test &&
+		git checkout synced_feature3 >/dev/null &&
+		git config status.goalBranch origin/nonexistent &&
+		git status --long -b
+	) >actual &&
+	cat >expect <<-\EOF &&
+On branch synced_feature3
+Your branch is up to date with '\''origin/feature'\''.
+
+nothing to commit, working tree clean
+EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 30, 2025, 16:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v10 2/3] improve tests

From: Harald Nordgren <haraldnordgren@gmail.com>
Simplify tests based on feedback.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 t/t6040-tracking-info.sh | 249 +++++++++++++++++++--------------------
 1 file changed, 124 insertions(+), 125 deletions(-)
Show changes to t/t6040-tracking-info.sh +124 −125
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index fe34ddf0ab..a875b4c73b 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -308,45 +308,44 @@ test_expect_success 'setup for ahead of non-main tracking branch' '
 test_expect_success 'status shows ahead of both tracked branch and origin/main' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Ahead of '\''origin/main'\'' by 3 commits.
+	Ahead of ${SQ}origin/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
 	(
 		cd test &&
-		git checkout main >/dev/null &&
+		git checkout main &&
 		git config status.goalBranch origin/main &&
-		git checkout work 2>&1
-	) >actual &&
-	cat >expect <<-\EOF &&
-Switched to branch '\''work'\''
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git checkout work >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Ahead of '\''origin/main'\'' by 3 commits.
-EOF
+	Ahead of ${SQ}origin/main${SQ} by 3 commits.
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status tracking origin/main shows only main' '
 	(
 		cd test &&
-		git checkout b4 >/dev/null &&
-		git status --long -b
-	) >actual &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
 	test_grep "ahead of .origin/main. by 2 commits" actual &&
 	test_grep ! "Ahead of" actual
 '
@@ -369,19 +368,19 @@ test_expect_success 'setup for ahead of tracked but diverged from main' '
 test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
 	(
 		cd test &&
-		git checkout work2 >/dev/null &&
+		git checkout work2 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work2
-Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work2
+	Your branch is ahead of ${SQ}origin/oldfeature${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
 
-Diverged from '\''origin/main'\'' by 3 commits.
+	Diverged from ${SQ}origin/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -401,20 +400,20 @@ test_expect_success 'setup for diverged from tracked but behind main' '
 test_expect_success 'status shows diverged from tracked and behind origin/main' '
 	(
 		cd test &&
-		git checkout work2b >/dev/null &&
+		git checkout work2b &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work2b
-Your branch and '\''origin/oldfeature'\'' have diverged,
-and have 1 and 1 different commits each, respectively.
-  (use "git pull" if you want to integrate the remote branch with yours)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work2b
+	Your branch and ${SQ}origin/oldfeature${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
 
-Behind '\''origin/main'\'' by 1 commit.
+	Behind ${SQ}origin/main${SQ} by 1 commit.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -436,19 +435,19 @@ test_expect_success 'setup for behind tracked but ahead of main' '
 test_expect_success 'status shows behind tracked and ahead of origin/main' '
 	(
 		cd test &&
-		git checkout work3 >/dev/null &&
+		git checkout work3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work3
-Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
-  (use "git pull" to update your local branch)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work3
+	Your branch is behind ${SQ}origin/feature3${SQ} by 2 commits, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
 
-Ahead of '\''origin/main'\'' by 1 commit.
+	Ahead of ${SQ}origin/main${SQ} by 1 commit.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -464,19 +463,19 @@ test_expect_success 'setup upstream remote preference' '
 test_expect_success 'status prefers upstream remote over origin for comparison' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch upstream/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-Diverged from '\''upstream/main'\'' by 5 commits.
+	Diverged from ${SQ}upstream/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -494,18 +493,18 @@ test_expect_success 'setup for up to date with tracked but ahead of default' '
 test_expect_success 'status shows up to date with tracked but diverged from default' '
 	(
 		cd test &&
-		git checkout synced_feature >/dev/null &&
+		git checkout synced_feature &&
 		git config status.goalBranch upstream/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''upstream/main'\'' by 3 commits.
+	Diverged from ${SQ}upstream/main${SQ} by 3 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -524,18 +523,18 @@ test_expect_success 'setup for up to date with tracked but ahead of origin/main'
 test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
 	(
 		cd test &&
-		git checkout synced_feature2 >/dev/null &&
+		git checkout synced_feature2 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature2
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature2
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
@@ -550,85 +549,85 @@ test_expect_success 'setup for up to date with tracked but purely ahead of origi
 test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch unset shows no default comparison' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
-		git config --unset status.goalBranch 2>/dev/null || true &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git checkout synced_feature3 &&
+		git config --unset status.goalBranch || true &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set uses configured branch' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/main &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-Diverged from '\''origin/main'\'' by 5 commits.
+	Diverged from ${SQ}origin/main${SQ} by 5 commits.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set to different remote/branch' '
 	(
 		cd test &&
-		git checkout work >/dev/null &&
+		git checkout work &&
 		git config status.goalBranch origin/feature &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch work
-Your branch is ahead of '\''origin/feature'\'' by 2 commits.
-  (use "git push" to publish your local commits)
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch work
+	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
 test_expect_success 'status with status.goalBranch set to non-existent branch' '
 	(
 		cd test &&
-		git checkout synced_feature3 >/dev/null &&
+		git checkout synced_feature3 &&
 		git config status.goalBranch origin/nonexistent &&
-		git status --long -b
-	) >actual &&
-	cat >expect <<-\EOF &&
-On branch synced_feature3
-Your branch is up to date with '\''origin/feature'\''.
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch synced_feature3
+	Your branch is up to date with ${SQ}origin/feature${SQ}.
 
-nothing to commit, working tree clean
-EOF
+	nothing to commit, working tree clean
+	EOF
 	test_cmp expect actual
 '
 
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Dec 30, 2025, 16:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v10 3/3] use pushRemote and tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>
Use them for comparisons instead of config variable.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  71 +++++-----
 t/t6040-tracking-info.sh | 296 ++++++++-------------------------------
 2 files changed, 96 insertions(+), 271 deletions(-)
Show changes to 2 files +96 −271

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 7e13c027b5..2317725f7d 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,31 +2237,22 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-static char *get_goal_branch_ref(char **full_ref_out)
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
 {
-	const char *config_value;
+	const char *push_remote;
 	const char *resolved;
 	int flag;
 	struct strbuf ref_buf = STRBUF_INIT;
-	char *slash_pos;
 	char *ret = NULL;
 
-	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
-		return NULL;
-
-	if (!config_value || !*config_value)
+	if (!branch)
 		return NULL;
 
-	slash_pos = strchr(config_value, '/');
-	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
-		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
-			config_value);
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
 		return NULL;
-	}
 
-	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
-		    (int)(slash_pos - config_value), config_value,
-		    slash_pos + 1);
+	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
 
 	resolved = refs_resolve_ref_unsafe(
 		get_main_ref_store(the_repository),
@@ -2280,38 +2271,44 @@ static char *get_goal_branch_ref(char **full_ref_out)
 	return ret;
 }
 
-static void format_goal_branch_comparison(struct strbuf *sb,
+static void format_push_branch_comparison(struct strbuf *sb,
 					     const char *branch_refname,
-					     const char *goal_full,
-					     const char *goal_short,
+					     const char *push_full,
+					     const char *push_short,
 					     enum ahead_behind_flags abf)
 {
-	int goal_ahead = 0, goal_behind = 0;
+	int push_ahead = 0, push_behind = 0;
+	int stat_result;
 
-	if (stat_branch_pair(branch_refname, goal_full,
-			     &goal_ahead, &goal_behind, abf) <= 0)
+	stat_result = stat_branch_pair(branch_refname, push_full,
+				       &push_ahead, &push_behind, abf);
+	if (stat_result < 0)
 		return;
 
 	strbuf_addstr(sb, "\n");
 
-	if (goal_ahead > 0 && goal_behind == 0) {
+	if (stat_result == 0 || (push_ahead == 0 && push_behind == 0)) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			push_short);
+	} else if (push_ahead > 0 && push_behind == 0) {
 		strbuf_addf(sb,
 			Q_("Ahead of '%s' by %d commit.\n",
 			   "Ahead of '%s' by %d commits.\n",
-			   goal_ahead),
-			goal_short, goal_ahead);
-	} else if (goal_behind > 0 && goal_ahead == 0) {
+			   push_ahead),
+			push_short, push_ahead);
+	} else if (push_behind > 0 && push_ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Behind '%s' by %d commit.\n",
 			   "Behind '%s' by %d commits.\n",
-			   goal_behind),
-			goal_short, goal_behind);
-	} else if (goal_ahead > 0 && goal_behind > 0) {
+			   push_behind),
+			push_short, push_behind);
+	} else if (push_ahead > 0 && push_behind > 0) {
 		strbuf_addf(sb,
 			Q_("Diverged from '%s' by %d commit.\n",
 			   "Diverged from '%s' by %d commits.\n",
-			   goal_ahead + goal_behind),
-			goal_short, goal_ahead + goal_behind);
+			   push_ahead + push_behind),
+			push_short, push_ahead + push_behind);
 	}
 }
 
@@ -2392,15 +2389,15 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	}
 
 	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
-		char *goal_full = NULL;
-		char *goal_short = get_goal_branch_ref(&goal_full);
+		char *push_full = NULL;
+		char *push_short = get_remote_push_branch(branch, &push_full);
 
-		if (goal_short && strcmp(base, goal_short))
-			format_goal_branch_comparison(sb, branch->refname, goal_full,
-						     goal_short, abf);
+		if (push_short && strcmp(base, push_short))
+			format_push_branch_comparison(sb, branch->refname, push_full,
+						     push_short, abf);
 
-		free(goal_short);
-		free(goal_full);
+		free(push_short);
+		free(push_full);
 	}
 
 	free(base);
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index a875b4c73b..f27ae719ad 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -21,7 +21,6 @@ test_expect_success setup '
 	git clone . test &&
 	(
 		cd test &&
-		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
 		git checkout -b b1 origin &&
 		git reset --hard HEAD^ &&
 		advance d &&
@@ -293,340 +292,169 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
-test_expect_success 'setup for ahead of non-main tracking branch' '
-	(
-		cd test &&
-		git checkout -b feature origin/main &&
-		advance feature1 &&
-		git push origin feature &&
-		git checkout -b work --track origin/feature &&
-		advance work1 &&
-		advance work2
-	)
-'
-
-test_expect_success 'status shows ahead of both tracked branch and origin/main' '
+test_expect_success 'status tracking origin/main shows only main' '
 	(
 		cd test &&
-		git checkout work &&
-		git config status.goalBranch origin/main &&
+		git checkout b4 &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch work
-	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
 	  (use "git push" to publish your local commits)
 
-	Ahead of ${SQ}origin/main${SQ} by 3 commits.
-
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
-	(
-		cd test &&
-		git checkout main &&
-		git config status.goalBranch origin/main &&
-		git checkout work >../actual
-	) &&
-	cat >expect <<-EOF &&
-	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
-	  (use "git push" to publish your local commits)
-
-	Ahead of ${SQ}origin/main${SQ} by 3 commits.
-	EOF
-	test_cmp expect actual
-'
-
-test_expect_success 'status tracking origin/main shows only main' '
-	(
-		cd test &&
-		git checkout b4 &&
-		git status >../actual
-	) &&
-	test_grep "ahead of .origin/main. by 2 commits" actual &&
-	test_grep ! "Ahead of" actual
-'
-
-test_expect_success 'setup for ahead of tracked but diverged from main' '
-	(
-		cd test &&
-		git checkout origin/main &&
-		git checkout -b oldfeature &&
-		advance oldfeature1 &&
-		git push origin oldfeature &&
-		git checkout origin/main &&
-		advance main_newer &&
-		git push origin HEAD:main &&
-		git checkout -b work2 --track origin/oldfeature &&
-		advance work2_commit
-	)
-'
-
-test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
 	(
 		cd test &&
-		git checkout work2 &&
-		git config status.goalBranch origin/main &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch work2
-	Your branch is ahead of ${SQ}origin/oldfeature${SQ} by 1 commit.
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
 	  (use "git push" to publish your local commits)
 
-	Diverged from ${SQ}origin/main${SQ} by 3 commits.
+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
 
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'setup for diverged from tracked but behind main' '
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
 	(
 		cd test &&
-		git fetch origin &&
-		git checkout origin/main &&
-		git checkout -b work2b &&
-		git branch --set-upstream-to=origin/oldfeature &&
-		git checkout origin/main &&
-		advance main_extra &&
-		git push origin HEAD:main
-	)
-'
-
-test_expect_success 'status shows diverged from tracked and behind origin/main' '
-	(
-		cd test &&
-		git checkout work2b &&
-		git config status.goalBranch origin/main &&
-		git status >../actual
+		git checkout feature2 >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch work2b
-	Your branch and ${SQ}origin/oldfeature${SQ} have diverged,
-	and have 1 and 1 different commits each, respectively.
-	  (use "git pull" if you want to integrate the remote branch with yours)
-
-	Behind ${SQ}origin/main${SQ} by 1 commit.
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
 
-	nothing to commit, working tree clean
+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'setup for behind tracked but ahead of main' '
+test_expect_success 'setup for ahead of tracked but diverged from main' '
 	(
 		cd test &&
-		git fetch origin &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
 		git checkout origin/main &&
-		git checkout -b feature3 &&
-		advance feature3_1 &&
-		advance feature3_2 &&
-		advance feature3_3 &&
-		git push origin feature3 &&
-		git checkout -b work3 --track origin/feature3 &&
-		git reset --hard HEAD~2
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
 	)
 '
 
-test_expect_success 'status shows behind tracked and ahead of origin/main' '
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
 	(
 		cd test &&
-		git checkout work3 &&
-		git config status.goalBranch origin/main &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch work3
-	Your branch is behind ${SQ}origin/feature3${SQ} by 2 commits, and can be fast-forwarded.
-	  (use "git pull" to update your local branch)
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
 
-	Ahead of ${SQ}origin/main${SQ} by 1 commit.
+	Ahead of ${SQ}origin/feature4${SQ} by 1 commit.
 
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'setup upstream remote preference' '
+test_expect_success 'setup upstream remote' '
 	(
 		cd test &&
 		git remote add upstream ../. &&
 		git fetch upstream &&
-		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
+		git config remote.pushDefault origin
 	)
 '
 
-test_expect_success 'status prefers upstream remote over origin for comparison' '
+test_expect_success 'status with upstream remote and push.default set to origin' '
 	(
 		cd test &&
-		git checkout work &&
-		git config status.goalBranch upstream/main &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch work
-	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
 	  (use "git push" to publish your local commits)
 
-	Diverged from ${SQ}upstream/main${SQ} by 5 commits.
+	Ahead of ${SQ}origin/feature5${SQ} by 1 commit.
 
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'setup for up to date with tracked but ahead of default' '
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
 	(
 		cd test &&
-		git checkout origin/feature &&
-		git checkout -b synced_feature --track origin/feature &&
-		git checkout origin/main &&
-		advance main_ahead &&
-		git push origin HEAD:main
-	)
-'
-
-test_expect_success 'status shows up to date with tracked but diverged from default' '
-	(
-		cd test &&
-		git checkout synced_feature &&
-		git config status.goalBranch upstream/main &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch synced_feature
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
-
-	Diverged from ${SQ}upstream/main${SQ} by 3 commits.
-
-	nothing to commit, working tree clean
-	EOF
-	test_cmp expect actual
-'
-
-test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
-	(
-		cd test &&
-		git remote remove upstream &&
-		git checkout origin/feature &&
-		git checkout -b synced_feature2 --track origin/feature &&
-		git checkout origin/main &&
-		advance main_ahead2 &&
-		git push origin HEAD:main
-	)
-'
-
-test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
-	(
-		cd test &&
-		git checkout synced_feature2 &&
-		git config status.goalBranch origin/main &&
-		git status >../actual
-	) &&
-	cat >expect <<-EOF &&
-	On branch synced_feature2
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
-
-	Diverged from ${SQ}origin/main${SQ} by 5 commits.
-
-	nothing to commit, working tree clean
-	EOF
-	test_cmp expect actual
-'
-
-test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
-	(
-		cd test &&
-		git checkout origin/feature &&
-		git checkout -b synced_feature3 --track origin/feature
-	)
-'
-
-test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
-	(
-		cd test &&
-		git checkout synced_feature3 &&
-		git config status.goalBranch origin/main &&
-		git status >../actual
-	) &&
-	cat >expect <<-EOF &&
-	On branch synced_feature3
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
-
-	Diverged from ${SQ}origin/main${SQ} by 5 commits.
-
-	nothing to commit, working tree clean
-	EOF
-	test_cmp expect actual
-'
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
 
-test_expect_success 'status with status.goalBranch unset shows no default comparison' '
-	(
-		cd test &&
-		git checkout synced_feature3 &&
-		git config --unset status.goalBranch || true &&
-		git status >../actual
-	) &&
-	cat >expect <<-EOF &&
-	On branch synced_feature3
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
+	Diverged from ${SQ}origin/feature6${SQ} by 2 commits.
 
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with status.goalBranch set uses configured branch' '
+test_expect_success 'status with upstream remote and push branch up to date' '
 	(
 		cd test &&
-		git checkout synced_feature3 &&
-		git config status.goalBranch origin/main &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
 		git status >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch synced_feature3
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
 
-	Diverged from ${SQ}origin/main${SQ} by 5 commits.
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
 
 	nothing to commit, working tree clean
 	EOF
 	test_cmp expect actual
 '
 
-test_expect_success 'status with status.goalBranch set to different remote/branch' '
+test_expect_success 'checkout shows push branch up to date' '
 	(
 		cd test &&
-		git checkout work &&
-		git config status.goalBranch origin/feature &&
-		git status >../actual
-	) &&
-	cat >expect <<-EOF &&
-	On branch work
-	Your branch is ahead of ${SQ}origin/feature${SQ} by 2 commits.
-	  (use "git push" to publish your local commits)
-
-	nothing to commit, working tree clean
-	EOF
-	test_cmp expect actual
-'
-
-test_expect_success 'status with status.goalBranch set to non-existent branch' '
-	(
-		cd test &&
-		git checkout synced_feature3 &&
-		git config status.goalBranch origin/nonexistent &&
-		git status >../actual
+		git checkout feature7 >../actual
 	) &&
 	cat >expect <<-EOF &&
-	On branch synced_feature3
-	Your branch is up to date with ${SQ}origin/feature${SQ}.
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
 
-	nothing to commit, working tree clean
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
 	EOF
 	test_cmp expect actual
 '
-- 
gitgitgadget
Junio C Hamano· Jan 1, 2026, 23:09 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v10 0/3] status: show additional comparison with push branch when different from tracking branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 8 quoted lines
> cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
> ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net
> cc: Ben Knoble ben.knoble@gmail.com
>
> Harald Nordgren (3):
>   status: show comparison with configured goal branch
>   improve tests
>   use pushRemote and tracking branch

Again this seems to do a "step 1 goes in a direction, step 2 fixes its mistake, step 3 changes course" drunken-man's walk.

The same advice to restructure them into a logical incremental progression that moves the codebase in one consistent direction to eventually reach the goal at the end applies.

I see you are now using pushremote_for_branch() that is already used by branch_get_push(). If that gives us "the other thing" that we would want to compare, instead of adding yet another configuration variable users need to be aware of, that is really good.

Harald Nordgren· Jan 1, 2026, 23:38 UTC · re: Junio C Hamano · lore

Another look?

Show 6 quoted lines
> Again this seems to do a "step 1 goes in a direction, step 2 fixes
> its mistake, step 3 changes course" drunken-man's walk.
>
> The same advice to restructure them into a logical incremental
> progression that moves the codebase in one consistent direction to
> eventually reach the goal at the end applies.
Isn't programming always bit of drunken-man's walk?

I'm very hesitant to restructure my history before I am confident I will not need any of the old work later -- I would hate to lose history if I make a mistake.

One option is to keep my code backed up on a separate branch locally, but this gets problematic as I add more work (endless cherry-picking and squashing) between local branches before submitting new patches. So now you know some of my reasoning. I'm not saying I'm right, but it's a bit fear-based.

With that said, my idea has always been to squash everything into a single commit before merging this. The whole diff is not that big. I can split it into code in one commit and tests in another.

As a side-note: In my day job we only allow "squash and merge" on our GitHub. This gives devs the flexibility to treat their branches as a WIP area before merging, but still gives a pristine git history after merge. This feels to me like a good trade-offs. But again, happy to take instructions on how to do better.

> I see you are now using pushremote_for_branch() that is already used
> by branch_get_push().  If that gives us "the other thing" that we
> would want to compare, instead of adding yet another configuration
> variable users need to be aware of, that is really good.

Thanks for the encouragement! I put a lot of work into the tests as well, I hope they tell the story of what this code achieves now.

Harald
Kristoffer Haugsbakk· Jan 2, 2026, 09:48 UTC · re: Harald Nordgren · lore

Re: Another look?

On Fri, Jan 2, 2026, at 00:38, Harald Nordgren wrote:
Show 8 quoted lines
>> Again this seems to do a "step 1 goes in a direction, step 2 fixes
>> its mistake, step 3 changes course" drunken-man's walk.
>>
>> The same advice to restructure them into a logical incremental
>> progression that moves the codebase in one consistent direction to
>> eventually reach the goal at the end applies.
>
> Isn't programming always bit of drunken-man's walk?

We don’t have to present the chain of code changes as they “really happened”. An alternative is to present what will eventually be the final version as if you had both a borderline perfect plan, foresight, and execution. And that’s the convention in this project. Because that’s the natural progression of a patch series; each iteration you get help to arrive at what looks like the perfect iteration. (Until someone finds a off-by-one error two years later?)

What *really happened* is always fiction in any case.
Show 9 quoted lines
> I'm very hesitant to restructure my history before I am confident I will
> not need any of the old work later -- I would hate to lose history if I
> make a mistake.
>
> One option is to keep my code backed up on a separate branch locally, but
> this gets problematic as I add more work (endless cherry-picking and
> squashing) between local branches before submitting new patches. So now you
> know some of my reasoning. I'm not saying I'm right, but it's a bit
> fear-based.

To make a snapshot of each version seems inevitable in my book since you are encouraged to include a range-diff (and optionally also an interdiff) between each iteration.

Now you of course have the backup of each version because you can download the patches that you yourself posted. But that’s more work then just snapshotting each version, for me at least.

Show 9 quoted lines
> With that said, my idea has always been to squash everything into a single
> commit before merging this. The whole diff is not that big. I can split it
> into code in one commit and tests in another.
>
> As a side-note: In my day job we only allow "squash and merge" on our
> GitHub. This gives devs the flexibility to treat their branches as a WIP
> area before merging, but still gives a pristine git history after merge.
> This feels to me like a good trade-offs. But again, happy to take
> instructions on how to do better.

If that’s a good trade off, what are the variables involved in the trade off? So far it seems like:

1. Flexibility to iterate like you want
2. A final history without any back-and-forth noise (pristine/sober
   walk)
But a third variable here is
3. A series of logically separated commits

And you don’t get that with that approach. And a good final history is very important in many people’s eyes.

People call this mandatory squash strategy some word similar to *clean* presumably because there is no back-and-forth noise. That’s the memetic contagion. But there are other adjectives as well:

• Bloated: When the mandated squash strategy forces different concerns
  (code formatting, whitespace formatting, refactor, bug fix, ...) to be
  truncated into one commit
• Lossy: When so many commits get squashed that you cannot, with any
  amount of analysis, piece together what lines in the commit message
  correspond to some part of the diff (especially likely to happen if
  (1) the squash commit message is a bullet list and (2) the commit
  messages are just things like “WIP” and “fix”)

Someone might argue that the squash merges will not be large because the pull requests are not large and the tasks are not large. Or they should not be. But that demands more of both the project/task management and the pull request management:

1. Better project management foresight and planning. Notice that we have
   traded the rewriting commits strategy of “perfect plan, foresight,
   and execution” for demanding more foresight from project
   management. Just because we have mandated no more than one commit per
   pull request or patch series.
   And “perfect plan, foresight, and execution” is not even a
   requirement. Just a contrast to the one-commit requirement. A project
   could allow contributors to both present “perfect plan, foresight,
   and execution” series as well as messy series, with the latter being
   squashed at the integrator’s/reviewer’s/mantainer’s discretion.
2. Divide changes into more pull requests or patch series. At least with
   vanilla GitHub that causes overhead as you have to manually link all
   the pull requests. You might also have to manually link the pull
   requests using URLs if the target branch of the PR does not do it for
   you.
   On the other hand you might have little or no overhead with some
   “stacked PR” tool.

The one-commit rule works just as well if not better[1] than the alternatives in theory. The problem is that the theory demands much more from project management and pull request handling.

† 1: Part of the motivation is often avoiding merge commits. And if
     merge commits always cause point deductions then a perfectly
     executed squash merge strategy will always win over some strategy
     involving true merges.
     Although I would personally choose a rebase-with-trailer strategy
     over squash merges here; rebase the series/PR and add a trailer to
     each commit which links to the series/PR.
>
>>[snip]
Harald Nordgren via GitGitGadget· Jan 2, 2026, 11:17 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v11] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push remote's tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When a push remote is configured (via branch.<name>.pushRemote or
remote.pushDefault), git status shows both the comparison with the
upstream tracking branch (as before) and an additional comparison with
the push remote's tracking branch, if it differs from the upstream
tracking branch. The push branch comparison appears on a separate
line after the upstream branch status, using the same format:
- "Ahead of 'origin/feature' by N commits" when purely ahead
- "Behind 'origin/feature' by N commits" when purely behind
- "Diverged from 'origin/feature' by N commits" when diverged
Example output when tracking upstream/main with pushRemote set to origin:
    On branch feature
    Your branch is ahead of 'upstream/main' by 2 commits.
      (use "git pull" if you want to integrate the remote branch with yours)
    Ahead of 'origin/feature' by 5 commits.

The comparison is only shown when a push remote is configured and the push remote's tracking branch differs from the upstream tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    status: show comparison with push remote tracking branch
    
    cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
    ychin.macvim@gmail.com cc: "brian m. carlson"
    sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc:
    "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v11
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v11
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v10:
 1:  43a75944fb ! 1:  53bb1cb6bb status: show comparison with configured goal branch
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    status: show comparison with configured goal branch
     +    status: show comparison with push remote tracking branch
      
          "git status" on a branch that follows a remote branch compares
          commits on the current branch and the remote-tracking branch it
          builds upon, to show "ahead", "behind", or "diverged" status.
      
          When working on a feature branch that tracks a remote feature branch,
     -    but you also want to track progress relative to a goal branch (e.g.,
     -    upstream/main), you can configure status.goalBranch to show an
     -    additional comparison.
     +    but you also want to track progress relative to the push remote's
     +    tracking branch (which may differ from the upstream branch), git status
     +    now shows an additional comparison.
      
     -    Add support for status.goalBranch configuration option that specifies
     -    a remote/branch to compare against. When set, git status shows both
     -    the comparison with the tracking branch (as before) and an additional
     -    comparison with the configured goal branch, if it differs from the
     -    tracking branch. The goal branch comparison appears on a separate
     -    line after the tracking branch status, using the same format:
     -    - "Ahead of 'upstream/main' by N commits" when purely ahead
     -    - "Behind 'upstream/main' by N commits" when purely behind
     -    - "Diverged from 'upstream/main' by N commits" when diverged
     +    When a push remote is configured (via branch.<name>.pushRemote or
     +    remote.pushDefault), git status shows both the comparison with the
     +    upstream tracking branch (as before) and an additional comparison with
     +    the push remote's tracking branch, if it differs from the upstream
     +    tracking branch. The push branch comparison appears on a separate
     +    line after the upstream branch status, using the same format:
     +    - "Ahead of 'origin/feature' by N commits" when purely ahead
     +    - "Behind 'origin/feature' by N commits" when purely behind
     +    - "Diverged from 'origin/feature' by N commits" when diverged
      
     -    Example output when tracking a feature branch with status.goalBranch
     -    set to upstream/main:
     +    Example output when tracking upstream/main with pushRemote set to origin:
              On branch feature
     -        Your branch is ahead of 'origin/feature' by 2 commits.
     -          (use "git push" to publish your local commits)
     +        Your branch is ahead of 'upstream/main' by 2 commits.
     +          (use "git pull" if you want to integrate the remote branch with yours)
      
     -        Ahead of 'upstream/main' by 5 commits.
     +        Ahead of 'origin/feature' by 5 commits.
      
     -    The comparison is only shown when status.goalBranch is configured
     -    and the goal branch differs from the tracking branch.
     +    The comparison is only shown when a push remote is configured and the
     +    push remote's tracking branch differs from the upstream tracking branch.
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
       
     -+static char *get_goal_branch_ref(char **full_ref_out)
     ++static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
      +{
     -+	const char *config_value;
     ++	const char *push_remote;
      +	const char *resolved;
      +	int flag;
      +	struct strbuf ref_buf = STRBUF_INIT;
     -+	char *slash_pos;
      +	char *ret = NULL;
      +
     -+	if (repo_config_get_value(the_repository, "status.goalBranch", &config_value))
     ++	if (!branch)
      +		return NULL;
      +
     -+	if (!config_value || !*config_value)
     ++	push_remote = pushremote_for_branch(branch, NULL);
     ++	if (!push_remote)
      +		return NULL;
      +
     -+	slash_pos = strchr(config_value, '/');
     -+	if (!slash_pos || slash_pos == config_value || !slash_pos[1]) {
     -+		warning(_("invalid value for status.goalBranch: '%s' (expected format: remote/branch)"),
     -+			config_value);
     -+		return NULL;
     -+	}
     -+
     -+	strbuf_addf(&ref_buf, "refs/remotes/%.*s/%s",
     -+		    (int)(slash_pos - config_value), config_value,
     -+		    slash_pos + 1);
     ++	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
      +
      +	resolved = refs_resolve_ref_unsafe(
      +		get_main_ref_store(the_repository),
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +	return ret;
      +}
      +
     -+static void format_goal_branch_comparison(struct strbuf *sb,
     ++static void format_push_branch_comparison(struct strbuf *sb,
      +					     const char *branch_refname,
     -+					     const char *goal_full,
     -+					     const char *goal_short,
     ++					     const char *push_full,
     ++					     const char *push_short,
      +					     enum ahead_behind_flags abf)
      +{
     -+	int goal_ahead = 0, goal_behind = 0;
     ++	int push_ahead = 0, push_behind = 0;
     ++	int stat_result;
      +
     -+	if (stat_branch_pair(branch_refname, goal_full,
     -+			     &goal_ahead, &goal_behind, abf) <= 0)
     ++	stat_result = stat_branch_pair(branch_refname, push_full,
     ++				       &push_ahead, &push_behind, abf);
     ++	if (stat_result < 0)
      +		return;
      +
      +	strbuf_addstr(sb, "\n");
      +
     -+	if (goal_ahead > 0 && goal_behind == 0) {
     ++	if (stat_result == 0 || (push_ahead == 0 && push_behind == 0)) {
     ++		strbuf_addf(sb,
     ++			_("Your branch is up to date with '%s'.\n"),
     ++			push_short);
     ++	} else if (push_ahead > 0 && push_behind == 0) {
      +		strbuf_addf(sb,
      +			Q_("Ahead of '%s' by %d commit.\n",
      +			   "Ahead of '%s' by %d commits.\n",
     -+			   goal_ahead),
     -+			goal_short, goal_ahead);
     -+	} else if (goal_behind > 0 && goal_ahead == 0) {
     ++			   push_ahead),
     ++			push_short, push_ahead);
     ++	} else if (push_behind > 0 && push_ahead == 0) {
      +		strbuf_addf(sb,
      +			Q_("Behind '%s' by %d commit.\n",
      +			   "Behind '%s' by %d commits.\n",
     -+			   goal_behind),
     -+			goal_short, goal_behind);
     -+	} else if (goal_ahead > 0 && goal_behind > 0) {
     ++			   push_behind),
     ++			push_short, push_behind);
     ++	} else if (push_ahead > 0 && push_behind > 0) {
      +		strbuf_addf(sb,
      +			Q_("Diverged from '%s' by %d commit.\n",
      +			   "Diverged from '%s' by %d commits.\n",
     -+			   goal_ahead + goal_behind),
     -+			goal_short, goal_ahead + goal_behind);
     ++			   push_ahead + push_behind),
     ++			push_short, push_ahead + push_behind);
      +	}
      +}
      +
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	}
      +
      +	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
     -+		char *goal_full = NULL;
     -+		char *goal_short = get_goal_branch_ref(&goal_full);
     ++		char *push_full = NULL;
     ++		char *push_short = get_remote_push_branch(branch, &push_full);
      +
     -+		if (goal_short && strcmp(base, goal_short))
     -+			format_goal_branch_comparison(sb, branch->refname, goal_full,
     -+						     goal_short, abf);
     ++		if (push_short && strcmp(base, push_short))
     ++			format_push_branch_comparison(sb, branch->refname, push_full,
     ++						     push_short, abf);
      +
     -+		free(goal_short);
     -+		free(goal_full);
     ++		free(push_short);
     ++		free(push_full);
      +	}
      +
       	free(base);
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       }
      
       ## t/t6040-tracking-info.sh ##
     -@@ t/t6040-tracking-info.sh: test_expect_success setup '
     - 	git clone . test &&
     - 	(
     - 		cd test &&
     -+		git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main &&
     - 		git checkout -b b1 origin &&
     - 		git reset --hard HEAD^ &&
     - 		advance d &&
      @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
       	test_cmp expect actual
       '
       
     -+test_expect_success 'setup for ahead of non-main tracking branch' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature origin/main &&
     -+		advance feature1 &&
     -+		git push origin feature &&
     -+		git checkout -b work --track origin/feature &&
     -+		advance work1 &&
     -+		advance work2
     -+	)
     -+'
     -+
     -+test_expect_success 'status shows ahead of both tracked branch and origin/main' '
     ++test_expect_success 'status tracking origin/main shows only main' '
      +	(
      +		cd test &&
     -+		git checkout work >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work
     -+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     -+  (use "git push" to publish your local commits)
     -+
     -+Ahead of '\''origin/main'\'' by 3 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++		git checkout b4 &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch b4
     ++	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'checkout shows ahead of both tracked branch and origin/main' '
     ++test_expect_success 'status shows ahead of both origin/main and feature branch' '
      +	(
      +		cd test &&
     -+		git checkout main >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git checkout work 2>&1
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+Switched to branch '\''work'\''
     -+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     -+  (use "git push" to publish your local commits)
     -+
     -+Ahead of '\''origin/main'\'' by 3 commits.
     -+EOF
     ++		git checkout -b feature2 origin/main &&
     ++		git push origin HEAD &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature2
     ++	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status tracking origin/main shows only main' '
     ++test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
      +	(
      +		cd test &&
     -+		git checkout b4 >/dev/null &&
     -+		git status --long -b
     -+	) >actual &&
     -+	test_grep "ahead of .origin/main. by 2 commits" actual &&
     -+	test_grep ! "Ahead of" actual
     ++		git checkout feature2 >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	EOF
     ++	test_cmp expect actual
      +'
      +
      +test_expect_success 'setup for ahead of tracked but diverged from main' '
      +	(
      +		cd test &&
     ++		git checkout -b feature4 origin/main &&
     ++		advance work1 &&
      +		git checkout origin/main &&
     -+		git checkout -b oldfeature &&
     -+		advance oldfeature1 &&
     -+		git push origin oldfeature &&
     -+		git checkout origin/main &&
     -+		advance main_newer &&
     ++		advance work2 &&
      +		git push origin HEAD:main &&
     -+		git checkout -b work2 --track origin/oldfeature &&
     -+		advance work2_commit
     -+	)
     -+'
     -+
     -+test_expect_success 'status shows ahead of tracked and diverged from origin/main' '
     -+	(
     -+		cd test &&
     -+		git checkout work2 >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work2
     -+Your branch is ahead of '\''origin/oldfeature'\'' by 1 commit.
     -+  (use "git push" to publish your local commits)
     -+
     -+Diverged from '\''origin/main'\'' by 3 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'setup for diverged from tracked but behind main' '
     -+	(
     -+		cd test &&
     -+		git fetch origin &&
     -+		git checkout origin/main &&
     -+		git checkout -b work2b &&
     -+		git branch --set-upstream-to=origin/oldfeature &&
     -+		git checkout origin/main &&
     -+		advance main_extra &&
     -+		git push origin HEAD:main
     -+	)
     -+'
     -+
     -+test_expect_success 'status shows diverged from tracked and behind origin/main' '
     -+	(
     -+		cd test &&
     -+		git checkout work2b >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work2b
     -+Your branch and '\''origin/oldfeature'\'' have diverged,
     -+and have 1 and 1 different commits each, respectively.
     -+  (use "git pull" if you want to integrate the remote branch with yours)
     -+
     -+Behind '\''origin/main'\'' by 1 commit.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'setup for behind tracked but ahead of main' '
     -+	(
     -+		cd test &&
     -+		git fetch origin &&
     -+		git checkout origin/main &&
     -+		git checkout -b feature3 &&
     -+		advance feature3_1 &&
     -+		advance feature3_2 &&
     -+		advance feature3_3 &&
     -+		git push origin feature3 &&
     -+		git checkout -b work3 --track origin/feature3 &&
     -+		git reset --hard HEAD~2
     ++		git checkout feature4 &&
     ++		advance work3
      +	)
      +'
      +
     -+test_expect_success 'status shows behind tracked and ahead of origin/main' '
     ++test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
      +	(
      +		cd test &&
     -+		git checkout work3 >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work3
     -+Your branch is behind '\''origin/feature3'\'' by 2 commits, and can be fast-forwarded.
     -+  (use "git pull" to update your local branch)
     -+
     -+Ahead of '\''origin/main'\'' by 1 commit.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++		git checkout feature4 &&
     ++		git branch --set-upstream-to origin/main &&
     ++		git push origin HEAD &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature4
     ++	Your branch and ${SQ}origin/main${SQ} have diverged,
     ++	and have 3 and 1 different commits each, respectively.
     ++	  (use "git pull" if you want to integrate the remote branch with yours)
     ++
     ++	Ahead of ${SQ}origin/feature4${SQ} by 1 commit.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'setup upstream remote preference' '
     ++test_expect_success 'setup upstream remote' '
      +	(
      +		cd test &&
      +		git remote add upstream ../. &&
      +		git fetch upstream &&
     -+		git symbolic-ref refs/remotes/upstream/HEAD refs/remotes/upstream/main
     ++		git config remote.pushDefault origin
      +	)
      +'
      +
     -+test_expect_success 'status prefers upstream remote over origin for comparison' '
     ++test_expect_success 'status with upstream remote and push.default set to origin' '
      +	(
      +		cd test &&
     -+		git checkout work >/dev/null &&
     -+		git config status.goalBranch upstream/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work
     -+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     -+  (use "git push" to publish your local commits)
     -+
     -+Diverged from '\''upstream/main'\'' by 5 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++		git checkout -b feature5 upstream/main &&
     ++		git push origin &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature5
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Ahead of ${SQ}origin/feature5${SQ} by 1 commit.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'setup for up to date with tracked but ahead of default' '
     -+	(
     -+		cd test &&
     -+		git checkout origin/feature &&
     -+		git checkout -b synced_feature --track origin/feature &&
     -+		git checkout origin/main &&
     -+		advance main_ahead &&
     -+		git push origin HEAD:main
     -+	)
     -+'
     -+
     -+test_expect_success 'status shows up to date with tracked but diverged from default' '
     ++test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
      +	(
      +		cd test &&
     -+		git checkout synced_feature >/dev/null &&
     -+		git config status.goalBranch upstream/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+Diverged from '\''upstream/main'\'' by 3 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++		git checkout -b feature6 upstream/main &&
     ++		advance work &&
     ++		git push origin &&
     ++		git reset --hard upstream/main &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature6
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Diverged from ${SQ}origin/feature6${SQ} by 2 commits.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'setup for up to date with tracked but ahead of origin/main' '
     ++test_expect_success 'status with upstream remote and push branch up to date' '
      +	(
      +		cd test &&
     -+		git remote remove upstream &&
     -+		git checkout origin/feature &&
     -+		git checkout -b synced_feature2 --track origin/feature &&
     -+		git checkout origin/main &&
     -+		advance main_ahead2 &&
     -+		git push origin HEAD:main
     -+	)
     -+'
     -+
     -+test_expect_success 'status shows up to date with tracked but diverged from origin/main' '
     -+	(
     -+		cd test &&
     -+		git checkout synced_feature2 >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature2
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+Diverged from '\''origin/main'\'' by 5 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++		git checkout -b feature7 upstream/main &&
     ++		git push origin &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature7
     ++	Your branch is up to date with ${SQ}upstream/main${SQ}.
     ++
     ++	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'setup for up to date with tracked but purely ahead of origin/main' '
     ++test_expect_success 'checkout shows push branch up to date' '
      +	(
      +		cd test &&
     -+		git checkout origin/feature &&
     -+		git checkout -b synced_feature3 --track origin/feature
     -+	)
     -+'
     ++		git checkout feature7 >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+test_expect_success 'status shows up to date with tracked but shows default branch comparison' '
     -+	(
     -+		cd test &&
     -+		git checkout synced_feature3 >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature3
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+Diverged from '\''origin/main'\'' by 5 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'status with status.goalBranch unset shows no default comparison' '
     -+	(
     -+		cd test &&
     -+		git checkout synced_feature3 >/dev/null &&
     -+		git config --unset status.goalBranch 2>/dev/null || true &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature3
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'status with status.goalBranch set uses configured branch' '
     -+	(
     -+		cd test &&
     -+		git checkout synced_feature3 >/dev/null &&
     -+		git config status.goalBranch origin/main &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature3
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+Diverged from '\''origin/main'\'' by 5 commits.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'status with status.goalBranch set to different remote/branch' '
     -+	(
     -+		cd test &&
     -+		git checkout work >/dev/null &&
     -+		git config status.goalBranch origin/feature &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch work
     -+Your branch is ahead of '\''origin/feature'\'' by 2 commits.
     -+  (use "git push" to publish your local commits)
     -+
     -+nothing to commit, working tree clean
     -+EOF
     -+	test_cmp expect actual
     -+'
     -+
     -+test_expect_success 'status with status.goalBranch set to non-existent branch' '
     -+	(
     -+		cd test &&
     -+		git checkout synced_feature3 >/dev/null &&
     -+		git config status.goalBranch origin/nonexistent &&
     -+		git status --long -b
     -+	) >actual &&
     -+	cat >expect <<-\EOF &&
     -+On branch synced_feature3
     -+Your branch is up to date with '\''origin/feature'\''.
     -+
     -+nothing to commit, working tree clean
     -+EOF
     ++	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++	EOF
      +	test_cmp expect actual
      +'
      +
 2:  e6d24b8b6a < -:  ---------- improve tests
 3:  13c2a03b0a < -:  ---------- use pushRemote and tracking branch
 remote.c                 |  89 +++++++++++++++++++++
 t/t6040-tracking-info.sh | 167 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 256 insertions(+)
Show changes to 2 files +256 −0

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 59b3715120..2317725f7d 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,81 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	const char *push_remote;
+	const char *resolved;
+	int flag;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *ret = NULL;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, &flag);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
+static void format_push_branch_comparison(struct strbuf *sb,
+					     const char *branch_refname,
+					     const char *push_full,
+					     const char *push_short,
+					     enum ahead_behind_flags abf)
+{
+	int push_ahead = 0, push_behind = 0;
+	int stat_result;
+
+	stat_result = stat_branch_pair(branch_refname, push_full,
+				       &push_ahead, &push_behind, abf);
+	if (stat_result < 0)
+		return;
+
+	strbuf_addstr(sb, "\n");
+
+	if (stat_result == 0 || (push_ahead == 0 && push_behind == 0)) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			push_short);
+	} else if (push_ahead > 0 && push_behind == 0) {
+		strbuf_addf(sb,
+			Q_("Ahead of '%s' by %d commit.\n",
+			   "Ahead of '%s' by %d commits.\n",
+			   push_ahead),
+			push_short, push_ahead);
+	} else if (push_behind > 0 && push_ahead == 0) {
+		strbuf_addf(sb,
+			Q_("Behind '%s' by %d commit.\n",
+			   "Behind '%s' by %d commits.\n",
+			   push_behind),
+			push_short, push_behind);
+	} else if (push_ahead > 0 && push_behind > 0) {
+		strbuf_addf(sb,
+			Q_("Diverged from '%s' by %d commit.\n",
+			   "Diverged from '%s' by %d commits.\n",
+			   push_ahead + push_behind),
+			push_short, push_ahead + push_behind);
+	}
+}
+
 /*
  * Return true when there is anything to report, otherwise false.
  */
@@ -2258,6 +2333,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2311,6 +2387,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+
+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
+		char *push_full = NULL;
+		char *push_short = get_remote_push_branch(branch, &push_full);
+
+		if (push_short && strcmp(base, push_short))
+			format_push_branch_comparison(sb, branch->refname, push_full,
+						     push_short, abf);
+
+		free(push_short);
+		free(push_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..f27ae719ad 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,171 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Diverged from ${SQ}origin/feature6${SQ} by 2 commits.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
 test_done

base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
-- 
gitgitgadget
Phillip Wood· Jan 2, 2026, 15:18 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v11] status: show comparison with push remote tracking branch

Hi Harald
On 02/01/2026 11:17, Harald Nordgren via GitGitGadget wrote:
Show 10 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
> 
> "git status" on a branch that follows a remote branch compares
> commits on the current branch and the remote-tracking branch it
> builds upon, to show "ahead", "behind", or "diverged" status.
> 
> When working on a feature branch that tracks a remote feature branch,
> but you also want to track progress relative to the push remote's
> tracking branch (which may differ from the upstream branch), git status
> now shows an additional comparison.

This is great and it is really good that it is now using the default push destination rather than a custom config key.

Show 5 quoted lines
> When a push remote is configured (via branch.<name>.pushRemote or
> remote.pushDefault) git status shows both the comparison with the
> upstream tracking branch (as before) and an additional comparison with
> the push remote's tracking branch, if it differs from the upstream
> tracking branch.

Looking at the tests, it seems that the extra information is shown whenever the upstream branch differs from the default push destination even if they are on the same remote. I think that is sensible but this paragraph should be updated to reflect the fact that one does not need to set either of the "pushDefault" config settings to benefit from this.

Show 5 quoted lines
> The push branch comparison appears on a separate
> line after the upstream branch status, using the same format:
> - "Ahead of 'origin/feature' by N commits" when purely ahead
> - "Behind 'origin/feature' by N commits" when purely behind
> - "Diverged from 'origin/feature' by N commits" when diverged

I'm wondering why we don't reuse the existing messages - I don't see how using a different wording for the push destination compared to the upstream branch benefits the user. We should adjust the hints that are shown so that we only recommend pulling from the upstream branch and only recommend pushing to the default push destination but the comparisons should be the same. Also I don't find the message "Diverged from 'origin/feature' by N commits' very helpful, I'd find it more useful if it gave the ahead/behind count like we do for the upstream branch.

> Example output when tracking upstream/main with pushRemote set to origin:
>      On branch feature
>      Your branch is ahead of 'upstream/main' by 2 commits.
>        (use "git pull" if you want to integrate the remote branch with yours)

Is this a typo? - why are we recommending "git pull" when our branch is ahead of the upstream branch?

>      Ahead of 'origin/feature' by 5 commits.
It would be nice to have a hint suggesting the user runs "git push" here.
> The comparison is only shown when a push remote is configured and the
> push remote's tracking branch differs from the upstream tracking branch.

Looking at the tests, only the second half of that sentence appears to be true.

Show 24 quoted lines
> diff --git a/remote.c b/remote.c
> index 59b3715120..2317725f7d 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -2237,6 +2237,81 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
>   	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
>   }
>   
> +static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
> +{
> +	const char *push_remote;
> +	const char *resolved;
> +	int flag;
> +	struct strbuf ref_buf = STRBUF_INIT;
> +	char *ret = NULL;
> +
> +	if (!branch)
> +		return NULL;
> +
> +	push_remote = pushremote_for_branch(branch, NULL);
> +	if (!push_remote)
> +		return NULL;
> +
> +	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);

Shouldn't we be taking account of the push and fetch refspecs here? There is no guarantee that $branch maps to refs/remotes/$remote/$branch. To take a silly example, if we have

     remote.$remote.fetch = 
refs/heads/$branch:refs/remotes/$remote/abc-$branch
     remote.$remote.pull = refs/heads/$branch:refs/heads/xyz-$branch

Then we should be using "refs/remotes/$remote/abc-xyz-$branch" in the message above as "$branch" will be pushed to "xyz-$branch" on the remote which is fetched to "$remote/abc-xyz-$branch"

Show 6 quoted lines
> +
> +	resolved = refs_resolve_ref_unsafe(
> +		get_main_ref_store(the_repository),
> +		ref_buf.buf,
> +		RESOLVE_REF_READING,
> +		NULL, &flag);

As we don't use flag we can pass NULL - see the documentation for this function in refs.h

Show 15 quoted lines
> +static void format_push_branch_comparison(struct strbuf *sb,
> +					     const char *branch_refname,
> +					     const char *push_full,
> +					     const char *push_short,
> +					     enum ahead_behind_flags abf)
> +{
> +	int push_ahead = 0, push_behind = 0;
> +	int stat_result;
> +
> +	stat_result = stat_branch_pair(branch_refname, push_full,
> +				       &push_ahead, &push_behind, abf);
> +	if (stat_result < 0)
> +		return;
> +
> +	strbuf_addstr(sb, "\n");

As I said above it would be nice if we could use the existing messages here. Can we have a separate preparatory patch that refactors fromat_tracking_info() so that we can use the same messages for the upstream branch and the default push destination?

Show 6 quoted lines
> @@ -2311,6 +2387,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   			strbuf_addstr(sb,
>   				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
>   	}
> +
> +	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {

Why do we handle AHEAD_BEHIND_QUICK differently to the upstream branch? Surely it would be useful to tell the user whether the branch is up to date with the default push destination or not?

Show 17 quoted lines
> diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
> index 0b719bbae6..f27ae719ad 100755
> --- a/t/t6040-tracking-info.sh
> +++ b/t/t6040-tracking-info.sh
> [...]
> +test_expect_success 'status shows ahead of both origin/main and feature branch' '
> +	(
> +		cd test &&
> +		git checkout -b feature2 origin/main &&
> +		git push origin HEAD &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature2
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)

Why are we still suggesting pushing to the upstream branch when we know the user is pushing to a different remote branch?

> +	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
Why aren't we suggesting to use "git push" here?

Thanks for working on this. With a few tweaks it will be a really useful improvement to "git status"

Phillip
Show 139 quoted lines
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
> +	(
> +		cd test &&
> +		git checkout feature2 >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'setup for ahead of tracked but diverged from main' '
> +	(
> +		cd test &&
> +		git checkout -b feature4 origin/main &&
> +		advance work1 &&
> +		git checkout origin/main &&
> +		advance work2 &&
> +		git push origin HEAD:main &&
> +		git checkout feature4 &&
> +		advance work3
> +	)
> +'
> +
> +test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
> +	(
> +		cd test &&
> +		git checkout feature4 &&
> +		git branch --set-upstream-to origin/main &&
> +		git push origin HEAD &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature4
> +	Your branch and ${SQ}origin/main${SQ} have diverged,
> +	and have 3 and 1 different commits each, respectively.
> +	  (use "git pull" if you want to integrate the remote branch with yours)
> +
> +	Ahead of ${SQ}origin/feature4${SQ} by 1 commit.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'setup upstream remote' '
> +	(
> +		cd test &&
> +		git remote add upstream ../. &&
> +		git fetch upstream &&
> +		git config remote.pushDefault origin
> +	)
> +'
> +
> +test_expect_success 'status with upstream remote and push.default set to origin' '
> +	(
> +		cd test &&
> +		git checkout -b feature5 upstream/main &&
> +		git push origin &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature5
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	Ahead of ${SQ}origin/feature5${SQ} by 1 commit.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
> +	(
> +		cd test &&
> +		git checkout -b feature6 upstream/main &&
> +		advance work &&
> +		git push origin &&
> +		git reset --hard upstream/main &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature6
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	Diverged from ${SQ}origin/feature6${SQ} by 2 commits.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status with upstream remote and push branch up to date' '
> +	(
> +		cd test &&
> +		git checkout -b feature7 upstream/main &&
> +		git push origin &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature7
> +	Your branch is up to date with ${SQ}upstream/main${SQ}.
> +
> +	Your branch is up to date with ${SQ}origin/feature7${SQ}.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'checkout shows push branch up to date' '
> +	(
> +		cd test &&
> +		git checkout feature7 >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	Your branch is up to date with ${SQ}upstream/main${SQ}.
> +
> +	Your branch is up to date with ${SQ}origin/feature7${SQ}.
> +	EOF
> +	test_cmp expect actual
> +'
> +
>   test_done
> 
> base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Harald Nordgren· Jan 2, 2026, 20:27 UTC · re: Phillip Wood · lore

Another look?

Show 5 quoted lines
> Looking at the tests, it seems that the extra information is shown
> whenever the upstream branch differs from the default push destination
> even if they are on the same remote. I think that is sensible but this
> paragraph should be updated to reflect the fact that one does not need to
> set either of the "pushDefault" config settings to benefit from this.
Good catch, will be included in the next diff!
Show 8 quoted lines
> I'm wondering why we don't reuse the existing messages - I don't see
> how using a different wording for the push destination compared to the
> upstream branch benefits the user. We should adjust the hints that
> are shown so that we only recommend pulling from the upstream branch
> and only recommend pushing to the default push destination but the
> comparisons should be the same. Also I don't find the message "Diverged
> from 'origin/feature' by N commits' very helpful, I'd find it more useful
> if it gave the ahead/behind count like we do for the upstream branch.

Thanks for saying this, I actually did the majority of this work already but was hesitant to include it in the diff because it increases the surface area. But I will include it now!

Show 11 quoted lines
> Shouldn't we be taking account of the push and fetch refspecs here? There
> is no guarantee that $branch maps to refs/remotes/$remote/$branch. To take
> a silly example, if we have
>
>      remote.$remote.fetch =
> refs/heads/$branch:refs/remotes/$remote/abc-$branch remote.$remote.pull =
> refs/heads/$branch:refs/heads/xyz-$branch
>
> Then we should be using "refs/remotes/$remote/abc-xyz-$branch" in the
> message above as "$branch" will be pushed to "xyz-$branch" on the remote
> which is fetched to "$remote/abc-xyz-$branch"

I don't understand this one, can you maybe explain how to code will need to change?

> As we don't use flag we can pass NULL - see the documentation for this
> function in refs.h
Thanks, will be included in the next diff!
> Why are we still suggesting pushing to the upstream branch when we know
the user is pushing to a different remote branch?
Thanks, will be included in the next diff!
Harald
Harald Nordgren via GitGitGadget· Jan 2, 2026, 21:34 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v12 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com

Harald Nordgren (2):
  refactor: format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 164 +++++++++++++++++++++++++++++---------
 t/t6040-tracking-info.sh | 168 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 295 insertions(+), 37 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v12
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v12
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v11:
 -:  ---------- > 1:  a2c160c53e refactor: format_branch_comparison in preparation
 1:  53bb1cb6bb ! 2:  a586038d1f status: show comparison with push remote tracking branch
     @@ Commit message
          builds upon, to show "ahead", "behind", or "diverged" status.
      
          When working on a feature branch that tracks a remote feature branch,
     -    but you also want to track progress relative to the push remote's
     +    but you also want to track progress relative to the push destination
          tracking branch (which may differ from the upstream branch), git status
          now shows an additional comparison.
      
     -    When a push remote is configured (via branch.<name>.pushRemote or
     -    remote.pushDefault), git status shows both the comparison with the
     -    upstream tracking branch (as before) and an additional comparison with
     -    the push remote's tracking branch, if it differs from the upstream
     -    tracking branch. The push branch comparison appears on a separate
     -    line after the upstream branch status, using the same format:
     -    - "Ahead of 'origin/feature' by N commits" when purely ahead
     -    - "Behind 'origin/feature' by N commits" when purely behind
     -    - "Diverged from 'origin/feature' by N commits" when diverged
     +    When the upstream tracking branch differs from the push destination
     +    tracking branch, git status shows both the comparison with the upstream
     +    tracking branch (as before) and an additional comparison with the push
     +    destination tracking branch. The push branch comparison appears on a
     +    separate line after the upstream branch status, using the same format.
      
     -    Example output when tracking upstream/main with pushRemote set to origin:
     +    Example output when tracking origin/main but push destination is
     +    origin/feature:
              On branch feature
     -        Your branch is ahead of 'upstream/main' by 2 commits.
     +        Your branch and 'origin/main' have diverged,
     +        and have 3 and 1 different commits each, respectively.
                (use "git pull" if you want to integrate the remote branch with yours)
      
     -        Ahead of 'origin/feature' by 5 commits.
     +        Your branch is ahead of 'origin/feature' by 1 commit.
     +          (use "git push" to publish your local commits)
      
     -    The comparison is only shown when a push remote is configured and the
     -    push remote's tracking branch differs from the upstream tracking branch.
     +    The comparison is only shown when the push destination tracking branch
     +    differs from the upstream tracking branch, even if they are on the same
     +    remote.
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +{
      +	const char *push_remote;
      +	const char *resolved;
     -+	int flag;
      +	struct strbuf ref_buf = STRBUF_INIT;
      +	char *ret = NULL;
      +
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +		get_main_ref_store(the_repository),
      +		ref_buf.buf,
      +		RESOLVE_REF_READING,
     -+		NULL, &flag);
     ++		NULL, NULL);
      +
      +	if (resolved) {
      +		if (full_ref_out)
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +	return ret;
      +}
      +
     -+static void format_push_branch_comparison(struct strbuf *sb,
     -+					     const char *branch_refname,
     -+					     const char *push_full,
     -+					     const char *push_short,
     -+					     enum ahead_behind_flags abf)
     -+{
     -+	int push_ahead = 0, push_behind = 0;
     -+	int stat_result;
     -+
     -+	stat_result = stat_branch_pair(branch_refname, push_full,
     -+				       &push_ahead, &push_behind, abf);
     -+	if (stat_result < 0)
     -+		return;
     -+
     -+	strbuf_addstr(sb, "\n");
     -+
     -+	if (stat_result == 0 || (push_ahead == 0 && push_behind == 0)) {
     -+		strbuf_addf(sb,
     -+			_("Your branch is up to date with '%s'.\n"),
     -+			push_short);
     -+	} else if (push_ahead > 0 && push_behind == 0) {
     -+		strbuf_addf(sb,
     -+			Q_("Ahead of '%s' by %d commit.\n",
     -+			   "Ahead of '%s' by %d commits.\n",
     -+			   push_ahead),
     -+			push_short, push_ahead);
     -+	} else if (push_behind > 0 && push_ahead == 0) {
     -+		strbuf_addf(sb,
     -+			Q_("Behind '%s' by %d commit.\n",
     -+			   "Behind '%s' by %d commits.\n",
     -+			   push_behind),
     -+			push_short, push_behind);
     -+	} else if (push_ahead > 0 && push_behind > 0) {
     -+		strbuf_addf(sb,
     -+			Q_("Diverged from '%s' by %d commit.\n",
     -+			   "Diverged from '%s' by %d commits.\n",
     -+			   push_ahead + push_behind),
     -+			push_short, push_ahead + push_behind);
     -+	}
     -+}
     -+
     - /*
     -  * Return true when there is anything to report, otherwise false.
     -  */
     + static void format_branch_comparison(struct strbuf *sb,
     + 				     int ahead, int behind,
     + 				     const char *branch_name,
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     + 	const char *full_base;
     + 	char *base;
     + 	int upstream_is_gone = 0;
     ++	int push_ours = 0, push_theirs = 0;
     ++	int push_stat_result = -1;
     ++	int will_show_push_comparison = 0;
       
     + 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     + 	if (sti < 0) {
     +@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
       					    full_base, 0);
     + 
     ++	if (!upstream_is_gone && abf != AHEAD_BEHIND_QUICK) {
     ++		char *push_full = NULL;
     ++		char *push_short = get_remote_push_branch(branch, &push_full);
      +
     - 	if (upstream_is_gone) {
     - 		strbuf_addf(sb,
     - 			_("Your branch is based on '%s', but the upstream is gone.\n"),
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     ++		if (push_short && strcmp(base, push_short)) {
     ++			push_stat_result = stat_branch_pair(branch->refname, push_full,
     ++							    &push_ours, &push_theirs, abf);
     ++			if (push_stat_result >= 0)
     ++				will_show_push_comparison = 1;
     ++		}
     ++
     ++		free(push_short);
     ++		free(push_full);
     ++	}
     ++
     + 	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
     + 	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
     +-		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
     ++		if (!theirs && !will_show_push_comparison &&
     ++		    advice_enabled(ADVICE_STATUS_HINTS)) {
       			strbuf_addstr(sb,
     - 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
     + 				_("  (use \"git push\" to publish your local commits)\n"));
     + 		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
     +@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     + 		}
       	}
     -+
     -+	if (!upstream_is_gone && sti >= 0 && abf != AHEAD_BEHIND_QUICK) {
     + 
     ++	if (will_show_push_comparison) {
      +		char *push_full = NULL;
      +		char *push_short = get_remote_push_branch(branch, &push_full);
      +
     -+		if (push_short && strcmp(base, push_short))
     -+			format_push_branch_comparison(sb, branch->refname, push_full,
     -+						     push_short, abf);
     ++		if (push_short && strcmp(base, push_short)) {
     ++			strbuf_addstr(sb, "\n");
     ++			format_branch_comparison(sb, push_ours, push_theirs, push_short, 0, abf,
     ++						push_ours || push_theirs);
     ++			if (push_ours > 0 && push_theirs == 0 &&
     ++			    advice_enabled(ADVICE_STATUS_HINTS)) {
     ++				strbuf_addstr(sb,
     ++					_("  (use \"git push\" to publish your local commits)\n"));
     ++			}
     ++		}
      +
      +		free(push_short);
      +		free(push_full);
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	cat >expect <<-EOF &&
      +	On branch feature2
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     -+	  (use "git push" to publish your local commits)
      +
     -+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	) &&
      +	cat >expect <<-EOF &&
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     -+	  (use "git push" to publish your local commits)
      +
     -+	Ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
      +	EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	and have 3 and 1 different commits each, respectively.
      +	  (use "git pull" if you want to integrate the remote branch with yours)
      +
     -+	Ahead of ${SQ}origin/feature4${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	cat >expect <<-EOF &&
      +	On branch feature5
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     -+	  (use "git push" to publish your local commits)
      +
     -+	Ahead of ${SQ}origin/feature5${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	cat >expect <<-EOF &&
      +	On branch feature6
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     -+	  (use "git push" to publish your local commits)
      +
     -+	Diverged from ${SQ}origin/feature6${SQ} by 2 commits.
     ++	Your branch and ${SQ}origin/feature6${SQ} have diverged,
     ++	and have 1 and 1 different commits each, respectively.
      +
      +	nothing to commit, working tree clean
      +	EOF
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 2, 2026, 21:34 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v12 1/2] refactor: format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 99 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 59 insertions(+), 40 deletions(-)
Show changes to remote.c +59 −40
diff --git a/remote.c b/remote.c
index 59b3715120..58093f64b0 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,66 +2237,50 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+				     int ahead, int behind,
+				     const char *branch_name,
+				     int upstream_is_gone,
+				     enum ahead_behind_flags abf,
+				     int sti)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
+			branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else if (!sti) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
-	} else if (!theirs) {
+	} else if (ahead == 0 && behind == 0) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			branch_name);
+	} else if (ahead > 0 && behind == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
-			   ours),
-			base, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git push\" to publish your local commits)\n"));
-	} else if (!ours) {
+			   ahead),
+			branch_name, ahead);
+	} else if (behind > 0 && ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
 			       "and can be fast-forwarded.\n",
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
-			   theirs),
-			base, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git pull\" to update your local branch)\n"));
-	} else {
+			   behind),
+			branch_name, behind);
+	} else if (ahead > 0 && behind > 0) {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commit each, "
@@ -2304,13 +2288,48 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
-			   ours + theirs),
-			base, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+			   ahead + behind),
+			branch_name, ahead, behind);
+	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
+	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
+		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git push\" to publish your local commits)\n"));
+		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git pull\" to update your local branch)\n"));
+		} else if (ours && theirs && show_divergence_advice &&
+			   advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		}
 	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 2, 2026, 21:34 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v12 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  73 ++++++++++++++++-
 t/t6040-tracking-info.sh | 168 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 240 insertions(+), 1 deletion(-)
Show changes to 2 files +240 −1

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 58093f64b0..1663bd9236 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,39 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	const char *push_remote;
+	const char *resolved;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *ret = NULL;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int ahead, int behind,
 				     const char *branch_name,
@@ -2304,6 +2337,9 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int push_ours = 0, push_theirs = 0;
+	int push_stat_result = -1;
+	int will_show_push_comparison = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2315,9 +2351,25 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	if (!upstream_is_gone && abf != AHEAD_BEHIND_QUICK) {
+		char *push_full = NULL;
+		char *push_short = get_remote_push_branch(branch, &push_full);
+
+		if (push_short && strcmp(base, push_short)) {
+			push_stat_result = stat_branch_pair(branch->refname, push_full,
+							    &push_ours, &push_theirs, abf);
+			if (push_stat_result >= 0)
+				will_show_push_comparison = 1;
+		}
+
+		free(push_short);
+		free(push_full);
+	}
+
 	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
 	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
-		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+		if (!theirs && !will_show_push_comparison &&
+		    advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
@@ -2330,6 +2382,25 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		}
 	}
 
+	if (will_show_push_comparison) {
+		char *push_full = NULL;
+		char *push_short = get_remote_push_branch(branch, &push_full);
+
+		if (push_short && strcmp(base, push_short)) {
+			strbuf_addstr(sb, "\n");
+			format_branch_comparison(sb, push_ours, push_theirs, push_short, 0, abf,
+						push_ours || push_theirs);
+			if (push_ours > 0 && push_theirs == 0 &&
+			    advice_enabled(ADVICE_STATUS_HINTS)) {
+				strbuf_addstr(sb,
+					_("  (use \"git push\" to publish your local commits)\n"));
+			}
+		}
+
+		free(push_short);
+		free(push_full);
+	}
+
 	free(base);
 	return 1;
 }
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..8eb1f3e1f1 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,172 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 3, 2026, 03:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v13 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com

Harald Nordgren (2):
  refactor: format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 152 ++++++++++++++++++++++++++---------
 t/t6040-tracking-info.sh | 168 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 283 insertions(+), 37 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v13
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v13
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v12:
 1:  a2c160c53e = 1:  a2c160c53e refactor: format_branch_comparison in preparation
 2:  a586038d1f ! 2:  891239211e status: show comparison with push remote tracking branch
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
       	int upstream_is_gone = 0;
     -+	int push_ours = 0, push_theirs = 0;
     -+	int push_stat_result = -1;
     -+	int will_show_push_comparison = 0;
     ++	int push_ours, push_theirs, push_sti;
     ++	char *full_push = NULL;
     ++	char *push = NULL;
     ++	int show_push_comparison = 0;
       
       	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
       	if (sti < 0) {
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
       					    full_base, 0);
       
     -+	if (!upstream_is_gone && abf != AHEAD_BEHIND_QUICK) {
     -+		char *push_full = NULL;
     -+		char *push_short = get_remote_push_branch(branch, &push_full);
     -+
     -+		if (push_short && strcmp(base, push_short)) {
     -+			push_stat_result = stat_branch_pair(branch->refname, push_full,
     -+							    &push_ours, &push_theirs, abf);
     -+			if (push_stat_result >= 0)
     -+				will_show_push_comparison = 1;
     -+		}
     -+
     -+		free(push_short);
     -+		free(push_full);
     ++	push = get_remote_push_branch(branch, &full_push);
     ++	if (push && strcmp(base, push)) {
     ++		push_sti = stat_branch_pair(branch->refname, full_push,
     ++					   &push_ours, &push_theirs, abf);
     ++		if (push_sti >= 0)
     ++			show_push_comparison = 1;
      +	}
      +
       	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
       	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
      -		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
     -+		if (!theirs && !will_show_push_comparison &&
     ++		if (!theirs && !show_push_comparison &&
      +		    advice_enabled(ADVICE_STATUS_HINTS)) {
       			strbuf_addstr(sb,
       				_("  (use \"git push\" to publish your local commits)\n"));
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       		}
       	}
       
     -+	if (will_show_push_comparison) {
     -+		char *push_full = NULL;
     -+		char *push_short = get_remote_push_branch(branch, &push_full);
     -+
     -+		if (push_short && strcmp(base, push_short)) {
     -+			strbuf_addstr(sb, "\n");
     -+			format_branch_comparison(sb, push_ours, push_theirs, push_short, 0, abf,
     -+						push_ours || push_theirs);
     -+			if (push_ours > 0 && push_theirs == 0 &&
     -+			    advice_enabled(ADVICE_STATUS_HINTS)) {
     ++	if (show_push_comparison) {
     ++		strbuf_addstr(sb, "\n");
     ++		format_branch_comparison(sb, push_ours, push_theirs, push, 0, abf, push_sti);
     ++		if (push_sti > 0 && abf != AHEAD_BEHIND_QUICK) {
     ++			if (!push_theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
      +				strbuf_addstr(sb,
      +					_("  (use \"git push\" to publish your local commits)\n"));
      +			}
      +		}
     -+
     -+		free(push_short);
     -+		free(push_full);
      +	}
      +
       	free(base);
     ++	free(full_push);
     ++	free(push);
       	return 1;
       }
     + 
      
       ## t/t6040-tracking-info.sh ##
      @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 3, 2026, 03:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v13 1/2] refactor: format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 99 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 59 insertions(+), 40 deletions(-)
Show changes to remote.c +59 −40
diff --git a/remote.c b/remote.c
index 59b3715120..58093f64b0 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,66 +2237,50 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+				     int ahead, int behind,
+				     const char *branch_name,
+				     int upstream_is_gone,
+				     enum ahead_behind_flags abf,
+				     int sti)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
+			branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else if (!sti) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
-	} else if (!theirs) {
+	} else if (ahead == 0 && behind == 0) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			branch_name);
+	} else if (ahead > 0 && behind == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
-			   ours),
-			base, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git push\" to publish your local commits)\n"));
-	} else if (!ours) {
+			   ahead),
+			branch_name, ahead);
+	} else if (behind > 0 && ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
 			       "and can be fast-forwarded.\n",
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
-			   theirs),
-			base, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git pull\" to update your local branch)\n"));
-	} else {
+			   behind),
+			branch_name, behind);
+	} else if (ahead > 0 && behind > 0) {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commit each, "
@@ -2304,13 +2288,48 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
-			   ours + theirs),
-			base, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+			   ahead + behind),
+			branch_name, ahead, behind);
+	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
+	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
+		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git push\" to publish your local commits)\n"));
+		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git pull\" to update your local branch)\n"));
+		} else if (ours && theirs && show_divergence_advice &&
+			   advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		}
 	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 3, 2026, 03:08 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v13 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  61 +++++++++++++-
 t/t6040-tracking-info.sh | 168 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 228 insertions(+), 1 deletion(-)
Show changes to 2 files +228 −1

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 58093f64b0..c90441fe42 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,39 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	const char *push_remote;
+	const char *resolved;
+	struct strbuf ref_buf = STRBUF_INIT;
+	char *ret = NULL;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		ref_buf.buf,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (resolved) {
+		if (full_ref_out)
+			*full_ref_out = xstrdup(resolved);
+		ret = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), resolved, 0);
+	}
+
+	strbuf_release(&ref_buf);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int ahead, int behind,
 				     const char *branch_name,
@@ -2304,6 +2337,10 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	int show_push_comparison = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2315,9 +2352,18 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0)
+			show_push_comparison = 1;
+	}
+
 	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
 	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
-		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+		if (!theirs && !show_push_comparison &&
+		    advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
@@ -2330,7 +2376,20 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		}
 	}
 
+	if (show_push_comparison) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, push_ours, push_theirs, push, 0, abf, push_sti);
+		if (push_sti > 0 && abf != AHEAD_BEHIND_QUICK) {
+			if (!push_theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+				strbuf_addstr(sb,
+					_("  (use \"git push\" to publish your local commits)\n"));
+			}
+		}
+	}
+
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..8eb1f3e1f1 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,172 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Phillip Wood· Jan 3, 2026, 10:04 UTC · re: Harald Nordgren · lore

Re: Another look?

On 02/01/2026 20:27, Harald Nordgren wrote:
Show 15 quoted lines
> 
>> Shouldn't we be taking account of the push and fetch refspecs here? There
>> is no guarantee that $branch maps to refs/remotes/$remote/$branch. To take
>> a silly example, if we have
>>
>>       remote.$remote.fetch =
>> refs/heads/$branch:refs/remotes/$remote/abc-$branch remote.$remote.pull =
>> refs/heads/$branch:refs/heads/xyz-$branch
>>
>> Then we should be using "refs/remotes/$remote/abc-xyz-$branch" in the
>> message above as "$branch" will be pushed to "xyz-$branch" on the remote
>> which is fetched to "$remote/abc-xyz-$branch"
> 
> I don't understand this one, can you maybe explain how to code will need to
> change?

You want to display the remote tracking ref that tracks ref on the remote that we push to. When git pushes $branch to $remote it checks if any of the push refspecs remote.$remote.push match $branch. If there is a match then the branch name is mapped according to the refspec and that it the ref that is updated in the remote repository. It then takes that ref and checks the fetch refspecs remote.$remote.fetch and if it finds a match it maps the ref we've pushed to a remote tracking ref in the local repository.

It looks like you can find where we would push a branch to with remote_ref_for_branch() and you can map that to a remote tracking ref with tracking_for_push_dest(). We could use branch_get_push() but does more than just map the refs as it checks push.default to see whether "git push" would actually push the branch.

Thanks
Phillip
Harald Nordgren· Jan 3, 2026, 13:00 UTC · re: Phillip Wood · lore

Another look?

Show 14 quoted lines
> You want to display the remote tracking ref that tracks ref on the remote
> that we push to. When git pushes $branch to $remote it checks if any
> of the push refspecs remote.$remote.push match $branch. If there is a
> match then the branch name is mapped according to the refspec and that
> it the ref that is updated in the remote repository. It then takes that
> ref and checks the fetch refspecs remote.$remote.fetch and if it finds a
> match it maps the ref we've pushed to a remote tracking ref in the local
> repository.
>
> It looks like you can find where we would push a branch to with
> remote_ref_for_branch() and you can map that to a remote tracking ref with
> tracking_for_push_dest(). We could use branch_get_push() but does more
> than just map the refs as it checks push.default to see whether "git push"
> would actually push the branch.
Thanks! I have updated the code and added a test for remapped refspec.

With that said, I don't 100% understand what I'm doing with the refspecs, so maybe you can give it another review?

Harald
Harald Nordgren via GitGitGadget· Jan 3, 2026, 13:00 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v14 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com

Harald Nordgren (2):
  refactor: format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 171 ++++++++++++++++++++++++-------
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 344 insertions(+), 37 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v14
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v14
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v13:
 1:  a2c160c53e = 1:  a2c160c53e refactor: format_branch_comparison in preparation
 2:  891239211e ! 2:  b9b2f15498 status: show comparison with push remote tracking branch
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       
      +static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
      +{
     ++	struct remote *remote;
      +	const char *push_remote;
     ++	char *push_dst = NULL;
     ++	char *tracking_ref;
      +	const char *resolved;
     -+	struct strbuf ref_buf = STRBUF_INIT;
     -+	char *ret = NULL;
     ++	char *ret;
      +
      +	if (!branch)
      +		return NULL;
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +	if (!push_remote)
      +		return NULL;
      +
     -+	strbuf_addf(&ref_buf, "refs/remotes/%s/%s", push_remote, branch->name);
     ++	remote = remotes_remote_get(the_repository, push_remote);
     ++	if (!remote)
     ++		return NULL;
     ++
     ++	push_dst = remote_ref_for_branch(branch, 1);
     ++	if (!push_dst) {
     ++		if (remote->push.nr)
     ++			return NULL;
     ++		push_dst = xstrdup(branch->refname);
     ++	}
     ++
     ++	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
     ++	free(push_dst);
     ++
     ++	if (!tracking_ref)
     ++		return NULL;
      +
      +	resolved = refs_resolve_ref_unsafe(
      +		get_main_ref_store(the_repository),
     -+		ref_buf.buf,
     ++		tracking_ref,
      +		RESOLVE_REF_READING,
      +		NULL, NULL);
      +
     -+	if (resolved) {
     -+		if (full_ref_out)
     -+			*full_ref_out = xstrdup(resolved);
     -+		ret = refs_shorten_unambiguous_ref(
     -+			get_main_ref_store(the_repository), resolved, 0);
     ++	if (!resolved) {
     ++		free(tracking_ref);
     ++		return NULL;
      +	}
      +
     -+	strbuf_release(&ref_buf);
     ++	if (full_ref_out)
     ++		*full_ref_out = xstrdup(resolved);
     ++	ret = refs_shorten_unambiguous_ref(
     ++		get_main_ref_store(the_repository), resolved, 0);
     ++	free(tracking_ref);
      +	return ret;
      +}
      +
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	EOF
      +	test_cmp expect actual
      +'
     ++
     ++test_expect_success 'status shows remapped push refspec' '
     ++	(
     ++		cd test &&
     ++		git checkout -b feature8 origin/main &&
     ++		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
     ++		git push &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature8
     ++	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     ++
     ++	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status shows remapped push refspec with upstream remote' '
     ++	(
     ++		cd test &&
     ++		git checkout -b feature9 upstream/main &&
     ++		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
     ++		git push origin &&
     ++		advance work &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature9
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     ++
     ++	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
      +
       test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 3, 2026, 13:00 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v14 1/2] refactor: format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 99 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 59 insertions(+), 40 deletions(-)
Show changes to remote.c +59 −40
diff --git a/remote.c b/remote.c
index 59b3715120..58093f64b0 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,66 +2237,50 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+				     int ahead, int behind,
+				     const char *branch_name,
+				     int upstream_is_gone,
+				     enum ahead_behind_flags abf,
+				     int sti)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
+			branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else if (!sti) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
-	} else if (!theirs) {
+	} else if (ahead == 0 && behind == 0) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			branch_name);
+	} else if (ahead > 0 && behind == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
-			   ours),
-			base, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git push\" to publish your local commits)\n"));
-	} else if (!ours) {
+			   ahead),
+			branch_name, ahead);
+	} else if (behind > 0 && ahead == 0) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
 			       "and can be fast-forwarded.\n",
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
-			   theirs),
-			base, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git pull\" to update your local branch)\n"));
-	} else {
+			   behind),
+			branch_name, behind);
+	} else if (ahead > 0 && behind > 0) {
 		strbuf_addf(sb,
 			Q_("Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commit each, "
@@ -2304,13 +2288,48 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
-			   ours + theirs),
-			base, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+			   ahead + behind),
+			branch_name, ahead, behind);
+	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
+	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
+		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git push\" to publish your local commits)\n"));
+		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
+			strbuf_addstr(sb,
+				_("  (use \"git pull\" to update your local branch)\n"));
+		} else if (ours && theirs && show_divergence_advice &&
+			   advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
+		}
 	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 3, 2026, 13:00 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v14 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  80 ++++++++++++++-
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 289 insertions(+), 1 deletion(-)
Show changes to 2 files +289 −1

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 58093f64b0..f5d690d377 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,6 +2237,58 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int ahead, int behind,
 				     const char *branch_name,
@@ -2304,6 +2356,10 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	int show_push_comparison = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2315,9 +2371,18 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0)
+			show_push_comparison = 1;
+	}
+
 	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
 	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
-		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+		if (!theirs && !show_push_comparison &&
+		    advice_enabled(ADVICE_STATUS_HINTS)) {
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
@@ -2330,7 +2395,20 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		}
 	}
 
+	if (show_push_comparison) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, push_ours, push_theirs, push, 0, abf, push_sti);
+		if (push_sti > 0 && abf != AHEAD_BEHIND_QUICK) {
+			if (!push_theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
+				strbuf_addstr(sb,
+					_("  (use \"git push\" to publish your local commits)\n"));
+			}
+		}
+	}
+
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..68f298bf3a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,214 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 4, 2026, 02:17 UTC · re: Harald Nordgren · lore

Re: Another look?

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 8 quoted lines
>> Again this seems to do a "step 1 goes in a direction, step 2 fixes
>> its mistake, step 3 changes course" drunken-man's walk.
>>
>> The same advice to restructure them into a logical incremental
>> progression that moves the codebase in one consistent direction to
>> eventually reach the goal at the end applies.
>
> Isn't programming always bit of drunken-man's walk?

Yes, but the point is that other people do not have to see you taking roundabout route (or for that matter, they do not have to see you coding in your bathrobe like Linus, even if it may be true you ;-).

When other developers later need to figure out what you really wanted to achieve so that they do not break your intention while they update your code to fix bugs in it and/or adding new features on top of it, it would be far easier for them to work on a clean history.

Nico Williams· Jan 4, 2026, 02:41 UTC · re: Junio C Hamano · lore

Re: Another look?

On Sun, Jan 04, 2026 at 11:17:45AM +0900, Junio C Hamano wrote:
Show 7 quoted lines
> Harald Nordgren <haraldnordgren@gmail.com> writes:
> > Isn't programming always bit of drunken-man's walk?
> 
> Yes, but the point is that other people do not have to see you
> taking roundabout route (or for that matter, they do not have to see
> you coding in your bathrobe like Linus, even if it may be true you
> ;-).

Sun Microsystems, Inc. used a rebase workflow from 1992 to its end (RIP). Thousands of developers worked with stricly linear history (merges were not allowed!). Large team projects that had to have their own alternate repositories and do the Teamware equivalent of `git rebase --onto` periodically. There was no branching, only forking. Forks were kept for backporting and for history archival purposes.

It worked like a charm.

Merge workflows are -IMO- toxic and obnoxious. I do NOT want to see any programmer's "drunken-man's walk", unless it's because I'm interviewing them. There is no value to preserving that.

Show 5 quoted lines
> When other developers later need to figure out what you really
> wanted to achieve so that they do not break your intention while
> they update your code to fix bugs in it and/or adding new features
> on top of it, it would be far easier for them to work on a clean
> history.
Hear hear!

Strictly linear history makes it much easier to understand what's going on. The only merge turds I would allow are fast-forward ones where the purpose of the merge commit is to document push boundaries in the commit history, but even this I don't like.

Nico
Junio C Hamano· Jan 4, 2026, 04:17 UTC · re: Nico Williams · lore

Re: Another look?

Nico Williams <nico@cryptonector.com> writes:
> Sun Microsystems, Inc. used a rebase workflow from 1992 to its end
> ...
> It worked like a charm.

That's good for you. But I do not necessarily think merge workflows are bad, and my advice was certainly *not* about avoiding merges.

A linear logical progression of commits that is about a single theme is much nicer than drunken-man's walk that is also a linear sequence of commits. The distinction between them has nothing to do with merges.

And a history that bundles together a collection of linear logical progressions with merges of these topics one by one is much easier than rebasing these unrelated topics into a strictly linear single strand of pearls, by making it much clear where each of these topics concludes.

Junio C Hamano· Jan 4, 2026, 04:40 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v14 1/2] refactor: format_branch_comparison in preparation

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 58 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> Refactor format_branch_comparison function in preparation for showing
> comparison with push remote tracking branch.
>
> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
> ---
>  remote.c | 99 +++++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 59 insertions(+), 40 deletions(-)
>
> diff --git a/remote.c b/remote.c
> index 59b3715120..58093f64b0 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -2237,66 +2237,50 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
>  	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
>  }
>  
> -/*
> - * Return true when there is anything to report, otherwise false.
> - */
> -int format_tracking_info(struct branch *branch, struct strbuf *sb,
> -			 enum ahead_behind_flags abf,
> -			 int show_divergence_advice)
> +static void format_branch_comparison(struct strbuf *sb,
> +				     int ahead, int behind,
> +				     const char *branch_name,
> +				     int upstream_is_gone,
> +				     enum ahead_behind_flags abf,
> +				     int sti)
>  {
> -	int ours, theirs, sti;
> -	const char *full_base;
> -	char *base;
> -	int upstream_is_gone = 0;
> -
> -	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
> -	if (sti < 0) {
> -		if (!full_base)
> -			return 0;
> -		upstream_is_gone = 1;
> -	}
> -
> -	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
> -					    full_base, 0);
>  	if (upstream_is_gone) {
>  		strbuf_addf(sb,
>  			_("Your branch is based on '%s', but the upstream is gone.\n"),
> -			base);
> +			branch_name);
>  		if (advice_enabled(ADVICE_STATUS_HINTS))
>  			strbuf_addstr(sb,
>  				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
>  	} else if (!sti) {
>  		strbuf_addf(sb,
>  			_("Your branch is up to date with '%s'.\n"),
> -			base);
> +			branch_name);

So, we used to say this when stat-tracking-info gave 0, i.e., ours and theirs have not diverged. But now you decided to make the caller responsible for making the call to stat_tracking_info(), it seems to me that this if/else-if arm is somewhat redundant given that the caller can and will signal the same thing with both ahead and behind being zero.

IOW, it smells to me that leaving "sti" as a parameter to this function is an incomplete refactoring---I say "smell" because I haven't seen the other, new, caller that will be added in the future step of this patch series.

And if the new calling convention is to let the caller be responsible for calling stat_tracking_info() and figuring out the base branch name, it probably is also better to have the caller handle upstream_is_gone case. This is especially true if your plan is to reuse this "branch comparison" helper to compare a branch with another branch that is *NOT* its upstream (e.g., where the result is pushed to).

Show 9 quoted lines
>  	} else if (abf == AHEAD_BEHIND_QUICK) {
>  		strbuf_addf(sb,
>  			    _("Your branch and '%s' refer to different commits.\n"),
> -			    base);
> +			    branch_name);
>  		if (advice_enabled(ADVICE_STATUS_HINTS))
>  			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>  				    "git status --ahead-behind");
> -	} else if (!theirs) {

As the code already handled !sti, which is equivalent to (!theirs && !ours), in the original, !theirs here meant (!theirs && ours), which is (ahead && !behind) in the new world order, which we see a few lines below.

> +	} else if (ahead == 0 && behind == 0) {

And this is what the above (!sti) should have checked for, after the helper function lost the sti parameter.

Do not compare with 0 for equality.  Instead write it like so:
	} else if (!ahead && !behind) {
I won't repeat for other style violations of the same kind.
> +		strbuf_addf(sb,
> +			_("Your branch is up to date with '%s'.\n"),
> +			branch_name);
Show 83 quoted lines
> +	} else if (ahead > 0 && behind == 0) {
>  		strbuf_addf(sb,
>  			Q_("Your branch is ahead of '%s' by %d commit.\n",
>  			   "Your branch is ahead of '%s' by %d commits.\n",
> -			   ours),
> -			base, ours);
> -		if (advice_enabled(ADVICE_STATUS_HINTS))
> -			strbuf_addstr(sb,
> -				_("  (use \"git push\" to publish your local commits)\n"));
> -	} else if (!ours) {
> +			   ahead),
> +			branch_name, ahead);
> +	} else if (behind > 0 && ahead == 0) {
>  		strbuf_addf(sb,
>  			Q_("Your branch is behind '%s' by %d commit, "
>  			       "and can be fast-forwarded.\n",
>  			   "Your branch is behind '%s' by %d commits, "
>  			       "and can be fast-forwarded.\n",
> -			   theirs),
> -			base, theirs);
> -		if (advice_enabled(ADVICE_STATUS_HINTS))
> -			strbuf_addstr(sb,
> -				_("  (use \"git pull\" to update your local branch)\n"));
> -	} else {
> +			   behind),
> +			branch_name, behind);
> +	} else if (ahead > 0 && behind > 0) {
>  		strbuf_addf(sb,
>  			Q_("Your branch and '%s' have diverged,\n"
>  			       "and have %d and %d different commit each, "
> @@ -2304,13 +2288,48 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>  			   "Your branch and '%s' have diverged,\n"
>  			       "and have %d and %d different commits each, "
>  			       "respectively.\n",
> -			   ours + theirs),
> -			base, ours, theirs);
> -		if (show_divergence_advice &&
> -		    advice_enabled(ADVICE_STATUS_HINTS))
> +			   ahead + behind),
> +			branch_name, ahead, behind);
> +	}
> +}
> +
> +/*
> + * Return true when there is anything to report, otherwise false.
> + */
> +int format_tracking_info(struct branch *branch, struct strbuf *sb,
> +			 enum ahead_behind_flags abf,
> +			 int show_divergence_advice)
> +{
> +	int ours, theirs, sti;
> +	const char *full_base;
> +	char *base;
> +	int upstream_is_gone = 0;
> +
> +	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
> +	if (sti < 0) {
> +		if (!full_base)
> +			return 0;
> +		upstream_is_gone = 1;
> +	}
> +
> +	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
> +					    full_base, 0);
> +
> +	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
> +	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
> +		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
> +			strbuf_addstr(sb,
> +				_("  (use \"git push\" to publish your local commits)\n"));
> +		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
> +			strbuf_addstr(sb,
> +				_("  (use \"git pull\" to update your local branch)\n"));
> +		} else if (ours && theirs && show_divergence_advice &&
> +			   advice_enabled(ADVICE_STATUS_HINTS)) {
>  			strbuf_addstr(sb,
>  				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
> +		}
>  	}
> +
>  	free(base);
>  	return 1;
>  }

Overall I think this is a reasonable direction to go, even though passing the "sti" thing is iffy, and the implementation in the function may have small rooms for improvements.

I didn't look at minute details to ensure that the output for all cases are identical to that of the original, though.

Harald Nordgren· Jan 4, 2026, 10:27 UTC · re: Junio C Hamano · lore

Another look?

Show 6 quoted lines
> And if the new calling convention is to let the caller be responsible for
> calling stat_tracking_info() and figuring out the base branch name, it
> probably is also better to have the caller handle upstream_is_gone case.
> This is especially true if your plan is to reuse this "branch comparison"
> helper to compare a branch with another branch that is *NOT* its upstream
> (e.g., where the result is pushed to).
Done, will be in the next patch!
Show 10 quoted lines
> IOW, it smells to me that leaving "sti" as a parameter to this function
> is an incomplete refactoring---I say "smell" because I haven't seen the
> other, new, caller that will be added in the future step of this patch
> series.
>
> [...]
>
> As the code already handled !sti, which is equivalent to (!theirs &&
> !ours), in the original, !theirs here meant (!theirs && ours), which is
> (ahead && !behind) in the new world order, which we see a few lines below.

This sounds reasonable, however, if I replace '!sti' with 'ahead && !behind' then old tests are breaking, one why to avoid them breaking is to switch the order of these cases, to this, would that be acceptable?

``` } else if (abf == AHEAD_BEHIND_QUICK) { strbuf_addf(sb, _("Your branch and '%s' refer to different commits.\n"), branch_name); if (advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addf(sb, _(" (use \"%s\" for details)\n"), "git status --ahead-behind"); } else if (!theirs && !ours) { strbuf_addf(sb, _("Your branch is up to date with '%s'.\n"), branch_name); ```

> Do not compare with 0 for equality. Instead write it like so:
Good point! I will upate it!
Thanks for you continued attention to this, much appreciated!
Harald
Harald Nordgren· Jan 4, 2026, 10:48 UTC · re: Harald Nordgren · lore

Another look?

Sorry, here's what the code block will look like:
```
	} else if (abf == AHEAD_BEHIND_QUICK) {
		strbuf_addf(sb,
			    _("Your branch and '%s' refer to different commits.\n"),
			    branch_name);
		if (advice_enabled(ADVICE_STATUS_HINTS))
			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
				    "git status --ahead-behind");
	} else if (!theirs && !ours) {
		strbuf_addf(sb,
			_("Your branch is up to date with '%s'.\n"),
			branch_name);
```
Harald
Harald Nordgren via GitGitGadget· Jan 4, 2026, 11:53 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v15 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 168 ++++++++++++++++++++++++-------
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 342 insertions(+), 36 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v15
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v15
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v14:
 1:  a2c160c53e ! 1:  cf4e9779c5 refactor: format_branch_comparison in preparation
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    refactor: format_branch_comparison in preparation
     +    refactor format_branch_comparison in preparation
      
          Refactor format_branch_comparison function in preparation for showing
          comparison with push remote tracking branch.
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -			 enum ahead_behind_flags abf,
      -			 int show_divergence_advice)
      +static void format_branch_comparison(struct strbuf *sb,
     -+				     int ahead, int behind,
     ++				     int ours, int theirs,
      +				     const char *branch_name,
     -+				     int upstream_is_gone,
      +				     enum ahead_behind_flags abf,
     -+				     int sti)
     ++				     int show_divergence_advice)
       {
      -	int ours, theirs, sti;
      -	const char *full_base;
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -
      -	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
      -					    full_base, 0);
     - 	if (upstream_is_gone) {
     - 		strbuf_addf(sb,
     - 			_("Your branch is based on '%s', but the upstream is gone.\n"),
     +-	if (upstream_is_gone) {
     +-		strbuf_addf(sb,
     +-			_("Your branch is based on '%s', but the upstream is gone.\n"),
      -			base);
     -+			branch_name);
     - 		if (advice_enabled(ADVICE_STATUS_HINTS))
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     - 	} else if (!sti) {
     - 		strbuf_addf(sb,
     - 			_("Your branch is up to date with '%s'.\n"),
     +-		if (advice_enabled(ADVICE_STATUS_HINTS))
     +-			strbuf_addstr(sb,
     +-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     +-	} else if (!sti) {
     +-		strbuf_addf(sb,
     +-			_("Your branch is up to date with '%s'.\n"),
      -			base);
     -+			branch_name);
     - 	} else if (abf == AHEAD_BEHIND_QUICK) {
     +-	} else if (abf == AHEAD_BEHIND_QUICK) {
     ++	if (abf == AHEAD_BEHIND_QUICK) {
       		strbuf_addf(sb,
       			    _("Your branch and '%s' refer to different commits.\n"),
      -			    base);
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       		if (advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
       				    "git status --ahead-behind");
     --	} else if (!theirs) {
     -+	} else if (ahead == 0 && behind == 0) {
     ++	} else if (!ours && !theirs) {
      +		strbuf_addf(sb,
      +			_("Your branch is up to date with '%s'.\n"),
      +			branch_name);
     -+	} else if (ahead > 0 && behind == 0) {
     + 	} else if (!theirs) {
       		strbuf_addf(sb,
       			Q_("Your branch is ahead of '%s' by %d commit.\n",
       			   "Your branch is ahead of '%s' by %d commits.\n",
     --			   ours),
     + 			   ours),
      -			base, ours);
     --		if (advice_enabled(ADVICE_STATUS_HINTS))
     --			strbuf_addstr(sb,
     --				_("  (use \"git push\" to publish your local commits)\n"));
     --	} else if (!ours) {
     -+			   ahead),
     -+			branch_name, ahead);
     -+	} else if (behind > 0 && ahead == 0) {
     - 		strbuf_addf(sb,
     - 			Q_("Your branch is behind '%s' by %d commit, "
     - 			       "and can be fast-forwarded.\n",
     ++			branch_name, ours);
     + 		if (advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git push\" to publish your local commits)\n"));
     +@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       			   "Your branch is behind '%s' by %d commits, "
       			       "and can be fast-forwarded.\n",
     --			   theirs),
     + 			   theirs),
      -			base, theirs);
     --		if (advice_enabled(ADVICE_STATUS_HINTS))
     --			strbuf_addstr(sb,
     --				_("  (use \"git pull\" to update your local branch)\n"));
     --	} else {
     -+			   behind),
     -+			branch_name, behind);
     -+	} else if (ahead > 0 && behind > 0) {
     - 		strbuf_addf(sb,
     - 			Q_("Your branch and '%s' have diverged,\n"
     - 			       "and have %d and %d different commit each, "
     ++			branch_name, theirs);
     + 		if (advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git pull\" to update your local branch)\n"));
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 			   "Your branch and '%s' have diverged,\n"
       			       "and have %d and %d different commits each, "
       			       "respectively.\n",
     --			   ours + theirs),
     + 			   ours + theirs),
      -			base, ours, theirs);
     --		if (show_divergence_advice &&
     --		    advice_enabled(ADVICE_STATUS_HINTS))
     -+			   ahead + behind),
     -+			branch_name, ahead, behind);
     -+	}
     ++			branch_name, ours, theirs);
     + 		if (show_divergence_advice &&
     + 		    advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
     + 	}
      +}
      +
      +/*
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
      +					    full_base, 0);
      +
     -+	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
     -+	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
     -+		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
     -+			strbuf_addstr(sb,
     -+				_("  (use \"git push\" to publish your local commits)\n"));
     -+		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
     ++	if (upstream_is_gone) {
     ++		strbuf_addf(sb,
     ++			_("Your branch is based on '%s', but the upstream is gone.\n"),
     ++			base);
     ++		if (advice_enabled(ADVICE_STATUS_HINTS))
      +			strbuf_addstr(sb,
     -+				_("  (use \"git pull\" to update your local branch)\n"));
     -+		} else if (ours && theirs && show_divergence_advice &&
     -+			   advice_enabled(ADVICE_STATUS_HINTS)) {
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
     -+		}
     - 	}
     ++				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     ++	} else {
     ++		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
     ++	}
      +
       	free(base);
       	return 1;
 2:  b9b2f15498 ! 2:  a435cf4ce4 status: show comparison with push remote tracking branch
     @@ Commit message
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## remote.c ##
     +@@
     + 
     + enum map_direction { FROM_SRC, FROM_DST };
     + 
     ++enum branch_type {
     ++	PUSH = 1 << 0,
     ++	PULL = 1 << 1
     ++};
     ++
     + struct counted_string {
     + 	size_t len;
     + 	const char *s;
      @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +}
      +
       static void format_branch_comparison(struct strbuf *sb,
     - 				     int ahead, int behind,
     + 				     int ours, int theirs,
       				     const char *branch_name,
     + 				     enum ahead_behind_flags abf,
     ++				     enum branch_type bt,
     + 				     int show_divergence_advice)
     + {
     + 	if (abf == AHEAD_BEHIND_QUICK) {
     +@@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     + 			   "Your branch is ahead of '%s' by %d commits.\n",
     + 			   ours),
     + 			branch_name, ours);
     +-		if (advice_enabled(ADVICE_STATUS_HINTS))
     ++		if ((bt & PUSH) && advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git push\" to publish your local commits)\n"));
     + 	} else if (!ours) {
     +@@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     + 			       "and can be fast-forwarded.\n",
     + 			   theirs),
     + 			branch_name, theirs);
     +-		if (advice_enabled(ADVICE_STATUS_HINTS))
     ++		if ((bt & PULL) && advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git pull\" to update your local branch)\n"));
     + 	} else {
     +@@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     + 			       "respectively.\n",
     + 			   ours + theirs),
     + 			branch_name, ours, theirs);
     +-		if (show_divergence_advice &&
     ++		if ((bt & PULL) &&
     ++		    show_divergence_advice &&
     + 		    advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
       	int upstream_is_gone = 0;
     ++	enum branch_type base_bt = PUSH | PULL;
      +	int push_ours, push_theirs, push_sti;
      +	char *full_push = NULL;
      +	char *push = NULL;
     -+	int show_push_comparison = 0;
     ++	enum branch_type push_bt = 0;
       
       	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
       	if (sti < 0) {
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +	if (push && strcmp(base, push)) {
      +		push_sti = stat_branch_pair(branch->refname, full_push,
      +					   &push_ours, &push_theirs, abf);
     -+		if (push_sti >= 0)
     -+			show_push_comparison = 1;
     ++		if (push_sti >= 0) {
     ++			base_bt = PULL;
     ++			push_bt = PUSH;
     ++		}
      +	}
      +
     - 	format_branch_comparison(sb, ours, theirs, base, upstream_is_gone, abf, sti);
     - 	if (sti > 0 && abf != AHEAD_BEHIND_QUICK) {
     --		if (!theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
     -+		if (!theirs && !show_push_comparison &&
     -+		    advice_enabled(ADVICE_STATUS_HINTS)) {
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git push\" to publish your local commits)\n"));
     - 		} else if (!ours && advice_enabled(ADVICE_STATUS_HINTS)) {
     + 	if (upstream_is_gone) {
     + 		strbuf_addf(sb,
     + 			_("Your branch is based on '%s', but the upstream is gone.\n"),
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 		}
     - 	}
     - 
     -+	if (show_push_comparison) {
     -+		strbuf_addstr(sb, "\n");
     -+		format_branch_comparison(sb, push_ours, push_theirs, push, 0, abf, push_sti);
     -+		if (push_sti > 0 && abf != AHEAD_BEHIND_QUICK) {
     -+			if (!push_theirs && advice_enabled(ADVICE_STATUS_HINTS)) {
     -+				strbuf_addstr(sb,
     -+					_("  (use \"git push\" to publish your local commits)\n"));
     -+			}
     -+		}
     + 			strbuf_addstr(sb,
     + 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     + 	} else {
     +-		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, ours, theirs, base, abf, base_bt,
     ++					 show_divergence_advice);
      +	}
      +
     ++	if (push_bt & PUSH) {
     ++		strbuf_addstr(sb, "\n");
     ++		format_branch_comparison(sb, push_ours, push_theirs, push, abf,
     ++					 push_bt, 0);
     + 	}
     + 
       	free(base);
      +	free(full_push);
      +	free(push);
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 4, 2026, 11:53 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v15 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 85 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 49 insertions(+), 36 deletions(-)
Show changes to remote.c +49 −36
diff --git a/remote.c b/remote.c
index 59b3715120..b6a9e14376 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,51 +2237,29 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     int show_divergence_advice)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
-		strbuf_addf(sb,
-			_("Your branch is up to date with '%s'.\n"),
-			base);
-	} else if (abf == AHEAD_BEHIND_QUICK) {
+	if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
+	} else if (!ours && !theirs) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			branch_name);
 	} else if (!theirs) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2270,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2283,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 4, 2026, 11:53 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v15 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  91 ++++++++++++++++-
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 297 insertions(+), 4 deletions(-)
Show changes to 2 files +297 −4

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index b6a9e14376..3a8706395a 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum branch_type {
+	PUSH = 1 << 0,
+	PULL = 1 << 1
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,10 +2242,63 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     enum branch_type bt,
 				     int show_divergence_advice)
 {
 	if (abf == AHEAD_BEHIND_QUICK) {
@@ -2260,7 +2318,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((bt & PUSH) && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2271,7 +2329,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((bt & PULL) && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2284,7 +2342,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
+		if ((bt & PULL) &&
+		    show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
@@ -2302,6 +2361,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	enum branch_type base_bt = PUSH | PULL;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	enum branch_type push_bt = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2313,6 +2377,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_bt = PULL;
+			push_bt = PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2321,10 +2395,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, ours, theirs, base, abf, base_bt,
+					 show_divergence_advice);
+	}
+
+	if (push_bt & PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, push_ours, push_theirs, push, abf,
+					 push_bt, 0);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..68f298bf3a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,214 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 4, 2026, 23:21 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v16 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 171 ++++++++++++++++++++++++-------
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 345 insertions(+), 36 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v16
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v16
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v15:
 1:  cf4e9779c5 = 1:  cf4e9779c5 refactor format_branch_comparison in preparation
 2:  a435cf4ce4 ! 2:  06cb483f61 status: show comparison with push remote tracking branch
     @@ remote.c
       
       enum map_direction { FROM_SRC, FROM_DST };
       
     -+enum branch_type {
     -+	PUSH = 1 << 0,
     -+	PULL = 1 << 1
     ++enum branch_mode_flags {
     ++	BRANCH_MODE_PULL = (1 << 0),
     ++	BRANCH_MODE_PUSH = (1 << 1),
      +};
      +
       struct counted_string {
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +
      +	if (full_ref_out)
      +		*full_ref_out = xstrdup(resolved);
     ++
      +	ret = refs_shorten_unambiguous_ref(
      +		get_main_ref_store(the_repository), resolved, 0);
      +	free(tracking_ref);
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       				     int ours, int theirs,
       				     const char *branch_name,
       				     enum ahead_behind_flags abf,
     -+				     enum branch_type bt,
     ++				     enum branch_mode_flags advice_flags,
       				     int show_divergence_advice)
       {
       	if (abf == AHEAD_BEHIND_QUICK) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours),
       			branch_name, ours);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if ((bt & PUSH) && advice_enabled(ADVICE_STATUS_HINTS))
     ++		if ((advice_flags & BRANCH_MODE_PUSH) &&
     ++		    advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git push\" to publish your local commits)\n"));
       	} else if (!ours) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   theirs),
       			branch_name, theirs);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if ((bt & PULL) && advice_enabled(ADVICE_STATUS_HINTS))
     ++		if ((advice_flags & BRANCH_MODE_PULL) &&
     ++		    advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours + theirs),
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
     -+		if ((bt & PULL) &&
     ++		if ((advice_flags & BRANCH_MODE_PULL) &&
      +		    show_divergence_advice &&
       		    advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
       	int upstream_is_gone = 0;
     -+	enum branch_type base_bt = PUSH | PULL;
     ++	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
      +	int push_ours, push_theirs, push_sti;
      +	char *full_push = NULL;
      +	char *push = NULL;
     -+	enum branch_type push_bt = 0;
     ++	enum branch_mode_flags push_branch_modes = 0;
       
       	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
       	if (sti < 0) {
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +		push_sti = stat_branch_pair(branch->refname, full_push,
      +					   &push_ours, &push_theirs, abf);
      +		if (push_sti >= 0) {
     -+			base_bt = PULL;
     -+			push_bt = PUSH;
     ++			base_branch_modes = BRANCH_MODE_PULL;
     ++			push_branch_modes = BRANCH_MODE_PUSH;
      +		}
      +	}
      +
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
       	} else {
      -		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
     -+		format_branch_comparison(sb, ours, theirs, base, abf, base_bt,
     -+					 show_divergence_advice);
     ++		format_branch_comparison(sb, ours, theirs, base, abf,
     ++					 base_branch_modes, show_divergence_advice);
      +	}
      +
     -+	if (push_bt & PUSH) {
     ++	if (push_branch_modes & BRANCH_MODE_PUSH) {
      +		strbuf_addstr(sb, "\n");
      +		format_branch_comparison(sb, push_ours, push_theirs, push, abf,
     -+					 push_bt, 0);
     ++					 push_branch_modes, 0);
       	}
       
       	free(base);
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 4, 2026, 23:21 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v16 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 85 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 49 insertions(+), 36 deletions(-)
Show changes to remote.c +49 −36
diff --git a/remote.c b/remote.c
index 59b3715120..b6a9e14376 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,51 +2237,29 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     int show_divergence_advice)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
-		strbuf_addf(sb,
-			_("Your branch is up to date with '%s'.\n"),
-			base);
-	} else if (abf == AHEAD_BEHIND_QUICK) {
+	if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
+	} else if (!ours && !theirs) {
+		strbuf_addf(sb,
+			_("Your branch is up to date with '%s'.\n"),
+			branch_name);
 	} else if (!theirs) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2270,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2283,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 4, 2026, 23:21 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v16 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  94 +++++++++++++++++-
 t/t6040-tracking-info.sh | 210 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 300 insertions(+), 4 deletions(-)
Show changes to 2 files +300 −4

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index b6a9e14376..74a394c2a6 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum branch_mode_flags {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,10 +2242,64 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     enum branch_mode_flags advice_flags,
 				     int show_divergence_advice)
 {
 	if (abf == AHEAD_BEHIND_QUICK) {
@@ -2260,7 +2319,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((advice_flags & BRANCH_MODE_PUSH) &&
+		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2271,7 +2331,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((advice_flags & BRANCH_MODE_PULL) &&
+		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2284,7 +2345,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
+		if ((advice_flags & BRANCH_MODE_PULL) &&
+		    show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
@@ -2302,6 +2364,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	enum branch_mode_flags push_branch_modes = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2313,6 +2380,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2321,10 +2398,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, push_ours, push_theirs, push, abf,
+					 push_branch_modes, 0);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..68f298bf3a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,214 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 5, 2026, 01:16 UTC · re: Harald Nordgren · lore

Re: Another look?

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 15 quoted lines
> Sorry, here's what the code block will look like:
>
> ```
> 	} else if (abf == AHEAD_BEHIND_QUICK) {
> 		strbuf_addf(sb,
> 			    _("Your branch and '%s' refer to different commits.\n"),
> 			    branch_name);
> 		if (advice_enabled(ADVICE_STATUS_HINTS))
> 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
> 				    "git status --ahead-behind");
> 	} else if (!theirs && !ours) {
> 		strbuf_addf(sb,
> 			_("Your branch is up to date with '%s'.\n"),
> 			branch_name);
> ```

I did not check what comes before or after this block, but the above looks like a reversed ordering. If you have "the branches are the same" check first, it would make more sense, as after ruling out that case, QUICK can short-cut comparison and asy "they are different", and presumably after these two else/if arms, you'd have cases for "theirs && !ours -> they are ahead of us", "!theirs && ours -> we are ahead of them", and "theirs && ours -> we diverged" to handle.

Junio C Hamano· Jan 5, 2026, 02:05 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v16 1/2] refactor format_branch_comparison in preparation

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 17 quoted lines
> -	} else if (!sti) {
> -		strbuf_addf(sb,
> -			_("Your branch is up to date with '%s'.\n"),
> -			base);
> -	} else if (abf == AHEAD_BEHIND_QUICK) {
> +	if (abf == AHEAD_BEHIND_QUICK) {
>  		strbuf_addf(sb,
>  			    _("Your branch and '%s' refer to different commits.\n"),
> -			    base);
> +			    branch_name);
>  		if (advice_enabled(ADVICE_STATUS_HINTS))
>  			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>  				    "git status --ahead-behind");
> +	} else if (!ours && !theirs) {
> +		strbuf_addf(sb,
> +			_("Your branch is up to date with '%s'.\n"),
> +			branch_name);

We used to check if there is nothing to report (i.e., !sti is a signal from stat_tracking_info() that there are no differences between the branches) and reported that first, so when abf was checked, we knew that there are some differences. Now, your patch reverses the order so whether there is or is not a change, abf codepath will always report "you have differences!".

This smells iffy.
Harald Nordgren· Jan 5, 2026, 09:15 UTC · re: Junio C Hamano · lore

Another look?

Show 24 quoted lines
> > -     } else if (!sti) {
> > -             strbuf_addf(sb,
> > -                     _("Your branch is up to date with '%s'.\n"),
> > -                     base);
> > -     } else if (abf == AHEAD_BEHIND_QUICK) {
> > +     if (abf == AHEAD_BEHIND_QUICK) {
> >               strbuf_addf(sb,
> >                           _("Your branch and '%s' refer to different commits.\n"),
> > -                         base);
> > +                         branch_name);
> >               if (advice_enabled(ADVICE_STATUS_HINTS))
> >                       strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
> >                                   "git status --ahead-behind");
> > +     } else if (!ours && !theirs) {
> > +             strbuf_addf(sb,
> > +                     _("Your branch is up to date with '%s'.\n"),
> > +                     branch_name);
>
> We used to check if there is nothing to report (i.e., !sti is a
> signal from stat_tracking_info() that there are no differences
> between the branches) and reported that first, so when abf was
> checked, we knew that there are some differences.  Now, your patch
> reverses the order so whether there is or is not a change, abf
> codepath will always report "you have differences!".

Agreed! This change was done to get rid of sti as a parameter. Maybe I misunderstood your previous comment around the sti.

I will bring back sti as a parameter to ’format_branch_comparison’ now, I don’t see any other way to solve this.

Harald
Harald Nordgren via GitGitGadget· Jan 5, 2026, 10:17 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v17 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 169 ++++++++++++++++++++-----
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 397 insertions(+), 34 deletions(-)
base-commit: 68cb7f9e92a5d8e9824f5b52ac3d0a9d8f653dbe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v17
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v17
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v16:
 1:  cf4e9779c5 ! 1:  b62a9feb4d refactor format_branch_comparison in preparation
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -int format_tracking_info(struct branch *branch, struct strbuf *sb,
      -			 enum ahead_behind_flags abf,
      -			 int show_divergence_advice)
     -+static void format_branch_comparison(struct strbuf *sb,
     -+				     int ours, int theirs,
     -+				     const char *branch_name,
     -+				     enum ahead_behind_flags abf,
     -+				     int show_divergence_advice)
     - {
     +-{
      -	int ours, theirs, sti;
      -	const char *full_base;
      -	char *base;
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -			strbuf_addstr(sb,
      -				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      -	} else if (!sti) {
     --		strbuf_addf(sb,
     --			_("Your branch is up to date with '%s'.\n"),
     ++static void format_branch_comparison(struct strbuf *sb,
     ++				     int sti,
     ++				     int ours, int theirs,
     ++				     const char *branch_name,
     ++				     enum ahead_behind_flags abf,
     ++				     int show_divergence_advice)
     ++{
     ++	if (!sti) {
     + 		strbuf_addf(sb,
     + 			_("Your branch is up to date with '%s'.\n"),
      -			base);
     --	} else if (abf == AHEAD_BEHIND_QUICK) {
     -+	if (abf == AHEAD_BEHIND_QUICK) {
     ++			branch_name);
     + 	} else if (abf == AHEAD_BEHIND_QUICK) {
       		strbuf_addf(sb,
       			    _("Your branch and '%s' refer to different commits.\n"),
      -			    base);
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       		if (advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
       				    "git status --ahead-behind");
     -+	} else if (!ours && !theirs) {
     -+		strbuf_addf(sb,
     -+			_("Your branch is up to date with '%s'.\n"),
     -+			branch_name);
     - 	} else if (!theirs) {
     - 		strbuf_addf(sb,
     +@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       			Q_("Your branch is ahead of '%s' by %d commit.\n",
       			   "Your branch is ahead of '%s' by %d commits.\n",
       			   ours),
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +			strbuf_addstr(sb,
      +				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      +	} else {
     -+		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
      +	}
      +
       	free(base);
 2:  06cb483f61 ! 2:  1348542edc status: show comparison with push remote tracking branch
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +}
      +
       static void format_branch_comparison(struct strbuf *sb,
     + 				     int sti,
       				     int ours, int theirs,
       				     const char *branch_name,
       				     enum ahead_behind_flags abf,
      +				     enum branch_mode_flags advice_flags,
       				     int show_divergence_advice)
       {
     - 	if (abf == AHEAD_BEHIND_QUICK) {
     + 	if (!sti) {
     +@@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     + 		strbuf_addf(sb,
     + 			    _("Your branch and '%s' refer to different commits.\n"),
     + 			    branch_name);
     +-		if (advice_enabled(ADVICE_STATUS_HINTS))
     ++		if ((advice_flags & BRANCH_MODE_PUSH) &&
     ++		    advice_enabled(ADVICE_STATUS_HINTS))
     + 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
     + 				    "git status --ahead-behind");
     + 	} else if (!theirs) {
      @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   "Your branch is ahead of '%s' by %d commits.\n",
       			   ours),
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       			strbuf_addstr(sb,
       				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
       	} else {
     --		format_branch_comparison(sb, ours, theirs, base, abf, show_divergence_advice);
     -+		format_branch_comparison(sb, ours, theirs, base, abf,
     +-		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, sti, ours, theirs, base, abf,
      +					 base_branch_modes, show_divergence_advice);
      +	}
      +
      +	if (push_branch_modes & BRANCH_MODE_PUSH) {
      +		strbuf_addstr(sb, "\n");
     -+		format_branch_comparison(sb, push_ours, push_theirs, push, abf,
     ++		format_branch_comparison(sb, push_sti, push_ours, push_theirs, push, abf,
      +					 push_branch_modes, 0);
       	}
       
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     ++test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
     ++	(
     ++		cd test &&
     ++		git checkout b4 &&
     ++		git status --no-ahead-behind >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch b4
     ++	Your branch and ${SQ}origin/main${SQ} refer to different commits.
     ++	  (use "git status --ahead-behind" for details)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
      +test_expect_success 'status shows ahead of both origin/main and feature branch' '
      +	(
      +		cd test &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     ++test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
     ++	(
     ++		cd test &&
     ++		git checkout feature4 &&
     ++		git status --no-ahead-behind >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature4
     ++	Your branch and ${SQ}origin/main${SQ} refer to different commits.
     ++
     ++	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
     ++	  (use "git status --ahead-behind" for details)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
      +test_expect_success 'setup upstream remote' '
      +	(
      +		cd test &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     ++test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
     ++	(
     ++		cd test &&
     ++		git checkout feature7 &&
     ++		git push origin &&
     ++		git status --no-ahead-behind >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature7
     ++	Your branch is up to date with ${SQ}upstream/main${SQ}.
     ++
     ++	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
      +test_expect_success 'checkout shows push branch up to date' '
      +	(
      +		cd test &&
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 5, 2026, 10:17 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v17 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index 59b3715120..7163a8ec28 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     int sti,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     int show_divergence_advice)
+{
+	if (!sti) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2271,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2284,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 5, 2026, 10:17 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v17 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 |  97 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 354 insertions(+), 5 deletions(-)
Show changes to 2 files +354 −5

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index 7163a8ec28..1f87b85b22 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum branch_mode_flags {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,11 +2242,65 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     int sti,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     enum branch_mode_flags advice_flags,
 				     int show_divergence_advice)
 {
 	if (!sti) {
@@ -2252,7 +2311,8 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((advice_flags & BRANCH_MODE_PUSH) &&
+		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2321,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((advice_flags & BRANCH_MODE_PUSH) &&
+		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2333,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if ((advice_flags & BRANCH_MODE_PULL) &&
+		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2285,7 +2347,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
+		if ((advice_flags & BRANCH_MODE_PULL) &&
+		    show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
@@ -2303,6 +2366,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	enum branch_mode_flags push_branch_modes = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2314,6 +2382,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2322,10 +2400,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, sti, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, push_sti, push_ours, push_theirs, push, abf,
+					 push_branch_modes, 0);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 5, 2026, 12:28 UTC · re: Harald Nordgren · lore

Re: Another look?

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 30 quoted lines
>> > -     } else if (!sti) {
>> > -             strbuf_addf(sb,
>> > -                     _("Your branch is up to date with '%s'.\n"),
>> > -                     base);
>> > -     } else if (abf == AHEAD_BEHIND_QUICK) {
>> > +     if (abf == AHEAD_BEHIND_QUICK) {
>> >               strbuf_addf(sb,
>> >                           _("Your branch and '%s' refer to different commits.\n"),
>> > -                         base);
>> > +                         branch_name);
>> >               if (advice_enabled(ADVICE_STATUS_HINTS))
>> >                       strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>> >                                   "git status --ahead-behind");
>> > +     } else if (!ours && !theirs) {
>> > +             strbuf_addf(sb,
>> > +                     _("Your branch is up to date with '%s'.\n"),
>> > +                     branch_name);
>>
>> We used to check if there is nothing to report (i.e., !sti is a
>> signal from stat_tracking_info() that there are no differences
>> between the branches) and reported that first, so when abf was
>> checked, we knew that there are some differences.  Now, your patch
>> reverses the order so whether there is or is not a change, abf
>> codepath will always report "you have differences!".
>
> Agreed! This change was done to get rid of sti as a parameter. Maybe I
> misunderstood your previous comment around the sti.
>
> I will bring back sti as a parameter to ’format_branch_comparison’ now,
> I don’t see any other way to solve this.

Please don't. Unless my assumption, which is that in the old code "!sti" and "!ours && !theirs" is equivalent, is wrong, all you need to do around that part is to first check "if (!ours && !theirs)" and say "your branch is up to date with...", and then have the check "else if (abf == ABQ)" next. That way, when we check abf we know the branches are different.

Harald Nordgren· Jan 5, 2026, 13:16 UTC · re: Junio C Hamano · lore

Another look?

Show 6 quoted lines
> Please don't.  Unless my assumption, which is that in the old code
> "!sti" and "!ours && !theirs" is equivalent, is wrong, all you need
> to do around that part is to first check "if (!ours && !theirs)" and
> say "your branch is up to date with...", and then have the check
> "else if (abf == ABQ)" next.  That way, when we check abf we know
> the branches are different.

It seems to be an incorrect assumption. This code change breaks several tests including old ones:

```
Show changes to remote.c +1 −1
diff --git a/remote.c b/remote.c
index 1f87b85b22..8db4fcd7b5 100644
--- a/remote.c
+++ b/remote.c
@@ -2303,7 +2303,7 @@ static void format_branch_comparison(struct strbuf *sb,
 				     enum branch_mode_flags advice_flags,
 				     int show_divergence_advice)
 {
-	if (!sti) {
+	if (!ours && !theirs) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			branch_name);
```


Harald
D. Ben Knoble· Jan 5, 2026, 21:55 UTC · re: Harald Nordgren · lore

Re: Another look?

On Thu, Jan 1, 2026 at 6:38 PM Harald Nordgren <haraldnordgren@gmail.com> wrote:
Show 15 quoted lines
>
> > Again this seems to do a "step 1 goes in a direction, step 2 fixes
> > its mistake, step 3 changes course" drunken-man's walk.
> >
> > The same advice to restructure them into a logical incremental
> > progression that moves the codebase in one consistent direction to
> > eventually reach the goal at the end applies.
>
> Isn't programming always bit of drunken-man's walk?
>
> I'm very hesitant to restructure my history before I am confident I will
> not need any of the old work later -- I would hate to lose history if I
> make a mistake.
>
> One option is to keep my code backed up on a separate branch locally, but
Git will already do this, more or less! See "git reflog". No need to worry :)
I don't fear-driven development to lead to optimal results ;)
Show 5 quoted lines
> As a side-note: In my day job we only allow "squash and merge" on our
> GitHub. This gives devs the flexibility to treat their branches as a WIP
> area before merging, but still gives a pristine git history after merge.
> This feels to me like a good trade-offs. But again, happy to take
> instructions on how to do better.

I think others have covered this, but you can both "branch is WIP" and "clean history" by iterating within a PR. The GitHub UI does not make this particularly nice [1], but my recipe is essentially

1. Make changes
2. Post range-diff [2] and force-push

[1]: https://benknoble.github.io/blog/2025/03/17/more-range-diff/ [2]: https://benknoble.github.io/blog/2024/10/04/copy-range-diff/

-- 
D. Ben Knoble
Junio C Hamano· Jan 5, 2026, 22:13 UTC · re: Harald Nordgren · lore

Re: Another look?

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 9 quoted lines
>> Please don't.  Unless my assumption, which is that in the old code
>> "!sti" and "!ours && !theirs" is equivalent, is wrong, all you need
>> to do around that part is to first check "if (!ours && !theirs)" and
>> say "your branch is up to date with...", and then have the check
>> "else if (abf == ABQ)" next.  That way, when we check abf we know
>> the branches are different.
>
> It seems to be an incorrect assumption. This code change breaks several
> tests including old ones:
Show 19 quoted lines
>
> ```
> diff --git a/remote.c b/remote.c
> index 1f87b85b22..8db4fcd7b5 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -2303,7 +2303,7 @@ static void format_branch_comparison(struct strbuf *sb,
>  				     enum branch_mode_flags advice_flags,
>  				     int show_divergence_advice)
>  {
> -	if (!sti) {
> +	if (!ours && !theirs) {
>  		strbuf_addf(sb,
>  			_("Your branch is up to date with '%s'.\n"),
>  			branch_name);
> ```
>
>
> Harald
That is unexpected.

Looking at what stat_branch_pair() does, before returning 0, the function always clears *num_theirs and *num_ours, so there is something else going on.

If your caller is *not* initializing ours and theirs, and if it is not detecting an error return from stat_tracking_info, and the test code is trying to see when stat_branch_info() signals failure by returning -1, then I can understand why the above change makes a difference (i.e., your code above with or without sti -> ours/theirs change is broken), but then, that should be handled at the caller of stat_tracking_info() by noticing its return value being negative, I would have to say.

Harald Nordgren· Jan 6, 2026, 00:08 UTC · re: Junio C Hamano · lore

ABQ

Note that this change also makes tests fail on the 'master' branch, so not an issue introdued by my code:

```
Show changes to remote.c +1 −1
diff --git a/remote.c b/remote.c
index 59b3715120..f84f2747b2 100644
--- a/remote.c
+++ b/remote.c
@@ -2265,7 +2265,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+	} else if (!ours && !theirs) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
 			base);
```

If you have the time, it might be interesting to run
'./t6040-tracking-info.sh' and to see what happens for the different cases.

It's failing for the ABQ cases, maybe because of this:

```
	if (abf == AHEAD_BEHIND_QUICK)
		return 1;
```

I would argue this is getting outside of the scope of push branch
comparison, so maybe better to do this as a follow-up? What do you say?


Harald
Phillip Wood· Jan 9, 2026, 14:56 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Hi Harald
On 05/01/2026 10:17, Harald Nordgren via GitGitGadget wrote:
> From: Harald Nordgren <haraldnordgren@gmail.com>
> 
> +static void format_branch_comparison(struct strbuf *sb,
> +				     int sti,

I wondered why we needed to pass sti as well as ours and theirs but it is because when we're using AHEAD_BEHIND_QUICK our and theirs are always zero and so we need to check sti to see if the branch is up to date. Perhaps we could make this a boolean called 'up_to_date' ?

> +				     int ours, int theirs,
> +				     const char *branch_name,
> +				     enum ahead_behind_flags abf,
> +				     int show_divergence_advice)
This could be 'bool' not 'int'

Everything else looks fine - it is a faithful conversion from the original and it makes sense to check if the upstream is gone in the caller.

Thanks
Phillip
Show 81 quoted lines
> +{
> +	if (!sti) {
>   		strbuf_addf(sb,
>   			_("Your branch is up to date with '%s'.\n"),
> -			base);
> +			branch_name);
>   	} else if (abf == AHEAD_BEHIND_QUICK) {
>   		strbuf_addf(sb,
>   			    _("Your branch and '%s' refer to different commits.\n"),
> -			    base);
> +			    branch_name);
>   		if (advice_enabled(ADVICE_STATUS_HINTS))
>   			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>   				    "git status --ahead-behind");
> @@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   			Q_("Your branch is ahead of '%s' by %d commit.\n",
>   			   "Your branch is ahead of '%s' by %d commits.\n",
>   			   ours),
> -			base, ours);
> +			branch_name, ours);
>   		if (advice_enabled(ADVICE_STATUS_HINTS))
>   			strbuf_addstr(sb,
>   				_("  (use \"git push\" to publish your local commits)\n"));
> @@ -2292,7 +2271,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   			   "Your branch is behind '%s' by %d commits, "
>   			       "and can be fast-forwarded.\n",
>   			   theirs),
> -			base, theirs);
> +			branch_name, theirs);
>   		if (advice_enabled(ADVICE_STATUS_HINTS))
>   			strbuf_addstr(sb,
>   				_("  (use \"git pull\" to update your local branch)\n"));
> @@ -2305,12 +2284,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   			       "and have %d and %d different commits each, "
>   			       "respectively.\n",
>   			   ours + theirs),
> -			base, ours, theirs);
> +			branch_name, ours, theirs);
>   		if (show_divergence_advice &&
>   		    advice_enabled(ADVICE_STATUS_HINTS))
>   			strbuf_addstr(sb,
>   				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
>   	}
> +}
> +
> +/*
> + * Return true when there is anything to report, otherwise false.
> + */
> +int format_tracking_info(struct branch *branch, struct strbuf *sb,
> +			 enum ahead_behind_flags abf,
> +			 int show_divergence_advice)
> +{
> +	int ours, theirs, sti;
> +	const char *full_base;
> +	char *base;
> +	int upstream_is_gone = 0;
> +
> +	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
> +	if (sti < 0) {
> +		if (!full_base)
> +			return 0;
> +		upstream_is_gone = 1;
> +	}
> +
> +	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
> +					    full_base, 0);
> +
> +	if (upstream_is_gone) {
> +		strbuf_addf(sb,
> +			_("Your branch is based on '%s', but the upstream is gone.\n"),
> +			base);
> +		if (advice_enabled(ADVICE_STATUS_HINTS))
> +			strbuf_addstr(sb,
> +				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
> +	} else {
> +		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
> +	}
> +
>   	free(base);
>   	return 1;
>   }
Phillip Wood· Jan 9, 2026, 14:56 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v17 2/2] status: show comparison with push remote tracking branch

Hi Harald
On 05/01/2026 10:17, Harald Nordgren via GitGitGadget wrote:
Show 26 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com>
> 
> "git status" on a branch that follows a remote branch compares
> commits on the current branch and the remote-tracking branch it
> builds upon, to show "ahead", "behind", or "diverged" status.
> 
> When working on a feature branch that tracks a remote feature branch,
> but you also want to track progress relative to the push destination
> tracking branch (which may differ from the upstream branch), git status
> now shows an additional comparison.
> 
> When the upstream tracking branch differs from the push destination
> tracking branch, git status shows both the comparison with the upstream
> tracking branch (as before) and an additional comparison with the push
> destination tracking branch. The push branch comparison appears on a
> separate line after the upstream branch status, using the same format.
> 
> Example output when tracking origin/main but push destination is
> origin/feature:
>      On branch feature
>      Your branch and 'origin/main' have diverged,
>      and have 3 and 1 different commits each, respectively.
>        (use "git pull" if you want to integrate the remote branch with yours)
> 
>      Your branch is ahead of 'origin/feature' by 1 commit.
>        (use "git push" to publish your local commits)
The advice looks good
> The comparison is only shown when the push destination tracking branch
> differs from the upstream tracking branch, even if they are on the same
> remote.
Sounds sensible
Show 12 quoted lines
> diff --git a/remote.c b/remote.c
> index 7163a8ec28..1f87b85b22 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -29,6 +29,11 @@
>   
>   enum map_direction { FROM_SRC, FROM_DST };
>   
> +enum branch_mode_flags {
> +	BRANCH_MODE_PULL = (1 << 0),
> +	BRANCH_MODE_PUSH = (1 << 1),
> +};
Using an enum for a set of flags is a bit confusing.
Show 29 quoted lines
> +static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
> +{
> +	struct remote *remote;
> +	const char *push_remote;
> +	char *push_dst = NULL;
> +	char *tracking_ref;
> +	const char *resolved;
> +	char *ret;
> +
> +	if (!branch)
> +		return NULL;
> +
> +	push_remote = pushremote_for_branch(branch, NULL);
> +	if (!push_remote)
> +		return NULL;
> +
> +	remote = remotes_remote_get(the_repository, push_remote);
> +	if (!remote)
> +		return NULL;
> +
> +	push_dst = remote_ref_for_branch(branch, 1);
> +	if (!push_dst) {
> +		if (remote->push.nr)
> +			return NULL;
> +		push_dst = xstrdup(branch->refname);
> +	}
> +
> +	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
> +	free(push_dst);

On reflection I wonder if we should be calling branch_get_push() instead of remote_ref_for_branch() and tracking_for_push_dest() as it respects 'push.default' and so the branch it returns is the one that "git push" without any arguments would push to.

Show 15 quoted lines
> +	if (!tracking_ref)
> +		return NULL;
> +
> +	resolved = refs_resolve_ref_unsafe(
> +		get_main_ref_store(the_repository),
> +		tracking_ref,
> +		RESOLVE_REF_READING,
> +		NULL, NULL);
> +
> +	if (!resolved) {
> +		free(tracking_ref);
> +		return NULL;
> +	}
> +
> +	if (full_ref_out)

I think it would be simpler to just return the full refname and let the caller shorten it.

Show 23 quoted lines
> +		*full_ref_out = xstrdup(resolved);
> +
> +	ret = refs_shorten_unambiguous_ref(
> +		get_main_ref_store(the_repository), resolved, 0);
> +	free(tracking_ref);
> +	return ret;
> +}
> +
>   static void format_branch_comparison(struct strbuf *sb,
>   				     int sti,
>   				     int ours, int theirs,
>   				     const char *branch_name,
>   				     enum ahead_behind_flags abf,
> +				     enum branch_mode_flags advice_flags,
>   				     int show_divergence_advice)
>   {
>   	if (!sti) {
> @@ -2252,7 +2311,8 @@ static void format_branch_comparison(struct strbuf *sb,
>   		strbuf_addf(sb,
>   			    _("Your branch and '%s' refer to different commits.\n"),
>   			    branch_name);
> -		if (advice_enabled(ADVICE_STATUS_HINTS))
> +		if ((advice_flags & BRANCH_MODE_PUSH) &&

Why are we checking for BRANCH_MODE_PUSH here? Don't we want to show this advice regardless of the mode?

 > +		    advice_enabled(ADVICE_STATUS_HINTS))
Show 10 quoted lines
>   			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>   				    "git status --ahead-behind");
>   	} else if (!theirs) {
> @@ -2261,7 +2321,8 @@ static void format_branch_comparison(struct strbuf *sb,
>   			   "Your branch is ahead of '%s' by %d commits.\n",
>   			   ours),
>   			branch_name, ours);
> -		if (advice_enabled(ADVICE_STATUS_HINTS))
> +		if ((advice_flags & BRANCH_MODE_PUSH) &&
> +		    advice_enabled(ADVICE_STATUS_HINTS))

Having to test the flags each time is a bit cumbersome. We could define a couple of local variables to simplify this

	bool want_push_advice = (advice_flags & BRANCH_MODE_PUSH) &&
				advice_enabled(ADVICE_STATUS_HINTS);
	bool want_pull_advice = advice_flags & BRANCH_MODE_PULL &&
				advice_enabled(ADVICE_STATUS_HINTS);
Then we can simplify the above to
	if (want_push_advice)
>   			strbuf_addstr(sb,
>   				_("  (use \"git push\" to publish your local commits)\n"));
>   	} else if (!ours) {
 > [...]
Show 7 quoted lines
> @@ -2285,7 +2347,8 @@ static void format_branch_comparison(struct strbuf *sb,
>   			       "respectively.\n",
>   			   ours + theirs),
>   			branch_name, ours, theirs);
> -		if (show_divergence_advice &&
> +		if ((advice_flags & BRANCH_MODE_PULL) &&
> +		    show_divergence_advice &&

If we don't want to show this can't we set show_divergance_adivce to false when we call this function - why is it guarded by BRANCH_MODE_PULL as well?

Show 8 quoted lines
>   		    advice_enabled(ADVICE_STATUS_HINTS))
>   			strbuf_addstr(sb,
>   				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
> @@ -2303,6 +2366,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   	const char *full_base;
>   	char *base;
>   	int upstream_is_gone = 0;
> +	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
Here we set an enum to a value that is not a member of the enum.
Show 13 quoted lines
> +	int push_ours, push_theirs, push_sti;
> +	char *full_push = NULL;
> +	char *push = NULL;
> +	enum branch_mode_flags push_branch_modes = 0;
>   
>   	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
>   	if (sti < 0) {
> @@ -2314,6 +2382,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
>   					    full_base, 0);
>   
> +	push = get_remote_push_branch(branch, &full_push);
> +	if (push && strcmp(base, push)) {

This is good - we only show the push branch separately if it differs from the upstream branch.

Show 6 quoted lines
> +		push_sti = stat_branch_pair(branch->refname, full_push,
> +					   &push_ours, &push_theirs, abf);
> +		if (push_sti >= 0) {
> +			base_branch_modes = BRANCH_MODE_PULL;
> +			push_branch_modes = BRANCH_MODE_PUSH;
> +		}

This combined with checking "push_branch_modes & BRANCH_MODE_PUSH" below ensures we skip the push branch if push_sti < 0. That's good but it is a bit hard to follow.

Thanks
Phillip
Show 297 quoted lines
> +	}
> +
>   	if (upstream_is_gone) {
>   		strbuf_addf(sb,
>   			_("Your branch is based on '%s', but the upstream is gone.\n"),
> @@ -2322,10 +2400,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>   			strbuf_addstr(sb,
>   				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
>   	} else {
> -		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
> +		format_branch_comparison(sb, sti, ours, theirs, base, abf,
> +					 base_branch_modes, show_divergence_advice);
> +	}
> +
> +	if (push_branch_modes & BRANCH_MODE_PUSH) {
> +		strbuf_addstr(sb, "\n");
> +		format_branch_comparison(sb, push_sti, push_ours, push_theirs, push, abf,
> +					 push_branch_modes, 0);
>   	}
>   
>   	free(base);
> +	free(full_push);
> +	free(push);
>   	return 1;
>   }
>   
> diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
> index 0b719bbae6..cf5a926dcd 100755
> --- a/t/t6040-tracking-info.sh
> +++ b/t/t6040-tracking-info.sh
> @@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
>   	test_cmp expect actual
>   '
>   
> +test_expect_success 'status tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status --no-ahead-behind >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
> +	  (use "git status --ahead-behind" for details)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status shows ahead of both origin/main and feature branch' '
> +	(
> +		cd test &&
> +		git checkout -b feature2 origin/main &&
> +		git push origin HEAD &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature2
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
> +	(
> +		cd test &&
> +		git checkout feature2 >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'setup for ahead of tracked but diverged from main' '
> +	(
> +		cd test &&
> +		git checkout -b feature4 origin/main &&
> +		advance work1 &&
> +		git checkout origin/main &&
> +		advance work2 &&
> +		git push origin HEAD:main &&
> +		git checkout feature4 &&
> +		advance work3
> +	)
> +'
> +
> +test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
> +	(
> +		cd test &&
> +		git checkout feature4 &&
> +		git branch --set-upstream-to origin/main &&
> +		git push origin HEAD &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature4
> +	Your branch and ${SQ}origin/main${SQ} have diverged,
> +	and have 3 and 1 different commits each, respectively.
> +	  (use "git pull" if you want to integrate the remote branch with yours)
> +
> +	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
> +	(
> +		cd test &&
> +		git checkout feature4 &&
> +		git status --no-ahead-behind >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature4
> +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
> +
> +	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
> +	  (use "git status --ahead-behind" for details)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'setup upstream remote' '
> +	(
> +		cd test &&
> +		git remote add upstream ../. &&
> +		git fetch upstream &&
> +		git config remote.pushDefault origin
> +	)
> +'
> +
> +test_expect_success 'status with upstream remote and push.default set to origin' '
> +	(
> +		cd test &&
> +		git checkout -b feature5 upstream/main &&
> +		git push origin &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature5
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
> +	(
> +		cd test &&
> +		git checkout -b feature6 upstream/main &&
> +		advance work &&
> +		git push origin &&
> +		git reset --hard upstream/main &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature6
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	Your branch and ${SQ}origin/feature6${SQ} have diverged,
> +	and have 1 and 1 different commits each, respectively.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status with upstream remote and push branch up to date' '
> +	(
> +		cd test &&
> +		git checkout -b feature7 upstream/main &&
> +		git push origin &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature7
> +	Your branch is up to date with ${SQ}upstream/main${SQ}.
> +
> +	Your branch is up to date with ${SQ}origin/feature7${SQ}.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
> +	(
> +		cd test &&
> +		git checkout feature7 &&
> +		git push origin &&
> +		git status --no-ahead-behind >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature7
> +	Your branch is up to date with ${SQ}upstream/main${SQ}.
> +
> +	Your branch is up to date with ${SQ}origin/feature7${SQ}.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'checkout shows push branch up to date' '
> +	(
> +		cd test &&
> +		git checkout feature7 >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	Your branch is up to date with ${SQ}upstream/main${SQ}.
> +
> +	Your branch is up to date with ${SQ}origin/feature7${SQ}.
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status shows remapped push refspec' '
> +	(
> +		cd test &&
> +		git checkout -b feature8 origin/main &&
> +		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
> +		git push &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature8
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status shows remapped push refspec with upstream remote' '
> +	(
> +		cd test &&
> +		git checkout -b feature9 upstream/main &&
> +		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
> +		git push origin &&
> +		advance work &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch feature9
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
>   test_done
Harald Nordgren· Jan 9, 2026, 15:23 UTC · re: Phillip Wood · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

> I wondered why we needed to pass sti as well as ours and theirs but it 
> is because when we're using AHEAD_BEHIND_QUICK our and theirs are always 
> zero and so we need to check sti to see if the branch is up to date. 
> Perhaps we could make this a boolean called 'up_to_date' ?

Yes, this is the reason. And I tried a lot of things to get around it. But yes, let's pass this boolean instead.

> This could be 'bool' not 'int'
> 
> Everything else looks fine - it is a faithful conversion from the 
> original and it makes sense to check if the upstream is gone in the caller.
Done, will be in the next patch.

I didn't update the signature of 'format_tracking_info' to have a bool there too, because it makes the surface area bigger of also updating the .h file there.

Harald
Harald Nordgren· Jan 9, 2026, 16:00 UTC · re: Phillip Wood · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

> Using an enum for a set of flags is a bit confusing.

The point of the flag and the bitmasking is to selectively turn off the push and pull advice advice from the relevant branch when the push branch comparison is active.

In an earlier implementation the advice logic was moved to the caller instead 'format_branch_comparison', but it's more faithful to the original to have the advice logic inside 'format_branch_comparison'. Maybe I misunderstood your comments around this?

Would happily take a suggestion on a nicer way to handle it.
> On reflection I wonder if we should be calling branch_get_push() instead 
> of remote_ref_for_branch() and tracking_for_push_dest() as it respects 
> 'push.default' and so the branch it returns is the one that "git push" 
> without any arguments would push to.
I'm not getting that to work without tests breaking.
> I think it would be simpler to just return the full refname and let the 
> caller shorten it.

We could to that but what's the benefit? Shouldn't a helper function reduce the work for the caller, not the other way around? 🤗

> Why are we checking for BRANCH_MODE_PUSH here? Don't we want to show 
> this advice regardless of the mode?

Yeah, that's a good point. However, this will often lead to the advice being shown twice, see this test:

``` status --no-ahead-behind shows diverged from origin/main and ahead of feature branch ```

Show 11 quoted lines
> Having to test the flags each time is a bit cumbersome. We could define
> a couple of local variables to simplify this
> 
> 	bool want_push_advice = (advice_flags & BRANCH_MODE_PUSH) &&
> 				advice_enabled(ADVICE_STATUS_HINTS);
> 	bool want_pull_advice = advice_flags & BRANCH_MODE_PULL &&
> 				advice_enabled(ADVICE_STATUS_HINTS);
> 
> Then we can simplify the above to
> 
> 	if (want_push_advice)
Done, will be in the next batch!
> If we don't want to show this can't we set show_divergance_adivce to 
> false when we call this function - why is it guarded by BRANCH_MODE_PULL 
> as well?

This will already have been shown the the pull branch (the upstream branch).

And in the cases when it's NOT shown for the pull branch but only the push branch has diverged, then pulling won't solve the problem anyway. (Unless you would try to pull from your push branch.)

> Here we set an enum to a value that is not a member of the enum.

I use bitmasking to enable both modes by default. But see my comments above maybe there is a nicer way to handle all of this.

> This combined with checking "push_branch_modes & BRANCH_MODE_PUSH" below 
> ensures we skip the push branch if push_sti < 0. That's good but it is a 
> bit hard to follow.

Possibly this can be done in a nicer way too. But at least I try to encapsulate the complexity to only here, to allow the rest of the code to be more straightforward. A good trade-off I think.

Harald
Ben Knoble· Jan 9, 2026, 16:22 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Show 16 quoted lines
> Le 9 janv. 2026 à 11:07, Harald Nordgren <haraldnordgren@gmail.com> a écrit :
> 
> 
>> 
>> Using an enum for a set of flags is a bit confusing.
> 
> The point of the flag and the bitmasking is to selectively turn off the push
> and pull advice advice from the relevant branch when the push branch
> comparison is active.
> 
> In an earlier implementation the advice logic was moved to the caller
> instead 'format_branch_comparison', but it's more faithful to the original
> to have the advice logic inside 'format_branch_comparison'. Maybe I
> misunderstood your comments around this?
> 
> Would happily take a suggestion on a nicer way to handle it.
I think other uses in Git declare a bunch of integer constants for the bitfields, not an enum. Why? Because or-ing flags together creates a value not in the enum…
>> Here we set an enum to a value that is not a member of the enum.
> 
> I use bitmasking to enable both modes by default. But see my comments above
> maybe there is a nicer way to handle all of this.
…which explains this comment.
IOW: set of flags makes sense, but there’s a more usual way to set that up?
Patrick Steinhardt· Jan 9, 2026, 16:32 UTC · re: Ben Knoble · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

On Fri, Jan 09, 2026 at 11:22:33AM -0500, Ben Knoble wrote:
Show 21 quoted lines
> 
> > Le 9 janv. 2026 à 11:07, Harald Nordgren <haraldnordgren@gmail.com> a écrit :
> > 
> > 
> >> 
> >> Using an enum for a set of flags is a bit confusing.
> > 
> > The point of the flag and the bitmasking is to selectively turn off the push
> > and pull advice advice from the relevant branch when the push branch
> > comparison is active.
> > 
> > In an earlier implementation the advice logic was moved to the caller
> > instead 'format_branch_comparison', but it's more faithful to the original
> > to have the advice logic inside 'format_branch_comparison'. Maybe I
> > misunderstood your comments around this?
> > 
> > Would happily take a suggestion on a nicer way to handle it.
> 
> I think other uses in Git declare a bunch of integer constants for the
> bitfields, not an enum. Why? Because or-ing flags together creates a
> value not in the enum…

We nowadays typically declare the flags as enum, but when accepting the bitfield we use `unsigned`.

Patrick
Harald Nordgren via GitGitGadget· Jan 9, 2026, 16:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v18 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 174 ++++++++++++++++++++------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 401 insertions(+), 35 deletions(-)
base-commit: d529f3a197364881746f558e5652f0236131eb86
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v18
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v18
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v17:
 1:  b62a9feb4d ! 1:  451d7a4986 refactor format_branch_comparison in preparation
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      -	} else if (!sti) {
      +static void format_branch_comparison(struct strbuf *sb,
     -+				     int sti,
     ++				     bool up_to_date,
      +				     int ours, int theirs,
      +				     const char *branch_name,
      +				     enum ahead_behind_flags abf,
     -+				     int show_divergence_advice)
     ++				     bool show_divergence_advice)
      +{
     -+	if (!sti) {
     ++	if (up_to_date) {
       		strbuf_addf(sb,
       			_("Your branch is up to date with '%s'.\n"),
      -			base);
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +			strbuf_addstr(sb,
      +				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      +	} else {
     -+		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
      +	}
      +
       	free(base);
 2:  1348542edc ! 2:  02476eb8e6 status: show comparison with push remote tracking branch
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +}
      +
       static void format_branch_comparison(struct strbuf *sb,
     - 				     int sti,
     + 				     bool up_to_date,
       				     int ours, int theirs,
       				     const char *branch_name,
       				     enum ahead_behind_flags abf,
      +				     enum branch_mode_flags advice_flags,
     - 				     int show_divergence_advice)
     + 				     bool show_divergence_advice)
       {
     - 	if (!sti) {
     ++	bool want_push_advice = (advice_flags & BRANCH_MODE_PUSH) &&
     ++		advice_enabled(ADVICE_STATUS_HINTS);
     ++	bool want_pull_advice = (advice_flags & BRANCH_MODE_PULL) &&
     ++		advice_enabled(ADVICE_STATUS_HINTS);
     ++	bool want_divergence_advice = (advice_flags & BRANCH_MODE_PULL) &&
     ++		show_divergence_advice &&
     ++		advice_enabled(ADVICE_STATUS_HINTS);
     ++
     + 	if (up_to_date) {
     + 		strbuf_addf(sb,
     + 			_("Your branch is up to date with '%s'.\n"),
      @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       		strbuf_addf(sb,
       			    _("Your branch and '%s' refer to different commits.\n"),
       			    branch_name);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if ((advice_flags & BRANCH_MODE_PUSH) &&
     -+		    advice_enabled(ADVICE_STATUS_HINTS))
     ++		if (want_push_advice)
       			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
       				    "git status --ahead-behind");
       	} else if (!theirs) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours),
       			branch_name, ours);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if ((advice_flags & BRANCH_MODE_PUSH) &&
     -+		    advice_enabled(ADVICE_STATUS_HINTS))
     ++		if (want_push_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git push\" to publish your local commits)\n"));
       	} else if (!ours) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   theirs),
       			branch_name, theirs);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if ((advice_flags & BRANCH_MODE_PULL) &&
     -+		    advice_enabled(ADVICE_STATUS_HINTS))
     ++		if (want_pull_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours + theirs),
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
     -+		if ((advice_flags & BRANCH_MODE_PULL) &&
     -+		    show_divergence_advice &&
     - 		    advice_enabled(ADVICE_STATUS_HINTS))
     +-		    advice_enabled(ADVICE_STATUS_HINTS))
     ++		if (want_divergence_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
     + 	}
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       			strbuf_addstr(sb,
       				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
       	} else {
     --		format_branch_comparison(sb, sti, ours, theirs, base, abf, show_divergence_advice);
     -+		format_branch_comparison(sb, sti, ours, theirs, base, abf,
     +-		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, !sti, ours, theirs, base, abf,
      +					 base_branch_modes, show_divergence_advice);
      +	}
      +
      +	if (push_branch_modes & BRANCH_MODE_PUSH) {
      +		strbuf_addstr(sb, "\n");
     -+		format_branch_comparison(sb, push_sti, push_ours, push_theirs, push, abf,
     -+					 push_branch_modes, 0);
     ++		format_branch_comparison(sb, !push_sti, push_ours, push_theirs, push, abf,
     ++					 push_branch_modes, show_divergence_advice);
       	}
       
       	free(base);
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 9, 2026, 16:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v18 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index 59b3715120..eba013b6b4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2271,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2284,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 9, 2026, 16:41 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v18 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 102 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 358 insertions(+), 6 deletions(-)
Show changes to 2 files +358 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index eba013b6b4..4c3f18c3c1 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum branch_mode_flags {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,13 +2242,75 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     enum branch_mode_flags advice_flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (advice_flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (advice_flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = (advice_flags & BRANCH_MODE_PULL) &&
+		show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2252,7 +2319,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2328,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2339,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2285,8 +2352,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2303,6 +2369,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	enum branch_mode_flags push_branch_modes = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2314,6 +2385,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2322,10 +2403,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_sti, push_ours, push_theirs, push, abf,
+					 push_branch_modes, show_divergence_advice);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren· Jan 9, 2026, 18:03 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Show 6 quoted lines
> > I think other uses in Git declare a bunch of integer constants for the
> > bitfields, not an enum. Why? Because or-ing flags together creates a
> > value not in the enum…
> 
> We nowadays typically declare the flags as enum, but when accepting the
> bitfield we use `unsigned`.

Good points! I have updated it to remove the name branch_mode_flags now, define the enums and pass them around as unsigned now. Will be in the next patch.

Harald
Harald Nordgren via GitGitGadget· Jan 9, 2026, 18:40 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v19 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 174 ++++++++++++++++++++------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 401 insertions(+), 35 deletions(-)
base-commit: d529f3a197364881746f558e5652f0236131eb86
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v19
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v19
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v18:
 1:  451d7a4986 = 1:  451d7a4986 refactor format_branch_comparison in preparation
 2:  02476eb8e6 ! 2:  dc8ab23158 status: show comparison with push remote tracking branch
     @@ remote.c
       
       enum map_direction { FROM_SRC, FROM_DST };
       
     -+enum branch_mode_flags {
     ++enum {
      +	BRANCH_MODE_PULL = (1 << 0),
      +	BRANCH_MODE_PUSH = (1 << 1),
      +};
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       				     int ours, int theirs,
       				     const char *branch_name,
       				     enum ahead_behind_flags abf,
     -+				     enum branch_mode_flags advice_flags,
     ++				     unsigned flags,
       				     bool show_divergence_advice)
       {
     -+	bool want_push_advice = (advice_flags & BRANCH_MODE_PUSH) &&
     ++	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool want_pull_advice = (advice_flags & BRANCH_MODE_PULL) &&
     ++	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool want_divergence_advice = (advice_flags & BRANCH_MODE_PULL) &&
     ++	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
      +		show_divergence_advice &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
      +
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
       	int upstream_is_gone = 0;
     -+	enum branch_mode_flags base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
     ++	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
      +	int push_ours, push_theirs, push_sti;
      +	char *full_push = NULL;
      +	char *push = NULL;
     -+	enum branch_mode_flags push_branch_modes = 0;
     ++	unsigned push_branch_modes = 0;
       
       	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
       	if (sti < 0) {
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 9, 2026, 18:40 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v19 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index 59b3715120..eba013b6b4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2271,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2284,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 9, 2026, 18:40 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v19 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 102 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 358 insertions(+), 6 deletions(-)
Show changes to 2 files +358 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index eba013b6b4..b8d2d1360a 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,13 +2242,75 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     unsigned flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
+		show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2252,7 +2319,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2328,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2339,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2285,8 +2352,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2303,6 +2369,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	unsigned push_branch_modes = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2314,6 +2385,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2322,10 +2403,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_sti, push_ours, push_theirs, push, abf,
+					 push_branch_modes, show_divergence_advice);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 10, 2026, 02:02 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v19 1/2] refactor format_branch_comparison in preparation

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 20 quoted lines
> +static void format_branch_comparison(struct strbuf *sb,
> +				     bool up_to_date,
> +				     int ours, int theirs,
> +				     const char *branch_name,
> +				     enum ahead_behind_flags abf,
> +				     bool show_divergence_advice)
> +{
> +	if (up_to_date) {
>  		strbuf_addf(sb,
>  			_("Your branch is up to date with '%s'.\n"),
> -			base);
> +			branch_name);
>  	} else if (abf == AHEAD_BEHIND_QUICK) {
>  		strbuf_addf(sb,
>  			    _("Your branch and '%s' refer to different commits.\n"),
> -			    base);
> +			    branch_name);
>  		if (advice_enabled(ADVICE_STATUS_HINTS))
>  			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>  				    "git status --ahead-behind");

OK. We _could_, just like we moved the "upstream_gone" case to the caller, move the "up to date" case to the caller as well, but this reads well now. And we do not need the (!ours && !theirs) case in this if/else if/... cascade. Very good.

Show 36 quoted lines
> +/*
> + * Return true when there is anything to report, otherwise false.
> + */
> +int format_tracking_info(struct branch *branch, struct strbuf *sb,
> +			 enum ahead_behind_flags abf,
> +			 int show_divergence_advice)
> +{
> +	int ours, theirs, sti;
> +	const char *full_base;
> +	char *base;
> +	int upstream_is_gone = 0;
> +
> +	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
> +	if (sti < 0) {
> +		if (!full_base)
> +			return 0;
> +		upstream_is_gone = 1;
> +	}
> +
> +	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
> +					    full_base, 0);
> +
> +	if (upstream_is_gone) {
> +		strbuf_addf(sb,
> +			_("Your branch is based on '%s', but the upstream is gone.\n"),
> +			base);
> +		if (advice_enabled(ADVICE_STATUS_HINTS))
> +			strbuf_addstr(sb,
> +				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
> +	} else {
> +		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
> +	}
> +
>  	free(base);
>  	return 1;
>  }
Junio C Hamano· Jan 10, 2026, 02:13 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v19 2/2] status: show comparison with push remote tracking branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 10 quoted lines
> @@ -2285,8 +2352,7 @@ static void format_branch_comparison(struct strbuf *sb,
>  			       "respectively.\n",
>  			   ours + theirs),
>  			branch_name, ours, theirs);
> -		if (show_divergence_advice &&
> -		    advice_enabled(ADVICE_STATUS_HINTS))
> +		if (want_divergence_advice)
>  			strbuf_addstr(sb,
>  				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
>  	}

This is not a new issue introduced by this series, but it is curious there is "ours + theirs" there #leftoverbits.

It is part of ngetext() aka Q_() call, used this way:
	} else {
		strbuf_addf(sb,
			Q_("Your branch and '%s' have diverged,\n"
			       "and have %d and %d different commit each, "
			       "respectively.\n",
			   "Your branch and '%s' have diverged,\n"
			       "and have %d and %d different commits each, "
			       "respectively.\n",
			   ours + theirs),
			branch_name, ours, theirs);

But in this if/else if/... cascade, we have ruled out cases where either/both of ours and theirs is 0 already, so ours + theirs has to be at least two (because each has to be at least one). Q_() based on a value that is always plural would always use the latter form (i.e., "commits").

Junio C Hamano· Jan 10, 2026, 02:21 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v19 2/2] status: show comparison with push remote tracking branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 20 quoted lines
> @@ -2303,6 +2369,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>  	const char *full_base;
>  	char *base;
>  	int upstream_is_gone = 0;
> +	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
> +	int push_ours, push_theirs, push_sti;
> +	char *full_push = NULL;
> +	char *push = NULL;
> +	unsigned push_branch_modes = 0;
>  
>  	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
>  	if (sti < 0) {
> @@ -2314,6 +2385,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
>  	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
>  					    full_base, 0);
>  
> +	push = get_remote_push_branch(branch, &full_push);
> +	if (push && strcmp(base, push)) {
> +		push_sti = stat_branch_pair(branch->refname, full_push,
> +					   &push_ours, &push_theirs, abf);

Why is this variable called "push_sti"? Calling the return value of stat_tracking_info() "sti" was klumsy but understandable. It would have been much easier to follow the code if the variable were named after what it _means_ in this particular caller's code flow, like "cmp_fetch" (comparison on the fetching side, by convention negative signals an error, and zero signals 'the same'). Perhaps rename "sti" and "push_sti" at the same time to make them more symmetric?

Other than this minor nit, this step looks nicely done (and the previous one is also good).

Thanks.
Harald Nordgren· Jan 10, 2026, 11:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

It's following my naming scheme where each push version of a base variable tacks on _push at the end. Since the push logic introduces a second version of most variables, it's hard to make it fully logical when the base versions still retain their "unqualified" names.

I can rename the sti's to 'cmp_fetch' and 'cmp_fetch_push', but does it help?

Probably best would be to create a struct for the common variables between base and push cases. But that's a large refactoring, so maybe better done as a separate follow-up to all of this instead? Here's what is looks like:

```
Show changes to remote.c +30 −25
diff --git a/remote.c b/remote.c
index eba013b6b4..581a62c266 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,41 +2237,47 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+struct format_branch {
+	int ours, theirs;
+	int cmp_fetch;
+	bool up_to_date;
+	const char *full;
+	char *name;
+};
+
 static void format_branch_comparison(struct strbuf *sb,
-				     bool up_to_date,
-				     int ours, int theirs,
-				     const char *branch_name,
+				     const struct format_branch *branch,
 				     enum ahead_behind_flags abf,
 				     bool show_divergence_advice)
 {
-	if (up_to_date) {
+	if (branch->up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			branch_name);
+			branch->name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    branch_name);
+			    branch->name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
-	} else if (!theirs) {
+	} else if (!branch->theirs) {
 		strbuf_addf(sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
-			   ours),
-			branch_name, ours);
+			   branch->ours),
+			branch->name, branch->ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
-	} else if (!ours) {
+	} else if (!branch->ours) {
 		strbuf_addf(sb,
 			Q_("Your branch is behind '%s' by %d commit, "
 			       "and can be fast-forwarded.\n",
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
-			   theirs),
-			branch_name, theirs);
+			   branch->theirs),
+			branch->name, branch->theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2283,8 +2289,8 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
-			   ours + theirs),
-			branch_name, ours, theirs);
+			   branch->ours + branch->theirs),
+			branch->name, branch->ours, branch->theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
@@ -2299,33 +2305,32 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
 	int upstream_is_gone = 0;
+	struct format_branch base;
 
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
+	base.cmp_fetch = stat_tracking_info(branch, &base.ours, &base.theirs, &base.full, 0, abf);
+	base.up_to_date = !base.cmp_fetch;
+	if (base.cmp_fetch < 0) {
+		if (!base.full)
 			return 0;
 		upstream_is_gone = 1;
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	base.name = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    base.full, 0);
 
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
+			base.name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, &base, abf, show_divergence_advice);
 	}
 
-	free(base);
+	free(base.name);
 	return 1;
 }
```


Harald
Harald Nordgren· Jan 10, 2026, 11:14 UTC · re: Junio C Hamano · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

> This is not a new issue introduced by this series, but it is curious
> there is "ours + theirs" there #leftoverbits.
>
> It is part of ngetext() aka Q_() call, used this way:
Agreed, I thought about this one too. Will update it!
Harald
Harald Nordgren via GitGitGadget· Jan 10, 2026, 13:30 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v20 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 183 ++++++++++++++++++++-------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 403 insertions(+), 42 deletions(-)
base-commit: d529f3a197364881746f558e5652f0236131eb86
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v20
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v20
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v19:
 1:  451d7a4986 ! 1:  bb3e00863b refactor format_branch_comparison in preparation
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       		if (advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 			       "and have %d and %d different commits each, "
     - 			       "respectively.\n",
     - 			   ours + theirs),
     + 	} else {
     + 		strbuf_addf(sb,
     +-			Q_("Your branch and '%s' have diverged,\n"
     +-			       "and have %d and %d different commit each, "
     +-			       "respectively.\n",
     +-			   "Your branch and '%s' have diverged,\n"
     +-			       "and have %d and %d different commits each, "
     +-			       "respectively.\n",
     +-			   ours + theirs),
      -			base, ours, theirs);
     ++			"Your branch and '%s' have diverged,\n"
     ++			       "and have %d and %d different commits each, respectively.\n",
      +			branch_name, ours, theirs);
       		if (show_divergence_advice &&
       		    advice_enabled(ADVICE_STATUS_HINTS))
 2:  dc8ab23158 ! 2:  050197eac3 status: show comparison with push remote tracking branch
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
      @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     - 			       "respectively.\n",
     - 			   ours + theirs),
     + 			"Your branch and '%s' have diverged,\n"
     + 			       "and have %d and %d different commits each, respectively.\n",
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
      -		    advice_enabled(ADVICE_STATUS_HINTS))
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 13:30 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v20 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 91 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 41 deletions(-)
Show changes to remote.c +50 −41
diff --git a/remote.c b/remote.c
index 59b3715120..b053d4e443 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,25 +2271,55 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
 		strbuf_addf(sb,
-			Q_("Your branch and '%s' have diverged,\n"
-			       "and have %d and %d different commit each, "
-			       "respectively.\n",
-			   "Your branch and '%s' have diverged,\n"
-			       "and have %d and %d different commits each, "
-			       "respectively.\n",
-			   ours + theirs),
-			base, ours, theirs);
+			"Your branch and '%s' have diverged,\n"
+			       "and have %d and %d different commits each, respectively.\n",
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, sti;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (sti < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 13:30 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v20 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 102 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 358 insertions(+), 6 deletions(-)
Show changes to 2 files +358 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index b053d4e443..dd8f94fa23 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,13 +2242,75 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     unsigned flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
+		show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2252,7 +2319,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2328,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2339,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2280,8 +2347,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			"Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, respectively.\n",
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2298,6 +2364,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_sti;
+	char *full_push = NULL;
+	char *push = NULL;
+	unsigned push_branch_modes = 0;
 
 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (sti < 0) {
@@ -2309,6 +2380,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_sti = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_sti >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2317,10 +2398,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !sti, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_sti, push_ours, push_theirs, push, abf,
+					 push_branch_modes, show_divergence_advice);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren· Jan 10, 2026, 14:44 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Show 7 quoted lines
> Why is this variable called "push_sti"?  Calling the return value of
> stat_tracking_info() "sti" was klumsy but understandable.  It would
> have been much easier to follow the code if the variable were named
> after what it _means_ in this particular caller's code flow, like
> "cmp_fetch" (comparison on the fetching side, by convention negative
> signals an error, and zero signals 'the same').  Perhaps rename "sti"
> and "push_sti" at the same time to make them more symmetric?

I renamed to 'push_cmp_fetch' and 'push_cmp_fetch' now. I think on my first reading I misunderstood your comment.

> Other than this minor nit, this step looks nicely done (and the
> previous one is also good).
Thanks a lot!
Harald
Harald Nordgren via GitGitGadget· Jan 10, 2026, 15:24 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v21 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 183 ++++++++++++++++++++-------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 403 insertions(+), 42 deletions(-)
base-commit: d529f3a197364881746f558e5652f0236131eb86
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v21
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v21
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v20:
 1:  bb3e00863b ! 1:  ce1f1eebb5 refactor format_branch_comparison in preparation
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +			 enum ahead_behind_flags abf,
      +			 int show_divergence_advice)
      +{
     -+	int ours, theirs, sti;
     ++	int ours, theirs, cmp_fetch;
      +	const char *full_base;
      +	char *base;
      +	int upstream_is_gone = 0;
      +
     -+	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     -+	if (sti < 0) {
     ++	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     ++	if (cmp_fetch < 0) {
      +		if (!full_base)
      +			return 0;
      +		upstream_is_gone = 1;
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +			strbuf_addstr(sb,
      +				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      +	} else {
     -+		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
      +	}
      +
       	free(base);
 2:  050197eac3 ! 2:  51d8486fe0 status: show comparison with push remote tracking branch
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	char *base;
       	int upstream_is_gone = 0;
      +	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
     -+	int push_ours, push_theirs, push_sti;
     ++	int push_ours, push_theirs, push_cmp_fetch;
      +	char *full_push = NULL;
      +	char *push = NULL;
      +	unsigned push_branch_modes = 0;
       
     - 	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     - 	if (sti < 0) {
     + 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     + 	if (cmp_fetch < 0) {
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
       					    full_base, 0);
       
      +	push = get_remote_push_branch(branch, &full_push);
      +	if (push && strcmp(base, push)) {
     -+		push_sti = stat_branch_pair(branch->refname, full_push,
     ++		push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
      +					   &push_ours, &push_theirs, abf);
     -+		if (push_sti >= 0) {
     ++		if (push_cmp_fetch >= 0) {
      +			base_branch_modes = BRANCH_MODE_PULL;
      +			push_branch_modes = BRANCH_MODE_PUSH;
      +		}
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       			strbuf_addstr(sb,
       				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
       	} else {
     --		format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice);
     -+		format_branch_comparison(sb, !sti, ours, theirs, base, abf,
     +-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
     ++		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
      +					 base_branch_modes, show_divergence_advice);
      +	}
      +
      +	if (push_branch_modes & BRANCH_MODE_PUSH) {
      +		strbuf_addstr(sb, "\n");
     -+		format_branch_comparison(sb, !push_sti, push_ours, push_theirs, push, abf,
     ++		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
      +					 push_branch_modes, show_divergence_advice);
       	}
       
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 15:24 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v21 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 91 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 41 deletions(-)
Show changes to remote.c +50 −41
diff --git a/remote.c b/remote.c
index 59b3715120..af078817a4 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,25 +2271,55 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
 		strbuf_addf(sb,
-			Q_("Your branch and '%s' have diverged,\n"
-			       "and have %d and %d different commit each, "
-			       "respectively.\n",
-			   "Your branch and '%s' have diverged,\n"
-			       "and have %d and %d different commits each, "
-			       "respectively.\n",
-			   ours + theirs),
-			base, ours, theirs);
+			"Your branch and '%s' have diverged,\n"
+			       "and have %d and %d different commits each, respectively.\n",
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 15:24 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v21 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 102 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 358 insertions(+), 6 deletions(-)
Show changes to 2 files +358 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index af078817a4..40e4ebb3b7 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,13 +2242,75 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     unsigned flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
+		show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2252,7 +2319,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2328,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2339,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2280,8 +2347,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			"Your branch and '%s' have diverged,\n"
 			       "and have %d and %d different commits each, respectively.\n",
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2298,6 +2364,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_cmp_fetch;
+	char *full_push = NULL;
+	char *push = NULL;
+	unsigned push_branch_modes = 0;
 
 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (cmp_fetch < 0) {
@@ -2309,6 +2380,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_cmp_fetch >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2317,10 +2398,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
+					 push_branch_modes, show_divergence_advice);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 10, 2026, 17:31 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Harald Nordgren <haraldnordgren@gmail.com> writes:
> I can rename the sti's to 'cmp_fetch' and 'cmp_fetch_push', but does it
> help?

Not at all. Unless the contrast were "something_fetch" vs "something_push", that is. And that something being cryptic "sti" (recall my comment on it, being the name of the function that returns the value, which is less understandable than using words that signals what the variable _means_), would not make much sense for the "push" direction, as "sti" is not even an abbreviation for the function that gives the value. "cmp" (or "compare" for that matter) still has the ambiguity "compare with what and what for?", but at least it would be better than "sti".

Junio C Hamano· Jan 10, 2026, 17:34 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 6 quoted lines
>> This is not a new issue introduced by this series, but it is curious
>> there is "ours + theirs" there #leftoverbits.
>>
>> It is part of ngetext() aka Q_() call, used this way:
>
> Agreed, I thought about this one too. Will update it!

Please do so as a separate patch, not mix it into a patch that does something else.

I do not think we mind even if it is left outside the current topic we are discussing, if it distracts the main theme of the topic.

Junio C Hamano· Jan 10, 2026, 17:41 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v20 0/2] status: show comparison with push remote tracking branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:

Here is a place for you to explain what helps people to decypher the differences since the previous iteration in Range-diff (which is not very easy to read).

Show 5 quoted lines
> cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
> ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net
> cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk"
> kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com
> cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im
I am not sure what good these lines are doing (to GGG).
Show 40 quoted lines
> Harald Nordgren (2):
>   refactor format_branch_comparison in preparation
>   status: show comparison with push remote tracking branch
>
>  remote.c                 | 183 ++++++++++++++++++++-------
>  t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 403 insertions(+), 42 deletions(-)
>
>
> base-commit: d529f3a197364881746f558e5652f0236131eb86
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v20
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v20
> Pull-Request: https://github.com/git/git/pull/2138
>
> Range-diff vs v19:
>
>  1:  451d7a4986 ! 1:  bb3e00863b refactor format_branch_comparison in preparation
>      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
>        		if (advice_enabled(ADVICE_STATUS_HINTS))
>        			strbuf_addstr(sb,
>        				_("  (use \"git pull\" to update your local branch)\n"));
>      -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
>      - 			       "and have %d and %d different commits each, "
>      - 			       "respectively.\n",
>      - 			   ours + theirs),
>      + 	} else {
>      + 		strbuf_addf(sb,
>      +-			Q_("Your branch and '%s' have diverged,\n"
>      +-			       "and have %d and %d different commit each, "
>      +-			       "respectively.\n",
>      +-			   "Your branch and '%s' have diverged,\n"
>      +-			       "and have %d and %d different commits each, "
>      +-			       "respectively.\n",
>      +-			   ours + theirs),
>       -			base, ours, theirs);
>      ++			"Your branch and '%s' have diverged,\n"
>      ++			       "and have %d and %d different commits each, respectively.\n",
>       +			branch_name, ours, theirs);
>        		if (show_divergence_advice &&
>        		    advice_enabled(ADVICE_STATUS_HINTS))

Could you not mix the ours+theirs thing into the same step? Either make it a standalone patch to clean up before or after your main 2 patches, or leave it totally outside the series and send it after this series settles.

Harald Nordgren· Jan 10, 2026, 19:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v20 0/2] status: show comparison with push remote tracking branch

> Here is a place for you to explain what helps people to decypher the
> differences since the previous iteration in Range-diff (which is not
> very easy to read).
?
> Could you not mix the ours+theirs thing into the same step?  Either
> make it a standalone patch to clean up before or after your main 2
> patches, or leave it totally outside the series and send it after
> this series settles.
Got it, I'll revert that change.
Harald
Harald Nordgren via GitGitGadget· Jan 10, 2026, 19:56 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v22 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 174 ++++++++++++++++++++------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 401 insertions(+), 35 deletions(-)
base-commit: d529f3a197364881746f558e5652f0236131eb86
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v22
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v22
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v21:
 1:  ce1f1eebb5 ! 1:  4aa4f1abc8 refactor format_branch_comparison in preparation
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       		if (advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
     - 	} else {
     - 		strbuf_addf(sb,
     --			Q_("Your branch and '%s' have diverged,\n"
     --			       "and have %d and %d different commit each, "
     --			       "respectively.\n",
     --			   "Your branch and '%s' have diverged,\n"
     --			       "and have %d and %d different commits each, "
     --			       "respectively.\n",
     --			   ours + theirs),
     +@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     + 			       "and have %d and %d different commits each, "
     + 			       "respectively.\n",
     + 			   ours + theirs),
      -			base, ours, theirs);
     -+			"Your branch and '%s' have diverged,\n"
     -+			       "and have %d and %d different commits each, respectively.\n",
      +			branch_name, ours, theirs);
       		if (show_divergence_advice &&
       		    advice_enabled(ADVICE_STATUS_HINTS))
 2:  51d8486fe0 ! 2:  b7e29887d9 status: show comparison with push remote tracking branch
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
      @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
     - 			"Your branch and '%s' have diverged,\n"
     - 			       "and have %d and %d different commits each, respectively.\n",
     + 			       "respectively.\n",
     + 			   ours + theirs),
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
      -		    advice_enabled(ADVICE_STATUS_HINTS))
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 19:56 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v22 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index 59b3715120..d5a6486026 100644
--- a/remote.c
+++ b/remote.c
@@ -2237,42 +2237,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2281,7 +2260,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2292,7 +2271,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2305,12 +2284,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 10, 2026, 19:56 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v22 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 102 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 358 insertions(+), 6 deletions(-)
Show changes to 2 files +358 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index d5a6486026..f9aa4c61bc 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2237,13 +2242,75 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     unsigned flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
+		show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2252,7 +2319,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2261,7 +2328,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2272,7 +2339,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2285,8 +2352,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2303,6 +2369,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_cmp_fetch;
+	char *full_push = NULL;
+	char *push = NULL;
+	unsigned push_branch_modes = 0;
 
 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (cmp_fetch < 0) {
@@ -2314,6 +2385,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_cmp_fetch >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2322,10 +2403,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
+					 push_branch_modes, show_divergence_advice);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren· Jan 10, 2026, 20:04 UTC · re: Junio C Hamano · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Show 9 quoted lines
> Not at all.  Unless the contrast were "something_fetch" vs
> "something_push", that is.  And that something being cryptic "sti"
> (recall my comment on it, being the name of the function that
> returns the value, which is less understandable than using words
> that signals what the variable _means_), would not make much sense
> for the "push" direction, as "sti" is not even an abbreviation for
> the function that gives the value.  "cmp" (or "compare" for that
> matter) still has the ambiguity "compare with what and what for?",
> but at least it would be better than "sti".

Maybe simplest would be 'upstream_diff' and 'push_diff'? But this begs the question of why not all the old 'ours', 'theirs', etc variables are missing an 'upstream_' prefix.

I feel like this can turn into endless refactoring, latest version now has 'cmp_fetch' and 'push_cmp_fetch'.

I have on my TODO list to fix the pluralization of the "diverged" text after this patch series has been merged. I'm also working on refactoring this by introducing a struct to pass data to 'format_branch_comparison'. This has the very nice benefit on natural namespacing of variables when they become fields in struct variables like 'upstream_branch' and 'push_branch'.

In the meantime I think I will leave this patch series for now 🤗
Harald
Junio C Hamano· Jan 11, 2026, 03:39 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Harald Nordgren <haraldnordgren@gmail.com> writes:
> In the meantime I think I will leave this patch series for now 🤗

I think we are reaching the point of diminishing returns after polishing the series enough at the 22nd iteration.

Let's wait for a few days to see if what others find in the series and then mark the topic for 'next'. Hopefully any minor nits can be addressed later as a follow-up, just like the Q_(ours+theirs) thing.

Thanks.
Kristoffer Haugsbakk· Jan 12, 2026, 07:33 UTC · re: Harald Nordgren · lore

Re: [PATCH v20 0/2] status: show comparison with push remote tracking branch

On Sat, Jan 10, 2026, at 20:06, Harald Nordgren wrote:
Show 5 quoted lines
>> Here is a place for you to explain what helps people to decypher the
>> differences since the previous iteration in Range-diff (which is not
>> very easy to read).
>
> ?

It’s a bit opaque. He’s saying that you should reserve some space in the cover letter email (`PATCH 0/2`) for explaining what has changed in this version compared to the previous one.

People often write:
    Here is what this patch series does.
    ### Changes in v3
    Fixed the commit message typos.
I don’t know where this should be filled in when using gitgitgadget. ;)
Phillip Wood· Jan 12, 2026, 14:45 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

On 09/01/2026 16:00, Harald Nordgren wrote:
Show 8 quoted lines
>> Using an enum for a set of flags is a bit confusing.
> 
>> On reflection I wonder if we should be calling branch_get_push() instead
>> of remote_ref_for_branch() and tracking_for_push_dest() as it respects
>> 'push.default' and so the branch it returns is the one that "git push"
>> without any arguments would push to.
> 
> I'm not getting that to work without tests breaking.

It is hard to discuss this without knowing what actually breaks. Are you talking about the tests added in this series? If so that means we're expecting a different behavior to what "git push" actually does. As Ben has pointed out elsewhere in this thread, if you're pushing back to a different branch on the same remote as the upstream branch you need to set `push.default=current`.

The advantage would be that it simplifies the code and it means we respect `push.default=upstream`.

The current implementation returns NULL if there is a push refspec exists but it does not match the current branch - in that case we should return a copy of the branch name instead.

Show 5 quoted lines
>> I think it would be simpler to just return the full refname and let the
>> caller shorten it.
> 
> We could to that but what's the benefit? Shouldn't a helper function reduce
> the work for the caller, not the other way around? 🤗

The benefit is that you get a sane interface rather that returning two different versions of the same string in two different ways (one from the function's return value and the other from a function parameter). It also matches what we do for the upstream branch.

Show 9 quoted lines
>> Why are we checking for BRANCH_MODE_PUSH here? Don't we want to show
>> this advice regardless of the mode?
> 
> Yeah, that's a good point. However, this will often lead to the advice
> being shown twice, see this test:
> 
> ```
> status --no-ahead-behind shows diverged from origin/main and ahead of feature branch
> ```

I can't seem to see that test. If we're printing the advice once for the upstream branch and once for the default push remote I think that would be ok.

Show 10 quoted lines
>> If we don't want to show this can't we set show_divergance_adivce to
>> false when we call this function - why is it guarded by BRANCH_MODE_PULL
>> as well?
> 
> This will already have been shown the the pull branch (the upstream
> branch).
> 
> And in the cases when it's NOT shown for the pull branch but only the push
> branch has diverged, then pulling won't solve the problem anyway. (Unless
> you would try to pull from your push branch.)

But we set show_divergance_advice to false for the push branch so there is no need to check the flag.

Thanks
Phillip
Harald Nordgren· Jan 12, 2026, 19:47 UTC · re: Phillip Wood · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

Show 6 quoted lines
> It is hard to discuss this without knowing what actually breaks. Are you
> talking about the tests added in this series? If so that means we're
> expecting a different behavior to what "git push" actually does. As Ben
> has pointed out elsewhere in this thread, if you're pushing back to a
> different branch on the same remote as the upstream branch you need to
> set `push.default=current`.

Yes, it's my new tests that are breaking. Maybe it's easiest if you check out the `seen` branch which now has this logic, play with the code and run the tests to see when it breaks.

I designed the feature around 'push.default=current' which I use.

If we would design the feature around 'push.default=upstream' then what is the point? 🤗 Why do we need to show status for both an upstream and a push branch if we are already pushing to our upstream branch?

> The benefit is that you get a sane interface rather that returning two
> different versions of the same string in two different ways (one from
> the function's return value and the other from a function parameter). It
> also matches what we do for the upstream branch.

That's a good point about matching what we do for upstream branch, I'll take a look.

> I can't seem to see that test. If we're printing the advice once for the
> upstream branch and once for the default push remote I think that would
> be ok.
This test is also part of my patch 🤗
I disagree about showing the same advice twice.
> But we set show_divergance_advice to false for the push branch so there
> is no need to check the flag.
Good point! I'll update it!
Harald
Harald Nordgren via GitGitGadget· Jan 12, 2026, 20:26 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v23 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 173 ++++++++++++++++++++------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 400 insertions(+), 35 deletions(-)
base-commit: 8745eae506f700657882b9e32b2aa00f234a6fb6
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v23
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v23
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v22:
 1:  4aa4f1abc8 = 1:  fd05c7b778 refactor format_branch_comparison in preparation
 2:  b7e29887d9 ! 2:  f1ad7a1b6f status: show comparison with push remote tracking branch
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +		advice_enabled(ADVICE_STATUS_HINTS);
      +	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool want_divergence_advice = (flags & BRANCH_MODE_PULL) &&
     -+		show_divergence_advice &&
     ++	bool want_divergence_advice = show_divergence_advice &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
      +
       	if (up_to_date) {
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +	if (push_branch_modes & BRANCH_MODE_PUSH) {
      +		strbuf_addstr(sb, "\n");
      +		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
     -+					 push_branch_modes, show_divergence_advice);
     ++					 push_branch_modes, 0);
       	}
       
       	free(base);
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 12, 2026, 20:26 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v23 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 12, 2026, 20:26 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v23 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 101 ++++++++++++++-
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 357 insertions(+), 6 deletions(-)
Show changes to 2 files +357 −6

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index fd592ec659..132fc6849f 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,11 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	BRANCH_MODE_PULL = (1 << 0),
+	BRANCH_MODE_PUSH = (1 << 1),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2230,13 +2235,74 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	if (full_ref_out)
+		*full_ref_out = xstrdup(resolved);
+
+	ret = refs_shorten_unambiguous_ref(
+		get_main_ref_store(the_repository), resolved, 0);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
+				     unsigned flags,
 				     bool show_divergence_advice)
 {
+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool want_divergence_advice = show_divergence_advice &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2311,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2320,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2331,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2344,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (want_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,6 +2361,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
+	int push_ours, push_theirs, push_cmp_fetch;
+	char *full_push = NULL;
+	char *push = NULL;
+	unsigned push_branch_modes = 0;
 
 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (cmp_fetch < 0) {
@@ -2307,6 +2377,16 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	push = get_remote_push_branch(branch, &full_push);
+	if (push && strcmp(base, push)) {
+		push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
+					   &push_ours, &push_theirs, abf);
+		if (push_cmp_fetch >= 0) {
+			base_branch_modes = BRANCH_MODE_PULL;
+			push_branch_modes = BRANCH_MODE_PUSH;
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2315,10 +2395,19 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
+					 base_branch_modes, show_divergence_advice);
+	}
+
+	if (push_branch_modes & BRANCH_MODE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
+					 push_branch_modes, 0);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 12, 2026, 20:46 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v23 2/2] status: show comparison with push remote tracking branch

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> +	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
> +		advice_enabled(ADVICE_STATUS_HINTS);
> +	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
> +		advice_enabled(ADVICE_STATUS_HINTS);
> +	bool want_divergence_advice = show_divergence_advice &&
> +		advice_enabled(ADVICE_STATUS_HINTS);

Just an observation, and not an objection, but it looks curious why the last one is so different from the other two. IOW, the above makes me wonder if it makes sense to roll the show_divergence_advice bit into the base_branch_modes flag word.

Other than that, this round is a pleasant read.  Thanks.
Harald Nordgren· Jan 13, 2026, 09:54 UTC · re: Junio C Hamano · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

> Just an observation, and not an objection, but it looks curious why
> the last one is so different from the other two.  IOW, the above
> makes me wonder if it makes sense to roll the show_divergence_advice
> bit into the base_branch_modes flag word.
Good point. I changed it, it makes it nicer with one fewer parameter.
> Other than that, this round is a pleasant read.  Thanks.
Thanks for all the help so far!
Harald
Harald Nordgren via GitGitGadget· Jan 13, 2026, 09:55 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v24 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 175 ++++++++++++++++++++------
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 402 insertions(+), 35 deletions(-)
base-commit: 8745eae506f700657882b9e32b2aa00f234a6fb6
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v24
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v24
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v23:
 1:  fd05c7b778 = 1:  fd05c7b778 refactor format_branch_comparison in preparation
 2:  f1ad7a1b6f ! 2:  138b79a0b9 status: show comparison with push remote tracking branch
     @@ remote.c
       enum map_direction { FROM_SRC, FROM_DST };
       
      +enum {
     -+	BRANCH_MODE_PULL = (1 << 0),
     -+	BRANCH_MODE_PUSH = (1 << 1),
     ++	ENABLE_ADVICE_PULL       = (1 << 0),
     ++	ENABLE_ADVICE_PUSH       = (1 << 1),
     ++	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
      +};
      +
       struct counted_string {
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
       
     -+static char *get_remote_push_branch(struct branch *branch, char **full_ref_out)
     ++static char *get_remote_push_branch(struct branch *branch)
      +{
      +	struct remote *remote;
      +	const char *push_remote;
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +		return NULL;
      +	}
      +
     -+	if (full_ref_out)
     -+		*full_ref_out = xstrdup(resolved);
     -+
     -+	ret = refs_shorten_unambiguous_ref(
     -+		get_main_ref_store(the_repository), resolved, 0);
     ++	ret = xstrdup(resolved);
      +	free(tracking_ref);
      +	return ret;
      +}
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       				     int ours, int theirs,
       				     const char *branch_name,
       				     enum ahead_behind_flags abf,
     -+				     unsigned flags,
     - 				     bool show_divergence_advice)
     +-				     bool show_divergence_advice)
     ++				     unsigned flags)
       {
     -+	bool want_push_advice = (flags & BRANCH_MODE_PUSH) &&
     ++	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool want_pull_advice = (flags & BRANCH_MODE_PULL) &&
     ++	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool want_divergence_advice = show_divergence_advice &&
     ++	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
      +		advice_enabled(ADVICE_STATUS_HINTS);
      +
       	if (up_to_date) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			    _("Your branch and '%s' refer to different commits.\n"),
       			    branch_name);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (want_push_advice)
     ++		if (enable_push_advice)
       			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
       				    "git status --ahead-behind");
       	} else if (!theirs) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours),
       			branch_name, ours);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (want_push_advice)
     ++		if (enable_push_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git push\" to publish your local commits)\n"));
       	} else if (!ours) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   theirs),
       			branch_name, theirs);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (want_pull_advice)
     ++		if (enable_pull_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
      -		    advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (want_divergence_advice)
     ++		if (enable_divergence_advice)
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
       	}
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	const char *full_base;
       	char *base;
       	int upstream_is_gone = 0;
     -+	unsigned base_branch_modes = BRANCH_MODE_PULL | BRANCH_MODE_PUSH;
     ++	unsigned base_branch_flags = ENABLE_ADVICE_PULL | ENABLE_ADVICE_PUSH;
      +	int push_ours, push_theirs, push_cmp_fetch;
     -+	char *full_push = NULL;
     ++	char *full_push;
      +	char *push = NULL;
     -+	unsigned push_branch_modes = 0;
     ++	unsigned push_branch_flags = 0;
       
       	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
       	if (cmp_fetch < 0) {
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
       					    full_base, 0);
       
     -+	push = get_remote_push_branch(branch, &full_push);
     -+	if (push && strcmp(base, push)) {
     -+		push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
     -+					   &push_ours, &push_theirs, abf);
     -+		if (push_cmp_fetch >= 0) {
     -+			base_branch_modes = BRANCH_MODE_PULL;
     -+			push_branch_modes = BRANCH_MODE_PUSH;
     ++	full_push = get_remote_push_branch(branch);
     ++	if (full_push) {
     ++		push = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
     ++						     full_push, 0);
     ++		if (push && base && strcmp(base, push)) {
     ++			push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
     ++						   &push_ours, &push_theirs, abf);
     ++			if (push_cmp_fetch >= 0) {
     ++				base_branch_flags = ENABLE_ADVICE_PULL;
     ++				push_branch_flags = ENABLE_ADVICE_PUSH;
     ++			}
      +		}
      +	}
      +
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
       	} else {
      -		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
     ++		if (show_divergence_advice)
     ++			base_branch_flags |= ENABLE_ADVICE_DIVERGENCE;
      +		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
     -+					 base_branch_modes, show_divergence_advice);
     ++					 base_branch_flags);
      +	}
      +
     -+	if (push_branch_modes & BRANCH_MODE_PUSH) {
     ++	if (push_branch_flags & ENABLE_ADVICE_PUSH) {
      +		strbuf_addstr(sb, "\n");
      +		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
     -+					 push_branch_modes, 0);
     ++					 push_branch_flags);
       	}
       
       	free(base);
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 13, 2026, 09:55 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v24 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 13, 2026, 09:55 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v24 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 105 ++++++++++++++--
 t/t6040-tracking-info.sh | 262 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 360 insertions(+), 7 deletions(-)
Show changes to 2 files +360 −7

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index fd592ec659..8d64a3bee6 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2230,13 +2236,69 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	ret = xstrdup(resolved);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2307,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2316,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2327,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2340,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,6 +2357,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_flags = ENABLE_ADVICE_PULL | ENABLE_ADVICE_PUSH;
+	int push_ours, push_theirs, push_cmp_fetch;
+	char *full_push;
+	char *push = NULL;
+	unsigned push_branch_flags = 0;
 
 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (cmp_fetch < 0) {
@@ -2307,6 +2373,20 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	full_push = get_remote_push_branch(branch);
+	if (full_push) {
+		push = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+						     full_push, 0);
+		if (push && base && strcmp(base, push)) {
+			push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
+						   &push_ours, &push_theirs, abf);
+			if (push_cmp_fetch >= 0) {
+				base_branch_flags = ENABLE_ADVICE_PULL;
+				push_branch_flags = ENABLE_ADVICE_PUSH;
+			}
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2315,10 +2395,21 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+		if (show_divergence_advice)
+			base_branch_flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
+					 base_branch_flags);
+	}
+
+	if (push_branch_flags & ENABLE_ADVICE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
+					 push_branch_flags);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..cf5a926dcd 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,266 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature8 origin/main &&
+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature9 upstream/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Phillip Wood· Jan 13, 2026, 10:41 UTC · re: Harald Nordgren · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

On 12/01/2026 19:47, Harald Nordgren wrote:
Show 16 quoted lines
>> It is hard to discuss this without knowing what actually breaks. Are you
>> talking about the tests added in this series? If so that means we're
>> expecting a different behavior to what "git push" actually does. As Ben
>> has pointed out elsewhere in this thread, if you're pushing back to a
>> different branch on the same remote as the upstream branch you need to
>> set `push.default=current`.
> 
> Yes, it's my new tests that are breaking. Maybe it's easiest if you check
> out the `seen` branch which now has this logic, play with the code and run
> the tests to see when it breaks.
> 
> I designed the feature around 'push.default=current' which I use.
> 
> If we would design the feature around 'push.default=upstream' then what is
> the point? 🤗 Why do we need to show status for both an upstream and a push
> branch if we are already pushing to our upstream branch?

I'm not suggesting we design the feature around 'push.default=upstream', I'm suggesting that we design it to respect 'push.default' so it gives sensible output (i.e. something that resembles what "git push" would do) whatever the setting.

Show 7 quoted lines
>> The benefit is that you get a sane interface rather that returning two
>> different versions of the same string in two different ways (one from
>> the function's return value and the other from a function parameter). It
>> also matches what we do for the upstream branch.
> 
> That's a good point about matching what we do for upstream branch, I'll
> take a look.
Thanks, I think that would be cleaner
Show 5 quoted lines
>> I can't seem to see that test. If we're printing the advice once for the
>> upstream branch and once for the default push remote I think that would
>> be ok.
> 
> This test is also part of my patch 🤗

Oh, I was looking in junio's "seen" branch from the 8th and it wasn't there. I've updated and I can see it now. I can see tests for

  - upstream differs from the local branch, no push branch shown
  - upstream and push branches differ from the local branch
  - upstream and push branches match the local branch

I can't see a test for the local branch differing from the upstream branch when the push branch matches the local branch. As far as I can see in that case we don't show the advice when we should do (and we currently do show it)

> I disagree about showing the same advice twice.

I think it is less bad than not showing it when the upstream branch does not match the local branch.

>> But we set show_divergance_advice to false for the push branch so there
>> is no need to check the flag.
> 
> Good point! I'll update it!
That's great
Thanks
Phillip
> 
> Harald
Harald Nordgren· Jan 13, 2026, 12:11 UTC · re: Phillip Wood · lore

Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation

> I'm not suggesting we design the feature around 'push.default=upstream',
> I'm suggesting that we design it to respect 'push.default' so it gives
> sensible output (i.e. something that resembles what "git push" would do)
> whatever the setting.
I guess I'm still not getting it, maybe explain it like I'm more stupid 😅

'push.default' does not change the output of git status, which shows a comparison with the tracking branch. I wrote the code to only report push branch status when it differs from the upstream. If we use 'push.default=upstream' they will be the same, so no need to report it twice.

> Thanks, I think that would be cleaner
It's in the latest patch now.
Show 10 quoted lines
> I can see tests for
> 
>  - upstream differs from the local branch, no push branch shown
>  - upstream and push branches differ from the local branch
>  - upstream and push branches match the local branch
> 
> I can't see a test for the local branch differing from the upstream
> branch when the push branch matches the local branch. As far as I can see
> in that case we don't show the advice when we should do (and we currently
> do show it)

I added a test for this now, which I think proves that it's working correctly:

``` test_expect_success 'status with upstream ahead and push branch up to date' ' ```

Harald
Harald Nordgren via GitGitGadget· Jan 13, 2026, 12:11 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v25 0/2] status: show comparison with push remote tracking branch

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: show comparison with push remote tracking branch
 remote.c                 | 175 +++++++++++++++++++-----
 t/t6040-tracking-info.sh | 285 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 425 insertions(+), 35 deletions(-)
base-commit: 8745eae506f700657882b9e32b2aa00f234a6fb6
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v25
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v25
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v24:
 1:  fd05c7b778 = 1:  fd05c7b778 refactor format_branch_comparison in preparation
 2:  138b79a0b9 ! 2:  fa744efc59 status: show comparison with push remote tracking branch
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     ++test_expect_success 'status with upstream ahead and push branch up to date' '
     ++	(
     ++		cd test &&
     ++		git checkout -b ahead upstream/main &&
     ++		advance work &&
     ++		git push upstream HEAD &&
     ++		git checkout -b feature8 upstream/main &&
     ++		git push origin &&
     ++		git branch --set-upstream-to upstream/ahead &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature8
     ++	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
     ++	  (use "git pull" to update your local branch)
     ++
     ++	Your branch is up to date with ${SQ}origin/feature8${SQ}.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
      +test_expect_success 'status shows remapped push refspec' '
      +	(
      +		cd test &&
     -+		git checkout -b feature8 origin/main &&
     -+		git config remote.origin.push refs/heads/feature8:refs/heads/remapped &&
     ++		git checkout -b feature9 origin/main &&
     ++		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
      +		git push &&
      +		advance work &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature8
     ++	On branch feature9
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +test_expect_success 'status shows remapped push refspec with upstream remote' '
      +	(
      +		cd test &&
     -+		git checkout -b feature9 upstream/main &&
     -+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
     ++		git checkout -b feature10 upstream/main &&
     ++		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
      +		git push origin &&
      +		advance work &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature9
     ++	On branch feature10
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 13, 2026, 12:11 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v25 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 13, 2026, 12:11 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v25 2/2] status: show comparison with push remote tracking branch

From: Harald Nordgren <haraldnordgren@gmail.com>

"git status" on a branch that follows a remote branch compares commits on the current branch and the remote-tracking branch it builds upon, to show "ahead", "behind", or "diverged" status.

When working on a feature branch that tracks a remote feature branch, but you also want to track progress relative to the push destination tracking branch (which may differ from the upstream branch), git status now shows an additional comparison.

When the upstream tracking branch differs from the push destination tracking branch, git status shows both the comparison with the upstream tracking branch (as before) and an additional comparison with the push destination tracking branch. The push branch comparison appears on a separate line after the upstream branch status, using the same format.

Example output when tracking origin/main but push destination is
origin/feature:
    On branch feature
    Your branch and 'origin/main' have diverged,
    and have 3 and 1 different commits each, respectively.
      (use "git pull" if you want to integrate the remote branch with yours)
    Your branch is ahead of 'origin/feature' by 1 commit.
      (use "git push" to publish your local commits)

The comparison is only shown when the push destination tracking branch differs from the upstream tracking branch, even if they are on the same remote.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c                 | 105 ++++++++++++++-
 t/t6040-tracking-info.sh | 285 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 383 insertions(+), 7 deletions(-)
Show changes to 2 files +383 −7

remote.c, t/t6040-tracking-info.sh

diff --git a/remote.c b/remote.c
index fd592ec659..8d64a3bee6 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2230,13 +2236,69 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *get_remote_push_branch(struct branch *branch)
+{
+	struct remote *remote;
+	const char *push_remote;
+	char *push_dst = NULL;
+	char *tracking_ref;
+	const char *resolved;
+	char *ret;
+
+	if (!branch)
+		return NULL;
+
+	push_remote = pushremote_for_branch(branch, NULL);
+	if (!push_remote)
+		return NULL;
+
+	remote = remotes_remote_get(the_repository, push_remote);
+	if (!remote)
+		return NULL;
+
+	push_dst = remote_ref_for_branch(branch, 1);
+	if (!push_dst) {
+		if (remote->push.nr)
+			return NULL;
+		push_dst = xstrdup(branch->refname);
+	}
+
+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
+	free(push_dst);
+
+	if (!tracking_ref)
+		return NULL;
+
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		tracking_ref,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+
+	if (!resolved) {
+		free(tracking_ref);
+		return NULL;
+	}
+
+	ret = xstrdup(resolved);
+	free(tracking_ref);
+	return ret;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2307,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2316,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2327,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2340,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,6 +2357,11 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	const char *full_base;
 	char *base;
 	int upstream_is_gone = 0;
+	unsigned base_branch_flags = ENABLE_ADVICE_PULL | ENABLE_ADVICE_PUSH;
+	int push_ours, push_theirs, push_cmp_fetch;
+	char *full_push;
+	char *push = NULL;
+	unsigned push_branch_flags = 0;
 
 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
 	if (cmp_fetch < 0) {
@@ -2307,6 +2373,20 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
 					    full_base, 0);
 
+	full_push = get_remote_push_branch(branch);
+	if (full_push) {
+		push = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+						     full_push, 0);
+		if (push && base && strcmp(base, push)) {
+			push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
+						   &push_ours, &push_theirs, abf);
+			if (push_cmp_fetch >= 0) {
+				base_branch_flags = ENABLE_ADVICE_PULL;
+				push_branch_flags = ENABLE_ADVICE_PUSH;
+			}
+		}
+	}
+
 	if (upstream_is_gone) {
 		strbuf_addf(sb,
 			_("Your branch is based on '%s', but the upstream is gone.\n"),
@@ -2315,10 +2395,21 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			strbuf_addstr(sb,
 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
 	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+		if (show_divergence_advice)
+			base_branch_flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
+					 base_branch_flags);
+	}
+
+	if (push_branch_flags & ENABLE_ADVICE_PUSH) {
+		strbuf_addstr(sb, "\n");
+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
+					 push_branch_flags);
 	}
 
 	free(base);
+	free(full_push);
+	free(push);
 	return 1;
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..da5696113e 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,289 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout shows push branch up to date' '
+	(
+		cd test &&
+		git checkout feature7 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status with upstream ahead and push branch up to date' '
+	(
+		cd test &&
+		git checkout -b ahead upstream/main &&
+		advance work &&
+		git push upstream HEAD &&
+		git checkout -b feature8 upstream/main &&
+		git push origin &&
+		git branch --set-upstream-to upstream/ahead &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature9 origin/main &&
+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status shows remapped push refspec with upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature10 upstream/main &&
+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Jeff King· Jan 13, 2026, 17:03 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v25 2/2] status: show comparison with push remote tracking branch

On Tue, Jan 13, 2026 at 12:11:56PM +0000, Harald Nordgren via GitGitGadget wrote:
Show 9 quoted lines
> Example output when tracking origin/main but push destination is
> origin/feature:
>     On branch feature
>     Your branch and 'origin/main' have diverged,
>     and have 3 and 1 different commits each, respectively.
>       (use "git pull" if you want to integrate the remote branch with yours)
> 
>     Your branch is ahead of 'origin/feature' by 1 commit.
>       (use "git push" to publish your local commits)
Can we make this configurable?

I build my daily driver off of the 'jch' branch, which now includes this series, and I've found that for my triangular workflow the ahead/behind for the push branch is just useless noise. I treat my push destination like a mirror, where I always just push up everything at the end of the day.

I know that the output can be disabled with status.aheadbehind, but:
  1. I noticed this first via "git checkout", which does not have such a
     flag (AFAIK).
  2. That flag would also disable the upstream branch ahead/behind
     output, which I do find useful.
-Peff
Harald Nordgren· Jan 13, 2026, 18:35 UTC · re: Jeff King · lore

Triangular workflow

> For my triangular workflow the ahead/behind for the push branch is just
> useless noise. I treat my push destination like a mirror, where I always
> just push up everything at the end of the day.
This seems like a sub-optimal workflow 🤗

May I ask where you normally push (unless you only ever push once at the end) of the day? If it's another branch than your mirror, why not set that as your push destination then, and push to your push destination with the longer

    git push origin my-mirror

It makes more sense to me to reserve the shorter and more convenient 'git push' for something you do many times a day.

Harald
Jeff King· Jan 13, 2026, 21:40 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

On Tue, Jan 13, 2026 at 07:35:57PM +0100, Harald Nordgren wrote:
Show 15 quoted lines
> > For my triangular workflow the ahead/behind for the push branch is just
> > useless noise. I treat my push destination like a mirror, where I always
> > just push up everything at the end of the day.
> 
> This seems like a sub-optimal workflow 🤗
> 
> May I ask where you normally push (unless you only ever push once at the
> end) of the day? If it's another branch than your mirror, why not set that
> as your push destination then, and push to your push destination with the
> longer
> 
>     git push origin my-mirror
> 
> It makes more sense to me to reserve the shorter and more convenient
> 'git push' for something you do many times a day.

I couldn't quite parse what you meant by "another branch than your mirror", but I'll describe my workflow:

  1. My "origin" remote is https://github.com/gitster/git.
  2. Most branches have origin/master as their upstream, though
     occasionally I'll have dependent branches that use "." as their
     remote and the local base branch for the "branch" field.
     E.g.
       git checkout -b jk/some-topic origin/master
       ...
       git checkout -t -b jk/another-topic jk/some-topic
  3. My mirror is https://github.com/peff/git, which I call "github" in
     the remote (arguably confusing, but back when I started that
     convention I was pulling Junio's tree from kernel.org ;) ). I point
     remote.pushdefault to the "github" remote.
  4. My push.default setting is "current", so if I want to push up a
     single branch, I can just "git push". I hardly ever do that,
     though.
  5. I usually push once per day or so. After integrating my topics into
     a shared daily-driver branch, I run something like:
       make test && make install && git push -f github refs/heads/jk/*
     (it's not exactly that; I have a script which picks out the local
      refs, but it's the moral equivalent of that).

So I don't ever care about the relationship between a specific jk/foo and my mirror's version of it. I don't expect anybody to look at those, and they exist mostly for backup and CI purposes (though I don't run CI on the individual branches, but only the integrated result).

And having the extra output from "git checkout" is just extra noise for me, especially because it is easy to see only the second message (which looks just like the upstream ahead/behind message, of course) and get confused. The first time I saw it I thought I had misconfigured something with my branch.

-Peff
Harald Nordgren· Jan 13, 2026, 23:01 UTC · re: Jeff King · lore

Triangular workflow

Hi Jeff!

I'm very happy that your responded respectfully despite me basically saying that you were using Git wrong. It's nice to see how some of the pros do it!

I'm wondering if since you are scripting this anyway, if you really need a push branch at all? Can't you just as easily switch to doing this in the script:

    git config push.default upstream
    git push github jk/some-topic

As a note, before I started working on this feature, I don't realize that there was such a thing as a push branch (i.e. something different from the tracking branch). So I had the habit of checking out and pushing like this:

    git branch --set-upstream-to upstream/master
    git push origin $(git rev-parse --abbrev-ref HEAD)

I worked really well for me. The only issue was missing the status info from my own branch -- which is why I started writing this feature.

Harald
D. Ben Knoble· Jan 14, 2026, 02:22 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

On Tue, Jan 13, 2026 at 6:01 PM Harald Nordgren <haraldnordgren@gmail.com> wrote:

Show 13 quoted lines
>
> Hi Jeff!
>
> I'm very happy that your responded respectfully despite me basically
> saying that you were using Git wrong. It's nice to see how some of the pros
> do it!
>
> I'm wondering if since you are scripting this anyway, if you really need a
> push branch at all? Can't you just as easily switch to doing this in the
> script:
>
>     git config push.default upstream
>     git push github jk/some-topic

The script is doing the "moral equivalent of" "git push -f github refs/heads/jk/*", so I'm not sure I follow the suggestion here.

Show 13 quoted lines
> As a note, before I started working on this feature, I don't realize
> that there was such a thing as a push branch (i.e. something different from
> the tracking branch). So I had the habit of checking out and pushing like
> this:
>
>     git branch --set-upstream-to upstream/master
>     git push origin $(git rev-parse --abbrev-ref HEAD)
>
> I worked really well for me. The only issue was missing the status info
> from my own branch -- which is why I started writing this feature.
>
>
> Harald

My workflow is different from Peff's, but it is similar along at least one line: it's really convenient to have "git push" with no further arguments (only possibly flags) to push my branch to a remote mirror.

-- 
D. Ben Knoble
Jeff King· Jan 14, 2026, 02:34 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 12:01:07AM +0100, Harald Nordgren wrote:
Show 6 quoted lines
> I'm wondering if since you are scripting this anyway, if you really need a
> push branch at all? Can't you just as easily switch to doing this in the
> script:
> 
>     git config push.default upstream
>     git push github jk/some-topic

I could (and in fact the script names the remote directly already, because you can't pass refspecs without specifying the remote). But I do occasionally push a single branch with a bare "git push". Usually this is the integration branch, when I am trying to trigger CI manually (e.g., when piling hacks on top in order to debug a CI failure ;) ).

So even if I only do it infrequently, it feels weird that a bare "git push" would try to push to the upstream remote (which I don't even have write access to!).

Show 10 quoted lines
> As a note, before I started working on this feature, I don't realize
> that there was such a thing as a push branch (i.e. something different from
> the tracking branch). So I had the habit of checking out and pushing like
> this:
> 
>     git branch --set-upstream-to upstream/master
>     git push origin $(git rev-parse --abbrev-ref HEAD)
> 
> I worked really well for me. The only issue was missing the status info
> from my own branch -- which is why I started writing this feature.

Yeah, though @{push} is usually not explicitly configured in the same way @{upstream} is, but rather a consequence of how push.default and remote.pushdefault interact. But it was added for exactly this kind of triangular workflow. I sometimes will do stuff like:

  git range-diff origin @{push} HEAD

to compare two iterations of a branch if I know that I haven't pushed. It is a bit of a cheat, because what I really mean is "do a range-diff since the last thing I sent to the list". But if I have just been working on a branch, and I haven't run an integration cycle since then, then I know that the pushed version will match it.

There is also branch.*.pushRemote, but I have not found that useful (for my triangular flow there is always a single repo to push to, not one per branch).

-Peff
Harald Nordgren· Jan 14, 2026, 07:53 UTC · re: Jeff King · lore

Triangular workflow

Show 9 quoted lines
> I could (and in fact the script names the remote directly already,
> because you can't pass refspecs without specifying the remote). But I do
> occasionally push a single branch with a bare "git push". Usually this
> is the integration branch, when I am trying to trigger CI manually
> (e.g., when piling hacks on top in order to debug a CI failure ;) ).
> 
> So even if I only do it infrequently, it feels weird that a bare "git
> push" would try to push to the upstream remote (which I don't even have
> write access to!).

Maybe ’push.default=simple’ or ’push.default=nothing’ are better settings in your scenario. Then you get explicit pushing because no push branch gets set. And thus 'git status' reports not additinal status.

Show 6 quoted lines
> Yeah, though @{push} is usually not explicitly configured in the same
> way @{upstream} is, but rather a consequence of how push.default and
> remote.pushdefault interact. But it was added for exactly this kind of
> triangular workflow. I sometimes will do stuff like:
> 
>   git range-diff origin @{push} HEAD
I imagine the same thing could be achieved with
    origin/$(git rev-parse --abbrev-ref HEAD)
Harald
Harald Nordgren· Jan 14, 2026, 07:59 UTC · re: D. Ben Knoble · lore

Triangular workflow

> My workflow is different from Peff's, but it is similar along at least
> one line: it's really convenient to have "git push" with no further
> arguments (only possibly flags) to push my branch to a remote mirror.

Would you also like the status reporting to be off for your push branch? I asking because that's what Jeff is arguing for.

I fully agree on the convenience of bare 'git push', I use it everyday together with these settings and commands for max convenience:

    git config --global remote.pushDefault origin # As opposed to upstream
    git config --global push.default current
    git config --global pull.rebase true
    git config --global rebase.autoStash true
    git checkout $(gr | rg '^(upstream|origin)$' | tail -n1)/HEAD -b new_branch
    git push
    git status
    git pull
Harald
Junio C Hamano· Jan 14, 2026, 14:15 UTC · re: Jeff King · lore

Re: Triangular workflow

Jeff King <peff@peff.net> writes:
> So even if I only do it infrequently, it feels weird that a bare "git
> push" would try to push to the upstream remote (which I don't even have
> write access to!).

Yes, exactly. I was wondering where Harald's suggestion to swap the two remotes came from.

Show 12 quoted lines
> Yeah, though @{push} is usually not explicitly configured in the same
> way @{upstream} is, but rather a consequence of how push.default and
> remote.pushdefault interact. But it was added for exactly this kind of
> triangular workflow. I sometimes will do stuff like:
>
>   git range-diff origin @{push} HEAD
>
> to compare two iterations of a branch if I know that I haven't pushed.
> It is a bit of a cheat, because what I really mean is "do a range-diff
> since the last thing I sent to the list". But if I have just been
> working on a branch, and I haven't run an integration cycle since then,
> then I know that the pushed version will match it.

Great suggestion. It is fun to see that comparing notes among people with different workflows brings out these gems ;-)

> There is also branch.*.pushRemote, but I have not found that useful (for
> my triangular flow there is always a single repo to push to, not one per
> branch).
Jeff King· Jan 14, 2026, 16:24 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 08:53:09AM +0100, Harald Nordgren wrote:
Show 13 quoted lines
> > I could (and in fact the script names the remote directly already,
> > because you can't pass refspecs without specifying the remote). But I do
> > occasionally push a single branch with a bare "git push". Usually this
> > is the integration branch, when I am trying to trigger CI manually
> > (e.g., when piling hacks on top in order to debug a CI failure ;) ).
> > 
> > So even if I only do it infrequently, it feels weird that a bare "git
> > push" would try to push to the upstream remote (which I don't even have
> > write access to!).
> 
> Maybe ’push.default=simple’ or ’push.default=nothing’ are better settings
> in your scenario. Then you get explicit pushing because no push branch gets
> set. And thus 'git status' reports not additinal status.

If I did that, then the occasional "git push" (without arguments) that I do would fail. Likewise, @{push} would not be usable.

Show 10 quoted lines
> > Yeah, though @{push} is usually not explicitly configured in the same
> > way @{upstream} is, but rather a consequence of how push.default and
> > remote.pushdefault interact. But it was added for exactly this kind of
> > triangular workflow. I sometimes will do stuff like:
> > 
> >   git range-diff origin @{push} HEAD
> 
> I imagine the same thing could be achieved with
> 
>     origin/$(git rev-parse --abbrev-ref HEAD)
Sure, but:
  1. It is a lot shorter to type @{push}. ;)
  2. Using @{push} works everywhere, even on my non-triangular repos,
     because it takes into account the push configuration. So it's a
     much nicer muscle-memory to acquire.
-Peff
Harald Nordgren· Jan 14, 2026, 17:48 UTC · re: Jeff King · lore

Triangular workflow

Show 7 quoted lines
> Sure, but:
> 
>   1. It is a lot shorter to type @{push}. ;)
> 
>   2. Using @{push} works everywhere, even on my non-triangular repos,
>      because it takes into account the push configuration. So it's a
>      much nicer muscle-memory to acquire.

Makes sense, I’m all out of arguments here 😅 Please don’t take this the wrong way, but it reminds me of this XKCD: https://xkcd.com/1172/

As long as this is turned on by default then I’m mostly happy. Maybe JCH and others can weigh in on if this needs to be this configurable or always on.

Harald
Junio C Hamano· Jan 14, 2026, 18:54 UTC · re: Jeff King · lore

Re: Triangular workflow

Jeff King <peff@peff.net> writes:
Show 5 quoted lines
> And having the extra output from "git checkout" is just extra noise for
> me, especially because it is easy to see only the second message (which
> looks just like the upstream ahead/behind message, of course) and get
> confused. The first time I saw it I thought I had misconfigured
> something with my branch.

It now is clear to me that this should be _optional_, so that those who do really want extra output from the command should explicitly opt into the feature. After all, any optional new feature that you must opt into by definition cannot regress end user experience for those who do not ;-)

At the same time, I suspect that extra comparison on top of what we already give against the @{upstream} may not be limited to what Harald implemented (is it essentially the same as specyfing @{push}, or something else?). I wonder if we can come up with a flexible and extensible notation to specify what branch(es) to compare with, so that we can use it as the value of this opt-in configuration variable? Something like

	[status] compareBranches = @{upstream} @{push}

signals that the current branch is compared against these two branches, and not having the configuration (i.e., traditional behaviour, which is left the default) would be equivalent to have

	[status] compareBranches = @{upstream}
or something like that, perhaps?
Thanks.
Jeff King· Jan 14, 2026, 21:01 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 06:48:45PM +0100, Harald Nordgren wrote:
Show 10 quoted lines
> > Sure, but:
> > 
> >   1. It is a lot shorter to type @{push}. ;)
> > 
> >   2. Using @{push} works everywhere, even on my non-triangular repos,
> >      because it takes into account the push configuration. So it's a
> >      much nicer muscle-memory to acquire.
> 
> Makes sense, I’m all out of arguments here 😅 Please don’t take this the
> wrong way, but it reminds me of this XKCD: https://xkcd.com/1172/

Perhaps if the person in that comic was the one who implemented @{push} in the first place. ;)

That said, I really don't think my use case is exotic. I've set up "git push" in the only way that makes sense for my triangular setup. I just don't happen to use it that often, and I don't want to be reminded me unnecessarily of the relationship between each branch and its push destination.

> As long as this is turned on by default then I’m mostly happy. Maybe JCH
> and others can weigh in on if this needs to be this configurable or always
> on.

IMHO not making it configurable is a show-stopper, because without that it leaves people who don't like the new behavior with no escape hatch.

As to the default for that config option, I don't have a strong opinion. I'd lean towards "off", as I do not find the feature that compelling myself, and I don't recall ever hearing of it as a pain point for other Git users. But I also recognize that's based on vague vibes, and I think users are generally not very savvy about setting up triangular workflows in the first place.

-Peff
Jeff King· Jan 14, 2026, 21:10 UTC · re: Junio C Hamano · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 10:54:53AM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > And having the extra output from "git checkout" is just extra noise for
> > me, especially because it is easy to see only the second message (which
> > looks just like the upstream ahead/behind message, of course) and get
> > confused. The first time I saw it I thought I had misconfigured
> > something with my branch.
> 
> It now is clear to me that this should be _optional_, so that those
> who do really want extra output from the command should explicitly
> opt into the feature.  After all, any optional new feature that you
> must opt into by definition cannot regress end user experience for
> those who do not ;-)

True, but then it also cannot pleasantly surprise people who didn't realize they wanted it.

Having your user experience regressed and then tweaking a config option to fix it is not too bad. The deciding factor to me is whether more people will be pleasantly surprised or annoyed. ;) I don't have a strong sense there.

As a general principle, though, I think a reasonable path forward for any behavior change is:

  1. Implement the new behavior, hidden behind a config option.
  2. Wait a while to see how people like the new option, and shake out
     any bugs.
  3. If people like the option and are puzzled why it isn't the default,
     then flip the default on.

In other words, let the utility of the feature be proven in practice by people opting into it. There is a chicken-and-egg problem if they don't know about it, but if it is truly solving a problem people have, then hopefully some of them would look for a solution and find it.

End philosophical rambling. ;)
> At the same time, I suspect that extra comparison on top of what we
> already give against the @{upstream} may not be limited to what
> Harald implemented (is it essentially the same as specyfing @{push},
> or something else?).

I haven't been following the feature closely, but my understanding is that yes, it's basically "also compare to @{push} if it is not the same as @{upstream}".

Show 13 quoted lines
> I wonder if we can come up with a flexible and extensible notation to
> specify what branch(es) to compare with, so that we can use it as the
> value of this opt-in configuration variable?  Something like
> 
> 	[status] compareBranches = @{upstream} @{push}
> 
> signals that the current branch is compared against these two
> branches, and not having the configuration (i.e., traditional
> behaviour, which is left the default) would be equivalent to have
> 
> 	[status] compareBranches = @{upstream}
> 
> or something like that, perhaps?

Interesting. That is more flexible, though I'm not sure how useful that flexibility is. I guess you could imagine putting in a static branch. E.g., if you base your branches off of "master", might you want to show ahead/behind to "maint" or "next"? I have trouble imagining a workflow where I would want to do that often enough for git-status (and checkout) to do it automatically, though.

But assuming it suppresses duplicates, then yeah, this feels like a more flexible superset of the functionality.

-Peff
Ben Knoble· Jan 14, 2026, 21:38 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

Show 9 quoted lines
> Le 14 janv. 2026 à 02:59, Harald Nordgren <haraldnordgren@gmail.com> a écrit :
> 
> 
>> My workflow is different from Peff's, but it is similar along at least
>> one line: it's really convenient to have "git push" with no further
>> arguments (only possibly flags) to push my branch to a remote mirror.
> 
> Would you also like the status reporting to be off for your push branch?
> I asking because that's what Jeff is arguing for.
I’m somewhat indifferent (mildly for having it on by default), but I meant that you don’t want to suggest that in order to use your new feature you don’t want folks to have to break their existing workflows ;)
Ben Knoble· Jan 14, 2026, 21:38 UTC · re: Jeff King · lore

Re: Triangular workflow

Show 15 quoted lines
> Le 14 janv. 2026 à 11:31, Jeff King <peff@peff.net> a écrit :
> 
>>> Yeah, though @{push} is usually not explicitly configured in the same
>>> way @{upstream} is, but rather a consequence of how push.default and
>>> remote.pushdefault interact. But it was added for exactly this kind of
>>> triangular workflow. I sometimes will do stuff like:
>>> git range-diff origin @{push} HEAD
>> I imagine the same thing could be achieved with
>>  origin/$(git rev-parse --abbrev-ref HEAD)
> 
> Sure, but:
> 
> 1. It is a lot shorter to type @{push}. ;)
> 
> 2. Using @{push} works everywhere, even on my non-triangular repos,
Just so I’m clear, this is only with push.default=current, right? I could never make @{push} work otherwise.
Ben Knoble· Jan 14, 2026, 21:38 UTC · re: Jeff King · lore

Re: Triangular workflow

Show 41 quoted lines
> Le 14 janv. 2026 à 16:11, Jeff King <peff@peff.net> a écrit :
> 
> On Wed, Jan 14, 2026 at 10:54:53AM -0800, Junio C Hamano wrote:
> 
>> Jeff King <peff@peff.net> writes:
>>> And having the extra output from "git checkout" is just extra noise for
>>> me, especially because it is easy to see only the second message (which
>>> looks just like the upstream ahead/behind message, of course) and get
>>> confused. The first time I saw it I thought I had misconfigured
>>> something with my branch.
>> It now is clear to me that this should be _optional_, so that those
>> who do really want extra output from the command should explicitly
>> opt into the feature.  After all, any optional new feature that you
>> must opt into by definition cannot regress end user experience for
>> those who do not ;-)
> 
> True, but then it also cannot pleasantly surprise people who didn't
> realize they wanted it.
> 
> Having your user experience regressed and then tweaking a config option
> to fix it is not too bad. The deciding factor to me is whether more
> people will be pleasantly surprised or annoyed. ;) I don't have a strong
> sense there.
> 
> As a general principle, though, I think a reasonable path forward for
> any behavior change is:
> 
> 1. Implement the new behavior, hidden behind a config option.
> 
> 2. Wait a while to see how people like the new option, and shake out
>   any bugs.
> 
> 3. If people like the option and are puzzled why it isn't the default,
>   then flip the default on.
> 
> In other words, let the utility of the feature be proven in practice by
> people opting into it. There is a chicken-and-egg problem if they don't
> know about it, but if it is truly solving a problem people have, then
> hopefully some of them would look for a solution and find it.
> 
> End philosophical rambling. ;)
Agreed generally, but the chicken-egg goes 2 layers deep here due to triangular workflows ;)
I favor something similar to what Junio described but also including @{push} by default (and ignoring it if non-existent), so that folks discovering triangular workflows for the first time are easily able to see what is happening.
Us ”already triangular” squares are probably well-versed enough in Git to find and tweak the new feature if desired.
Idk though. I think more folks at work should try triangular flows, so I’m biased :p
Jeff King· Jan 14, 2026, 22:17 UTC · re: Ben Knoble · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 04:38:33PM -0500, Ben Knoble wrote:
Show 17 quoted lines
> > Le 14 janv. 2026 à 11:31, Jeff King <peff@peff.net> a écrit :
> > 
> >>> Yeah, though @{push} is usually not explicitly configured in the same
> >>> way @{upstream} is, but rather a consequence of how push.default and
> >>> remote.pushdefault interact. But it was added for exactly this kind of
> >>> triangular workflow. I sometimes will do stuff like:
> >>> git range-diff origin @{push} HEAD
> >> I imagine the same thing could be achieved with
> >>  origin/$(git rev-parse --abbrev-ref HEAD)
> > 
> > Sure, but:
> > 
> > 1. It is a lot shorter to type @{push}. ;)
> > 
> > 2. Using @{push} works everywhere, even on my non-triangular repos,
> 
> Just so I’m clear, this is only with push.default=current, right? I could never make @{push} work otherwise.

I always use push.default=current, though I think @{push} should work with other modes. E.g., with this setup:

  git checkout -b foo
  git clone . tmp
  cd tmp
  # for the sake of simplicity, our triangle goes to the same place ;)
  git remote add triangle ..
  git fetch triangle
  git config remote.pushdefault triangle
then doing:
  git -c push.default=current rev-parse --symbolic-full-name @{push}
and:
  git -c push.default=matching rev-parse --symbolic-full-name @{push}

should both point to refs/remotes/triangle/foo. Using "simple" will not work, because it demands that the upstream and the push destination are the same (so it doesn't really make sense in a triangular flow at all).

But in a non-triangular flow, it will happily point @{push} to the same as @{upstream}. I use a triangular flow for git.git, but most of my other repos are just personal projects, and I push/fetch from a single central source.

-Peff
Harald Nordgren· Jan 14, 2026, 23:08 UTC · re: Ben Knoble · lore

Triangular workflow

Show 7 quoted lines
> Agreed generally, but the chicken-egg goes 2 layers deep here due to triangular workflows ;)
> 
> I favor something similar to what Junio described but also including @{push} by default (and ignoring it if non-existent), so that folks discovering > triangular workflows for the first time are easily able to see what is happening.
> 
> Us ”already triangular” squares are probably well-versed enough in Git to find and tweak the new feature if desired.
> 
> Idk though. I think more folks at work should try triangular flows, so I’m biased :p

Ben has an excellent point about triangular workflows, and Jeff of course, your philosophical instincts seem correct to me, interesting to read!

I would think that most people don't have separate tracking and push branches to begin with. So it's not a lot of people would be bothered by having this be on by default. The people who know about triangular workflows will also be able to find how to turn this off.

Harald
Harald Nordgren· Jan 14, 2026, 23:12 UTC · re: Junio C Hamano · lore

Triangular workflow

Show 6 quoted lines
> I wonder if we can come up with a flexible and
> extensible notation to specify what branch(es) to compare with, so
> that we can use it as the value of this opt-in configuration
> variable?  Something like
> 
>         [status] compareBranches = @{upstream} @{push}

If we go with that, then it becomes trickier code-wise to show push/pull advice for the correct branches. But not impossible since we can check if branch is the same as @{upstream} or @{push}.

Philosophically, two main git commands are 'git push' and 'git pull'. So it makes perfect sense to me to signal that those two are special, and not allow other compareBranches.

Harald
Junio C Hamano· Jan 14, 2026, 23:31 UTC · re: Harald Nordgren · lore

Re: Triangular workflow

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 14 quoted lines
>> I wonder if we can come up with a flexible and
>> extensible notation to specify what branch(es) to compare with, so
>> that we can use it as the value of this opt-in configuration
>> variable?  Something like
>> 
>>         [status] compareBranches = @{upstream} @{push}
>
> If we go with that, then it becomes trickier code-wise to show push/pull
> advice for the correct branches. But not impossible since we can check if
> branch is the same as @{upstream} or @{push}.
>
> Philosophically, two main git commands are 'git push' and 'git pull'. So it
> makes perfect sense to me to signal that those two are special, and not
> allow other compareBranches.

You are falling into the same trap as what the folks who designed the original ahead/behind comparison did by limiting yourself to push and pull. They said object transfer always interacts with the @{upstream}, hence only comparison with that is sufficient and it makes sense not to allow comparing with anything else.

Until you started wishing that you want to also compare with @{push}, that is ;-).

A static point that has nothing to do with pushing and pulling (e.g., "the latest official release tag") may be something useful to compare with in certain workflows (presumably workflow of the maintainer type), so limiting our sights to object transfer like push and pull is likely to be a mistake.

Phillip Wood· Jan 15, 2026, 10:31 UTC · re: Jeff King · lore

Re: [PATCH v25 2/2] status: show comparison with push remote tracking branch

On 13/01/2026 17:03, Jeff King wrote:
Show 8 quoted lines
> 
> Can we make this configurable?
> 
> I build my daily driver off of the 'jch' branch, which now includes this
> series, and I've found that for my triangular workflow the ahead/behind
> for the push branch is just useless noise. I treat my push destination
> like a mirror, where I always just push up everything at the end of the
> day.

This is a slight tangent but if we're taking about making things configurable I've never found the actual values of the ahead/behind numbers particularly useful. In a triangular workflow all I'm really interested in is whether (a) my local branch is behind the upstream branch and if so is it going to be a pig to rebase it, and (b) my local branch matches the push branch (i.e. do I need to run "git push"). I don't really care exactly how many commits ahead or behind the local branch is, the counts are just distracting and don't really answer the "is it going to be a pig to rebase" question - the only way to answer that is to try it and see.

So as well as configuring whether we show the comparison the push branch it would be nice to be able to configure a simpler output as well. That does not need to be part of this series but perhaps we should design the configuration to be extendable.

Thanks
Phillip
Show 10 quoted lines
> I know that the output can be disabled with status.aheadbehind, but:
> 
>    1. I noticed this first via "git checkout", which does not have such a
>       flag (AFAIK).
> 
>    2. That flag would also disable the upstream branch ahead/behind
>       output, which I do find useful.
> 
> -Peff
> 
Junio C Hamano· Jan 15, 2026, 13:38 UTC · re: Phillip Wood · lore

Re: [PATCH v25 2/2] status: show comparison with push remote tracking branch

Phillip Wood <phillip.wood123@gmail.com> writes:
> So as well as configuring whether we show the comparison the push branch 
> it would be nice to be able to configure a simpler output as well. That 
> does not need to be part of this series but perhaps we should design the 
> configuration to be extendable.

So, there are orthogonal axes that might benefit from configurabiliity.

What other branch to compare (e.g., @{upstream}, @{push}, something else?) and how detailed comparison we want (e.g., only tell me when we are ahead with just a single bit, give me ahead/behind count, only tell me when we are behind with just a single bit, etc.).

There may be other interesting axes people may be able to come up with in the future. We do not have to be exhaustive on values on all axes on the first day, but it would be nice to have a fairly complete coverage of what axes matter to users, and it is a great suggestion that the level of details can be one. Thanks.

D. Ben Knoble· Jan 15, 2026, 16:17 UTC · re: Jeff King · lore

Re: Triangular workflow

On Wed, Jan 14, 2026 at 5:17 PM Jeff King <peff@peff.net> wrote:
Show 44 quoted lines
>
> On Wed, Jan 14, 2026 at 04:38:33PM -0500, Ben Knoble wrote:
>
> > > Le 14 janv. 2026 à 11:31, Jeff King <peff@peff.net> a écrit :
> > >
> > >>> Yeah, though @{push} is usually not explicitly configured in the same
> > >>> way @{upstream} is, but rather a consequence of how push.default and
> > >>> remote.pushdefault interact. But it was added for exactly this kind of
> > >>> triangular workflow. I sometimes will do stuff like:
> > >>> git range-diff origin @{push} HEAD
> > >> I imagine the same thing could be achieved with
> > >>  origin/$(git rev-parse --abbrev-ref HEAD)
> > >
> > > Sure, but:
> > >
> > > 1. It is a lot shorter to type @{push}. ;)
> > >
> > > 2. Using @{push} works everywhere, even on my non-triangular repos,
> >
> > Just so I’m clear, this is only with push.default=current, right? I could never make @{push} work otherwise.
>
> I always use push.default=current, though I think @{push} should work
> with other modes. E.g., with this setup:
>
>   git checkout -b foo
>   git clone . tmp
>   cd tmp
>
>   # for the sake of simplicity, our triangle goes to the same place ;)
>   git remote add triangle ..
>   git fetch triangle
>   git config remote.pushdefault triangle
>
> then doing:
>
>   git -c push.default=current rev-parse --symbolic-full-name @{push}
>
> and:
>
>   git -c push.default=matching rev-parse --symbolic-full-name @{push}
>
> should both point to refs/remotes/triangle/foo. Using "simple" will not
> work, because it demands that the upstream and the push destination are
> the same (so it doesn't really make sense in a triangular flow at all).

Gotcha. Yeah, simple (the default) doesn't work. I suppose upstream might, too. (I also have push.default=current globally, was just wondering about what the minimum was to enabled triangular workflows; see <https://lore.kernel.org/git/CALnO6CAUSU-Pq_r-WYm3o0to6H8MdqiYOuoKaRfL1PTt30VaoQ@mail.gmail.com/>.)

Show 6 quoted lines
> But in a non-triangular flow, it will happily point @{push} to the same
> as @{upstream}. I use a triangular flow for git.git, but most of my
> other repos are just personal projects, and I push/fetch from a single
> central source.
>
> -Peff

Ditto, yeah (_sometimes_ I actually do the triangle origin/main, local branch, origin/branch, so I almost always set upstream as origin/main anyways). Cool and thanks!

-- 
D. Ben Knoble
Jeff King· Jan 15, 2026, 19:53 UTC · re: Phillip Wood · lore

Re: [PATCH v25 2/2] status: show comparison with push remote tracking branch

On Thu, Jan 15, 2026 at 10:31:41AM +0000, Phillip Wood wrote:
Show 19 quoted lines
> On 13/01/2026 17:03, Jeff King wrote:
> > 
> > Can we make this configurable?
> > 
> > I build my daily driver off of the 'jch' branch, which now includes this
> > series, and I've found that for my triangular workflow the ahead/behind
> > for the push branch is just useless noise. I treat my push destination
> > like a mirror, where I always just push up everything at the end of the
> > day.
> 
> This is a slight tangent but if we're taking about making things
> configurable I've never found the actual values of the ahead/behind numbers
> particularly useful. In a triangular workflow all I'm really interested in
> is whether (a) my local branch is behind the upstream branch and if so is it
> going to be a pig to rebase it, and (b) my local branch matches the push
> branch (i.e. do I need to run "git push"). I don't really care exactly how
> many commits ahead or behind the local branch is, the counts are just
> distracting and don't really answer the "is it going to be a pig to rebase"
> question - the only way to answer that is to try it and see.

I mostly feel the same way. The only time I recall thinking the numbers were important is when they are wildly out of place. You "git checkout" a branch and it is 1500 commits ahead/behind or something, and you know you've screwed up setting the tracking branch somehow. ;)

> So as well as configuring whether we show the comparison the push branch it
> would be nice to be able to configure a simpler output as well. That does
> not need to be part of this series but perhaps we should design the
> configuration to be extendable.
Yeah, I agree with all of that.
-Peff
Harald Nordgren· Jan 18, 2026, 19:58 UTC · re: Junio C Hamano · lore

Triangular workflow

Show 29 quoted lines
> > And having the extra output from "git checkout" is just extra noise for
> > me, especially because it is easy to see only the second message (which
> > looks just like the upstream ahead/behind message, of course) and get
> > confused. The first time I saw it I thought I had misconfigured
> > something with my branch.
> 
> It now is clear to me that this should be _optional_, so that those
> who do really want extra output from the command should explicitly
> opt into the feature.  After all, any optional new feature that you
> must opt into by definition cannot regress end user experience for
> those who do not ;-)
> 
> At the same time, I suspect that extra comparison on top of what we
> already give against the @{upstream} may not be limited to what
> Harald implemented (is it essentially the same as specyfing @{push},
> or something else?).  I wonder if we can come up with a flexible and
> extensible notation to specify what branch(es) to compare with, so
> that we can use it as the value of this opt-in configuration
> variable?  Something like
> 
> 	[status] compareBranches = @{upstream} @{push}
> 
> signals that the current branch is compared against these two
> branches, and not having the configuration (i.e., traditional
> behaviour, which is left the default) would be equivalent to have
> 
> 	[status] compareBranches = @{upstream}
> 
> or something like that, perhaps?
I'm implemented this now. Please take a look at the latest patch!

But there seems to be a memory leak that I can't figure out after spending some hours running the CI over and over.

Harald
Harald Nordgren via GitGitGadget· Jan 18, 2026, 19:59 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v26 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 187 ++++++++++++++----
 t/t6040-tracking-info.sh         | 322 +++++++++++++++++++++++++++++++
 3 files changed, 492 insertions(+), 37 deletions(-)
base-commit: b5c409c40f1595e3e590760c6f14a16b6683e22c
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v26
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v26
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v25:
 1:  fd05c7b778 = 1:  27a46f8d9c refactor format_branch_comparison in preparation
 2:  fa744efc59 ! 2:  caa761f615 status: show comparison with push remote tracking branch
     @@ Metadata
      Author: Harald Nordgren <haraldnordgren@gmail.com>
      
       ## Commit message ##
     -    status: show comparison with push remote tracking branch
     +    status: add status.compareBranches config for multiple branch comparisons
      
     -    "git status" on a branch that follows a remote branch compares
     -    commits on the current branch and the remote-tracking branch it
     -    builds upon, to show "ahead", "behind", or "diverged" status.
     +    Add a new configuration variable `status.compareBranches` that allows
     +    users to specify a space-separated list of branches to compare against
     +    the current branch in `git status` output.
      
     -    When working on a feature branch that tracks a remote feature branch,
     -    but you also want to track progress relative to the push destination
     -    tracking branch (which may differ from the upstream branch), git status
     -    now shows an additional comparison.
     +    Each branch in the list can be:
     +    - A remote-tracking branch name (e.g., `origin/main`)
     +    - The special reference `@{upstream}` for the tracking branch
     +    - The special reference `@{push}` for the push destination
      
     -    When the upstream tracking branch differs from the push destination
     -    tracking branch, git status shows both the comparison with the upstream
     -    tracking branch (as before) and an additional comparison with the push
     -    destination tracking branch. The push branch comparison appears on a
     -    separate line after the upstream branch status, using the same format.
     +    When not configured, the default behavior is equivalent to setting
     +    `status.compareBranches = @{upstream}`, preserving backward compatibility.
      
     -    Example output when tracking origin/main but push destination is
     -    origin/feature:
     -        On branch feature
     -        Your branch and 'origin/main' have diverged,
     -        and have 3 and 1 different commits each, respectively.
     -          (use "git pull" if you want to integrate the remote branch with yours)
     +    The advice messages shown are context-aware:
     +    - "git pull" advice is shown only when comparing against @{upstream}
     +    - "git push" advice is shown only when comparing against @{push}
     +    - Divergence advice is shown for upstream branch comparisons
      
     -        Your branch is ahead of 'origin/feature' by 1 commit.
     -          (use "git push" to publish your local commits)
     +    This is useful for triangular workflows where the upstream tracking
     +    branch differs from the push destination, allowing users to see their
     +    status relative to both branches at once.
      
     -    The comparison is only shown when the push destination tracking branch
     -    differs from the upstream tracking branch, even if they are on the same
     -    remote.
     +    Example configuration:
     +        [status]
     +            compareBranches = @{upstream} @{push}
      
          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
      
     + ## Documentation/config/status.adoc ##
     +@@ Documentation/config/status.adoc: status.aheadBehind::
     + 	`--no-ahead-behind` by default in linkgit:git-status[1] for
     + 	non-porcelain status formats.  Defaults to true.
     + 
     ++status.compareBranches::
     ++	A space-separated list of branches to compare the current branch
     ++	against in linkgit:git-status[1]. Each branch specification can be
     ++	a remote-tracking branch name (e.g. `origin/main`), or a special
     ++	reference like `@{upstream}` or `@{push}`. For each branch in the
     ++	list, git status shows whether the current branch is ahead, behind,
     ++	or has diverged from that branch.
     +++
     ++If not set, the default behavior is equivalent to `@{upstream}`, which
     ++compares against the configured upstream tracking branch.
     +++
     ++Example:
     +++
     ++----
     ++[status]
     ++	compareBranches = origin/main origin/develop
     ++----
     +++
     ++This would show comparisons against both `origin/main` and `origin/develop`.
     ++
     + status.displayCommentPrefix::
     + 	If set to true, linkgit:git-status[1] will insert a comment
     + 	prefix before each output line (starting with
     +
       ## remote.c ##
      @@
       
     @@ remote.c
       struct counted_string {
       	size_t len;
       	const char *s;
     +@@ remote.c: static void branch_release(struct branch *branch)
     + 	free((char *)branch->refname);
     + 	free(branch->remote_name);
     + 	free(branch->pushremote_name);
     ++	free((char *)branch->push_tracking_ref);
     + 	merge_clear(branch);
     + }
     + 
      @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
       
     -+static char *get_remote_push_branch(struct branch *branch)
     ++static char *resolve_compare_branch(struct branch *branch, const char *name)
      +{
     -+	struct remote *remote;
     -+	const char *push_remote;
     -+	char *push_dst = NULL;
     -+	char *tracking_ref;
     -+	const char *resolved;
     ++	struct strbuf buf = STRBUF_INIT;
     ++	const char *resolved = NULL;
      +	char *ret;
      +
     -+	if (!branch)
     -+		return NULL;
     -+
     -+	push_remote = pushremote_for_branch(branch, NULL);
     -+	if (!push_remote)
     ++	if (!branch || !name)
      +		return NULL;
      +
     -+	remote = remotes_remote_get(the_repository, push_remote);
     -+	if (!remote)
     -+		return NULL;
     ++	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
     ++		resolved = branch_get_upstream(branch, NULL);
     ++	else if (!strcasecmp(name, "@{push}"))
     ++		resolved = branch_get_push(branch, NULL);
      +
     -+	push_dst = remote_ref_for_branch(branch, 1);
     -+	if (!push_dst) {
     -+		if (remote->push.nr)
     -+			return NULL;
     -+		push_dst = xstrdup(branch->refname);
     -+	}
     -+
     -+	tracking_ref = (char *)tracking_for_push_dest(remote, push_dst, NULL);
     -+	free(push_dst);
     -+
     -+	if (!tracking_ref)
     -+		return NULL;
     ++	if (resolved)
     ++		return xstrdup(resolved);
      +
     ++	strbuf_addf(&buf, "refs/remotes/%s", name);
      +	resolved = refs_resolve_ref_unsafe(
      +		get_main_ref_store(the_repository),
     -+		tracking_ref,
     ++		buf.buf,
      +		RESOLVE_REF_READING,
      +		NULL, NULL);
     -+
     -+	if (!resolved) {
     -+		free(tracking_ref);
     -+		return NULL;
     ++	if (resolved) {
     ++		ret = xstrdup(resolved);
     ++		strbuf_release(&buf);
     ++		return ret;
      +	}
      +
     -+	ret = xstrdup(resolved);
     -+	free(tracking_ref);
     -+	return ret;
     ++	strbuf_release(&buf);
     ++	return NULL;
      +}
      +
       static void format_branch_comparison(struct strbuf *sb,
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
       	}
      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 	const char *full_base;
     - 	char *base;
     - 	int upstream_is_gone = 0;
     -+	unsigned base_branch_flags = ENABLE_ADVICE_PULL | ENABLE_ADVICE_PUSH;
     -+	int push_ours, push_theirs, push_cmp_fetch;
     -+	char *full_push;
     -+	char *push = NULL;
     -+	unsigned push_branch_flags = 0;
     + 			 enum ahead_behind_flags abf,
     + 			 int show_divergence_advice)
     + {
     +-	int ours, theirs, cmp_fetch;
     +-	const char *full_base;
     +-	char *base;
     +-	int upstream_is_gone = 0;
     +-
     +-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     +-	if (cmp_fetch < 0) {
     +-		if (!full_base)
     +-			return 0;
     +-		upstream_is_gone = 1;
     ++	char *compare_branches_config = NULL;
     ++	struct string_list compare_branches = STRING_LIST_INIT_DUP;
     ++	struct string_list_item *item;
     ++	int reported = 0;
     ++	size_t i;
     ++	const char *upstream_ref;
     ++	const char *push_ref;
     ++
     ++	repo_config_get_string(the_repository, "status.comparebranches",
     ++			       &compare_branches_config);
     ++
     ++	if (compare_branches_config) {
     ++		string_list_split(&compare_branches, compare_branches_config,
     ++				  " ", -1);
     ++		string_list_remove_empty_items(&compare_branches, 0);
     ++	} else {
     ++		string_list_append(&compare_branches, "@{upstream}");
     + 	}
       
     - 	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
     - 	if (cmp_fetch < 0) {
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
     - 					    full_base, 0);
     +-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
     +-					    full_base, 0);
     ++	upstream_ref = branch_get_upstream(branch, NULL);
     ++	push_ref = branch_get_push(branch, NULL);
       
     -+	full_push = get_remote_push_branch(branch);
     -+	if (full_push) {
     -+		push = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
     -+						     full_push, 0);
     -+		if (push && base && strcmp(base, push)) {
     -+			push_cmp_fetch = stat_branch_pair(branch->refname, full_push,
     -+						   &push_ours, &push_theirs, abf);
     -+			if (push_cmp_fetch >= 0) {
     -+				base_branch_flags = ENABLE_ADVICE_PULL;
     -+				push_branch_flags = ENABLE_ADVICE_PUSH;
     +-	if (upstream_is_gone) {
     +-		strbuf_addf(sb,
     +-			_("Your branch is based on '%s', but the upstream is gone.\n"),
     +-			base);
     +-		if (advice_enabled(ADVICE_STATUS_HINTS))
     +-			strbuf_addstr(sb,
     +-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     +-	} else {
     +-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
     ++	for (i = 0; i < compare_branches.nr; i++) {
     ++		char *full_ref;
     ++		char *short_ref;
     ++		int ours, theirs, cmp;
     ++		int is_upstream, is_push;
     ++		unsigned flags = 0;
     ++
     ++		item = &compare_branches.items[i];
     ++		full_ref = resolve_compare_branch(branch, item->string);
     ++		if (!full_ref)
     ++			continue;
     ++
     ++		short_ref = refs_shorten_unambiguous_ref(
     ++			get_main_ref_store(the_repository), full_ref, 0);
     ++
     ++		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
     ++		is_push = push_ref && !strcmp(full_ref, push_ref);
     ++
     ++		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
     ++			is_push = 1;
     ++
     ++		cmp = stat_branch_pair(branch->refname, full_ref,
     ++				       &ours, &theirs, abf);
     ++
     ++		if (cmp < 0) {
     ++			if (is_upstream) {
     ++				strbuf_addf(sb,
     ++					_("Your branch is based on '%s', but the upstream is gone.\n"),
     ++					short_ref);
     ++				if (advice_enabled(ADVICE_STATUS_HINTS))
     ++					strbuf_addstr(sb,
     ++						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     ++				reported = 1;
      +			}
     ++			free(full_ref);
     ++			free(short_ref);
     ++			continue;
      +		}
     -+	}
      +
     - 	if (upstream_is_gone) {
     - 		strbuf_addf(sb,
     - 			_("Your branch is based on '%s', but the upstream is gone.\n"),
     -@@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
     - 			strbuf_addstr(sb,
     - 				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
     - 	} else {
     --		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
     -+		if (show_divergence_advice)
     -+			base_branch_flags |= ENABLE_ADVICE_DIVERGENCE;
     -+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf,
     -+					 base_branch_flags);
     -+	}
     -+
     -+	if (push_branch_flags & ENABLE_ADVICE_PUSH) {
     -+		strbuf_addstr(sb, "\n");
     -+		format_branch_comparison(sb, !push_cmp_fetch, push_ours, push_theirs, push, abf,
     -+					 push_branch_flags);
     ++		if (reported)
     ++			strbuf_addstr(sb, "\n");
     ++
     ++		if (is_upstream)
     ++			flags |= ENABLE_ADVICE_PULL;
     ++		if (is_push)
     ++			flags |= ENABLE_ADVICE_PUSH;
     ++		if (show_divergence_advice && is_upstream)
     ++			flags |= ENABLE_ADVICE_DIVERGENCE;
     ++		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
     ++					 abf, flags);
     ++		reported = 1;
     ++
     ++		free(full_ref);
     ++		free(short_ref);
       	}
       
     - 	free(base);
     -+	free(full_push);
     -+	free(push);
     - 	return 1;
     +-	free(base);
     +-	return 1;
     ++	string_list_clear(&compare_branches, 0);
     ++	free(compare_branches_config);
     ++	return reported;
       }
       
     + static int one_local_ref(const struct reference *ref, void *cb_data)
      
       ## t/t6040-tracking-info.sh ##
      @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status shows ahead of both origin/main and feature branch' '
     ++test_expect_success 'setup for compareBranches tests' '
     ++	(
     ++		cd test &&
     ++		git config push.default current &&
     ++		git config status.compareBranches "@{upstream} @{push}"
     ++	)
     ++'
     ++
     ++test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
      +	(
      +		cd test &&
      +		git checkout -b feature2 origin/main &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'checkout shows ahead of both origin/main and feature branch' '
     ++test_expect_success 'checkout with status.compareBranches shows both branches' '
      +	(
      +		cd test &&
      +		git checkout feature2 >../actual
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	)
      +'
      +
     -+test_expect_success 'status shows diverged from origin/main and ahead of feature branch' '
     ++test_expect_success 'status.compareBranches shows diverged and ahead' '
      +	(
      +		cd test &&
      +		git checkout feature4 &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status --no-ahead-behind shows diverged from origin/main and ahead of feature branch' '
     ++test_expect_success 'status --no-ahead-behind with status.compareBranches' '
      +	(
      +		cd test &&
      +		git checkout feature4 &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	)
      +'
      +
     -+test_expect_success 'status with upstream remote and push.default set to origin' '
     ++test_expect_success 'status.compareBranches with upstream and origin remotes' '
      +	(
      +		cd test &&
      +		git checkout -b feature5 upstream/main &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status with upstream remote and push.default set to origin and diverged' '
     ++test_expect_success 'status.compareBranches with upstream and origin remotes multiple compare branches' '
      +	(
      +		cd test &&
      +		git checkout -b feature6 upstream/main &&
     ++		git push origin &&
     ++		advance work &&
     ++		git -c status.compareBranches="upstream/main origin/feature6 origin/feature5" status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature6
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     ++
     ++	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status.compareBranches with diverged push branch' '
     ++	(
     ++		cd test &&
     ++		git checkout -b feature7 upstream/main &&
      +		advance work &&
      +		git push origin &&
      +		git reset --hard upstream/main &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature6
     ++	On branch feature7
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
     -+	Your branch and ${SQ}origin/feature6${SQ} have diverged,
     ++	Your branch and ${SQ}origin/feature7${SQ} have diverged,
      +	and have 1 and 1 different commits each, respectively.
      +
      +	nothing to commit, working tree clean
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status with upstream remote and push branch up to date' '
     ++test_expect_success 'status.compareBranches shows up to date branches' '
      +	(
      +		cd test &&
     -+		git checkout -b feature7 upstream/main &&
     ++		git checkout -b feature8 upstream/main &&
      +		git push origin &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature7
     ++	On branch feature8
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature8${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status --no-ahead-behind with upstream remote and push branch up to date' '
     ++test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
      +	(
      +		cd test &&
     -+		git checkout feature7 &&
     ++		git checkout feature8 &&
      +		git push origin &&
      +		git status --no-ahead-behind >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature7
     ++	On branch feature8
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature8${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'checkout shows push branch up to date' '
     ++test_expect_success 'checkout with status.compareBranches shows up to date' '
      +	(
      +		cd test &&
     -+		git checkout feature7 >../actual
     ++		git checkout feature8 >../actual
      +	) &&
      +	cat >expect <<-EOF &&
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature7${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature8${SQ}.
      +	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status with upstream ahead and push branch up to date' '
     ++test_expect_success 'status.compareBranches with upstream behind and push up to date' '
      +	(
      +		cd test &&
      +		git checkout -b ahead upstream/main &&
      +		advance work &&
      +		git push upstream HEAD &&
     -+		git checkout -b feature8 upstream/main &&
     ++		git checkout -b feature9 upstream/main &&
      +		git push origin &&
      +		git branch --set-upstream-to upstream/ahead &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature8
     ++	On branch feature9
      +	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
      +	  (use "git pull" to update your local branch)
      +
     -+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature9${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status shows remapped push refspec' '
     ++test_expect_success 'status.compareBranches with remapped push refspec' '
      +	(
      +		cd test &&
     -+		git checkout -b feature9 origin/main &&
     -+		git config remote.origin.push refs/heads/feature9:refs/heads/remapped &&
     ++		git checkout -b feature10 origin/main &&
     ++		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
      +		git push &&
      +		advance work &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature9
     ++	On branch feature10
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status shows remapped push refspec with upstream remote' '
     ++test_expect_success 'status.compareBranches with remapped push and upstream remote' '
      +	(
      +		cd test &&
     -+		git checkout -b feature10 upstream/main &&
     -+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
     ++		git checkout -b feature11 upstream/main &&
     ++		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
      +		git push origin &&
      +		advance work &&
      +		git status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
     -+	On branch feature10
     ++	On branch feature11
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	EOF
      +	test_cmp expect actual
      +'
     ++
     ++test_expect_success 'clean up after compareBranches tests' '
     ++	(
     ++		cd test &&
     ++		git config --unset status.compareBranches
     ++	)
     ++'
      +
       test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 18, 2026, 19:59 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v26 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 18, 2026, 19:59 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v26 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable `status.compareBranches` that allows users to specify a space-separated list of branches to compare against the current branch in `git status` output.

Each branch in the list can be:
- A remote-tracking branch name (e.g., `origin/main`)
- The special reference `@{upstream}` for the tracking branch
- The special reference `@{push}` for the push destination

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 157 ++++++++++++---
 t/t6040-tracking-info.sh         | 322 +++++++++++++++++++++++++++++++
 3 files changed, 470 insertions(+), 29 deletions(-)
Show changes to 3 files +470 −29

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..ab54f25fbc 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,26 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branches to compare the current branch
+	against in linkgit:git-status[1]. Each branch specification can be
+	a remote-tracking branch name (e.g. `origin/main`), or a special
+	reference like `@{upstream}` or `@{push}`. For each branch in the
+	list, git status shows whether the current branch is ahead, behind,
+	or has diverged from that branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+Example:
++
+----
+[status]
+	compareBranches = origin/main origin/develop
+----
++
+This would show comparisons against both `origin/main` and `origin/develop`.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index fd592ec659..1ea17cbbfa 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -272,6 +278,7 @@ static void branch_release(struct branch *branch)
 	free((char *)branch->refname);
 	free(branch->remote_name);
 	free(branch->pushremote_name);
+	free((char *)branch->push_tracking_ref);
 	merge_clear(branch);
 }
 
@@ -2230,13 +2237,53 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	struct strbuf buf = STRBUF_INIT;
+	const char *resolved = NULL;
+	char *ret;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
+		resolved = branch_get_upstream(branch, NULL);
+	else if (!strcasecmp(name, "@{push}"))
+		resolved = branch_get_push(branch, NULL);
+
+	if (resolved)
+		return xstrdup(resolved);
+
+	strbuf_addf(&buf, "refs/remotes/%s", name);
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		buf.buf,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+	if (resolved) {
+		ret = xstrdup(resolved);
+		strbuf_release(&buf);
+		return ret;
+	}
+
+	strbuf_release(&buf);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2292,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2301,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2312,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2325,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2292,34 +2338,87 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	char *compare_branches_config = NULL;
+	struct string_list compare_branches = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
+
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches_config);
+
+	if (compare_branches_config) {
+		string_list_split(&compare_branches, compare_branches_config,
+				  " ", -1);
+		string_list_remove_empty_items(&compare_branches, 0);
+	} else {
+		string_list_append(&compare_branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < compare_branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		item = &compare_branches.items[i];
+		full_ref = resolve_compare_branch(branch, item->string);
+		if (!full_ref)
+			continue;
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&compare_branches, 0);
+	free(compare_branches_config);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..ea899a5679 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,326 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for compareBranches tests' '
+	(
+		cd test &&
+		git config push.default current &&
+		git config status.compareBranches "@{upstream} @{push}"
+	)
+'
+
+test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes multiple compare branches' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		git push origin &&
+		advance work &&
+		git -c status.compareBranches="upstream/main origin/feature6 origin/feature5" status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	(
+		cd test &&
+		git checkout -b feature8 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	(
+		cd test &&
+		git checkout feature8 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	(
+		cd test &&
+		git checkout feature8 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	(
+		cd test &&
+		git checkout -b ahead upstream/main &&
+		advance work &&
+		git push upstream HEAD &&
+		git checkout -b feature9 upstream/main &&
+		git push origin &&
+		git branch --set-upstream-to upstream/ahead &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature10 origin/main &&
+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature11 upstream/main &&
+		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'clean up after compareBranches tests' '
+	(
+		cd test &&
+		git config --unset status.compareBranches
+	)
+'
+
 test_done
-- 
gitgitgadget
Jeff King· Jan 19, 2026, 05:14 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v26 2/2] status: add status.compareBranches config for multiple branch comparisons

On Sun, Jan 18, 2026 at 07:59:13PM +0000, Harald Nordgren via GitGitGadget wrote:
Show 8 quoted lines
> @@ -272,6 +278,7 @@ static void branch_release(struct branch *branch)
>  	free((char *)branch->refname);
>  	free(branch->remote_name);
>  	free(branch->pushremote_name);
> +	free((char *)branch->push_tracking_ref);
>  	merge_clear(branch);
>  }
>  

I don't think this line is right. The push_tracking_ref field is const because it may or may not be a newly allocated string. In particular, with push.default=upstream, it will be a copy of the string returned from branch_get_upstream(), which is covered by the merge_clear() call. So we'd get a double-free.

This is due to some memory-management hackery in e291c75a95 (remote.c: add branch_get_push, 2015-05-21). The good news is that not only is it not too hard to untangle this, but it is closely related to leaks you are seeing on your topic.

I'll post a series in a moment fixing both.
-Peff
Chris Torek· Jan 19, 2026, 05:58 UTC · re: Ben Knoble · lore

Re: Triangular workflow

(I'm somewhat behind all week so this is from Wednesday...)
Re: status.compareBranch (or similar): I like the idea; I'm not sure what sort
of details might be needed in the end though.
On Wed, Jan 14, 2026 at 1:42 PM Ben Knoble <ben.knoble@gmail.com> wrote:
> I favor something similar to what Junio described but also including @{push} by default (and ignoring it if non-existent), so that folks discovering triangular workflows for the first time are easily able to see what is happening.

This also seems to me likely to be the right default. It's useful for a lot of GitHub and similar forges, where you send fixes upstream by first forking some official repository and then cloning your fork (e.g., to a laptop), setting up your local clone (on laptop) to have two remotes: the official repository, and your fork, both on the same forge.

It's a little annoying to have to deal with *three* potential "upstream" repos, if you need to back up your local work to a corporate server or forge *plus* a push-for-pull-request at the forge as well, of course. But then at least the "compare with all three" option becomes available. How you wish to spell the default push location is then up to you :-)

Chris
Harald Nordgren· Jan 20, 2026, 08:35 UTC · re: Chris Torek · lore

Triangular workflow

>> I favor something similar to what Junio described but also including
>> @{push} by default (and ignoring it if non-existent), so that folks
>> discovering triangular workflows for the first time are easily able to
>> see what is happening.
Show 5 quoted lines
> This also seems to me likely to be the right default. It's useful for a lot of
> GitHub and similar forges, where you send fixes upstream by first forking
> some official repository and then cloning your fork (e.g., to a laptop), setting
> up your local clone (on laptop) to have two remotes: the official repository,
> and your fork, both on the same forge.

I agree with this too, would like to make '@{upstream} @{push}' the default.

Harald
Harald Nordgren· Jan 20, 2026, 09:49 UTC · re: Jeff King · lore

Memory leak

Show 6 quoted lines
> This is due to some memory-management hackery in e291c75a95 (remote.c:
> add branch_get_push, 2015-05-21). The good news is that not only is it
> not too hard to untangle this, but it is closely related to leaks you
> are seeing on your topic.
> 
> I'll post a series in a moment fixing both.

This seems to not have helped my memory leak. I squashed and applied your commits on top of my work as a separate pull request on GitHub, the CI leak tests are still failing:

https://github.com/git/git/pull/2170
Harald
Junio C Hamano· Jan 20, 2026, 21:42 UTC · re: Harald Nordgren · lore

Re: Memory leak

Harald Nordgren <haraldnordgren@gmail.com> writes:
> My bad, it does fix it! I think I applied your patch incorrectly the first
> time around.

I've queued your v26 on top of a merge of Peff's leakfix patches into 'master'. It hopefully will appear in today's pushout.

Thanks, both.
Junio C Hamano· Jan 21, 2026, 18:47 UTC · re: Junio C Hamano · lore

Re: Memory leak

Junio C Hamano <gitster@pobox.com> writes:
Show 9 quoted lines
> Harald Nordgren <haraldnordgren@gmail.com> writes:
>
>> My bad, it does fix it! I think I applied your patch incorrectly the first
>> time around.
>
> I've queued your v26 on top of a merge of Peff's leakfix patches
> into 'master'.  It hopefully will appear in today's pushout.
>
> Thanks, both.
So are we all happy with v26 of the topic?
Jeff King· Jan 21, 2026, 20:49 UTC · re: Junio C Hamano · lore

Re: Memory leak

On Wed, Jan 21, 2026 at 10:47:28AM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > Harald Nordgren <haraldnordgren@gmail.com> writes:
> >
> >> My bad, it does fix it! I think I applied your patch incorrectly the first
> >> time around.
> >
> > I've queued your v26 on top of a merge of Peff's leakfix patches
> > into 'master'.  It hopefully will appear in today's pushout.
> >
> > Thanks, both.
> 
> So are we all happy with v26 of the topic?

It addresses my main concern, which was having a way to turn it off. ;) (And I see it is not even changed by default now).

One thing that puzzled me a bit is that the show_divergence_advice flag is only respected for "use git pull if you want to integrate..." hint and not other advice. But that was true even before these patches. It feels funny to me that it is treated differently than the other advice. But I think we can ignore that for the purposes of this topic. If anything it is a defect in b6f3da5132 (wt-status: don't show divergence advice when committing, 2023-07-12), which perhaps should have covered more code paths.

Other than that, I did not see anything wrong from my (admittedly pretty cursory) read.

-Peff
Harald Nordgren· Jan 22, 2026, 15:03 UTC · re: Junio C Hamano · lore

Memory leak

I realized now that my code would benefit from branch de-duplication. When running it from the main branch (which has upsteam+push = origin/main) and my git setting is this which I intended to run from now on:

    git config --global status.compareBranches "@{upstream} @{push}"

then it reports status twice. I have a small fix ready and will push it out once the CI passes on GitHub.

Harald
Harald Nordgren via GitGitGadget· Jan 22, 2026, 15:37 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 192 ++++++++++++++----
 t/t6040-tracking-info.sh         | 337 +++++++++++++++++++++++++++++++
 3 files changed, 512 insertions(+), 37 deletions(-)
base-commit: b5c409c40f1595e3e590760c6f14a16b6683e22c
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v27
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v27
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v26:
 1:  27a46f8d9c = 1:  27a46f8d9c refactor format_branch_comparison in preparation
 2:  caa761f615 ! 2:  0993420fc1 status: add status.compareBranches config for multiple branch comparisons
     @@ remote.c
       struct counted_string {
       	size_t len;
       	const char *s;
     -@@ remote.c: static void branch_release(struct branch *branch)
     - 	free((char *)branch->refname);
     - 	free(branch->remote_name);
     - 	free(branch->pushremote_name);
     -+	free((char *)branch->push_tracking_ref);
     - 	merge_clear(branch);
     - }
     - 
      @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
       	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
       }
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      -	const char *full_base;
      -	char *base;
      -	int upstream_is_gone = 0;
     --
     ++	char *compare_branches = NULL;
     ++	struct string_list branches = STRING_LIST_INIT_DUP;
     ++	struct string_list processed_refs = STRING_LIST_INIT_DUP;
     ++	int reported = 0;
     ++	size_t i;
     ++	const char *upstream_ref;
     ++	const char *push_ref;
     + 
      -	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
      -	if (cmp_fetch < 0) {
      -		if (!full_base)
      -			return 0;
      -		upstream_is_gone = 1;
     -+	char *compare_branches_config = NULL;
     -+	struct string_list compare_branches = STRING_LIST_INIT_DUP;
     -+	struct string_list_item *item;
     -+	int reported = 0;
     -+	size_t i;
     -+	const char *upstream_ref;
     -+	const char *push_ref;
     -+
      +	repo_config_get_string(the_repository, "status.comparebranches",
     -+			       &compare_branches_config);
     ++			       &compare_branches);
      +
     -+	if (compare_branches_config) {
     -+		string_list_split(&compare_branches, compare_branches_config,
     -+				  " ", -1);
     -+		string_list_remove_empty_items(&compare_branches, 0);
     ++	if (compare_branches) {
     ++		string_list_split(&branches, compare_branches, " ", -1);
     ++		string_list_remove_empty_items(&branches, 0);
      +	} else {
     -+		string_list_append(&compare_branches, "@{upstream}");
     ++		string_list_append(&branches, "@{upstream}");
       	}
       
      -	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      -				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
      -	} else {
      -		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
     -+	for (i = 0; i < compare_branches.nr; i++) {
     ++	for (i = 0; i < branches.nr; i++) {
      +		char *full_ref;
      +		char *short_ref;
      +		int ours, theirs, cmp;
      +		int is_upstream, is_push;
      +		unsigned flags = 0;
      +
     -+		item = &compare_branches.items[i];
     -+		full_ref = resolve_compare_branch(branch, item->string);
     ++		full_ref = resolve_compare_branch(branch,
     ++						  branches.items[i].string);
      +		if (!full_ref)
      +			continue;
      +
     ++		if (string_list_has_string(&processed_refs, full_ref)) {
     ++			free(full_ref);
     ++			continue;
     ++		}
     ++		string_list_insert(&processed_refs, full_ref);
     ++
      +		short_ref = refs_shorten_unambiguous_ref(
      +			get_main_ref_store(the_repository), full_ref, 0);
      +
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
       
      -	free(base);
      -	return 1;
     -+	string_list_clear(&compare_branches, 0);
     -+	free(compare_branches_config);
     ++	string_list_clear(&branches, 0);
     ++	string_list_clear(&processed_refs, 0);
     ++	free(compare_branches);
      +	return reported;
       }
       
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	)
      +'
      +
     ++test_expect_success 'status.compareBranches from upstream has no duplicates' '
     ++	(
     ++		cd test &&
     ++		git checkout main &&
     ++		git status >../actual
     ++	) &&
     ++	cat >expect <<-EOF &&
     ++	On branch main
     ++	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 'status.compareBranches shows ahead of both upstream and push branch' '
      +	(
      +		cd test &&
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 22, 2026, 15:37 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v27 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 22, 2026, 15:37 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable `status.compareBranches` that allows users to specify a space-separated list of branches to compare against the current branch in `git status` output.

Each branch in the list can be:
- A remote-tracking branch name (e.g., `origin/main`)
- The special reference `@{upstream}` for the tracking branch
- The special reference `@{push}` for the push destination

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 160 ++++++++++++---
 t/t6040-tracking-info.sh         | 337 +++++++++++++++++++++++++++++++
 3 files changed, 489 insertions(+), 28 deletions(-)
Show changes to 3 files +489 −28

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..ab54f25fbc 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,26 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branches to compare the current branch
+	against in linkgit:git-status[1]. Each branch specification can be
+	a remote-tracking branch name (e.g. `origin/main`), or a special
+	reference like `@{upstream}` or `@{push}`. For each branch in the
+	list, git status shows whether the current branch is ahead, behind,
+	or has diverged from that branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+Example:
++
+----
+[status]
+	compareBranches = origin/main origin/develop
+----
++
+This would show comparisons against both `origin/main` and `origin/develop`.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index fd592ec659..e256cc9b81 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2230,13 +2236,53 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	struct strbuf buf = STRBUF_INIT;
+	const char *resolved = NULL;
+	char *ret;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
+		resolved = branch_get_upstream(branch, NULL);
+	else if (!strcasecmp(name, "@{push}"))
+		resolved = branch_get_push(branch, NULL);
+
+	if (resolved)
+		return xstrdup(resolved);
+
+	strbuf_addf(&buf, "refs/remotes/%s", name);
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		buf.buf,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+	if (resolved) {
+		ret = xstrdup(resolved);
+		strbuf_release(&buf);
+		return ret;
+	}
+
+	strbuf_release(&buf);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2291,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2300,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2311,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2324,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2292,34 +2337,93 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
+	char *compare_branches = NULL;
+	struct string_list branches = STRING_LIST_INIT_DUP;
+	struct string_list processed_refs = STRING_LIST_INIT_DUP;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
 
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches);
+
+	if (compare_branches) {
+		string_list_split(&branches, compare_branches, " ", -1);
+		string_list_remove_empty_items(&branches, 0);
+	} else {
+		string_list_append(&branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		full_ref = resolve_compare_branch(branch,
+						  branches.items[i].string);
+		if (!full_ref)
+			continue;
+
+		if (string_list_has_string(&processed_refs, full_ref)) {
+			free(full_ref);
+			continue;
+		}
+		string_list_insert(&processed_refs, full_ref);
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&branches, 0);
+	string_list_clear(&processed_refs, 0);
+	free(compare_branches);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..f7859d2e5a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,341 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for compareBranches tests' '
+	(
+		cd test &&
+		git config push.default current &&
+		git config status.compareBranches "@{upstream} @{push}"
+	)
+'
+
+test_expect_success 'status.compareBranches from upstream has no duplicates' '
+	(
+		cd test &&
+		git checkout main &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches shows ahead of both upstream and push branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes multiple compare branches' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		git push origin &&
+		advance work &&
+		git -c status.compareBranches="upstream/main origin/feature6 origin/feature5" status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	(
+		cd test &&
+		git checkout -b feature8 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	(
+		cd test &&
+		git checkout feature8 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	(
+		cd test &&
+		git checkout feature8 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	(
+		cd test &&
+		git checkout -b ahead upstream/main &&
+		advance work &&
+		git push upstream HEAD &&
+		git checkout -b feature9 upstream/main &&
+		git push origin &&
+		git branch --set-upstream-to upstream/ahead &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature10 origin/main &&
+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature11 upstream/main &&
+		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'clean up after compareBranches tests' '
+	(
+		cd test &&
+		git config --unset status.compareBranches
+	)
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 22, 2026, 18:19 UTC · re: Harald Nordgren · lore

Re: Memory leak

Harald Nordgren <haraldnordgren@gmail.com> writes:
> I realized now that my code would benefit from branch
> de-duplication.
... meaning that ...
Show 5 quoted lines
> When
> running it from the main branch (which has upsteam+push = origin/main) and
> my git setting is this which I intended to run from now on:
>
>     git config --global status.compareBranches "@{upstream} @{push}"

... when @{upstream} and @{push} ends up being the same branch? Yes, if that happens, it would be irritating for the users if we do not deduplicate.

Thanks.
Junio C Hamano· Jan 22, 2026, 18:53 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:

Before these list of CC's (which does not seem to have any effect, by the way. The message is going only to the list with your personal address on CC: and to nobody else), ...

Show 6 quoted lines
> cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
> ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net
> cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk"
> kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com
> cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc:
> Jeff King peff@peff.net
... please summarize
 (1) what this series of patches are about, to help those who
     encounter these patches for the first time, and
 (2) what changed in this iteration (v27) relative to the previous
     iteration (v26).

I happen to have seen your other message so I can guess this is about deduping when more than one comparison target is listed and some happen to become the same branches, but others may not have seen that other message, and it is not helpful to just dump the range-diff and force them to read it to deduce what you did.

Thanks.
Show 140 quoted lines
> Harald Nordgren (2):
>   refactor format_branch_comparison in preparation
>   status: add status.compareBranches config for multiple branch
>     comparisons
>
>  Documentation/config/status.adoc |  20 ++
>  remote.c                         | 192 ++++++++++++++----
>  t/t6040-tracking-info.sh         | 337 +++++++++++++++++++++++++++++++
>  3 files changed, 512 insertions(+), 37 deletions(-)
>
>
> base-commit: b5c409c40f1595e3e590760c6f14a16b6683e22c
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v27
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v27
> Pull-Request: https://github.com/git/git/pull/2138
>
> Range-diff vs v26:
>
>  1:  27a46f8d9c = 1:  27a46f8d9c refactor format_branch_comparison in preparation
>  2:  caa761f615 ! 2:  0993420fc1 status: add status.compareBranches config for multiple branch comparisons
>      @@ remote.c
>        struct counted_string {
>        	size_t len;
>        	const char *s;
>      -@@ remote.c: static void branch_release(struct branch *branch)
>      - 	free((char *)branch->refname);
>      - 	free(branch->remote_name);
>      - 	free(branch->pushremote_name);
>      -+	free((char *)branch->push_tracking_ref);
>      - 	merge_clear(branch);
>      - }
>      - 
>       @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
>        	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
>        }
>      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
>       -	const char *full_base;
>       -	char *base;
>       -	int upstream_is_gone = 0;
>      --
>      ++	char *compare_branches = NULL;
>      ++	struct string_list branches = STRING_LIST_INIT_DUP;
>      ++	struct string_list processed_refs = STRING_LIST_INIT_DUP;
>      ++	int reported = 0;
>      ++	size_t i;
>      ++	const char *upstream_ref;
>      ++	const char *push_ref;
>      + 
>       -	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
>       -	if (cmp_fetch < 0) {
>       -		if (!full_base)
>       -			return 0;
>       -		upstream_is_gone = 1;
>      -+	char *compare_branches_config = NULL;
>      -+	struct string_list compare_branches = STRING_LIST_INIT_DUP;
>      -+	struct string_list_item *item;
>      -+	int reported = 0;
>      -+	size_t i;
>      -+	const char *upstream_ref;
>      -+	const char *push_ref;
>      -+
>       +	repo_config_get_string(the_repository, "status.comparebranches",
>      -+			       &compare_branches_config);
>      ++			       &compare_branches);
>       +
>      -+	if (compare_branches_config) {
>      -+		string_list_split(&compare_branches, compare_branches_config,
>      -+				  " ", -1);
>      -+		string_list_remove_empty_items(&compare_branches, 0);
>      ++	if (compare_branches) {
>      ++		string_list_split(&branches, compare_branches, " ", -1);
>      ++		string_list_remove_empty_items(&branches, 0);
>       +	} else {
>      -+		string_list_append(&compare_branches, "@{upstream}");
>      ++		string_list_append(&branches, "@{upstream}");
>        	}
>        
>       -	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
>      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
>       -				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
>       -	} else {
>       -		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
>      -+	for (i = 0; i < compare_branches.nr; i++) {
>      ++	for (i = 0; i < branches.nr; i++) {
>       +		char *full_ref;
>       +		char *short_ref;
>       +		int ours, theirs, cmp;
>       +		int is_upstream, is_push;
>       +		unsigned flags = 0;
>       +
>      -+		item = &compare_branches.items[i];
>      -+		full_ref = resolve_compare_branch(branch, item->string);
>      ++		full_ref = resolve_compare_branch(branch,
>      ++						  branches.items[i].string);
>       +		if (!full_ref)
>       +			continue;
>       +
>      ++		if (string_list_has_string(&processed_refs, full_ref)) {
>      ++			free(full_ref);
>      ++			continue;
>      ++		}
>      ++		string_list_insert(&processed_refs, full_ref);
>      ++
>       +		short_ref = refs_shorten_unambiguous_ref(
>       +			get_main_ref_store(the_repository), full_ref, 0);
>       +
>      @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
>        
>       -	free(base);
>       -	return 1;
>      -+	string_list_clear(&compare_branches, 0);
>      -+	free(compare_branches_config);
>      ++	string_list_clear(&branches, 0);
>      ++	string_list_clear(&processed_refs, 0);
>      ++	free(compare_branches);
>       +	return reported;
>        }
>        
>      @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
>       +	)
>       +'
>       +
>      ++test_expect_success 'status.compareBranches from upstream has no duplicates' '
>      ++	(
>      ++		cd test &&
>      ++		git checkout main &&
>      ++		git status >../actual
>      ++	) &&
>      ++	cat >expect <<-EOF &&
>      ++	On branch main
>      ++	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 'status.compareBranches shows ahead of both upstream and push branch' '
>       +	(
>       +		cd test &&
Jeff King· Jan 22, 2026, 19:07 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

On Thu, Jan 22, 2026 at 03:37:20PM +0000, Harald Nordgren via GitGitGadget wrote:
Show 5 quoted lines
> +		if (string_list_has_string(&processed_refs, full_ref)) {
> +			free(full_ref);
> +			continue;
> +		}
> +		string_list_insert(&processed_refs, full_ref);

Using a string list like this is quadratic. It probably doesn't matter too much here since we wouldn't expect the list of configured branches to be very long, though. But a strset is probably the better tool, like the diff below (note that its "add" can be used as a single operation to insert and check).

I don't know if it's worth re-rolling for this or not. I doubt anybody would hit it in practice, but I'd be more concerned about people auditing for accidentally-quadratic uses of string_list and stumbling upon it.

-Peff
Show changes to remote.c +3 −4
diff --git a/remote.c b/remote.c
index e256cc9b81..1071a23567 100644
--- a/remote.c
+++ b/remote.c
@@ -2339,7 +2339,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 {
 	char *compare_branches = NULL;
 	struct string_list branches = STRING_LIST_INIT_DUP;
-	struct string_list processed_refs = STRING_LIST_INIT_DUP;
+	struct strset processed_refs = STRSET_INIT;
 	int reported = 0;
 	size_t i;
 	const char *upstream_ref;
@@ -2370,11 +2370,10 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		if (!full_ref)
 			continue;
 
-		if (string_list_has_string(&processed_refs, full_ref)) {
+		if (!strset_add(&processed_refs, full_ref)) {
 			free(full_ref);
 			continue;
 		}
-		string_list_insert(&processed_refs, full_ref);
 
 		short_ref = refs_shorten_unambiguous_ref(
 			get_main_ref_store(the_repository), full_ref, 0);
@@ -2421,7 +2420,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 	}
 
 	string_list_clear(&branches, 0);
-	string_list_clear(&processed_refs, 0);
+	strset_clear(&processed_refs);
 	free(compare_branches);
 	return reported;
 }
Harald Nordgren· Jan 22, 2026, 19:09 UTC · re: Junio C Hamano · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

> Before these list of CC's (which does not seem to have any effect,
> by the way.  The message is going only to the list with your
> personal address on CC: and to nobody else), ...

Hmm, I always just the 'git send-email' snippet from example here: https://lore.kernel.org/git/xmqqbjilo6h9.fsf@gitster.g/, which for this message is:

  git send-email \
    --in-reply-to=xmqqbjilo6h9.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY
Show 13 quoted lines
> ... please summarize
> 
>  (1) what this series of patches are about, to help those who
>      encounter these patches for the first time, and
> 
>  (2) what changed in this iteration (v27) relative to the previous
>      iteration (v26).
> 
> I happen to have seen your other message so I can guess this is
> about deduping when more than one comparison target is listed and
> some happen to become the same branches, but others may not have
> seen that other message, and it is not helpful to just dump the
> range-diff and force them to read it to deduce what you did.

Fair point, but I always always use GitGitGadget to submit patches to here, I'm sure sure if there is a way to do that from there?

I just write '/submit' to create the whole patch: https://github.com/git/git/pull/2138#issuecomment-3785055635

Harald
Junio C Hamano· Jan 22, 2026, 19:20 UTC · re: Harald Nordgren · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 16 quoted lines
>> Before these list of CC's (which does not seem to have any effect,
>> by the way.  The message is going only to the list with your
>> personal address on CC: and to nobody else), ...
>
> Hmm, I always just the 'git send-email' snippet from example here:
> https://lore.kernel.org/git/xmqqbjilo6h9.fsf@gitster.g/, which for this
> message is:
>
>   git send-email \
>     --in-reply-to=xmqqbjilo6h9.fsf@gitster.g \
>     --to=gitster@pobox.com \
>     --cc=git@vger.kernel.org \
>     --cc=gitgitgadget@gmail.com \
>     --cc=haraldnordgren@gmail.com \
>     --cc=peff@peff.net \
>     /path/to/YOUR_REPLY

But the patch you sent was not using "git send-email", was it? I am not sure where those annoying and unused CC addresses comes from, but it would be from GGG, not from "git send-email", I suspect.

Show 16 quoted lines
>> ... please summarize
>> 
>>  (1) what this series of patches are about, to help those who
>>      encounter these patches for the first time, and
>> 
>>  (2) what changed in this iteration (v27) relative to the previous
>>      iteration (v26).
>> 
>> I happen to have seen your other message so I can guess this is
>> about deduping when more than one comparison target is listed and
>> some happen to become the same branches, but others may not have
>> seen that other message, and it is not helpful to just dump the
>> range-diff and force them to read it to deduce what you did.
>
> Fair point, but I always always use GitGitGadget to submit patches to here,
> I'm sure sure if there is a way to do that from there?

Sorry, I do not work on or use GGG; other users of it may be able to chime in?

Thanks.
Harald Nordgren· Jan 22, 2026, 19:22 UTC · re: Jeff King · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

Show 10 quoted lines
> Using a string list like this is quadratic. It probably doesn't matter
> too much here since we wouldn't expect the list of configured branches
> to be very long, though. But a strset is probably the better tool, like
> the diff below (note that its "add" can be used as a single operation to
> insert and check).
> 
> I don't know if it's worth re-rolling for this or not. I doubt anybody
> would hit it in practice, but I'd be more concerned about people
> auditing for accidentally-quadratic uses of string_list and stumbling
> upon it.

Sounds like a good change. Very nice to have insert-and-check as a single operation!

I'll update it after passing CI on GitHub -- I'm running CI from two separate GitHub pull requests, because one has your memory leak fix on top to allow the leak tests to not fail. 🤗

Harald
Harald Nordgren· Jan 22, 2026, 19:24 UTC · re: Junio C Hamano · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

> But the patch you sent was not using "git send-email", was it?  I am
> not sure where those annoying and unused CC addresses comes from,
> but it would be from GGG, not from "git send-email", I suspect.
Yes you are right, I misunderstood your point about the CC's.
Patches are 100% via GGG.
Harald
Harald Nordgren via GitGitGadget· Jan 22, 2026, 20:07 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v28 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 191 ++++++++++++++----
 t/t6040-tracking-info.sh         | 337 +++++++++++++++++++++++++++++++
 3 files changed, 511 insertions(+), 37 deletions(-)
base-commit: 83a69f19359e6d9bc980563caca38b2b5729808c
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v28
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v28
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v27:
 1:  27a46f8d9c = 1:  f3c8c782b0 refactor format_branch_comparison in preparation
 2:  0993420fc1 ! 2:  067978dd09 status: add status.compareBranches config for multiple branch comparisons
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      -	int upstream_is_gone = 0;
      +	char *compare_branches = NULL;
      +	struct string_list branches = STRING_LIST_INIT_DUP;
     -+	struct string_list processed_refs = STRING_LIST_INIT_DUP;
     ++	struct strset processed_refs = STRSET_INIT;
      +	int reported = 0;
      +	size_t i;
      +	const char *upstream_ref;
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      +		if (!full_ref)
      +			continue;
      +
     -+		if (string_list_has_string(&processed_refs, full_ref)) {
     ++		if (!strset_add(&processed_refs, full_ref)) {
      +			free(full_ref);
      +			continue;
      +		}
     -+		string_list_insert(&processed_refs, full_ref);
      +
      +		short_ref = refs_shorten_unambiguous_ref(
      +			get_main_ref_store(the_repository), full_ref, 0);
     @@ remote.c: int format_tracking_info(struct branch *branch, struct strbuf *sb,
      -	free(base);
      -	return 1;
      +	string_list_clear(&branches, 0);
     -+	string_list_clear(&processed_refs, 0);
     ++	strset_clear(&processed_refs);
      +	free(compare_branches);
      +	return reported;
       }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 22, 2026, 20:07 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v28 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index b756ff6f15..fd592ec659 100644
--- a/remote.c
+++ b/remote.c
@@ -2230,42 +2230,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2274,7 +2253,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2298,12 +2277,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Jan 22, 2026, 20:07 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable `status.compareBranches` that allows users to specify a space-separated list of branches to compare against the current branch in `git status` output.

Each branch in the list can be:
- A remote-tracking branch name (e.g., `origin/main`)
- The special reference `@{upstream}` for the tracking branch
- The special reference `@{push}` for the push destination

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  20 ++
 remote.c                         | 159 ++++++++++++---
 t/t6040-tracking-info.sh         | 337 +++++++++++++++++++++++++++++++
 3 files changed, 488 insertions(+), 28 deletions(-)
Show changes to 3 files +488 −28

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..ab54f25fbc 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,26 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branches to compare the current branch
+	against in linkgit:git-status[1]. Each branch specification can be
+	a remote-tracking branch name (e.g. `origin/main`), or a special
+	reference like `@{upstream}` or `@{push}`. For each branch in the
+	list, git status shows whether the current branch is ahead, behind,
+	or has diverged from that branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+Example:
++
+----
+[status]
+	compareBranches = origin/main origin/develop
+----
++
+This would show comparisons against both `origin/main` and `origin/develop`.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index fd592ec659..1071a23567 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2230,13 +2236,53 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	struct strbuf buf = STRBUF_INIT;
+	const char *resolved = NULL;
+	char *ret;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
+		resolved = branch_get_upstream(branch, NULL);
+	else if (!strcasecmp(name, "@{push}"))
+		resolved = branch_get_push(branch, NULL);
+
+	if (resolved)
+		return xstrdup(resolved);
+
+	strbuf_addf(&buf, "refs/remotes/%s", name);
+	resolved = refs_resolve_ref_unsafe(
+		get_main_ref_store(the_repository),
+		buf.buf,
+		RESOLVE_REF_READING,
+		NULL, NULL);
+	if (resolved) {
+		ret = xstrdup(resolved);
+		strbuf_release(&buf);
+		return ret;
+	}
+
+	strbuf_release(&buf);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2245,7 +2291,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2254,7 +2300,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2265,7 +2311,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2278,8 +2324,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2292,34 +2337,92 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
+	char *compare_branches = NULL;
+	struct string_list branches = STRING_LIST_INIT_DUP;
+	struct strset processed_refs = STRSET_INIT;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
 
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches);
+
+	if (compare_branches) {
+		string_list_split(&branches, compare_branches, " ", -1);
+		string_list_remove_empty_items(&branches, 0);
+	} else {
+		string_list_append(&branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		full_ref = resolve_compare_branch(branch,
+						  branches.items[i].string);
+		if (!full_ref)
+			continue;
+
+		if (!strset_add(&processed_refs, full_ref)) {
+			free(full_ref);
+			continue;
+		}
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&branches, 0);
+	strset_clear(&processed_refs);
+	free(compare_branches);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..f7859d2e5a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,341 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for compareBranches tests' '
+	(
+		cd test &&
+		git config push.default current &&
+		git config status.compareBranches "@{upstream} @{push}"
+	)
+'
+
+test_expect_success 'status.compareBranches from upstream has no duplicates' '
+	(
+		cd test &&
+		git checkout main &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches shows ahead of both upstream and push branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes multiple compare branches' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		git push origin &&
+		advance work &&
+		git -c status.compareBranches="upstream/main origin/feature6 origin/feature5" status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	(
+		cd test &&
+		git checkout -b feature8 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	(
+		cd test &&
+		git checkout feature8 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	(
+		cd test &&
+		git checkout feature8 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	(
+		cd test &&
+		git checkout -b ahead upstream/main &&
+		advance work &&
+		git push upstream HEAD &&
+		git checkout -b feature9 upstream/main &&
+		git push origin &&
+		git branch --set-upstream-to upstream/ahead &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature10 origin/main &&
+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature11 upstream/main &&
+		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'clean up after compareBranches tests' '
+	(
+		cd test &&
+		git config --unset status.compareBranches
+	)
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Jan 22, 2026, 20:18 UTC · re: Harald Nordgren · lore

Re: [PATCH v27 0/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 17 quoted lines
>> Using a string list like this is quadratic. It probably doesn't matter
>> too much here since we wouldn't expect the list of configured branches
>> to be very long, though. But a strset is probably the better tool, like
>> the diff below (note that its "add" can be used as a single operation to
>> insert and check).
>> 
>> I don't know if it's worth re-rolling for this or not. I doubt anybody
>> would hit it in practice, but I'd be more concerned about people
>> auditing for accidentally-quadratic uses of string_list and stumbling
>> upon it.
>
> Sounds like a good change. Very nice to have insert-and-check as a single
> operation!
>
> I'll update it after passing CI on GitHub -- I'm running CI from two
> separate GitHub pull requests, because one has your memory leak fix on top
> to allow the leak tests to not fail. 🤗

Thanks. I think we are getting to the finish line with this topic, finally ;-)

Junio C Hamano· Jan 22, 2026, 20:37 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 34 quoted lines
> diff --git a/remote.c b/remote.c
> index fd592ec659..e256cc9b81 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -29,6 +29,12 @@
>  
>  enum map_direction { FROM_SRC, FROM_DST };
>  
> +enum {
> +	ENABLE_ADVICE_PULL       = (1 << 0),
> +	ENABLE_ADVICE_PUSH       = (1 << 1),
> +	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
> +};
> +
>  struct counted_string {
>  	size_t len;
>  	const char *s;
> @@ -2230,13 +2236,53 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
>  	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
>  }
>  
> +static char *resolve_compare_branch(struct branch *branch, const char *name)
> +{
> +	struct strbuf buf = STRBUF_INIT;
> +	const char *resolved = NULL;
> +	char *ret;
> +
> +	if (!branch || !name)
> +		return NULL;
> +
> +	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
> +		resolved = branch_get_upstream(branch, NULL);
> +	else if (!strcasecmp(name, "@{push}"))
> +		resolved = branch_get_push(branch, NULL);

OK. Usually @{upstream} without anything before the at-sign means the upstream of the current branch, but we need to force pretend that branch were the current branch, so we'd need to special case like this, which looks reasonable.

Show 14 quoted lines
> +	if (resolved)
> +		return xstrdup(resolved);
> +
> +	strbuf_addf(&buf, "refs/remotes/%s", name);
> +	resolved = refs_resolve_ref_unsafe(
> +		get_main_ref_store(the_repository),
> +		buf.buf,
> +		RESOLVE_REF_READING,
> +		NULL, NULL);
> +	if (resolved) {
> +		ret = xstrdup(resolved);
> +		strbuf_release(&buf);
> +		return ret;
> +	}

It would be handy to be able to say "origin/master" (or even just "origin", which is interpreted as "origin/HEAD" via the DWIM machinery) and prepending of "refs/remotes/" above does help such DWIMmery, but I wonder if it is too limiting? Would there be situations where you would want to compare with something outside refs/remotes/ hierarchy?

For example, writing "v2.52.0" there to see how far we came since the last release would become impossible if we always force prepend "refs/remotes/". I wonder if we can reuse already existing DWIMmery that uses refs.c::ref_rev_parse_rules[], which should allow such use case, while still allowing you to write "origin/master"?

Harald Nordgren· Jan 22, 2026, 20:56 UTC · re: Junio C Hamano · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Show 5 quoted lines
> For example, writing "v2.52.0" there to see how far we came since
> the last release would become impossible if we always force prepend
> "refs/remotes/".  I wonder if we can reuse already existing DWIMmery
> that uses refs.c::ref_rev_parse_rules[], which should allow such use
> case, while still allowing you to write "origin/master"?
Sounds like a follow-up rather of doing now, right? 🤗

Since the inteface won't change, just adding more functionality a new feature, we should be able to fix this behind the scenes later.

Harald
Junio C Hamano· Jan 22, 2026, 21:11 UTC · re: Harald Nordgren · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 10 quoted lines
>> For example, writing "v2.52.0" there to see how far we came since
>> the last release would become impossible if we always force prepend
>> "refs/remotes/".  I wonder if we can reuse already existing DWIMmery
>> that uses refs.c::ref_rev_parse_rules[], which should allow such use
>> case, while still allowing you to write "origin/master"?
>
> Sounds like a follow-up rather of doing now, right? 🤗
>
> Since the inteface won't change, just adding more functionality a new
> feature, we should be able to fix this behind the scenes later.

Disambiguation will make it harder or impossible to add such an enhancement later, though.

When the user says "git log origin" or "git show origin/main", refs.c::ref_rev_parse_rules[] is applied and turns these into refs/refs/remotes/origin/HEAD or refs/remotes/origin/main, but as you can see in refs.c::ref_rev_parse_rules[], these are at relatively low precedence order. Your version has refs/remotes/ as the first (and only) choice, so if a user has refs/heads/origin/main and refs/remotes/origin/main at the same time, "git show origin/main" would use the former. With the implementation of this round, "[status] compareBranches = origin/main" would pick refs/remotes/origin/main and nothing else first, and then would start behaving differently once such an enhancement to use the standard DWIMmery rules is introduced, which may appear to be an unnecessary regression to end-users.

So, no, I wouldn't recommend it as a follow-up. We should decide to do so now or declare that we would never do so.

Junio C Hamano· Jan 22, 2026, 21:36 UTC · re: Junio C Hamano · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Junio C Hamano <gitster@pobox.com> writes:
Show 15 quoted lines
> Harald Nordgren <haraldnordgren@gmail.com> writes:
>
>>> For example, writing "v2.52.0" there to see how far we came since
>>> the last release would become impossible if we always force prepend
>>> "refs/remotes/".  I wonder if we can reuse already existing DWIMmery
>>> that uses refs.c::ref_rev_parse_rules[], which should allow such use
>>> case, while still allowing you to write "origin/master"?
>>
>> Sounds like a follow-up rather of doing now, right? 🤗
>>
>> Since the inteface won't change, just adding more functionality a new
>> feature, we should be able to fix this behind the scenes later.
> ...
> So, no, I wouldn't recommend it as a follow-up.  We should decide to
> do so now or declare that we would never do so.

Alternatively, if we remove the "a string that is not @{upstream} or @{push} gets refs/remotes/ prefixed" altogether, then "the interface won't change, just adding more functionality" would become true.

It is not my itch, so I can go either way, but I'd prefer not to see us take the "declare we will never use the unified ref DIWMmery rules here" route, if we can.

Jeff King· Jan 22, 2026, 22:01 UTC · re: Junio C Hamano · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

On Thu, Jan 22, 2026 at 12:37:16PM -0800, Junio C Hamano wrote:
Show 18 quoted lines
> > +static char *resolve_compare_branch(struct branch *branch, const char *name)
> > +{
> > +	struct strbuf buf = STRBUF_INIT;
> > +	const char *resolved = NULL;
> > +	char *ret;
> > +
> > +	if (!branch || !name)
> > +		return NULL;
> > +
> > +	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
> > +		resolved = branch_get_upstream(branch, NULL);
> > +	else if (!strcasecmp(name, "@{push}"))
> > +		resolved = branch_get_push(branch, NULL);
> 
> OK.  Usually @{upstream} without anything before the at-sign means
> the upstream of the current branch, but we need to force pretend
> that branch were the current branch, so we'd need to special case
> like this, which looks reasonable.

Yeah, it is unfortunate to have to special-case these names, even though the resolving functions already know about them. If we are looking at branch "foo" we could rewrite them as "foo@{upstream}", etc, but that also requires special-casing (though maybe slightly less, if we just accept the @ sign?).

I can think of two alternatives, though.

One is that repo_interpret_branch_name() could accept a field in its options struct to set the default branch (rather than "HEAD"). Something like this (totally untested):

Show changes to 2 files +7 −2

object-name.c, object-name.h

diff --git a/object-name.c b/object-name.c
index 8b862c124e..925a487d84 100644
--- a/object-name.c
+++ b/object-name.c
@@ -1732,7 +1732,7 @@ static int interpret_branch_mark(struct repository *r,
 		branch = branch_get(name_str);
 		free(name_str);
 	} else
-		branch = branch_get(NULL);
+		branch = branch_get(options->default_branch);
 
 	value = get_data(branch, &err);
 	if (!value) {
diff --git a/object-name.h b/object-name.h
index cda4934cd5..962f99b0f6 100644
--- a/object-name.h
+++ b/object-name.h
@@ -119,6 +119,12 @@ struct interpret_branch_name_options {
 	 * of die()-ing.
 	 */
 	unsigned nonfatal_dangling_mark : 1;
+
+	/*
+	 * Pass this to branch_get() when interpreting @-marks without a
+	 * branch, rather than using HEAD.
+	 */
+	const char *default_branch;
 };
 int repo_interpret_branch_name(struct repository *r,
 			       const char *str, int len,

Most callers would continue to pass NULL in the usual way, but the
resolution here would pass in the current branch name.


The second is a bit more complicated, but is even more flexible. Part of
the point of this status.compareBranches approach is that you can add
regular refnames to the list. But would a user want to use a name that
is derived from the comparison branch, rather than just a static name?
That is, to compare branch "foo" against "origin/foo"? Usually that is
exactly the kind of refspec-application that @{upstream} and @{push} are
computing (after taking into account various config). But if you have a
third source of refs, would you want to be able to compare to
"origin/%s", where %s is the shortened branch name?

In which case these values could become "%s@{upstream}" and "%s@{push}",
and they could just be fed straight to the branch-interpret machinery.

> > +	strbuf_addf(&buf, "refs/remotes/%s", name);
> > +	resolved = refs_resolve_ref_unsafe(
> > +		get_main_ref_store(the_repository),
> > +		buf.buf,
> > +		RESOLVE_REF_READING,
> > +		NULL, NULL);
> > +	if (resolved) {
> > +		ret = xstrdup(resolved);
> > +		strbuf_release(&buf);
> > +		return ret;
> > +	}
> 
> It would be handy to be able to say "origin/master" (or even just
> "origin", which is interpreted as "origin/HEAD" via the DWIM
> machinery) and prepending of "refs/remotes/" above does help such
> DWIMmery, but I wonder if it is too limiting?  Would there be
> situations where you would want to compare with something outside
> refs/remotes/ hierarchy?
> 
> For example, writing "v2.52.0" there to see how far we came since
> the last release would become impossible if we always force prepend
> "refs/remotes/".  I wonder if we can reuse already existing DWIMmery
> that uses refs.c::ref_rev_parse_rules[], which should allow such use
> case, while still allowing you to write "origin/master"?

Yeah, I think tags or even local branches would be plausible candidates.
But at any rate, I'd expect these to be resolved in the "usual" way that
we do elsewhere. If we switch to using repo_dwim_ref() or something that
interprets @-marks, then that matches nicely with the suggestions I gave
above.

-Peff
Jeff King· Jan 22, 2026, 22:44 UTC · re: Jeff King · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

On Thu, Jan 22, 2026 at 05:01:54PM -0500, Jeff King wrote:
Show 12 quoted lines
> The second is a bit more complicated, but is even more flexible. Part of
> the point of this status.compareBranches approach is that you can add
> regular refnames to the list. But would a user want to use a name that
> is derived from the comparison branch, rather than just a static name?
> That is, to compare branch "foo" against "origin/foo"? Usually that is
> exactly the kind of refspec-application that @{upstream} and @{push} are
> computing (after taking into account various config). But if you have a
> third source of refs, would you want to be able to compare to
> "origin/%s", where %s is the shortened branch name?
> 
> In which case these values could become "%s@{upstream}" and "%s@{push}",
> and they could just be fed straight to the branch-interpret machinery.
So here's a sketch to show what that might look like:
Show changes to 2 files +31 −22

remote.c, branch.c

diff --git a/remote.c b/remote.c
index 1071a23567..6f9a28970c 100644
--- a/remote.c
+++ b/remote.c
@@ -2239,34 +2239,30 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 static char *resolve_compare_branch(struct branch *branch, const char *name)
 {
 	struct strbuf buf = STRBUF_INIT;
-	const char *resolved = NULL;
+	struct object_id oid;
 	char *ret;
 
 	if (!branch || !name)
 		return NULL;
 
-	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
-		resolved = branch_get_upstream(branch, NULL);
-	else if (!strcasecmp(name, "@{push}"))
-		resolved = branch_get_push(branch, NULL);
-
-	if (resolved)
-		return xstrdup(resolved);
-
-	strbuf_addf(&buf, "refs/remotes/%s", name);
-	resolved = refs_resolve_ref_unsafe(
-		get_main_ref_store(the_repository),
-		buf.buf,
-		RESOLVE_REF_READING,
-		NULL, NULL);
-	if (resolved) {
-		ret = xstrdup(resolved);
-		strbuf_release(&buf);
-		return ret;
+	while (strbuf_expand_step(&buf, &name)) {
+		if (skip_prefix(name, "%", &name))
+			strbuf_addch(&buf, '%');
+		else if (skip_prefix(name, "s", &name))
+			strbuf_addstr(&buf, branch->name);
+		else
+			die("bad compareBranches format: %%%s", name);
 	}
 
+	/*
+	 * This will leave "ret" as NULL if we don't find anything. If there
+	 * are multiple ambiguous matches it will pick the first. Should we
+	 * return an error instead?
+	 */
+	repo_dwim_ref(the_repository, buf.buf, buf.len, &oid, &ret, 1);
+
 	strbuf_release(&buf);
-	return NULL;
+	return ret;
 }
 
 static void format_branch_comparison(struct strbuf *sb,
@@ -2352,7 +2348,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 		string_list_split(&branches, compare_branches, " ", -1);
 		string_list_remove_empty_items(&branches, 0);
 	} else {
-		string_list_append(&branches, "@{upstream}");
+		string_list_append(&branches, "%s@{upstream}");
 	}
 
 	upstream_ref = branch_get_upstream(branch, NULL);


I ran into two small complications. The first is that there's an
interesting race "git-checkout -b":

  1. We read branch and remote config via remote.c:read_config(),
     caching it.

  2. Then we write some new config for the branch we just created.

  3. Then we try to report tracking info. This will read the cached
     config from step 1, without regard to the changes in 2.

It works without my patch above because the tracking code currently
operates directly on a "struct branch" (that we just updated), rather
than using the stale config from (1).

We can fix that like this:

diff --git a/branch.c b/branch.c
index 243db7d0fc..cc24e8522d 100644
--- a/branch.c
+++ b/branch.c
@@ -178,6 +178,12 @@ static int install_branch_config_multiple_remotes(int flag, const char *local,
 		string_list_clear(&friendly_ref_names, 0);
 	}
 
+	/*
+	 * We installed new config; flush any internal cache of the on-disk
+	 * config.
+	 */
+	remote_state_clear(the_repository->remote_state);
+
 	return 0;
 
 out_err:
diff --git a/remote.c b/remote.c
index 6f9a28970c..e6056199f0 100644
--- a/remote.c
+++ b/remote.c
@@ -2859,6 +2859,14 @@ void remote_state_clear(struct remote_state *remote_state)
 		free(b);
 	}
 	hashmap_clear(&remote_state->branches_hash);
+
+	/*
+	 * Eek, we just cleared everything out; we should mark ourselves as
+	 * uninitialized so we can be used again. Nobody seems to have noticed
+	 * because we only remote_state_clear() when we are about to discard
+	 * the struct entirely.
+	 */
+	remote_state->initialized = 0;
 }
 
 /*


The second issue concerns the case when an upstream is configured, but
the tracking ref for it is missing. So imagine "foo" is configured with
"refs/remotes/origin/foo" as its upstream, but that branch is gone.

Using branch_get_upstream() will return the name, even if it doesn't
exist. And then the tracking-info code recognizes this and reports it.
But repo_dwim_ref() won't return a missing ref at all, even with
nonfatal_dangling_mark. So I think we'd need to teach it a new option to
do so.


I don't know what all of it means. The "%s" thing was short to
implement, but the real source of the extra complications is using
repo_dwim_ref() to do the resolution. But I think the overall direction
is more consistent with how the rest of Git behaves.

-Peff
Jeff King· Jan 22, 2026, 23:06 UTC · re: Jeff King · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

On Thu, Jan 22, 2026 at 05:44:27PM -0500, Jeff King wrote:
Show 9 quoted lines
> The second issue concerns the case when an upstream is configured, but
> the tracking ref for it is missing. So imagine "foo" is configured with
> "refs/remotes/origin/foo" as its upstream, but that branch is gone.
> 
> Using branch_get_upstream() will return the name, even if it doesn't
> exist. And then the tracking-info code recognizes this and reports it.
> But repo_dwim_ref() won't return a missing ref at all, even with
> nonfatal_dangling_mark. So I think we'd need to teach it a new option to
> do so.

So I think that nonfatal_dangling_mark could arguably return the name (but no oid) for this case, like so:

Show changes to 3 files +16 −3

refs.c, object-name.c, object-name.h

diff --git a/refs.c b/refs.c
index 627b7f8698..2316d0b8a9 100644
--- a/refs.c
+++ b/refs.c
@@ -813,7 +813,13 @@ int repo_dwim_ref(struct repository *r, const char *str, int len,
 	char *last_branch = substitute_branch_name(r, &str, &len,
 						   nonfatal_dangling_mark);
 	int   refs_found  = expand_ref(r, str, len, oid, ref);
-	free(last_branch);
+	if (nonfatal_dangling_mark && !refs_found && last_branch) {
+		oidclr(oid, r->hash_algo);
+		*ref = last_branch;
+		refs_found = 1;
+	} else {
+		free(last_branch);
+	}
 	return refs_found;
 }
 

But this brings up yet another corner case: substitute_branch_name()
will call shorten_unambiguous_ref() on the result when expanding
@-marks. So last_branch here might be "origin/foo" instead of
"refs/remotes/origin/foo". It's usually not a big deal because
expand_ref() will then reverse that shortening.  Which is maybe
wasteful, but not so bad (though if somebody racily creates an ambiguous
ref, it could affect the results).

But if we return last_branch explicitly, then information is lost: we
don't know what the fully-qualified version of "origin/foo" was supposed
to be. And so the tracking-info code gets confused, because it doesn't
realize that "origin/foo" was supposed to be our @{upstream}.

I think probably the interpret_branch_name() code should have an option
to avoid that shortening. So if we do this on top:

diff --git a/object-name.c b/object-name.c
index 8b862c124e..0663946f81 100644
--- a/object-name.c
+++ b/object-name.c
@@ -1747,7 +1747,12 @@ static int interpret_branch_mark(struct repository *r,
 	if (!branch_interpret_allowed(value, options->allowed))
 		return -1;
 
-	set_shortened_ref(r, buf, value);
+	if (options->do_not_shorten) {
+		strbuf_reset(buf);
+		strbuf_addstr(buf, value);
+	} else {
+		set_shortened_ref(r, buf, value);
+	}
 	return len + at;
 }
 
diff --git a/object-name.h b/object-name.h
index cda4934cd5..20393cb213 100644
--- a/object-name.h
+++ b/object-name.h
@@ -119,6 +119,8 @@ struct interpret_branch_name_options {
 	 * of die()-ing.
 	 */
 	unsigned nonfatal_dangling_mark : 1;
+
+	unsigned do_not_shorten : 1;
 };
 int repo_interpret_branch_name(struct repository *r,
 			       const char *str, int len,
diff --git a/refs.c b/refs.c
index 2316d0b8a9..58f945e213 100644
--- a/refs.c
+++ b/refs.c
@@ -746,6 +746,7 @@ static char *substitute_branch_name(struct repository *r,
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct interpret_branch_name_options options = {
+		.do_not_shorten = 1,
 		.nonfatal_dangling_mark = nonfatal_dangling_mark
 	};
 	int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options);

then all of t6040 passes (curiously without me swapping in "%s@{push}"
as appropriate; I guess we are often on the branch of interest anyway,
so it accidentally works with just @{push}).


So I don't love how deep the rabbit-hole has gone here. But at the same
time, it feels like all of these "if we just do this on top" fixes are
actually smoothing some rough edges in the rest of Git. So maybe it's
worth it.

-Peff
Junio C Hamano· Jan 22, 2026, 23:14 UTC · re: Jeff King · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Jeff King <peff@peff.net> writes:
> I don't know what all of it means. The "%s" thing was short to
> implement, but the real source of the extra complications is using
> repo_dwim_ref() to do the resolution. But I think the overall direction
> is more consistent with how the rest of Git behaves.

Yeah, I tend to agree. As long as we do not hardcode the prefixing of "refs/remotes/", we won't paint ourselves into a corner we cannot get out of, I guess.

Thanks.
Harald Nordgren· Jan 24, 2026, 08:50 UTC · re: Jeff King · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

I can apply the changes from these three messages, but I don't really know the side-effects of it. Should I do it and submit a patch?

I noticed that it still won't work with tags (although it starts working with 'origin' which then defaults to origin/HEAD). I can imagine we need a few more tests on top of that.

Harald
Junio C Hamano· Jan 25, 2026, 17:29 UTC · re: Harald Nordgren · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
> I can apply the changes from these three messages, but I don't really
> know the side-effects of it. Should I do it and submit a patch?

Please do not send code you cannot answer to questions on it, whether it is written by somebody else, genAI, or your cat rolling on your keyboard ;-).

I think we are getting close but will need more polish before we can allow users to reuse their already acquired knowledge of how refname DWIMmery can be used to spell various refs they mean.

If we support only @{push} and @{upstream} and error out when we see anything else (like "origin" or "origin/main") in the initial version we ship to the end-users, that would probably be a good stopping point. On top of it, we can later add the DWIMmery Peff has shown (with necessary tweaks, as you found out, like supporting tags, perhaps), and that will be purely new feature that does not change any behaviour of what used to work for our users in the initial version.

Going that way is much safer and does not break end-user experiences, like shipping the first version with "we always prefix hardcoded refs/remotes/ unless it is @{something}", which will have to change the behaviour once the proper DWIMmery gets implemented.

In any case, that will have to happen all after the current cycle is over, it is way too late even for "@{push} and @{upstream} only" version for this cycle.

Harald Nordgren· Feb 8, 2026, 13:33 UTC · re: Jeff King · lore

Re: [PATCH v27 2/2] status: add status.compareBranches config for multiple branch comparisons

Hi Jeff!
Do you think this is ready to be merged? I think you are the holdout here 🤗
Harald
Harald Nordgren· Feb 21, 2026, 08:02 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Hi Junio and Jeff!

I see that this topic has now been marked as "Stale". Isn't it time to merge this now?

We went to several rounds of reviews and edits to reach something that I think everyone agrees with 🤗

Harald
Junio C Hamano· Feb 21, 2026, 17:17 UTC · re: Harald Nordgren · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 7 quoted lines
> Hi Junio and Jeff!
>
> I see that this topic has now been marked as "Stale". Isn't it time to
> merge this now?
>
> We went to several rounds of reviews and edits to reach something that I
> think everyone agrees with 🤗

The entry has been asking "What's the status of this topic?" in the past handful of issues of "What's cooking" report and we heard nothing from anybody (until you responded ;-)), so I moved it together with others to the [Stalled] section.

The topic has been paged out of my consciousness for quite a while, so it may take some time for me to answer this question myself, but list participants, do you imagine yourselves using this feature in your daily life?

Thanks.
D. Ben Knoble· Feb 22, 2026, 14:50 UTC · re: Junio C Hamano · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

On Sat, Feb 21, 2026 at 12:18 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 22 quoted lines
>
> Harald Nordgren <haraldnordgren@gmail.com> writes:
>
> > Hi Junio and Jeff!
> >
> > I see that this topic has now been marked as "Stale". Isn't it time to
> > merge this now?
> >
> > We went to several rounds of reviews and edits to reach something that I
> > think everyone agrees with 🤗
>
> The entry has been asking "What's the status of this topic?" in the
> past handful of issues of "What's cooking" report and we heard
> nothing from anybody (until you responded ;-)), so I moved it
> together with others to the [Stalled] section.
>
> The topic has been paged out of my consciousness for quite a while,
> so it may take some time for me to answer this question myself, but
> list participants, do you imagine yourselves using this feature in
> your daily life?
>
> Thanks.

I'm trying to run something closer to the tip of "next" at home and work, and I can see a few places where I might try this, at least.

-- 
D. Ben Knoble
Jeff King· Feb 23, 2026, 13:30 UTC · re: Junio C Hamano · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

On Sat, Feb 21, 2026 at 09:17:50AM -0800, Junio C Hamano wrote:
Show 19 quoted lines
> Harald Nordgren <haraldnordgren@gmail.com> writes:
> 
> > Hi Junio and Jeff!
> >
> > I see that this topic has now been marked as "Stale". Isn't it time to
> > merge this now?
> >
> > We went to several rounds of reviews and edits to reach something that I
> > think everyone agrees with 🤗
> 
> The entry has been asking "What's the status of this topic?" in the
> past handful of issues of "What's cooking" report and we heard
> nothing from anybody (until you responded ;-)), so I moved it
> together with others to the [Stalled] section.
> 
> The topic has been paged out of my consciousness for quite a while,
> so it may take some time for me to answer this question myself, but
> list participants, do you imagine yourselves using this feature in
> your daily life?

I do not personally have any interest in using the feature (I only joined the thread to complain that it could not be turned off ;) ).

So in that sense I don't care how far it takes things along the path of what was discussed in the v27 review. My main concern is that we not paint ourselves into a corner and be stuck forever with an interface that is inconsistent with the rest of Git.

So what about this. We add a config option that takes a list of items to compare against. But it _only_ supports @{upstream} and @{push} for now, and interprets them as branch@{upstream} and branch@{push} (but probably done manually, not via dwim_ref). That limitation gets documented.

Then later, if we choose to allow resolving arbitrary refs via dwim_ref(), we can add that support on top:

  - rather than manually resolving @{upstream}, quietly rewrite it to
    branch@{upstream}. This is OK since branchless @{upstream} makes no
    sense in this context.
  - otherwise, pass what the user gives us to repo_dwim_ref()
  - optionally allow substitution via %s or similar. Technically I think
    "%" is allowed in a ref. I think that's probably OK in practice,
    though if we want to be really careful, then "[branch]" or something
    that's forbidden in the ref format would be a possible substitution.

But if we stop short of all of that today, we don't have to worry about all of those weird corner cases I dug up in the earlier thread. And if nobody ever _really_ cares about sticking arbitrary refs into their compare-list, then we just never go there.

-Peff
Junio C Hamano· Feb 24, 2026, 22:21 UTC · re: Jeff King · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Jeff King <peff@peff.net> writes:
> So what about this. We add a config option that takes a list of items to
> compare against. But it _only_ supports @{upstream} and @{push} for now,
> and interprets them as branch@{upstream} and branch@{push} (but probably
> done manually, not via dwim_ref). That limitation gets documented.
Sounds like a good way forward.
Show 18 quoted lines
> Then later, if we choose to allow resolving arbitrary refs via
> dwim_ref(), we can add that support on top:
>
>   - rather than manually resolving @{upstream}, quietly rewrite it to
>     branch@{upstream}. This is OK since branchless @{upstream} makes no
>     sense in this context.
>
>   - otherwise, pass what the user gives us to repo_dwim_ref()
>
>   - optionally allow substitution via %s or similar. Technically I think
>     "%" is allowed in a ref. I think that's probably OK in practice,
>     though if we want to be really careful, then "[branch]" or something
>     that's forbidden in the ref format would be a possible substitution.
>
> But if we stop short of all of that today, we don't have to worry about
> all of those weird corner cases I dug up in the earlier thread. And if
> nobody ever _really_ cares about sticking arbitrary refs into their
> compare-list, then we just never go there.
;-).
Harald Nordgren· Feb 25, 2026, 10:22 UTC · re: Junio C Hamano · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Show 6 quoted lines
>> So what about this. We add a config option that takes a list of items to
>> compare against. But it _only_ supports @{upstream} and @{push} for now,
>> and interprets them as branch@{upstream} and branch@{push} (but probably
>> done manually, not via dwim_ref). That limitation gets documented.
>
> Sounds like a good way forward.
So if I do these changes, are we ready to merge then?

I worked on this feature for 2 months, and then it got marked as stale instead of being merged. Will this time be different?

Harald
Junio C Hamano· Feb 25, 2026, 15:44 UTC · re: Harald Nordgren · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 8 quoted lines
>>> So what about this. We add a config option that takes a list of items to
>>> compare against. But it _only_ supports @{upstream} and @{push} for now,
>>> and interprets them as branch@{upstream} and branch@{push} (but probably
>>> done manually, not via dwim_ref). That limitation gets documented.
>>
>> Sounds like a good way forward.
>
> So if I do these changes, are we ready to merge then?

I do not think of other things that needs to be done to the design at this moment. It of course does not give any guarantee that others won't find flaws in what we have discussed so far, though ;-)

> I worked on this feature for 2 months, and then it got marked as stale
> instead of being merged. Will this time be different?

A topic becoming stalled is something the original author can (and has the primary responsibility to) avoid by keeping the discussion thread alive by responding to reviews, pinging the thread with comments similar to "now I think this one is done, all comments are addressed by either updating the code or replying why we would not want to go there (which the reviewer who made the comment hasn't responded yet, so the ball is in their court)", etc. Even though I try to help keep the ball rolling by pinging discussion threads that smell about to go stalled from time to time, I cannot guarantee that it would not happen again. But you can help ;-).

Thanks for working on this topic.
Jeff King· Feb 25, 2026, 16:08 UTC · re: Junio C Hamano · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

On Wed, Feb 25, 2026 at 07:44:56AM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> > I worked on this feature for 2 months, and then it got marked as stale
> > instead of being merged. Will this time be different?
> 
> A topic becoming stalled is something the original author can (and
> has the primary responsibility to) avoid by keeping the discussion
> thread alive by responding to reviews, pinging the thread with
> comments similar to "now I think this one is done, all comments are
> addressed by either updating the code or replying why we would not
> want to go there (which the reviewer who made the comment hasn't
> responded yet, so the ball is in their court)", etc.  Even though I
> try to help keep the ball rolling by pinging discussion threads that
> smell about to go stalled from time to time, I cannot guarantee that
> it would not happen again.   But you can help ;-).

To be fair to Harald, he did ping a few times, but I think was just unsure how to proceed after the issues raised in the last round.

I think what would have moved things forward more than just a ping, though, is generating a plan for moving forward in the face of confusing review. That can be quite hard for first-time contributors, though.

That was why I tried to lay something out in the last email. I _think_ it would not be too much work to get there from the last iteration. It would mostly be removing code/feature that we're not ready to commit to yet.

-Peff
Junio C Hamano· Feb 25, 2026, 16:52 UTC · re: Jeff King · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

Jeff King <peff@peff.net> writes:
> That was why I tried to lay something out in the last email. I _think_
> it would not be too much work to get there from the last iteration. It
> would mostly be removing code/feature that we're not ready to commit to
> yet.
Sounds fair.  Thanks.
D. Ben Knoble· Feb 25, 2026, 17:04 UTC · re: Harald Nordgren · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

On Wed, Feb 25, 2026 at 5:24 AM Harald Nordgren <haraldnordgren@gmail.com> wrote:

Show 15 quoted lines
>
> >> So what about this. We add a config option that takes a list of items to
> >> compare against. But it _only_ supports @{upstream} and @{push} for now,
> >> and interprets them as branch@{upstream} and branch@{push} (but probably
> >> done manually, not via dwim_ref). That limitation gets documented.
> >
> > Sounds like a good way forward.
>
> So if I do these changes, are we ready to merge then?
>
> I worked on this feature for 2 months, and then it got marked as stale
> instead of being merged. Will this time be different?
>
>
> Harald

Try not to be discouraged. It is not unusual for a series to undergo lengthy work and many revisions. It's part of what keeps Git working so well.

-- 
D. Ben Knoble
Harald Nordgren via GitGitGadget· Feb 25, 2026, 21:51 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v29 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  19 ++
 remote.c                         | 180 +++++++++++++----
 t/t6040-tracking-info.sh         | 335 +++++++++++++++++++++++++++++++
 3 files changed, 497 insertions(+), 37 deletions(-)
base-commit: 7c02d39fc2ed2702223c7674f73150d9a7e61ba4
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v29
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v29
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v28:
 1:  f3c8c782b0 = 1:  48db1f4847 refactor format_branch_comparison in preparation
 2:  067978dd09 ! 2:  6a88f41fa5 status: add status.compareBranches config for multiple branch comparisons
     @@ Metadata
       ## Commit message ##
          status: add status.compareBranches config for multiple branch comparisons
      
     -    Add a new configuration variable `status.compareBranches` that allows
     -    users to specify a space-separated list of branches to compare against
     -    the current branch in `git status` output.
     +    Add a new configuration variable status.compareBranches that allows
     +    users to specify a space-separated list of branch comparisons in
     +    git status output.
      
     -    Each branch in the list can be:
     -    - A remote-tracking branch name (e.g., `origin/main`)
     -    - The special reference `@{upstream}` for the tracking branch
     -    - The special reference `@{push}` for the push destination
     +    Supported values:
     +    - @{upstream} for the current branch's upstream tracking branch
     +    - @{push} for the current branch's push destination
     +
     +    Any other value is ignored and a warning is shown.
      
          When not configured, the default behavior is equivalent to setting
     -    `status.compareBranches = @{upstream}`, preserving backward compatibility.
     +    `status.compareBranches = @{upstream}`, preserving backward
     +    compatibility.
      
          The advice messages shown are context-aware:
          - "git pull" advice is shown only when comparing against @{upstream}
     @@ Documentation/config/status.adoc: status.aheadBehind::
       	non-porcelain status formats.  Defaults to true.
       
      +status.compareBranches::
     -+	A space-separated list of branches to compare the current branch
     -+	against in linkgit:git-status[1]. Each branch specification can be
     -+	a remote-tracking branch name (e.g. `origin/main`), or a special
     -+	reference like `@{upstream}` or `@{push}`. For each branch in the
     -+	list, git status shows whether the current branch is ahead, behind,
     -+	or has diverged from that branch.
     ++	A space-separated list of branch comparison specifiers to use in
     ++	linkgit:git-status[1]. Currently, only `@{upstream}` and `@{push}`
     ++	are supported. They are interpreted as `branch@{upstream}` and
     ++	`branch@{push}` for the current branch.
      ++
      +If not set, the default behavior is equivalent to `@{upstream}`, which
      +compares against the configured upstream tracking branch.
     @@ Documentation/config/status.adoc: status.aheadBehind::
      ++
      +----
      +[status]
     -+	compareBranches = origin/main origin/develop
     ++	compareBranches = @{upstream} @{push}
      +----
      ++
     -+This would show comparisons against both `origin/main` and `origin/develop`.
     ++This would show comparisons against both the configured upstream and push
     ++tracking branches for the current branch.
      +
       status.displayCommentPrefix::
       	If set to true, linkgit:git-status[1] will insert a comment
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
       
      +static char *resolve_compare_branch(struct branch *branch, const char *name)
      +{
     -+	struct strbuf buf = STRBUF_INIT;
      +	const char *resolved = NULL;
     -+	char *ret;
      +
      +	if (!branch || !name)
      +		return NULL;
      +
     -+	if (!strcasecmp(name, "@{upstream}") || !strcasecmp(name, "@{u}"))
     ++	if (!strcasecmp(name, "@{upstream}"))
      +		resolved = branch_get_upstream(branch, NULL);
      +	else if (!strcasecmp(name, "@{push}"))
      +		resolved = branch_get_push(branch, NULL);
     ++	else {
     ++		warning(_("ignoring value '%s' for status.compareBranches; only @{upstream} and @{push} are supported"),
     ++			name);
     ++		return NULL;
     ++	}
      +
      +	if (resolved)
      +		return xstrdup(resolved);
     -+
     -+	strbuf_addf(&buf, "refs/remotes/%s", name);
     -+	resolved = refs_resolve_ref_unsafe(
     -+		get_main_ref_store(the_repository),
     -+		buf.buf,
     -+		RESOLVE_REF_READING,
     -+		NULL, NULL);
     -+	if (resolved) {
     -+		ret = xstrdup(resolved);
     -+		strbuf_release(&buf);
     -+		return ret;
     -+	}
     -+
     -+	strbuf_release(&buf);
      +	return NULL;
      +}
      +
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status.compareBranches with upstream and origin remotes multiple compare branches' '
     ++test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
      +	(
      +		cd test &&
      +		git checkout -b feature6 upstream/main &&
      +		git push origin &&
      +		advance work &&
     -+		git -c status.compareBranches="upstream/main origin/feature6 origin/feature5" status >../actual
     ++		git -c status.compareBranches="@{push} @{upstream}" status >../actual
      +	) &&
      +	cat >expect <<-EOF &&
      +	On branch feature6
     -+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     -+
      +	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
      +	  (use "git push" to publish your local commits)
      +
     -+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
      +	nothing to commit, working tree clean
      +	EOF
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Feb 25, 2026, 21:51 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v29 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index f6980dc656..e9e2f56ed6 100644
--- a/remote.c
+++ b/remote.c
@@ -2234,42 +2234,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2278,7 +2257,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2289,7 +2268,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2302,12 +2281,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Feb 25, 2026, 21:51 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v29 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable status.compareBranches that allows users to specify a space-separated list of branch comparisons in git status output.

Supported values:
- @{upstream} for the current branch's upstream tracking branch
- @{push} for the current branch's push destination
Any other value is ignored and a warning is shown.

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  19 ++
 remote.c                         | 148 +++++++++++---
 t/t6040-tracking-info.sh         | 335 +++++++++++++++++++++++++++++++
 3 files changed, 474 insertions(+), 28 deletions(-)
Show changes to 3 files +474 −28

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..15ccd0116b 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,25 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branch comparison specifiers to use in
+	linkgit:git-status[1]. Currently, only `@{upstream}` and `@{push}`
+	are supported. They are interpreted as `branch@{upstream}` and
+	`branch@{push}` for the current branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+Example:
++
+----
+[status]
+	compareBranches = @{upstream} @{push}
+----
++
+This would show comparisons against both the configured upstream and push
+tracking branches for the current branch.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index e9e2f56ed6..cc6ff29438 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2234,13 +2240,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	const char *resolved = NULL;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}"))
+		resolved = branch_get_upstream(branch, NULL);
+	else if (!strcasecmp(name, "@{push}"))
+		resolved = branch_get_push(branch, NULL);
+	else {
+		warning(_("ignoring value '%s' for status.compareBranches; only @{upstream} and @{push} are supported"),
+			name);
+		return NULL;
+	}
+
+	if (resolved)
+		return xstrdup(resolved);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
+		advice_enabled(ADVICE_STATUS_HINTS);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2249,7 +2284,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2258,7 +2293,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_push_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2269,7 +2304,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_pull_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2282,8 +2317,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (enable_divergence_advice)
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,34 +2330,92 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
+	char *compare_branches = NULL;
+	struct string_list branches = STRING_LIST_INIT_DUP;
+	struct strset processed_refs = STRSET_INIT;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
 
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches);
+
+	if (compare_branches) {
+		string_list_split(&branches, compare_branches, " ", -1);
+		string_list_remove_empty_items(&branches, 0);
+	} else {
+		string_list_append(&branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		full_ref = resolve_compare_branch(branch,
+						  branches.items[i].string);
+		if (!full_ref)
+			continue;
+
+		if (!strset_add(&processed_refs, full_ref)) {
+			free(full_ref);
+			continue;
+		}
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&branches, 0);
+	strset_clear(&processed_refs);
+	free(compare_branches);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..aa9456bb61 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,339 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for compareBranches tests' '
+	(
+		cd test &&
+		git config push.default current &&
+		git config status.compareBranches "@{upstream} @{push}"
+	)
+'
+
+test_expect_success 'status.compareBranches from upstream has no duplicates' '
+	(
+		cd test &&
+		git checkout main &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches shows ahead of both upstream and push branch' '
+	(
+		cd test &&
+		git checkout -b feature2 origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	(
+		cd test &&
+		git checkout feature2 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git branch --set-upstream-to origin/main &&
+		git push origin HEAD &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	(
+		cd test &&
+		git checkout feature4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream &&
+		git config remote.pushDefault origin
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	(
+		cd test &&
+		git checkout -b feature5 upstream/main &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
+	(
+		cd test &&
+		git checkout -b feature6 upstream/main &&
+		git push origin &&
+		advance work &&
+		git -c status.compareBranches="@{push} @{upstream}" status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	(
+		cd test &&
+		git checkout -b feature7 upstream/main &&
+		advance work &&
+		git push origin &&
+		git reset --hard upstream/main &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	(
+		cd test &&
+		git checkout -b feature8 upstream/main &&
+		git push origin &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	(
+		cd test &&
+		git checkout feature8 &&
+		git push origin &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	(
+		cd test &&
+		git checkout feature8 >../actual
+	) &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	(
+		cd test &&
+		git checkout -b ahead upstream/main &&
+		advance work &&
+		git push upstream HEAD &&
+		git checkout -b feature9 upstream/main &&
+		git push origin &&
+		git branch --set-upstream-to upstream/ahead &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	(
+		cd test &&
+		git checkout -b feature10 origin/main &&
+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+		git push &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	(
+		cd test &&
+		git checkout -b feature11 upstream/main &&
+		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+		git push origin &&
+		advance work &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'clean up after compareBranches tests' '
+	(
+		cd test &&
+		git config --unset status.compareBranches
+	)
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Feb 25, 2026, 23:18 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v29 2/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 11 quoted lines
> +static char *resolve_compare_branch(struct branch *branch, const char *name)
> +{
> +	const char *resolved = NULL;
> +
> +	if (!branch || !name)
> +		return NULL;
> +
> +	if (!strcasecmp(name, "@{upstream}"))
> +		resolved = branch_get_upstream(branch, NULL);
> +	else if (!strcasecmp(name, "@{push}"))
> +		resolved = branch_get_push(branch, NULL);

As one of the if/else if/ cascade needs {} around a multi-statement block, everybody else needs {}.

If the current branch does not have @{upstream}, we will silently receive NULL in resolved, and return NULL from here, which is exactly what we want. The same stroy for missing @{push}.

Sounds very sensible.
Show 5 quoted lines
> +	else {
> +		warning(_("ignoring value '%s' for status.compareBranches; only @{upstream} and @{push} are supported"),
> +			name);
> +		return NULL;
> +	}

I don't know if which one between the above "warning" and die("unknown/unsupported") is a better design. In any case, the warning() line is overly long and needs to be wrapped (my litmus test to complain about "overly long lines" is after losing three columns to the left for "> +" in e-mail quote like the above the right edge of the line does not fit on my 92-column terminal, which will never happen if you stick to the official "fit 80-column after quoted for a few times in e-mail" guideline).

Show 14 quoted lines
>  static void format_branch_comparison(struct strbuf *sb,
>  				     bool up_to_date,
>  				     int ours, int theirs,
>  				     const char *branch_name,
>  				     enum ahead_behind_flags abf,
> -				     bool show_divergence_advice)
> +				     unsigned flags)
>  {
> +	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
> +		advice_enabled(ADVICE_STATUS_HINTS);
> +	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
> +		advice_enabled(ADVICE_STATUS_HINTS);
> +	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
> +		advice_enabled(ADVICE_STATUS_HINTS);

You do use enable_push_advice twice so that reduces repetition a little bit, but other than that, I am not sure if the above makes it easier to follow the code.

Especially, the roundabout way these three variables are computed, together with the fact that ENABLE_ADVICE_* are not really about "enabling", but the caller decides that it is an applicable advice (e.g., ENABLE_ADVICE_PULL is passed when the iteration over branches.items[] the caller is making is on the upstream branch and "you should pull" is the appropriate situation. It is more like "advice-pull is applicable in this situation", unlike what the advice_enabled() returns, which is "the user wants this kind of advice messages"), I find the resulting code below somehow harder to follow than without these intermediate variables. If they were named "use_*_advice" (instead of "enable_"), perhaps I may find it more palatable.

I find it a lot more disturbing that what the caller specifies in flags and advice_enabled() is pre-combined in these variables. Perhaps splitting them into two separate concerns, like this ...

	bool use_push_advice = (flags & USE_ADVICE_PUSH);
	bool use_pull_advice = (flags & USE_ADVICE_PULL);
	bool use_divergence_advice = (flags & USE_ADVICE_DIVERGENCE);
	...
	if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS))
		... you should pull first ...
... may make it easier to understand and follow?  I dunno.
> -		if (advice_enabled(ADVICE_STATUS_HINTS))
> +		if (enable_push_advice)
>  			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
>  				    "git status --ahead-behind");
Show 47 quoted lines
> diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
> index 0b719bbae6..aa9456bb61 100755
> --- a/t/t6040-tracking-info.sh
> +++ b/t/t6040-tracking-info.sh
> @@ -292,4 +292,339 @@ test_expect_success '--set-upstream-to @{-1}' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'status tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status --no-ahead-behind >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
> +	  (use "git status --ahead-behind" for details)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'setup for compareBranches tests' '
> +	(
> +		cd test &&
> +		git config push.default current &&
> +		git config status.compareBranches "@{upstream} @{push}"
> +	)
> +'

Shoudln't this be part of the next test, with its own teardown? I.e.,

	test_expect_success 'check both @{upstream} and @{push}' '
        	test_config -C test push.default current &&
        	test_config -C test status.compareBranches "..." &&
		git -C test checkout main &&
		git -C status >actual &&
		cat >expect <<-EOF &&
		...
		EOF
		test_cmp expect actual
	'
Show 14 quoted lines
> +test_expect_success 'status.compareBranches from upstream has no duplicates' '
> +	(
> +		cd test &&
> +		git checkout main &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch main
> +	Your branch is up to date with ${SQ}origin/main${SQ}.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

That way, the tests can be kept more independent from each other, and will clean after themselves, which means that we do not have to assume that ...

> ...
Show 6 quoted lines
> +test_expect_success 'clean up after compareBranches tests' '
> +	(
> +		cd test &&
> +		git config --unset status.compareBranches
> +	)
> +'

... this step will never be skipped or fail, which would result in disturbing the tests that come after this step.

In any case, the design of the main part of the patch is looking much nicer than the open-ended one we saw previously.

Thanks.
Harald Nordgren via GitGitGadget· Feb 26, 2026, 10:33 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  19 ++
 remote.c                         | 178 ++++++++++++++----
 t/t6040-tracking-info.sh         | 310 +++++++++++++++++++++++++++++++
 3 files changed, 470 insertions(+), 37 deletions(-)
base-commit: 7b2bccb0d58d4f24705bf985de1f4612e4cf06e5
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v30
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v30
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v29:
 1:  48db1f4847 = 1:  7f517b8c7f refactor format_branch_comparison in preparation
 2:  6a88f41fa5 ! 2:  501bd40294 status: add status.compareBranches config for multiple branch comparisons
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      +	if (!branch || !name)
      +		return NULL;
      +
     -+	if (!strcasecmp(name, "@{upstream}"))
     ++	if (!strcasecmp(name, "@{upstream}")) {
      +		resolved = branch_get_upstream(branch, NULL);
     -+	else if (!strcasecmp(name, "@{push}"))
     ++	} else if (!strcasecmp(name, "@{push}")) {
      +		resolved = branch_get_push(branch, NULL);
     -+	else {
     -+		warning(_("ignoring value '%s' for status.compareBranches; only @{upstream} and @{push} are supported"),
     ++	} else {
     ++		warning(_("ignoring value '%s' for status.compareBranches, "
     ++			  "only @{upstream} and @{push} are supported"),
      +			name);
      +		return NULL;
      +	}
     @@ remote.c: int stat_tracking_info(struct branch *branch, int *num_ours, int *num_
      -				     bool show_divergence_advice)
      +				     unsigned flags)
       {
     -+	bool enable_push_advice = (flags & ENABLE_ADVICE_PUSH) &&
     -+		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool enable_pull_advice = (flags & ENABLE_ADVICE_PULL) &&
     -+		advice_enabled(ADVICE_STATUS_HINTS);
     -+	bool enable_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE) &&
     -+		advice_enabled(ADVICE_STATUS_HINTS);
     ++	bool use_push_advice = (flags & ENABLE_ADVICE_PUSH);
     ++	bool use_pull_advice = (flags & ENABLE_ADVICE_PULL);
     ++	bool use_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE);
      +
       	if (up_to_date) {
       		strbuf_addf(sb,
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			    _("Your branch and '%s' refer to different commits.\n"),
       			    branch_name);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (enable_push_advice)
     ++		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
       				    "git status --ahead-behind");
       	} else if (!theirs) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   ours),
       			branch_name, ours);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (enable_push_advice)
     ++		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git push\" to publish your local commits)\n"));
       	} else if (!ours) {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			   theirs),
       			branch_name, theirs);
      -		if (advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (enable_pull_advice)
     ++		if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" to update your local branch)\n"));
       	} else {
     @@ remote.c: static void format_branch_comparison(struct strbuf *sb,
       			branch_name, ours, theirs);
      -		if (show_divergence_advice &&
      -		    advice_enabled(ADVICE_STATUS_HINTS))
     -+		if (enable_divergence_advice)
     ++		if (use_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS))
       			strbuf_addstr(sb,
       				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
       	}
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'setup for compareBranches tests' '
     -+	(
     -+		cd test &&
     -+		git config push.default current &&
     -+		git config status.compareBranches "@{upstream} @{push}"
     -+	)
     -+'
     -+
      +test_expect_success 'status.compareBranches from upstream has no duplicates' '
     -+	(
     -+		cd test &&
     -+		git checkout main &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout main &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch main
      +	Your branch is up to date with ${SQ}origin/main${SQ}.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature2 origin/main &&
     -+		git push origin HEAD &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature2 origin/main &&
     ++	git -C test push origin HEAD &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature2
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'checkout with status.compareBranches shows both branches' '
     -+	(
     -+		cd test &&
     -+		git checkout feature2 >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout feature2 >actual &&
      +	cat >expect <<-EOF &&
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches shows diverged and ahead' '
     -+	(
     -+		cd test &&
     -+		git checkout feature4 &&
     -+		git branch --set-upstream-to origin/main &&
     -+		git push origin HEAD &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout feature4 &&
     ++	git -C test branch --set-upstream-to origin/main &&
     ++	git -C test push origin HEAD &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature4
      +	Your branch and ${SQ}origin/main${SQ} have diverged,
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status --no-ahead-behind with status.compareBranches' '
     -+	(
     -+		cd test &&
     -+		git checkout feature4 &&
     -+		git status --no-ahead-behind >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout feature4 &&
     ++	git -C test status --no-ahead-behind >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature4
      +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	(
      +		cd test &&
      +		git remote add upstream ../. &&
     -+		git fetch upstream &&
     -+		git config remote.pushDefault origin
     ++		git fetch upstream
      +	)
      +'
      +
      +test_expect_success 'status.compareBranches with upstream and origin remotes' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature5 upstream/main &&
     -+		git push origin &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature5 upstream/main &&
     ++	git -C test push origin &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature5
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature6 upstream/main &&
     -+		git push origin &&
     -+		advance work &&
     -+		git -c status.compareBranches="@{push} @{upstream}" status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{push} @{upstream}" &&
     ++	git -C test checkout -b feature6 upstream/main &&
     ++	git -C test push origin &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature6
      +	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches with diverged push branch' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature7 upstream/main &&
     -+		advance work &&
     -+		git push origin &&
     -+		git reset --hard upstream/main &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature7 upstream/main &&
     ++	(cd test && advance work71) &&
     ++	git -C test push origin &&
     ++	git -C test reset --hard upstream/main &&
     ++	(cd test && advance work72) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature7
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches shows up to date branches' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature8 upstream/main &&
     -+		git push origin &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature8 upstream/main &&
     ++	git -C test push origin &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature8
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
     -+	(
     -+		cd test &&
     -+		git checkout feature8 &&
     -+		git push origin &&
     -+		git status --no-ahead-behind >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout feature8 >actual &&
     ++	git -C test push origin &&
     ++	git -C test status --no-ahead-behind >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature8
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'checkout with status.compareBranches shows up to date' '
     -+	(
     -+		cd test &&
     -+		git checkout feature8 >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout feature8 >actual &&
      +	cat >expect <<-EOF &&
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches with upstream behind and push up to date' '
     -+	(
     -+		cd test &&
     -+		git checkout -b ahead upstream/main &&
     -+		advance work &&
     -+		git push upstream HEAD &&
     -+		git checkout -b feature9 upstream/main &&
     -+		git push origin &&
     -+		git branch --set-upstream-to upstream/ahead &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b ahead upstream/main &&
     ++	(cd test && advance work) &&
     ++	git -C test push upstream HEAD &&
     ++	git -C test checkout -b feature9 upstream/main &&
     ++	git -C test push origin &&
     ++	git -C test branch --set-upstream-to upstream/ahead &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature9
      +	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches with remapped push refspec' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature10 origin/main &&
     -+		git config remote.origin.push refs/heads/feature10:refs/heads/remapped &&
     -+		git push &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test remote.origin.push refs/heads/feature10:refs/heads/remapped &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature10 origin/main &&
     ++	git -C test push &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature10
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches with remapped push and upstream remote' '
     -+	(
     -+		cd test &&
     -+		git checkout -b feature11 upstream/main &&
     -+		git config remote.origin.push refs/heads/feature11:refs/heads/remapped &&
     -+		git push origin &&
     -+		advance work &&
     -+		git status >../actual
     -+	) &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature11 upstream/main &&
     ++	git -C test push origin &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature11
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	EOF
      +	test_cmp expect actual
      +'
     -+
     -+test_expect_success 'clean up after compareBranches tests' '
     -+	(
     -+		cd test &&
     -+		git config --unset status.compareBranches
     -+	)
     -+'
      +
       test_done
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Feb 26, 2026, 10:33 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v30 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index f6980dc656..e9e2f56ed6 100644
--- a/remote.c
+++ b/remote.c
@@ -2234,42 +2234,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2278,7 +2257,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2289,7 +2268,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2302,12 +2281,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Feb 26, 2026, 10:33 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v30 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable status.compareBranches that allows users to specify a space-separated list of branch comparisons in git status output.

Supported values:
- @{upstream} for the current branch's upstream tracking branch
- @{push} for the current branch's push destination
Any other value is ignored and a warning is shown.

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  19 ++
 remote.c                         | 146 ++++++++++++---
 t/t6040-tracking-info.sh         | 310 +++++++++++++++++++++++++++++++
 3 files changed, 447 insertions(+), 28 deletions(-)
Show changes to 3 files +447 −28

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..15ccd0116b 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,25 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branch comparison specifiers to use in
+	linkgit:git-status[1]. Currently, only `@{upstream}` and `@{push}`
+	are supported. They are interpreted as `branch@{upstream}` and
+	`branch@{push}` for the current branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+Example:
++
+----
+[status]
+	compareBranches = @{upstream} @{push}
+----
++
+This would show comparisons against both the configured upstream and push
+tracking branches for the current branch.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index e9e2f56ed6..7ca2a6501b 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2234,13 +2240,40 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	const char *resolved = NULL;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}")) {
+		resolved = branch_get_upstream(branch, NULL);
+	} else if (!strcasecmp(name, "@{push}")) {
+		resolved = branch_get_push(branch, NULL);
+	} else {
+		warning(_("ignoring value '%s' for status.compareBranches, "
+			  "only @{upstream} and @{push} are supported"),
+			name);
+		return NULL;
+	}
+
+	if (resolved)
+		return xstrdup(resolved);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool use_push_advice = (flags & ENABLE_ADVICE_PUSH);
+	bool use_pull_advice = (flags & ENABLE_ADVICE_PULL);
+	bool use_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2249,7 +2282,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2258,7 +2291,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2269,7 +2302,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2282,8 +2315,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,34 +2328,92 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
+	char *compare_branches = NULL;
+	struct string_list branches = STRING_LIST_INIT_DUP;
+	struct strset processed_refs = STRSET_INIT;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
 
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches);
+
+	if (compare_branches) {
+		string_list_split(&branches, compare_branches, " ", -1);
+		string_list_remove_empty_items(&branches, 0);
+	} else {
+		string_list_append(&branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		full_ref = resolve_compare_branch(branch,
+						  branches.items[i].string);
+		if (!full_ref)
+			continue;
+
+		if (!strset_add(&processed_refs, full_ref)) {
+			free(full_ref);
+			continue;
+		}
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&branches, 0);
+	strset_clear(&processed_refs);
+	free(compare_branches);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..c24f545036 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,314 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	(
+		cd test &&
+		git checkout b4 &&
+		git status --no-ahead-behind >../actual
+	) &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches from upstream has no duplicates' '
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout main &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches shows ahead of both upstream and push branch' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature2 origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature2 >actual &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature4 &&
+	git -C test branch --set-upstream-to origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature4 &&
+	git -C test status --no-ahead-behind >actual &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature5 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{push} @{upstream}" &&
+	git -C test checkout -b feature6 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature7 upstream/main &&
+	(cd test && advance work71) &&
+	git -C test push origin &&
+	git -C test reset --hard upstream/main &&
+	(cd test && advance work72) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature8 upstream/main &&
+	git -C test push origin &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature8 >actual &&
+	git -C test push origin &&
+	git -C test status --no-ahead-behind >actual &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature8 >actual &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b ahead upstream/main &&
+	(cd test && advance work) &&
+	git -C test push upstream HEAD &&
+	git -C test checkout -b feature9 upstream/main &&
+	git -C test push origin &&
+	git -C test branch --set-upstream-to upstream/ahead &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	test_config -C test remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature10 origin/main &&
+	git -C test push &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature11 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Harald Nordgren· Feb 26, 2026, 13:47 UTC · re: Junio C Hamano · lore

Re: [PATCH v29 2/2] status: add status.compareBranches config for multiple branch comparisons

> You do use enable_push_advice twice so that reduces repetition a
> little bit, but other than that, I am not sure if the above makes it
> easier to follow the code.

This was orginally abstracted because of feedback from someone else in this patch. But I update it now, togethe with the rest of your feedback, thanks for the help!

Harald
Harald Nordgren· Feb 26, 2026, 13:50 UTC · re: Jeff King · lore

Re: [PATCH v28 2/2] status: add status.compareBranches config for multiple branch comparisons

> I think what would have moved things forward more than just a ping,
> though, is generating a plan for moving forward in the face of confusing
> review.
Very fair point!
> That can be quite hard for first-time contributors, though.

I'm not actually a first time contributor 🤗 I worked on this feature between around 2016-18. So I know it can take a while:

https://git.kernel.org/pub/scm/git/git.git/commit/?h=seen&id=1fb20dfd8ed70b4459312918a71444bc79ea6f0b 
Harald
Junio C Hamano· Feb 26, 2026, 15:28 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 16 quoted lines
> cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin
> ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net
> cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk"
> kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com
> cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc:
> Jeff King peff@peff.net
>
> Harald Nordgren (2):
>   refactor format_branch_comparison in preparation
>   status: add status.compareBranches config for multiple branch
>     comparisons
>
>  Documentation/config/status.adoc |  19 ++
>  remote.c                         | 178 ++++++++++++++----
>  t/t6040-tracking-info.sh         | 310 +++++++++++++++++++++++++++++++
>  3 files changed, 470 insertions(+), 37 deletions(-)

Will replace. Let's wait to see if we hear further comments on the topic for a day or two and then mark the topic for 'next'. I didn't spot anything unexpected in this round, but haven't had enough time to spend on the test part, which I want to read a bit more carefully before merging.

Thanks.
Harald Nordgren· Mar 1, 2026, 19:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

> Will replace.
What does this mean? 🤗
Show 5 quoted lines
> Let's wait to see if we hear further comments on the
> topic for a day or two and then mark the topic for 'next'.  I didn't
> spot anything unexpected in this round, but haven't had enough time
> to spend on the test part, which I want to read a bit more carefully
> before merging.
Did you have a chance to look at the tests?
Harald
Kristoffer Haugsbakk· Mar 1, 2026, 19:25 UTC · re: Harald Nordgren · lore

Re: [PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

On Sun, Mar 1, 2026, at 20:06, Harald Nordgren wrote:
>> Will replace.
>
> What does this mean? 🤗

That he will replace the branch he has for this topic (hn/status-compare-with-push) with this new round.

>[snip]
Junio C Hamano· Mar 2, 2026, 16:52 UTC · re: Harald Nordgren · lore

Re: [PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
Show 7 quoted lines
>> Let's wait to see if we hear further comments on the
>> topic for a day or two and then mark the topic for 'next'.  I didn't
>> spot anything unexpected in this round, but haven't had enough time
>> to spend on the test part, which I want to read a bit more carefully
>> before merging.
>
> Did you have a chance to look at the tests?
Not me, it was a bit too messy for reading in a single sitting.
Junio C Hamano· Mar 2, 2026, 23:52 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v30 2/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 42 quoted lines
>  t/t6040-tracking-info.sh         | 310 +++++++++++++++++++++++++++++++
>  3 files changed, 447 insertions(+), 28 deletions(-)
>
> diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
> index 0b719bbae6..c24f545036 100755
> --- a/t/t6040-tracking-info.sh
> +++ b/t/t6040-tracking-info.sh
> @@ -292,4 +292,314 @@ test_expect_success '--set-upstream-to @{-1}' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'status tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
> +	(
> +		cd test &&
> +		git checkout b4 &&
> +		git status --no-ahead-behind >../actual
> +	) &&
> +	cat >expect <<-EOF &&
> +	On branch b4
> +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
> +	  (use "git status --ahead-behind" for details)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

The above two uses ( cd test && git $command ) to do what it can do with "git -C test $command", but the rest of the script seems to mostly stick to the latter. Want to be a bit more consistent?

Show 11 quoted lines
> +test_expect_success 'status.compareBranches from upstream has no duplicates' '
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout main &&
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch main
> +	Your branch is up to date with ${SQ}origin/main${SQ}.
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

We see only one because... @{upstream} and @{push} are both origin/main and we can dedupe and there is no extra advice needed? If @{push} were missing and only @{upstream} existed, we would also see just one, so this test feels a bit under-explained.

Show 6 quoted lines
> +test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
> +	test_config -C test push.default current &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout -b feature2 origin/main &&
> +	git -C test push origin HEAD &&
> +	(cd test && advance work) &&

One thing the "advance" function does is to call "test_tick" to increment the mock timestamp, but the incremented mock timestamp would not survive beyond the end of a subshell. Not that it matters too much to have commits with the same timestamp in these tests, as long as things are made more reproducible by use of the "advance" function.

Show 12 quoted lines
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature2
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

Very straight-forward If we ask for @{upstream} and @{push} that resolve to different things, we would see two different reports.

I am curious what should [jc: not asking what the code happens to do; wondering what the sensible behaviour to help users is] happen when the user configures the variable to "@{push} @{upstream} @{push}" in this case. Should we see just two entries (one for @{push}, the other for @{upstream})?

I also am curious what should [jc: ditto] happen when @{upstream} and @{push} point at the same origin/main and our current branch is ahead by 1 commit. The pull side would say "you are ahead of origin/main" and stop wile the push side would say the same thing with advice to push it out for publishing? Or should they get deduped?

Show 12 quoted lines
> +test_expect_success 'checkout with status.compareBranches shows both branches' '
> +	test_config -C test push.default current &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout feature2 >actual &&
> +	cat >expect <<-EOF &&
> +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +	EOF
> +	test_cmp expect actual
> +'

If the previous step succeeded, we would be on feature2 branch in the "test" repository, so this checkout is a no-op-only-for-status checkout and we expect to see the same thing as the previous step.

Good.

If the previous step failed, this step may break, but that is what "cd t && sh t6040*.sh -i" is for ;-).

Show 16 quoted lines
> +test_expect_success 'setup for ahead of tracked but diverged from main' '
> +	(
> +		cd test &&
> +		git checkout -b feature4 origin/main &&
> +		advance work1 &&
> +		git checkout origin/main &&
> +		advance work2 &&
> +		git push origin HEAD:main &&
> +		git checkout feature4 &&
> +		advance work3
> +	)
> +'
> +
> +test_expect_success 'status.compareBranches shows diverged and ahead' '
> +	test_config -C test push.default current &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
Show 18 quoted lines
> +	git -C test checkout feature4 &&
> +	git -C test branch --set-upstream-to origin/main &&
> +	git -C test push origin HEAD &&
> +	(cd test && advance work) &&
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature4
> +	Your branch and ${SQ}origin/main${SQ} have diverged,
> +	and have 3 and 1 different commits each, respectively.
> +	  (use "git pull" if you want to integrate the remote branch with yours)
> +
> +	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
Very understandable.
Show 16 quoted lines
> +test_expect_success 'status --no-ahead-behind with status.compareBranches' '
> +	test_config -C test push.default current &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout feature4 &&
> +	git -C test status --no-ahead-behind >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature4
> +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
> +
> +	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
> +	  (use "git status --ahead-behind" for details)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'
OK.
Show 27 quoted lines
> +test_expect_success 'setup upstream remote' '
> +	(
> +		cd test &&
> +		git remote add upstream ../. &&
> +		git fetch upstream
> +	)
> +'
> +
> +test_expect_success 'status.compareBranches with upstream and origin remotes' '
> +	test_config -C test push.default current &&
> +	test_config -C test remote.pushDefault origin &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout -b feature5 upstream/main &&
> +	git -C test push origin &&
> +	(cd test && advance work) &&
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature5
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

OK. There aren't much essential difference from what we have already seen so far, which dealt with remote tracking branch(es) from a single remote repository. Instead of origin/{main,$feature}, we are now dealing with {upstream,origin}/{main,$feature} but we can only pick one for @{upstream} and one for @{push}, we do not have to worry about all 4 possible permutations.

Show 19 quoted lines
> +test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
> +	test_config -C test push.default current &&
> +	test_config -C test remote.pushDefault origin &&
> +	test_config -C test status.compareBranches "@{push} @{upstream}" &&
> +	git -C test checkout -b feature6 upstream/main &&
> +	git -C test push origin &&
> +	(cd test && advance work) &&
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature6
> +	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
> +	  (use "git push" to publish your local commits)
> +
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

Ah, I guess this example hints the answer to my earlier "@{push} @{upstream} @{push}" question? The user can control the order of entries shown by tweaking the order they list things on the configuration variable. So we would see the one for @{push} and then another for @{upstream}. We can show the one for @{push} again after these two and claim that it is working as the user told us to, or we can stop showing after showing @{push} and then @{upstream} and claim that we are deduping. Either is fine, but we need to be consistent and we need to have it documented.

Show 21 quoted lines
> +test_expect_success 'status.compareBranches with diverged push branch' '
> +	test_config -C test push.default current &&
> +	test_config -C test remote.pushDefault origin &&
> +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
> +	git -C test checkout -b feature7 upstream/main &&
> +	(cd test && advance work71) &&
> +	git -C test push origin &&
> +	git -C test reset --hard upstream/main &&
> +	(cd test && advance work72) &&
> +	git -C test status >actual &&
> +	cat >expect <<-EOF &&
> +	On branch feature7
> +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
> +
> +	Your branch and ${SQ}origin/feature7${SQ} have diverged,
> +	and have 1 and 1 different commits each, respectively.
> +
> +	nothing to commit, working tree clean
> +	EOF
> +	test_cmp expect actual
> +'

The same as before, I guess. Nothing unexpected to see here, or in the rest of the patch.

So td;lr is that the tests in this step mostly look great. It is just the semantics of "dedup" is a bit unclear, without a test that makes it clear (like the "@{push} @{upstream} @{push}" one), and perhaps a bit more documentation how having multiple tokens on the status.compareBranches variable affects the output. A (n easier) half of that additional documentation could be as small as the following, but I didn't attempt to deal with the "dedup" part.

 Documentation/config/status.adoc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Show changes to diff +2 −1
diff --git c/Documentation/config/status.adoc w/Documentation/config/status.adoc
index 15ccd0116b..e2c4a014a4 100644
--- c/Documentation/config/status.adoc
+++ w/Documentation/config/status.adoc
@@ -34,7 +34,8 @@ Example:
 ----
 +
 This would show comparisons against both the configured upstream and push
-tracking branches for the current branch.
+tracking branches for the current branch in this order (as that is how
+upstream and push appear in the value of the variable).
 
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
Harald Nordgren· Mar 4, 2026, 10:30 UTC · re: Junio C Hamano · lore

Re: [PATCH v30 0/2] status: add status.compareBranches config for multiple branch comparisons

> The above two uses ( cd test && git $command ) to do what it can do
> with "git -C test $command", but the rest of the script seems to
> mostly stick to the latter.  Want to be a bit more consistent?
Good point, will fix!
> We see only one because... @{upstream} and @{push} are both
> origin/main and we can dedupe and there is no extra advice needed?
> If @{push} were missing and only @{upstream} existed, we would also
> see just one, so this test feels a bit under-explained.

Renaming this this now to clarify that it tests deduplicating. Also added two new tests, for when reach of @{push} and @{upstream} are used alone.

Show 6 quoted lines
> One thing the "advance" function does is to call "test_tick" to
> increment the mock timestamp, but the incremented mock timestamp
> would not survive beyond the end of a subshell.  Not that it matters
> too much to have commits with the same timestamp in these tests, as
> long as things are made more reproducible by use of the "advance"
> function.

Yeah, it's not great when used with subshells, I ended up differentiting commits only by name like 'advance work2' because as you say, otherwise they will get the same commit hash.

Show 6 quoted lines
> I also am curious what should [jc: ditto] happen when @{upstream}
> and @{push} point at the same origin/main and our current branch is
> ahead by 1 commit.  The pull side would say "you are ahead of
> origin/main" and stop wile the push side would say the same thing
> with advice to push it out for publishing?  Or should they get
> deduped?
Jumping in altough the question was not directed to me:

When behind and ahead of the same branch we show the "diverged" message, and since only one line is shown when deduped I think it works in a logical way.

Show 7 quoted lines
> So td;lr is that the tests in this step mostly look great.  It is
> just the semantics of "dedup" is a bit unclear, without a test that
> makes it clear (like the "@{push} @{upstream} @{push}" one), and
> perhaps a bit more documentation how having multiple tokens on the
> status.compareBranches variable affects the output.  A (n easier)
> half of that additional documentation could be as small as the
> following, but I didn't attempt to deal with the "dedup" part.
Will add documentation for the depuping and a new test case.
Harald
Harald Nordgren via GitGitGadget· Mar 4, 2026, 12:25 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v31 0/2] status: add status.compareBranches config for multiple branch comparisons

cc: Chris Torek chris.torek@gmail.com cc: Yee Cheng Chin ychin.macvim@gmail.com cc: "brian m. carlson" sandals@crustytoothpaste.net cc: Ben Knoble ben.knoble@gmail.com cc: "Kristoffer Haugsbakk" kristofferhaugsbakk@fastmail.com cc: Phillip Wood phillip.wood123@gmail.com cc: Nico Williams nico@cryptonector.com cc: Patrick Steinhardt ps@pks.im cc: Jeff King peff@peff.net

Harald Nordgren (2):
  refactor format_branch_comparison in preparation
  status: add status.compareBranches config for multiple branch
    comparisons
 Documentation/config/status.adoc |  25 +++
 remote.c                         | 178 ++++++++++++----
 t/t6040-tracking-info.sh         | 354 +++++++++++++++++++++++++++++++
 3 files changed, 520 insertions(+), 37 deletions(-)
base-commit: 50d063e335afd5828fbb9de2f2b2fb44fd884d2b
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v31
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v31
Pull-Request: https://github.com/git/git/pull/2138
Range-diff vs v30:
 1:  7f517b8c7f = 1:  320c1ce55a refactor format_branch_comparison in preparation
 2:  501bd40294 ! 2:  f07ccb278c status: add status.compareBranches config for multiple branch comparisons
     @@ Documentation/config/status.adoc: status.aheadBehind::
      +If not set, the default behavior is equivalent to `@{upstream}`, which
      +compares against the configured upstream tracking branch.
      ++
     ++The entries are shown in the order they appear in the configuration.
     ++Duplicate entries that resolve to the same ref are suppressed after
     ++their first occurrence, so `@{push} @{upstream} @{push}` shows at
     ++most two comparisons.  When `@{upstream}` and `@{push}` resolve to
     ++the same remote-tracking branch, only one comparison is shown.
     +++
      +Example:
      ++
      +----
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
       '
       
      +test_expect_success 'status tracking origin/main shows only main' '
     -+	(
     -+		cd test &&
     -+		git checkout b4 &&
     -+		git status >../actual
     -+	) &&
     ++	git -C test checkout b4 &&
     ++	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch b4
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
     -+	(
     -+		cd test &&
     -+		git checkout b4 &&
     -+		git status --no-ahead-behind >../actual
     -+	) &&
     ++	git -C test checkout b4 &&
     ++	git -C test status --no-ahead-behind >actual &&
      +	cat >expect <<-EOF &&
      +	On branch b4
      +	Your branch and ${SQ}origin/main${SQ} refer to different commits.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status.compareBranches from upstream has no duplicates' '
     ++test_expect_success 'status.compareBranches deduplicates when upstream and push are the same' '
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
      +	git -C test checkout main &&
      +	git -C test status >actual &&
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     -+test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
     ++test_expect_success 'status.compareBranches with only upstream shows only upstream' '
     ++	test_config -C test status.compareBranches "@{upstream}" &&
     ++	git -C test checkout main &&
     ++	git -C test status >actual &&
     ++	cat >expect <<-EOF &&
     ++	On branch main
     ++	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 'status.compareBranches with only push shows only push' '
      +	test_config -C test push.default current &&
     -+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	test_config -C test status.compareBranches "@{push}" &&
      +	git -C test checkout -b feature2 origin/main &&
      +	git -C test push origin HEAD &&
      +	(cd test && advance work) &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
      +	On branch feature2
     ++	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
     ++	test_config -C test push.default current &&
     ++	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     ++	git -C test checkout -b feature3 origin/main &&
     ++	git -C test push origin HEAD &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature3
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
     -+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
      +	  (use "git push" to publish your local commits)
      +
      +	nothing to commit, working tree clean
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +test_expect_success 'checkout with status.compareBranches shows both branches' '
      +	test_config -C test push.default current &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout feature2 >actual &&
     ++	git -C test checkout feature3 >actual &&
      +	cat >expect <<-EOF &&
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
     -+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
     ++	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
      +	  (use "git push" to publish your local commits)
      +	EOF
      +	test_cmp expect actual
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_cmp expect actual
      +'
      +
     ++test_expect_success 'status.compareBranches deduplicates repeated specifiers' '
     ++	test_config -C test push.default current &&
     ++	test_config -C test remote.pushDefault origin &&
     ++	test_config -C test status.compareBranches "@{push} @{upstream} @{push}" &&
     ++	git -C test checkout -b feature7 upstream/main &&
     ++	git -C test push origin &&
     ++	(cd test && advance work) &&
     ++	git -C test status >actual &&
     ++	cat >expect <<-EOF &&
     ++	On branch feature7
     ++	Your branch is ahead of ${SQ}origin/feature7${SQ} by 1 commit.
     ++	  (use "git push" to publish your local commits)
     ++
     ++	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
     ++
     ++	nothing to commit, working tree clean
     ++	EOF
     ++	test_cmp expect actual
     ++'
     ++
      +test_expect_success 'status.compareBranches with diverged push branch' '
      +	test_config -C test push.default current &&
      +	test_config -C test remote.pushDefault origin &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout -b feature7 upstream/main &&
     -+	(cd test && advance work71) &&
     ++	git -C test checkout -b feature8 upstream/main &&
     ++	(cd test && advance work81) &&
      +	git -C test push origin &&
      +	git -C test reset --hard upstream/main &&
     -+	(cd test && advance work72) &&
     ++	(cd test && advance work82) &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature7
     ++	On branch feature8
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
     -+	Your branch and ${SQ}origin/feature7${SQ} have diverged,
     ++	Your branch and ${SQ}origin/feature8${SQ} have diverged,
      +	and have 1 and 1 different commits each, respectively.
      +
      +	nothing to commit, working tree clean
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_config -C test push.default current &&
      +	test_config -C test remote.pushDefault origin &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout -b feature8 upstream/main &&
     ++	git -C test checkout -b feature9 upstream/main &&
      +	git -C test push origin &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature8
     ++	On branch feature9
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature9${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_config -C test push.default current &&
      +	test_config -C test remote.pushDefault origin &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout feature8 >actual &&
     ++	git -C test checkout feature9 >actual &&
      +	git -C test push origin &&
      +	git -C test status --no-ahead-behind >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature8
     ++	On branch feature9
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature9${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	test_config -C test push.default current &&
      +	test_config -C test remote.pushDefault origin &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout feature8 >actual &&
     ++	git -C test checkout feature9 >actual &&
      +	cat >expect <<-EOF &&
      +	Your branch is up to date with ${SQ}upstream/main${SQ}.
      +
     -+	Your branch is up to date with ${SQ}origin/feature8${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature9${SQ}.
      +	EOF
      +	test_cmp expect actual
      +'
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +	git -C test checkout -b ahead upstream/main &&
      +	(cd test && advance work) &&
      +	git -C test push upstream HEAD &&
     -+	git -C test checkout -b feature9 upstream/main &&
     ++	git -C test checkout -b feature10 upstream/main &&
      +	git -C test push origin &&
      +	git -C test branch --set-upstream-to upstream/ahead &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature9
     ++	On branch feature10
      +	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
      +	  (use "git pull" to update your local branch)
      +
     -+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
     ++	Your branch is up to date with ${SQ}origin/feature10${SQ}.
      +
      +	nothing to commit, working tree clean
      +	EOF
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +'
      +
      +test_expect_success 'status.compareBranches with remapped push refspec' '
     -+	test_config -C test remote.origin.push refs/heads/feature10:refs/heads/remapped &&
     ++	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout -b feature10 origin/main &&
     ++	git -C test checkout -b feature11 origin/main &&
      +	git -C test push &&
      +	(cd test && advance work) &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature10
     ++	On branch feature11
      +	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
     @@ t/t6040-tracking-info.sh: test_expect_success '--set-upstream-to @{-1}' '
      +
      +test_expect_success 'status.compareBranches with remapped push and upstream remote' '
      +	test_config -C test remote.pushDefault origin &&
     -+	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
     ++	test_config -C test remote.origin.push refs/heads/feature12:refs/heads/remapped &&
      +	test_config -C test status.compareBranches "@{upstream} @{push}" &&
     -+	git -C test checkout -b feature11 upstream/main &&
     ++	git -C test checkout -b feature12 upstream/main &&
      +	git -C test push origin &&
      +	(cd test && advance work) &&
      +	git -C test status >actual &&
      +	cat >expect <<-EOF &&
     -+	On branch feature11
     ++	On branch feature12
      +	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
      +
      +	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Mar 4, 2026, 12:25 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v31 1/2] refactor format_branch_comparison in preparation

From: Harald Nordgren <haraldnordgren@gmail.com>

Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 remote.c | 82 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 34 deletions(-)
Show changes to remote.c +48 −34
diff --git a/remote.c b/remote.c
index f6980dc656..e9e2f56ed6 100644
--- a/remote.c
+++ b/remote.c
@@ -2234,42 +2234,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
-			 enum ahead_behind_flags abf,
-			 int show_divergence_advice)
-{
-	int ours, theirs, sti;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
-
-	sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (sti < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
-	}
-
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else if (!sti) {
+static void format_branch_comparison(struct strbuf *sb,
+				     bool up_to_date,
+				     int ours, int theirs,
+				     const char *branch_name,
+				     enum ahead_behind_flags abf,
+				     bool show_divergence_advice)
+{
+	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
-			base);
+			branch_name);
 	} else if (abf == AHEAD_BEHIND_QUICK) {
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
-			    base);
+			    branch_name);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
@@ -2278,7 +2257,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			Q_("Your branch is ahead of '%s' by %d commit.\n",
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
-			base, ours);
+			branch_name, ours);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
@@ -2289,7 +2268,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			   "Your branch is behind '%s' by %d commits, "
 			       "and can be fast-forwarded.\n",
 			   theirs),
-			base, theirs);
+			branch_name, theirs);
 		if (advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
@@ -2302,12 +2281,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			       "and have %d and %d different commits each, "
 			       "respectively.\n",
 			   ours + theirs),
-			base, ours, theirs);
+			branch_name, ours, theirs);
 		if (show_divergence_advice &&
 		    advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+			 enum ahead_behind_flags abf,
+			 int show_divergence_advice)
+{
+	int ours, theirs, cmp_fetch;
+	const char *full_base;
+	char *base;
+	int upstream_is_gone = 0;
+
+	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+	if (cmp_fetch < 0) {
+		if (!full_base)
+			return 0;
+		upstream_is_gone = 1;
+	}
+
+	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+					    full_base, 0);
+
+	if (upstream_is_gone) {
+		strbuf_addf(sb,
+			_("Your branch is based on '%s', but the upstream is gone.\n"),
+			base);
+		if (advice_enabled(ADVICE_STATUS_HINTS))
+			strbuf_addstr(sb,
+				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+	} else {
+		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	}
+
 	free(base);
 	return 1;
 }
-- 
gitgitgadget
Harald Nordgren via GitGitGadget· Mar 4, 2026, 12:25 UTC · re: Harald Nordgren via GitGitGadget · lore

[PATCH v31 2/2] status: add status.compareBranches config for multiple branch comparisons

From: Harald Nordgren <haraldnordgren@gmail.com>

Add a new configuration variable status.compareBranches that allows users to specify a space-separated list of branch comparisons in git status output.

Supported values:
- @{upstream} for the current branch's upstream tracking branch
- @{push} for the current branch's push destination
Any other value is ignored and a warning is shown.

When not configured, the default behavior is equivalent to setting `status.compareBranches = @{upstream}`, preserving backward compatibility.

The advice messages shown are context-aware:
- "git pull" advice is shown only when comparing against @{upstream}
- "git push" advice is shown only when comparing against @{push}
- Divergence advice is shown for upstream branch comparisons

This is useful for triangular workflows where the upstream tracking branch differs from the push destination, allowing users to see their status relative to both branches at once.

Example configuration:
    [status]
        compareBranches = @{upstream} @{push}
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 Documentation/config/status.adoc |  25 +++
 remote.c                         | 146 ++++++++++---
 t/t6040-tracking-info.sh         | 354 +++++++++++++++++++++++++++++++
 3 files changed, 497 insertions(+), 28 deletions(-)
Show changes to 3 files +497 −28

Documentation/config/status.adoc, remote.c, t/t6040-tracking-info.sh

diff --git a/Documentation/config/status.adoc b/Documentation/config/status.adoc
index 8caf90f51c..b5dd85b761 100644
--- a/Documentation/config/status.adoc
+++ b/Documentation/config/status.adoc
@@ -17,6 +17,31 @@ status.aheadBehind::
 	`--no-ahead-behind` by default in linkgit:git-status[1] for
 	non-porcelain status formats.  Defaults to true.
 
+status.compareBranches::
+	A space-separated list of branch comparison specifiers to use in
+	linkgit:git-status[1]. Currently, only `@{upstream}` and `@{push}`
+	are supported. They are interpreted as `branch@{upstream}` and
+	`branch@{push}` for the current branch.
++
+If not set, the default behavior is equivalent to `@{upstream}`, which
+compares against the configured upstream tracking branch.
++
+The entries are shown in the order they appear in the configuration.
+Duplicate entries that resolve to the same ref are suppressed after
+their first occurrence, so `@{push} @{upstream} @{push}` shows at
+most two comparisons.  When `@{upstream}` and `@{push}` resolve to
+the same remote-tracking branch, only one comparison is shown.
++
+Example:
++
+----
+[status]
+	compareBranches = @{upstream} @{push}
+----
++
+This would show comparisons against both the configured upstream and push
+tracking branches for the current branch.
+
 status.displayCommentPrefix::
 	If set to true, linkgit:git-status[1] will insert a comment
 	prefix before each output line (starting with
diff --git a/remote.c b/remote.c
index e9e2f56ed6..7ca2a6501b 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
 
 enum map_direction { FROM_SRC, FROM_DST };
 
+enum {
+	ENABLE_ADVICE_PULL       = (1 << 0),
+	ENABLE_ADVICE_PUSH       = (1 << 1),
+	ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
 struct counted_string {
 	size_t len;
 	const char *s;
@@ -2234,13 +2240,40 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
 	return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
 }
 
+static char *resolve_compare_branch(struct branch *branch, const char *name)
+{
+	const char *resolved = NULL;
+
+	if (!branch || !name)
+		return NULL;
+
+	if (!strcasecmp(name, "@{upstream}")) {
+		resolved = branch_get_upstream(branch, NULL);
+	} else if (!strcasecmp(name, "@{push}")) {
+		resolved = branch_get_push(branch, NULL);
+	} else {
+		warning(_("ignoring value '%s' for status.compareBranches, "
+			  "only @{upstream} and @{push} are supported"),
+			name);
+		return NULL;
+	}
+
+	if (resolved)
+		return xstrdup(resolved);
+	return NULL;
+}
+
 static void format_branch_comparison(struct strbuf *sb,
 				     bool up_to_date,
 				     int ours, int theirs,
 				     const char *branch_name,
 				     enum ahead_behind_flags abf,
-				     bool show_divergence_advice)
+				     unsigned flags)
 {
+	bool use_push_advice = (flags & ENABLE_ADVICE_PUSH);
+	bool use_pull_advice = (flags & ENABLE_ADVICE_PULL);
+	bool use_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE);
+
 	if (up_to_date) {
 		strbuf_addf(sb,
 			_("Your branch is up to date with '%s'.\n"),
@@ -2249,7 +2282,7 @@ static void format_branch_comparison(struct strbuf *sb,
 		strbuf_addf(sb,
 			    _("Your branch and '%s' refer to different commits.\n"),
 			    branch_name);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addf(sb, _("  (use \"%s\" for details)\n"),
 				    "git status --ahead-behind");
 	} else if (!theirs) {
@@ -2258,7 +2291,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			   "Your branch is ahead of '%s' by %d commits.\n",
 			   ours),
 			branch_name, ours);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git push\" to publish your local commits)\n"));
 	} else if (!ours) {
@@ -2269,7 +2302,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "and can be fast-forwarded.\n",
 			   theirs),
 			branch_name, theirs);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" to update your local branch)\n"));
 	} else {
@@ -2282,8 +2315,7 @@ static void format_branch_comparison(struct strbuf *sb,
 			       "respectively.\n",
 			   ours + theirs),
 			branch_name, ours, theirs);
-		if (show_divergence_advice &&
-		    advice_enabled(ADVICE_STATUS_HINTS))
+		if (use_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS))
 			strbuf_addstr(sb,
 				_("  (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
 	}
@@ -2296,34 +2328,92 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
 			 enum ahead_behind_flags abf,
 			 int show_divergence_advice)
 {
-	int ours, theirs, cmp_fetch;
-	const char *full_base;
-	char *base;
-	int upstream_is_gone = 0;
+	char *compare_branches = NULL;
+	struct string_list branches = STRING_LIST_INIT_DUP;
+	struct strset processed_refs = STRSET_INIT;
+	int reported = 0;
+	size_t i;
+	const char *upstream_ref;
+	const char *push_ref;
 
-	cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
-	if (cmp_fetch < 0) {
-		if (!full_base)
-			return 0;
-		upstream_is_gone = 1;
+	repo_config_get_string(the_repository, "status.comparebranches",
+			       &compare_branches);
+
+	if (compare_branches) {
+		string_list_split(&branches, compare_branches, " ", -1);
+		string_list_remove_empty_items(&branches, 0);
+	} else {
+		string_list_append(&branches, "@{upstream}");
 	}
 
-	base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
-					    full_base, 0);
+	upstream_ref = branch_get_upstream(branch, NULL);
+	push_ref = branch_get_push(branch, NULL);
 
-	if (upstream_is_gone) {
-		strbuf_addf(sb,
-			_("Your branch is based on '%s', but the upstream is gone.\n"),
-			base);
-		if (advice_enabled(ADVICE_STATUS_HINTS))
-			strbuf_addstr(sb,
-				_("  (use \"git branch --unset-upstream\" to fixup)\n"));
-	} else {
-		format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+	for (i = 0; i < branches.nr; i++) {
+		char *full_ref;
+		char *short_ref;
+		int ours, theirs, cmp;
+		int is_upstream, is_push;
+		unsigned flags = 0;
+
+		full_ref = resolve_compare_branch(branch,
+						  branches.items[i].string);
+		if (!full_ref)
+			continue;
+
+		if (!strset_add(&processed_refs, full_ref)) {
+			free(full_ref);
+			continue;
+		}
+
+		short_ref = refs_shorten_unambiguous_ref(
+			get_main_ref_store(the_repository), full_ref, 0);
+
+		is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+		is_push = push_ref && !strcmp(full_ref, push_ref);
+
+		if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+			is_push = 1;
+
+		cmp = stat_branch_pair(branch->refname, full_ref,
+				       &ours, &theirs, abf);
+
+		if (cmp < 0) {
+			if (is_upstream) {
+				strbuf_addf(sb,
+					_("Your branch is based on '%s', but the upstream is gone.\n"),
+					short_ref);
+				if (advice_enabled(ADVICE_STATUS_HINTS))
+					strbuf_addstr(sb,
+						_("  (use \"git branch --unset-upstream\" to fixup)\n"));
+				reported = 1;
+			}
+			free(full_ref);
+			free(short_ref);
+			continue;
+		}
+
+		if (reported)
+			strbuf_addstr(sb, "\n");
+
+		if (is_upstream)
+			flags |= ENABLE_ADVICE_PULL;
+		if (is_push)
+			flags |= ENABLE_ADVICE_PUSH;
+		if (show_divergence_advice && is_upstream)
+			flags |= ENABLE_ADVICE_DIVERGENCE;
+		format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+					 abf, flags);
+		reported = 1;
+
+		free(full_ref);
+		free(short_ref);
 	}
 
-	free(base);
-	return 1;
+	string_list_clear(&branches, 0);
+	strset_clear(&processed_refs);
+	free(compare_branches);
+	return reported;
 }
 
 static int one_local_ref(const struct reference *ref, void *cb_data)
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 0b719bbae6..0242b5bf7a 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -292,4 +292,358 @@ test_expect_success '--set-upstream-to @{-1}' '
 	test_cmp expect actual
 '
 
+test_expect_success 'status tracking origin/main shows only main' '
+	git -C test checkout b4 &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
+	git -C test checkout b4 &&
+	git -C test status --no-ahead-behind >actual &&
+	cat >expect <<-EOF &&
+	On branch b4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches deduplicates when upstream and push are the same' '
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout main &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches with only upstream shows only upstream' '
+	test_config -C test status.compareBranches "@{upstream}" &&
+	git -C test checkout main &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches with only push shows only push' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{push}" &&
+	git -C test checkout -b feature2 origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature2
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature3 origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature3
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows both branches' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature3 >actual &&
+	cat >expect <<-EOF &&
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup for ahead of tracked but diverged from main' '
+	(
+		cd test &&
+		git checkout -b feature4 origin/main &&
+		advance work1 &&
+		git checkout origin/main &&
+		advance work2 &&
+		git push origin HEAD:main &&
+		git checkout feature4 &&
+		advance work3
+	)
+'
+
+test_expect_success 'status.compareBranches shows diverged and ahead' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature4 &&
+	git -C test branch --set-upstream-to origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} have diverged,
+	and have 3 and 1 different commits each, respectively.
+	  (use "git pull" if you want to integrate the remote branch with yours)
+
+	Your branch is ahead of ${SQ}origin/feature4${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature4 &&
+	git -C test status --no-ahead-behind >actual &&
+	cat >expect <<-EOF &&
+	On branch feature4
+	Your branch and ${SQ}origin/main${SQ} refer to different commits.
+
+	Your branch and ${SQ}origin/feature4${SQ} refer to different commits.
+	  (use "git status --ahead-behind" for details)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'setup upstream remote' '
+	(
+		cd test &&
+		git remote add upstream ../. &&
+		git fetch upstream
+	)
+'
+
+test_expect_success 'status.compareBranches with upstream and origin remotes' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature5 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature5
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/feature5${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches supports ordered upstream/push entries' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{push} @{upstream}" &&
+	git -C test checkout -b feature6 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature6
+	Your branch is ahead of ${SQ}origin/feature6${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches deduplicates repeated specifiers' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{push} @{upstream} @{push}" &&
+	git -C test checkout -b feature7 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}origin/feature7${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with diverged push branch' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature8 upstream/main &&
+	(cd test && advance work81) &&
+	git -C test push origin &&
+	git -C test reset --hard upstream/main &&
+	(cd test && advance work82) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature8
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch and ${SQ}origin/feature8${SQ} have diverged,
+	and have 1 and 1 different commits each, respectively.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows up to date branches' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature9 upstream/main &&
+	git -C test push origin &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status --no-ahead-behind with status.compareBranches up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature9 >actual &&
+	git -C test push origin &&
+	git -C test status --no-ahead-behind >actual &&
+	cat >expect <<-EOF &&
+	On branch feature9
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'checkout with status.compareBranches shows up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout feature9 >actual &&
+	cat >expect <<-EOF &&
+	Your branch is up to date with ${SQ}upstream/main${SQ}.
+
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with upstream behind and push up to date' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b ahead upstream/main &&
+	(cd test && advance work) &&
+	git -C test push upstream HEAD &&
+	git -C test checkout -b feature10 upstream/main &&
+	git -C test push origin &&
+	git -C test branch --set-upstream-to upstream/ahead &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature10
+	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
+	  (use "git pull" to update your local branch)
+
+	Your branch is up to date with ${SQ}origin/feature10${SQ}.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push refspec' '
+	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature11 origin/main &&
+	git -C test push &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature11
+	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches with remapped push and upstream remote' '
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test remote.origin.push refs/heads/feature12:refs/heads/remapped &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature12 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature12
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Junio C Hamano· Mar 4, 2026, 17:05 UTC · re: Harald Nordgren via GitGitGadget · lore

Re: [PATCH v31 0/2] status: add status.compareBranches config for multiple branch comparisons

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 29 quoted lines
> Harald Nordgren (2):
>   refactor format_branch_comparison in preparation
>   status: add status.compareBranches config for multiple branch
>     comparisons
>
>  Documentation/config/status.adoc |  25 +++
>  remote.c                         | 178 ++++++++++++----
>  t/t6040-tracking-info.sh         | 354 +++++++++++++++++++++++++++++++
>  3 files changed, 520 insertions(+), 37 deletions(-)
>
>
> base-commit: 50d063e335afd5828fbb9de2f2b2fb44fd884d2b
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2138%2FHaraldNordgren%2Fahead_of_main_status-v31
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2138/HaraldNordgren/ahead_of_main_status-v31
> Pull-Request: https://github.com/git/git/pull/2138
> ...
> Range-diff vs v30:
>
>  1:  7f517b8c7f = 1:  320c1ce55a refactor format_branch_comparison in preparation
>  2:  501bd40294 ! 2:  f07ccb278c status: add status.compareBranches config for multiple branch comparisons
>      @@ Documentation/config/status.adoc: status.aheadBehind::
>       +If not set, the default behavior is equivalent to `@{upstream}`, which
>       +compares against the configured upstream tracking branch.
>       ++
>      ++The entries are shown in the order they appear in the configuration.
>      ++Duplicate entries that resolve to the same ref are suppressed after
>      ++their first occurrence, so `@{push} @{upstream} @{push}` shows at
>      ++most two comparisons.  When `@{upstream}` and `@{push}` resolve to
>      ++the same remote-tracking branch, only one comparison is shown.

As the previous iteration of this series is already in 'next', we'd need to turn this into an incremental. Luckily, as the difference is fairly clear and only in [2/2], let me try to do so myself to potentially save one roundtrip.

Thanks.
----- >8 -----
From: Harald Nordgren <haraldnordgren@gmail.com>
Date: Wed, 04 Mar 2026 12:25:31 +0000
Subject: [PATCH 3/2] status: clarify how status.compareBranches deduplicates

The order of output when multiple branches are specified on the configuration variable was not clearly spelled out in the documentation.

Add a paragraph to describe the order and also how the branches are deduplicated. Update t6040 with additional tests to illustrate how multiple branches are shown and deduplicated.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
[jc: made a whole replacement into incremental; wrote log message.]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config/status.adoc |   6 ++
 t/t6040-tracking-info.sh         | 120 ++++++++++++++++++++++++++-------------
 2 files changed, 88 insertions(+), 38 deletions(-)
Show changes to diff +88 −38
diff --git c/Documentation/config/status.adoc w/Documentation/config/status.adoc
index 15ccd0116b..b5dd85b761 100644
--- c/Documentation/config/status.adoc
+++ w/Documentation/config/status.adoc
@@ -26,6 +26,12 @@ status.compareBranches::
 If not set, the default behavior is equivalent to `@{upstream}`, which
 compares against the configured upstream tracking branch.
 +
+The entries are shown in the order they appear in the configuration.
+Duplicate entries that resolve to the same ref are suppressed after
+their first occurrence, so `@{push} @{upstream} @{push}` shows at
+most two comparisons.  When `@{upstream}` and `@{push}` resolve to
+the same remote-tracking branch, only one comparison is shown.
++
 Example:
 +
 ----
diff --git c/t/t6040-tracking-info.sh w/t/t6040-tracking-info.sh
index c24f545036..0242b5bf7a 100755
--- c/t/t6040-tracking-info.sh
+++ w/t/t6040-tracking-info.sh
@@ -293,11 +293,8 @@ test_expect_success '--set-upstream-to @{-1}' '
 '
 
 test_expect_success 'status tracking origin/main shows only main' '
-	(
-		cd test &&
-		git checkout b4 &&
-		git status >../actual
-	) &&
+	git -C test checkout b4 &&
+	git -C test status >actual &&
 	cat >expect <<-EOF &&
 	On branch b4
 	Your branch is ahead of ${SQ}origin/main${SQ} by 2 commits.
@@ -309,11 +306,8 @@ test_expect_success 'status tracking origin/main shows only main' '
 '
 
 test_expect_success 'status --no-ahead-behind tracking origin/main shows only main' '
-	(
-		cd test &&
-		git checkout b4 &&
-		git status --no-ahead-behind >../actual
-	) &&
+	git -C test checkout b4 &&
+	git -C test status --no-ahead-behind >actual &&
 	cat >expect <<-EOF &&
 	On branch b4
 	Your branch and ${SQ}origin/main${SQ} refer to different commits.
@@ -324,7 +318,7 @@ test_expect_success 'status --no-ahead-behind tracking origin/main shows only ma
 	test_cmp expect actual
 '
 
-test_expect_success 'status.compareBranches from upstream has no duplicates' '
+test_expect_success 'status.compareBranches deduplicates when upstream and push are the same' '
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
 	git -C test checkout main &&
 	git -C test status >actual &&
@@ -337,18 +331,48 @@ test_expect_success 'status.compareBranches from upstream has no duplicates' '
 	test_cmp expect actual
 '
 
-test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
+test_expect_success 'status.compareBranches with only upstream shows only upstream' '
+	test_config -C test status.compareBranches "@{upstream}" &&
+	git -C test checkout main &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch main
+	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 'status.compareBranches with only push shows only push' '
 	test_config -C test push.default current &&
-	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	test_config -C test status.compareBranches "@{push}" &&
 	git -C test checkout -b feature2 origin/main &&
 	git -C test push origin HEAD &&
 	(cd test && advance work) &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
 	On branch feature2
+	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'status.compareBranches shows ahead of both upstream and push branch' '
+	test_config -C test push.default current &&
+	test_config -C test status.compareBranches "@{upstream} @{push}" &&
+	git -C test checkout -b feature3 origin/main &&
+	git -C test push origin HEAD &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature3
 	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
 
-	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
 	  (use "git push" to publish your local commits)
 
 	nothing to commit, working tree clean
@@ -359,11 +383,11 @@ test_expect_success 'status.compareBranches shows ahead of both upstream and pus
 test_expect_success 'checkout with status.compareBranches shows both branches' '
 	test_config -C test push.default current &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout feature2 >actual &&
+	git -C test checkout feature3 >actual &&
 	cat >expect <<-EOF &&
 	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
 
-	Your branch is ahead of ${SQ}origin/feature2${SQ} by 1 commit.
+	Your branch is ahead of ${SQ}origin/feature3${SQ} by 1 commit.
 	  (use "git push" to publish your local commits)
 	EOF
 	test_cmp expect actual
@@ -469,21 +493,41 @@ test_expect_success 'status.compareBranches supports ordered upstream/push entri
 	test_cmp expect actual
 '
 
+test_expect_success 'status.compareBranches deduplicates repeated specifiers' '
+	test_config -C test push.default current &&
+	test_config -C test remote.pushDefault origin &&
+	test_config -C test status.compareBranches "@{push} @{upstream} @{push}" &&
+	git -C test checkout -b feature7 upstream/main &&
+	git -C test push origin &&
+	(cd test && advance work) &&
+	git -C test status >actual &&
+	cat >expect <<-EOF &&
+	On branch feature7
+	Your branch is ahead of ${SQ}origin/feature7${SQ} by 1 commit.
+	  (use "git push" to publish your local commits)
+
+	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
+
+	nothing to commit, working tree clean
+	EOF
+	test_cmp expect actual
+'
+
 test_expect_success 'status.compareBranches with diverged push branch' '
 	test_config -C test push.default current &&
 	test_config -C test remote.pushDefault origin &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout -b feature7 upstream/main &&
-	(cd test && advance work71) &&
+	git -C test checkout -b feature8 upstream/main &&
+	(cd test && advance work81) &&
 	git -C test push origin &&
 	git -C test reset --hard upstream/main &&
-	(cd test && advance work72) &&
+	(cd test && advance work82) &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
-	On branch feature7
+	On branch feature8
 	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
 
-	Your branch and ${SQ}origin/feature7${SQ} have diverged,
+	Your branch and ${SQ}origin/feature8${SQ} have diverged,
 	and have 1 and 1 different commits each, respectively.
 
 	nothing to commit, working tree clean
@@ -495,14 +539,14 @@ test_expect_success 'status.compareBranches shows up to date branches' '
 	test_config -C test push.default current &&
 	test_config -C test remote.pushDefault origin &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout -b feature8 upstream/main &&
+	git -C test checkout -b feature9 upstream/main &&
 	git -C test push origin &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
-	On branch feature8
+	On branch feature9
 	Your branch is up to date with ${SQ}upstream/main${SQ}.
 
-	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
 
 	nothing to commit, working tree clean
 	EOF
@@ -513,14 +557,14 @@ test_expect_success 'status --no-ahead-behind with status.compareBranches up to
 	test_config -C test push.default current &&
 	test_config -C test remote.pushDefault origin &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout feature8 >actual &&
+	git -C test checkout feature9 >actual &&
 	git -C test push origin &&
 	git -C test status --no-ahead-behind >actual &&
 	cat >expect <<-EOF &&
-	On branch feature8
+	On branch feature9
 	Your branch is up to date with ${SQ}upstream/main${SQ}.
 
-	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
 
 	nothing to commit, working tree clean
 	EOF
@@ -531,11 +575,11 @@ test_expect_success 'checkout with status.compareBranches shows up to date' '
 	test_config -C test push.default current &&
 	test_config -C test remote.pushDefault origin &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout feature8 >actual &&
+	git -C test checkout feature9 >actual &&
 	cat >expect <<-EOF &&
 	Your branch is up to date with ${SQ}upstream/main${SQ}.
 
-	Your branch is up to date with ${SQ}origin/feature8${SQ}.
+	Your branch is up to date with ${SQ}origin/feature9${SQ}.
 	EOF
 	test_cmp expect actual
 '
@@ -547,16 +591,16 @@ test_expect_success 'status.compareBranches with upstream behind and push up to
 	git -C test checkout -b ahead upstream/main &&
 	(cd test && advance work) &&
 	git -C test push upstream HEAD &&
-	git -C test checkout -b feature9 upstream/main &&
+	git -C test checkout -b feature10 upstream/main &&
 	git -C test push origin &&
 	git -C test branch --set-upstream-to upstream/ahead &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
-	On branch feature9
+	On branch feature10
 	Your branch is behind ${SQ}upstream/ahead${SQ} by 1 commit, and can be fast-forwarded.
 	  (use "git pull" to update your local branch)
 
-	Your branch is up to date with ${SQ}origin/feature9${SQ}.
+	Your branch is up to date with ${SQ}origin/feature10${SQ}.
 
 	nothing to commit, working tree clean
 	EOF
@@ -564,14 +608,14 @@ test_expect_success 'status.compareBranches with upstream behind and push up to
 '
 
 test_expect_success 'status.compareBranches with remapped push refspec' '
-	test_config -C test remote.origin.push refs/heads/feature10:refs/heads/remapped &&
+	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout -b feature10 origin/main &&
+	git -C test checkout -b feature11 origin/main &&
 	git -C test push &&
 	(cd test && advance work) &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
-	On branch feature10
+	On branch feature11
 	Your branch is ahead of ${SQ}origin/main${SQ} by 1 commit.
 
 	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
@@ -584,14 +628,14 @@ test_expect_success 'status.compareBranches with remapped push refspec' '
 
 test_expect_success 'status.compareBranches with remapped push and upstream remote' '
 	test_config -C test remote.pushDefault origin &&
-	test_config -C test remote.origin.push refs/heads/feature11:refs/heads/remapped &&
+	test_config -C test remote.origin.push refs/heads/feature12:refs/heads/remapped &&
 	test_config -C test status.compareBranches "@{upstream} @{push}" &&
-	git -C test checkout -b feature11 upstream/main &&
+	git -C test checkout -b feature12 upstream/main &&
 	git -C test push origin &&
 	(cd test && advance work) &&
 	git -C test status >actual &&
 	cat >expect <<-EOF &&
-	On branch feature11
+	On branch feature12
 	Your branch is ahead of ${SQ}upstream/main${SQ} by 1 commit.
 
 	Your branch is ahead of ${SQ}origin/remapped${SQ} by 1 commit.
Junio C Hamano· Mar 9, 2026, 15:10 UTC · re: Harald Nordgren · lore

Re: [PATCH v31 0/2] status: add status.compareBranches config for multiple branch comparisons

Harald Nordgren <haraldnordgren@gmail.com> writes:
> Thanks for all your help here! Is there anything you need from me now?

Thanks for all the work. If you haven't heard any breakages from others, and if you haven't found any breakages yourself, there is nothing we need from you right at this moment.

The patch series is cooking in 'next' and unless people find something glaringly wrong, will be merged down to 'master' later (after which, people might still find something wrong in it, of course---any breakages and regressions need to be handled no matter anyway ;-).

← back to recent threads