git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* response to "git branch -f foo origin/foo"
@ 2025-04-28 20:11 Junio C Hamano
  2025-05-01 18:25 ` D. Ben Knoble
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2025-04-28 20:11 UTC (permalink / raw)
  To: git

When 'X' is a new branch I am creating to automatically (as the
default for branch.autoSetupMerge is true these days) track the
corresponding branch at the upstream, this output ...

    $ git branch [-f] X origin/X
    branch 'X' set up to track 'origin/X'.

... from the command, with or without -f, makes perfect sense.

It also makes sense if we reset the tip of 'X' to a slightly older
commit on the branch, i.e. after doing the above, running

    $ git branch -f X origin/X~4

does not say anything.  The branch is still set up to track
origin/X after doing the above two operations.

However, after doing these, and 'X' is _already_ tracking its
corresponding branch at the upstream, resetting the branch with '-f'
again will give us the same message as the first one:

    $ git branch -f X origin/X
    branch 'X' set up to track 'origin/X'.

and I think it is wrong for at least two reasons:

 * Does it make sense to say "set up to track" in this case?  If X
   used to be set to track nothing or some other branch, and if we
   changed the tracking information with the command, the existing
   message may make sense, but otherwise, I would say the current
   message is useless, and it is unnecessarily frustrating, because
   those who see the message may start to wonder what it was set to
   track before, but at that point, that information is long lost.

 * "git branch -f" on an existing branch is done to repoint the
   branch to point at some commit, which may or may not be the same
   one as before.  If the starting point happens to be a
   remote-tracking branch (like "origin/X"), it also may set up a
   tracking information as well, and as the first point argued, it
   may make sense to report the tracking information _if_ it
   changed.  But pointing the branch tip at a different commit is a
   change that is also, if not more, report-worthy event.

Suggesitons.

 - We should change the condition under which the "branch X set up
   to track origin/X" message is given.  We should limit it only to
   the case where X did *NOT* track origin/X, and we made X to track
   origin/X in this invocation of the command.

 - We may want to extend the "branch X set up to track origin/X"
   message so that the message mentions what X used to track, or the
   fact that X tracked nothing.

 - We should give another message when "git branch -f X" resets the
   commit an existing branch X points at.  Unlike "what was X
   tracking?" that is forever lost (hence the previous suggestion),
   what X used to point at can be found out as X@{1}, so it is not
   necessary to give the exact commit, but the fact that the branch
   existed already may be significant (especially if you habitually
   use "branch -f X" whether X exists or not).  Taking inspirations
   from "git checkout -B X origin/X" that says "Switched to and
   reset branch 'X'", perhaps "Reset branch 'X'" may be a good place
   to stop.

Comments?  Takers?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: response to "git branch -f foo origin/foo"
  2025-04-28 20:11 response to "git branch -f foo origin/foo" Junio C Hamano
@ 2025-05-01 18:25 ` D. Ben Knoble
  2025-05-01 21:38   ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: D. Ben Knoble @ 2025-05-01 18:25 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Apr 28, 2025 at 4:12 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> When 'X' is a new branch I am creating to automatically (as the
> default for branch.autoSetupMerge is true these days) track the
> corresponding branch at the upstream, this output ...
>
>     $ git branch [-f] X origin/X
>     branch 'X' set up to track 'origin/X'.
>
> ... from the command, with or without -f, makes perfect sense.
>
> It also makes sense if we reset the tip of 'X' to a slightly older
> commit on the branch, i.e. after doing the above, running
>
>     $ git branch -f X origin/X~4

But why not use `git reset --hard origin/X~4` here? (Answering myself:
presumably because this version works whatever the current branch is.)

>
> does not say anything.  The branch is still set up to track
> origin/X after doing the above two operations.
>
> However, after doing these, and 'X' is _already_ tracking its
> corresponding branch at the upstream, resetting the branch with '-f'
> again will give us the same message as the first one:
>
>     $ git branch -f X origin/X
>     branch 'X' set up to track 'origin/X'.
>
> and I think it is wrong for at least two reasons:
>
>  * Does it make sense to say "set up to track" in this case?  If X
>    used to be set to track nothing or some other branch, and if we
>    changed the tracking information with the command, the existing
>    message may make sense, but otherwise, I would say the current
>    message is useless, and it is unnecessarily frustrating, because
>    those who see the message may start to wonder what it was set to
>    track before, but at that point, that information is long lost.

The other perspective is that
- it's nice to know the tracking is setup; if I'm used to seeing that
message and I don't, I might wonder where the tracking has gone?
- use of -f is enough consent to lose the "what was there before"

Though, it might still be nice to do the following [reordered]

>  - We may want to extend the "branch X set up to track origin/X"
>    message so that the message mentions what X used to track, or the
>    fact that X tracked nothing.

This might be "noisy" for confident users, but seems like a nice QoL
improvement to me.

>  - We should give another message when "git branch -f X" resets the
>    commit an existing branch X points at.  Unlike "what was X
>    tracking?" that is forever lost (hence the previous suggestion),
>    what X used to point at can be found out as X@{1}, so it is not
>    necessary to give the exact commit, but the fact that the branch
>    existed already may be significant (especially if you habitually
>    use "branch -f X" whether X exists or not).  Taking inspirations
>    from "git checkout -B X origin/X" that says "Switched to and
>    reset branch 'X'", perhaps "Reset branch 'X'" may be a good place
>    to stop.

Didn't I see you recently suggest someone drop the habit of
unnecessary -r in rm? [1] :) This seems similar to me: don't use
unnecessary -f as a habit. I would personally find it noisy, but again
for newer users it may be helpful. I can't judge that well (only that
it would lose some "silence means success").

[1]: https://lore.kernel.org/git/xmqqecxk3u5l.fsf@gitster.g/

-- 
D. Ben Knoble

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: response to "git branch -f foo origin/foo"
  2025-05-01 18:25 ` D. Ben Knoble
@ 2025-05-01 21:38   ` Junio C Hamano
  0 siblings, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2025-05-01 21:38 UTC (permalink / raw)
  To: D. Ben Knoble; +Cc: git

"D. Ben Knoble" <ben.knoble@gmail.com> writes:

> On Mon, Apr 28, 2025 at 4:12 PM Junio C Hamano <gitster@pobox.com> wrote:
>>
>> When 'X' is a new branch I am creating to automatically (as the
>> default for branch.autoSetupMerge is true these days) track the
>> corresponding branch at the upstream, this output ...
>>
>>     $ git branch [-f] X origin/X
>>     branch 'X' set up to track 'origin/X'.
>>
>> ... from the command, with or without -f, makes perfect sense.
>>
>> It also makes sense if we reset the tip of 'X' to a slightly older
>> commit on the branch, i.e. after doing the above, running
>>
>>     $ git branch -f X origin/X~4
>
> But why not use `git reset --hard origin/X~4` here? (Answering myself:
> presumably because this version works whatever the current branch is.)

Exactly.  After you work on another branch (perhaps your primary
branch) to incorporate the work that was done near the tip of
origin/X, you may want to reset X to lose those now-redundant
commits, but you do not want to switch to X just for that.  After
all, you are "done" with X at that point and want to continue
working on your current branch.

>>  - We should give another message when "git branch -f X" resets the
>>    commit an existing branch X points at.  Unlike "what was X
>>    tracking?" that is forever lost (hence the previous suggestion),
>>    what X used to point at can be found out as X@{1}, so it is not
>>    necessary to give the exact commit, but the fact that the branch
>>    existed already may be significant (especially if you habitually
>>    use "branch -f X" whether X exists or not).  Taking inspirations
>>    from "git checkout -B X origin/X" that says "Switched to and
>>    reset branch 'X'", perhaps "Reset branch 'X'" may be a good place
>>    to stop.
>
> Didn't I see you recently suggest someone drop the habit of
> unnecessary -r in rm? [1] :) This seems similar to me: don't use
> unnecessary -f as a habit.

Even if you do not have that habit, think in what situation you
would use "branch -f X".  You do so when you think you _know_ X
exists already, i.e. you know you are resetting an existing branch,
and not creating a new branch.

But it is possible that the one you wanted to overwrite was Y and
not X---it would give you an extra clue to realize you screwed up if
we gave two different messages (or perhaps "-f" that resets stay
silent, but if "-f" created, give a "created" in a message, or
something).

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-05-01 21:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-28 20:11 response to "git branch -f foo origin/foo" Junio C Hamano
2025-05-01 18:25 ` D. Ben Knoble
2025-05-01 21:38   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).