“git checkout — *” returns “error: pathspec did not match any files known to 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?

  • HEAD and ORIG_HEAD in Git
  • Does a git commit hash equal a repository state?
  • Undoing a git pull --rebase
  • What is the opposite of `git filter-branch --subdirectory-filter`?
  • How to connect to a remote GIT repository?
  • How to amend older Git commit?
  • How to migrate part of an SVN repo with history to a new Git repository?
  • Recover history from foolish git-svn merge
  • Disable tag deletion
  • Git log without cloning the repository?
  • Reuse a Part of a git Repository
  • Visual studio 2010 git extention manage remotes unable to connect
  • One Solution collect form web for ““git checkout — *” returns “error: pathspec did not match any files known to git””

    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.

    Git Baby is a git and github fan, let's start git clone.