“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?

  • How to merge remote master at two commits earlier than current local master
  • Does git have an option to show the commits to be merged before performing the merge?
  • how to use git clone for two developers
  • git pull: update repo without changing the contents of a specific directory
  • How to guerilla VC with git while forced to use CVS at work?
  • Make a git deployment owned by www-data
  • Pull all commits from a branch, push specified commits to another
  • sometimes I will lose commit's signature
  • Visual studio 2012 git ssh
  • Git Push To Staging Server - Error message
  • Git best practice for a cross-repository folder (TortoiseGit)
  • How do I use git rebase -i after git merge without messing things up?
  • 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.