git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: David Mandelberg <david@mandelberg.org>
Cc: git@vger.kernel.org, Elijah Newren <newren@gmail.com>
Subject: Re: git switch --force vs --discard-changes: docs don't match behavior
Date: Mon, 17 Mar 2025 10:39:09 -0700	[thread overview]
Message-ID: <xmqqiko7a75u.fsf@gitster.g> (raw)
In-Reply-To: <xmqqo6xza838.fsf@gitster.g> (Junio C. Hamano's message of "Mon, 17 Mar 2025 10:19:07 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> I do not have a strong opinion either way.  It may appear to some
> users that giving a finer grained control is a merit.  Even when you
> are willing to throw away changes to already tracked content,
> getting stopped when you may lose a totally untracked thing might be
> nicer.
>
> On the other hand, I suspect to many others this finer grained
> control does not give much value while adding more confusion.
>
> I am obviously biased because I am accustomed not to have this
> distinction and accept "checkout -f" as a reasonable way to force
> switching to another branch discarding any and all local
> modifications including untracked new files that get in the way,
> though.  But I do not feel strongly enough to say that the behaviour
> and the feature itself is misguided and we should rip it out.  As
> long as that "finer grained control" is working in a consistent and
> explainable way, I'd actually vote for fixing the documentation to
> explain how "--discard-changes" is a bit milder than "--force'.

It seems that this was designed from day one to work this way, or
this was buggy from day one to work this way.  Take your pick ;-)

When 3ec37ad1 (switch: add --discard-changes, 2019-03-29) introduced
the --discard-changes option, it seems that it was intended to be
truly the synonym, but the code change was done in a somewhat
confusing way and added an extra bit (a new .discard_changes member
was added, and is initialized by the value given to the .force
member.  Many existing conditionals that looked at the .force bit
was changed to look at the .discard_changes bit, but not all of
them.

It did not offer the protection against overwriting untracked files
in builtin/checkout.c:reset_tree().  The .reset member was always
set to 1 to allow it.

Later 480d3d6b (Change unpack_trees' 'reset' flag into an enum,
2021-09-27) added the "protect against overwriting untracked files"
feature, and made it pay attention to the "force" option, not the
other member.  That is where this discrepancy was introduced.  In
retrospect, it might have been better if 3ec37ad1 removed .force and
kept only "--force" at the UI layer, internally used the new
.discard_changes everywhere.  Or it would have been even better if
the commit did not happen at all, and added "--discard-changes" only
at the UI layer.  Then the discrepancy would never have happened.

The author of 480d3d6b Cc'ed to ask for the original intention for
reference, but that is merely to satisfy my curiosity.  At this
point, I think it is way too late for the original intention to sway
the conclusion, as existing users of "switch" may be relying on the
current behaviour.  The best course of action may be to update the
documentation to match the behaviour.

Thanks.

  reply	other threads:[~2025-03-17 17:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-17 16:50 git switch --force vs --discard-changes: docs don't match behavior David Mandelberg
2025-03-17 17:19 ` Junio C Hamano
2025-03-17 17:39   ` Junio C Hamano [this message]
2025-03-17 18:13   ` David Mandelberg

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=xmqqiko7a75u.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=david@mandelberg.org \
    --cc=git@vger.kernel.org \
    --cc=newren@gmail.com \
    /path/to/YOUR_REPLY

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

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