6

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?

7
  • do you have any untracked files in your working directory? Commented Jan 22, 2015 at 7:00
  • 1
    That's your problem, you can't check out an untracked file from the index. So instead of just a * you need to list only the files you actually want to check out. Commented Jan 22, 2015 at 7:03
  • So why git returns information about file that is not on my current branch? Regarding to my current branch status I have some untracked files but as I remember git returns this information always if I want to checkout HEAD files, it doesn't matter that I have untracked files. Commented Jan 22, 2015 at 7:04
  • 1
    the command git checkout -- * is trying to fetch every file in the current directory from the index. Untracked files might as well not exist as far as git is concerned. So when you try to fetch one of them from the index it yells at you. Commented Jan 22, 2015 at 7:09
  • You can use git checkout -- . instead. Commented Jan 22, 2015 at 7:12

1 Answer 1

16

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to note that it works with paths too. If you're in the root of your repo you can do git checkout -- path/to/files/. to revert all files in that directory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.