From: Junio C Hamano <gitster@pobox.com>
To: David Mandelberg <david@mandelberg.org>
Cc: git@vger.kernel.org
Subject: Re: git switch --force vs --discard-changes: docs don't match behavior
Date: Mon, 17 Mar 2025 10:19:07 -0700 [thread overview]
Message-ID: <xmqqo6xza838.fsf@gitster.g> (raw)
In-Reply-To: <bce5a500-76c4-4462-8f99-b17a6f21f7ec@mandelberg.org> (David Mandelberg's message of "Mon, 17 Mar 2025 12:50:12 -0400")
David Mandelberg <david@mandelberg.org> writes:
> $ touch Makefile
You'd need to explain this example a bit better. The reproduction
must be done in a repository where the master branch has Makefile
tracked, you are not on the master branch, and the commit you have
checked out does not have Makefile tracked. IOW, this is not making
Makefile stat-dirty, but is creating a new untracked file.
As I wasted a few minutes scratching my head before figuring out the
above, I'm leaving a note for others.
> $ git switch master
> error: The following untracked working tree files would be overwritten
> by checkout:
> Makefile
> Please move or remove them before you switch branches.
> Aborting
Makes sense (note. I do not use "git switch" myself).
> $ git switch --discard-changes master
> error: Untracked working tree file 'Makefile' would be overwritten by merge.
I would guess that this "discard changes" takes the word "change" in
a reasonbly strict sense (note. I do not use "git switch" myself,
"git checkout" does not have this option, and I do not recall
thinking about how "--discard-changes" should operate when the
feature was added, so this is pretty much my first time to think
about this issue). Untracked "Makefile", by definition, is not
known by Git, so it cannot even tell if it was changed or not.
If "Makefile" is tracked in the current commit, this would have
discarded changes from that modified file and allowed you to switch
to the master branch.
> $ git switch --force master
> branch 'master' set up to track 'origin/master'.
> Switched to a new branch 'master'
And forcing would give us the same behaviour as "checkout -f".
> Is this a bug in the code or documentation?
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'.
Thanks.
next prev parent reply other threads:[~2025-03-17 17:19 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 [this message]
2025-03-17 17:39 ` Junio C Hamano
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=xmqqo6xza838.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=david@mandelberg.org \
--cc=git@vger.kernel.org \
/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).