"git checkout - *" returns "error: pathspec does not match files known by git"

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

+5
source share
1 answer

As the genisage noted in the comment , you ask git to explicitly check outside the databaseName.tmp file, as if you entered:

 git checkout -- databaseName.tmp 

This is because the entered * processed by the shell before git can see your command. The shell replaces * all the file names in your current working directory, 1 and then doing this, then running git , without any indication that the actual command entered had * in it, and not in all of these names.

Again, git does not know that you used the asterisk * character, all it sees is a list of file names, including any files with an ignored top level that are not stored in git.

It is vague if you manage somehow 2 to pass the letter asterisk * to git, git will expand * , but with a different set of file names: those known as git. This will do what you want.

There is an easier way: git will recursively check directories, checking all the files that it knows about in that directory. So instead of * just use . to ask git to check the current directory:

 git checkout -- . 

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


1 More precisely, files whose names do not begin with a leading point . .

2 And it’s actually quite easy to control, for example, just insert a backslash \ in front of the asterisk: git checkout -- \* . Or, use single or double quotes, both of which protect against shell pushing. Single quotes also prevent expansion of a shell variable, while double quotes allow expansion of a variable, but prevent globalization.

3 It is worth noting a slight difference here: * expands to file names that do not start with . while the git request for validation . forces it to check all files in the directory, including those whose names begin with . . Therefore, both git checkout -- \* and git checkout -- * , for example, will not .secret changes to a file named .secret , and git checkout -- . discard such changes.

+15
source

Source: https://habr.com/ru/post/1211685/


All Articles