git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: "git checkout" resets modified files

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 29, 2015, 07:58 UTC
Message-ID
<xmqqh9nizfue.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<xmqqlhcuzhsu.fsf@gitster.mtv.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
> I do not think this behaviour depends on the vintage of Git.  An
> empty string as pathspec has always matched "everything in here"
> AFAIR.

Having said all that, I think the DWIM used by "git checkout" has some room for improvement, namely, its "we didn't see a double-dash; is it a ref or is it a pathspec?" disambiguation should become similar to the revision parser's.

Notice that
    $ git log ""

will error out, saying that an empty string does not name either a revision or an existing path in the working tree. I think the same "refusal due to ambiguity" should be in "git checkout" when the user says

    $ git checkout ""

Right now, when there is no double-dash, the code checks if the first token X can be DWIMmed as a ref. It may either name an existing branch X, or it may be a non-existing branch name, but there is a single remotes/Y/X and the user is too lazy to say 'git checkout -t -b remotes/Y/X X' and instead said 'git checkout X'.

And if the first token X cannot be DWIMmed as a ref according to the above logic, unlike the revision parser, we do not check it (and all the subsequent tokens) name existing filesystem entity.

I think we really should. With that, Steve's command line would have correctly diagnosed as a liely error.

Of course, that still allows people to say
    $ git checkout -- ""
if they really mean it, but they can type
    $ git checkout -- :/
just as easily, so there is no big loss.

After following the existing code myself, I think this is a rather easy low-hanging fruit for any aspiring Git hacker. Hint, hint...

Show 10 quoted lines
> [Footnote]
>
> *1* Checking out a branch whose name is an empty string can be asked
>     explicitly by disambiguating double-dash like this:
>
>     $ git checkout "" --
>
> And it will be answered (correctly) with:
>
>     fatal: invalid reference
Previous: Junio C Hamano
Message 3 of 3 in “"git checkout" resets modified files”
  1. Steve HeynsAug 29, 2015
  2. Junio C HamanoAug 29, 2015
  3. Junio C HamanoAug 29, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.