Subject: Re: Update message_advice_pull_before_push to mention how to push after rebasing

From: Junio C Hamano <[email-masked]>
Date: Thu, 28 Mar 2024 08:23:06 -0700

zhang kai <[email-masked]> writes:

> Hi all,
>
> Our team uses git rebase a lot. A common hiccup we noticed during the
> daily work is that new devs often didn't know how to push properly
> after the rebase.
>
> When someone try to push to the remote after rebasing the branch, the
> push would mostly fail with the message_advice_pull_before_push:
>
>> Updates were rejected because the tip of your current branch is behind\n" "its remote counterpart. If you want to integrate the remote changes,\n" "use 'git pull' before pushing again.\n" "See the 'Note about fast-forwards' in 'git push --help' for details.
>
> However, in this situation, pull will "invalid" the rebasing. After
> pull then push, devs often will be surprised by the merge/pull request
> containing diffs that should not be in this branch. In this case, we
> often should just do `git push --force-with-lease`.

Use of --force-with-lease without specifying exactly what commit to
expect there is not a good hygiene, but when you rebase your topic
branch on the updated upstream, you are already committed to force
update your published topic branch.  So "your push does not
fast-forward; pull first before pushing again" is not sufficient.
If you know your push does not fast-forward, and if you want to
force push, then you should not pull first.

> If we add a message like "If you just rebased your branch, please
> consider 'git push --force-with-lease'" to the
> message_adivce_pull_before_push, we may save devs some time figuring
> out what's wrong with their branch.

I am not sure if that is a good idea in general, though.  That "if
you just rebased" applies only to the case where you rebased your
topic branch.  If you forked to work on topic from the upstream's
primary integration branch, worked on that topic, and then rebased
your topic on top of the updated upstream integration branch (which
contains new work from others since you forked and while you were
working on that topic), you do not want to force.  force-with-lease
may help you avoid overwriting and discarding others work, but the
only sensible next step in such a case is to pull-rebase again,
which will rebase your work on the topic to build on these recent
work from others.

So, I dunno.