status: suggest a force push after a clean rebase

After rebasing pushed work onto a newer upstream without changing it,
"git status" says the branch was rebased cleanly but gives no hint on
how to publish it. A plain "git push" is rejected, so suggest a force
push:

  Your branch and 'origin/topic' have diverged,
  and have 51 and 1 different commits each (rebased cleanly on 'upstream/main').
    (use "git push --force-with-lease" to publish your local commits)

The lease still stops the push if someone else updated the push branch
since it was last fetched.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Harald Nordgren 2026-10-06 07:08:21 +00:00 committed by Junio C Hamano
parent 4ba8ef0b13
commit 910e7ee42a
2 changed files with 6 additions and 2 deletions

View File

@ -2468,7 +2468,7 @@ static void format_branch_comparison(struct strbuf *sb,
_(" (use \"git pull\" to update your local branch)\n"));
}
} else {
if (same_changes)
if (same_changes) {
strbuf_addf(sb,
Q_("Your branch and '%s' have diverged,\n"
"and have %d and %d different commit each "
@ -2478,7 +2478,10 @@ static void format_branch_comparison(struct strbuf *sb,
"(rebased cleanly on '%s').\n",
ours + theirs),
branch_name, ours, theirs, upstream_name);
else if (upstream_name)
if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git push --force-with-lease\" to publish your local commits)\n"));
} else if (upstream_name)
strbuf_addf(sb,
Q_("Your branch and '%s' have diverged,\n"
"and have %d and %d different commit each "

View File

@ -802,6 +802,7 @@ test_expect_success 'status.compareBranches after a clean rebase of the push bra

Your branch and ${SQ}origin/feature19${SQ} have diverged,
and have 3 and 1 different commits each (rebased cleanly on ${SQ}origin/main${SQ}).
(use "git push --force-with-lease" to publish your local commits)

nothing to commit, working tree clean
EOF