Is that expected?
Other commands seem to not like that:
$ git checkout NonExistingFile error: pathspec 'NonExistingFile' did not match any file(s) known to git
threads / discuss / 52158
Subject: 'git reset -- NonExistingFile' succeeds
Is that expected?
Other commands seem to not like that:
$ git checkout NonExistingFile error: pathspec 'NonExistingFile' did not match any file(s) known to git
Re: 'git reset -- NonExistingFile' succeeds
Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:
> Is that expected? > > Other commands seem to not like that: > > $ git checkout NonExistingFile > error: pathspec 'NonExistingFile' did not match any file(s) known to git
It's sort of expected ;-)
"git $subcmd -- pathspec" is defined to perform $subcmd to the paths that match the given pathspec. "git checkout -- pathspec" checks out the contents out of the index for such paths and "git reset -- pathspec" resets the contents in the index to that of HEAD for such paths.
Historically, we did not consider it is an error for users to specify an empty set (i.e. a pathspec with an element that does not match any path). Because it is easy to mistype "git checkout -- *.rv", let the shell pass "*.rv" literally (because in a project where you meant to say "*.rb", it is very plausible that there is no such path for the glob to match) to "git checkout" and nobody would give you any indication of an error, we made it an error for "git checkout" to be fed a pathspec element that does not match [*1*].
It is expected that other commands that may benefit from a similar handholding mechanism against end-user typos have not learned to do so, simply because nobody bothered to bring it up ;-).
[Footnote]
*1* This cuts both ways and it used to hurt a lot more back when it was introduced.
When you have many tracked *.rb files and some throwaway *.rb files that are not tracked,
git checkout -- \*.rb
may checkout only the ones that are tracked (i.e. known to git---in this case, known to the index) without molesting the untracked ones. But people being people, they can be lazy and say
git checkout -- *.rb
instead (notice the lack of quoting for the asterisk). This would first let your shell expand it and match untracked ones, and then feed the resulting paths as a pathspec to "git checkout", so one of the pathspec elements may be the "untracked.rb" throw-away file. With the "it is an error for a pathspec element not to match any" rule, such a use will now error out.
The good thing is that people can learn, and after getting such an error message, they learn to quote the asterisk from the shell when they want to Git expand it. So it may not hurt that much these days as it used to.
Re: 'git reset -- NonExistingFile' succeeds
On 02.11.2019 5:49, Junio C Hamano wrote:
> It's sort of expected ;-)
Didn't expect such a detailed reply, thanks!
Not really a problem for me, just I was surprised and thought that I'd ask about it.
I came across this issue when writing a test for 'git reset'. I'll simply change the test to verify the repository state instead of expecting 'git reset' to fail.
Re: 'git reset -- NonExistingFile' succeeds
Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:
> On 02.11.2019 5:49, Junio C Hamano wrote: >> It's sort of expected ;-) > > Didn't expect such a detailed reply, thanks! > > Not really a problem for me, just I was surprised and thought that I'd > ask about it. > > I came across this issue when writing a test for 'git reset'. I'll > simply change the test to verify the repository state instead of > expecting 'git reset' to fail.
As I said, it is expected that Git is not perfect and there would be many little corners like this that can use improvements. IOW, it is not unexpected that "git reset -- NoSuchFile" does not raise an error, but that does not make it a bad idea to at least think about teaching it to do so. There _might_ be fallouts, though; there may be scripts by people that rely on 'git reset -- "$variable"' with some random pathspec in a $variable to quietly become no-op when no paths match (in which case, we'd have to mimic ls-files and add a "--error-unmatch" option, perhaps).