{"thread":{"id":"52158","subject":"'git reset -- NonExistingFile' succeeds","startedAt":"2019-10-30T16:29:18Z","lastAt":"2019-11-06T01:51:19Z","messageCount":4,"participants":["Alexandr Miloslavskiy","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"385161","messageId":"f6d853ce-6f27-ed58-a850-d9a6f245509a@syntevo.com","threadId":"52158","inReplyTo":null,"subject":"'git reset -- NonExistingFile' succeeds","fromName":"Alexandr Miloslavskiy","fromEmail":"alexandr.miloslavskiy@syntevo.com","sentAt":"2019-10-30T16:29:13Z","receivedAt":"2019-10-30T16:29:18Z","isPatch":false,"sender":{"key":"alexandr.miloslavskiy@syntevo.com","avatar":null},"body":"Is that expected?\n\nOther commands seem to not like that:\n\n$ git checkout NonExistingFile\nerror: pathspec 'NonExistingFile' did not match any file(s) known to git\n"},{"id":"385297","messageId":"xmqqo8xu51cv.fsf@gitster-ct.c.googlers.com","threadId":"52158","inReplyTo":"f6d853ce-6f27-ed58-a850-d9a6f245509a@syntevo.com","subject":"Re: 'git reset -- NonExistingFile' succeeds","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-11-02T04:49:36Z","receivedAt":"2019-11-02T04:49:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:\n\n> Is that expected?\n>\n> Other commands seem to not like that:\n>\n> $ git checkout NonExistingFile\n> error: pathspec 'NonExistingFile' did not match any file(s) known to git\n\nIt's sort of expected ;-)\n\n\"git $subcmd -- pathspec\" is defined to perform $subcmd to the paths\nthat match the given pathspec.  \"git checkout -- pathspec\" checks\nout the contents out of the index for such paths and \"git reset --\npathspec\" resets the contents in the index to that of HEAD for such\npaths.\n\nHistorically, we did not consider it is an error for users to\nspecify an empty set (i.e. a pathspec with an element that does not\nmatch any path).  Because it is easy to mistype \"git checkout --\n*.rv\", let the shell pass \"*.rv\" literally (because in a project\nwhere you meant to say \"*.rb\", it is very plausible that there is no\nsuch path for the glob to match) to \"git checkout\" and nobody would\ngive you any indication of an error, we made it an error for \"git\ncheckout\" to be fed a pathspec element that does not match [*1*].\n\nIt is expected that other commands that may benefit from a similar\nhandholding mechanism against end-user typos have not learned to do\nso, simply because nobody bothered to bring it up ;-).\n\n\n\n[Footnote]\n\n*1* This cuts both ways and it used to hurt a lot more back when it\nwas introduced.\n\nWhen you have many tracked *.rb files and some throwaway *.rb files\nthat are not tracked,\n\n    git checkout -- \\*.rb\n\nmay checkout only the ones that are tracked (i.e. known to git---in\nthis case, known to the index) without molesting the untracked ones.\nBut people being people, they can be lazy and say\n\n    git checkout -- *.rb\n\ninstead (notice the lack of quoting for the asterisk).  This would\nfirst let your shell expand it and match untracked ones, and then\nfeed the resulting paths as a pathspec to \"git checkout\", so one of\nthe pathspec elements may be the \"untracked.rb\" throw-away file.\nWith the \"it is an error for a pathspec element not to match any\"\nrule, such a use will now error out.\n\nThe good thing is that people can learn, and after getting such an\nerror message, they learn to quote the asterisk from the shell when\nthey want to Git expand it.  So it may not hurt that much these\ndays as it used to.\n\n"},{"id":"385376","messageId":"df35c20c-669e-35a1-9b2c-22940637b560@syntevo.com","threadId":"52158","inReplyTo":"xmqqo8xu51cv.fsf@gitster-ct.c.googlers.com","subject":"Re: 'git reset -- NonExistingFile' succeeds","fromName":"Alexandr Miloslavskiy","fromEmail":"alexandr.miloslavskiy@syntevo.com","sentAt":"2019-11-04T10:24:01Z","receivedAt":"2019-11-04T10:24:08Z","isPatch":false,"sender":{"key":"alexandr.miloslavskiy@syntevo.com","avatar":null},"body":"On 02.11.2019 5:49, Junio C Hamano wrote:\n> It's sort of expected ;-)\n\nDidn't expect such a detailed reply, thanks!\n\nNot really a problem for me, just I was surprised and thought that I'd \nask about it.\n\nI came across this issue when writing a test for 'git reset'. I'll \nsimply change the test to verify the repository state instead of \nexpecting 'git reset' to fail.\n"},{"id":"385508","messageId":"xmqq36f1ydps.fsf@gitster-ct.c.googlers.com","threadId":"52158","inReplyTo":"df35c20c-669e-35a1-9b2c-22940637b560@syntevo.com","subject":"Re: 'git reset -- NonExistingFile' succeeds","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-11-06T01:51:11Z","receivedAt":"2019-11-06T01:51:19Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:\n\n> On 02.11.2019 5:49, Junio C Hamano wrote:\n>> It's sort of expected ;-)\n>\n> Didn't expect such a detailed reply, thanks!\n>\n> Not really a problem for me, just I was surprised and thought that I'd\n> ask about it.\n>\n> I came across this issue when writing a test for 'git reset'. I'll\n> simply change the test to verify the repository state instead of\n> expecting 'git reset' to fail.\n\nAs I said, it is expected that Git is not perfect and there would be\nmany little corners like this that can use improvements.  IOW, it is\nnot unexpected that \"git reset -- NoSuchFile\" does not raise an\nerror, but that does not make it a bad idea to at least think about\nteaching it to do so.  There _might_ be fallouts, though; there may\nbe scripts by people that rely on 'git reset -- \"$variable\"' with\nsome random pathspec in a $variable to quietly become no-op when no\npaths match (in which case, we'd have to mimic ls-files and add a\n\"--error-unmatch\" option, perhaps).\n\n\n"}]}