Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git checkout -- *" returns "error: pathspec did not match any files known to git"

Tags:

git

As in question, if I want to discard changes in working directory I run command git checkout -- * but git returns information error: pathspec 'databaseName.tmp' did not match any file(s) known to git.. I'm not git master and I don't know how to solve it. Any idea?

like image 735
pepuch Avatar asked Jan 22 '15 06:01

pepuch


1 Answers

As genisage noted in a comment, you're asking git explicitly to check out the file databaseName.tmp, as if you'd typed in:

git checkout -- databaseName.tmp

This is because the * you typed in is handled by the shell, before git ever has a chance to see your command. The shell replaces * with all the file names in your current working directory,1 and then having done that, then runs git, without any indication that the actual command you entered had * in it, rather than all those names.

Again, git has no idea that you used the asterisk * character, all it sees is a list of file names, including any top-level ignored files that are not being stored in git.

Confusingly, if you manage somehow2 to pass a literal asterisk * to git, git will expand the *, but with a different set of file names: those known to git. That would do what you want.

There's an easier way, though: git will check out directories recursively by checking out all the files it knows about in that directory. So, instead of using *, simply use . to ask git to check out the current directory:

git checkout -- .

If git knows about ./a and ./b but not ./databaseName.tmp, this will check out ./a and ./b and not attempt to do anything with ./databaseName.tmp.3


1More accurately, files whose names do not start with a leading dot ..

2And in fact, it's quite easy to manage, e.g., simply insert a backslash \ in front of the asterisk: git checkout -- \*. Or, use single or double quotes, both of which protect against shell globbing. Single quotes also inhibit shell variable expansion, while double quotes permit variable expansion but inhibit globbing.

3It's worth pointing out a subtle difference here: * expands to file names that do not start with ., while asking git to check out . causes it to check out all files in the directory, including those whose names start with .. Hence both git checkout -- \* and git checkout -- * will not undo changes to a file named .secret, for instance, while git checkout -- . will undo such changes.

like image 140
torek Avatar answered Oct 15 '22 17:10

torek