Q . git error- did not match any file(s) known to git?

@mit $ingh. asked, Jul 19 ' 2016

I have added a pattern to ignore files recursively in exclude file inside .info in .git directory.

pattern : httpdocs/**/bak_*.*

If I commit from httpdocs it is working and from the changed directory it gives error.

git commit -m "formatting js" httpdocs/* [working]
git commit -m "formatting js" httpdocs/dir1/dir2/* [giving below error]

#error: pathspec 'httpdocs/dir1/dir2/bak_admin.abc.php' did not match any file(s) known to git.
Basically I want to know why this is happening.
  • git
  • git-commit
  • git-error

1 Answers

The * wildcard pattern is expanded by the shell (not by git).
That means git commit -m "formatting js" httpdocs/* is translated by

 git commit -m "formatting js" httpdocs/fileorDir1
 git commit -m "formatting js" httpdocs/fileorDir2
 git commit -m "formatting js" httpdocs/fileorDir3

If all those files were already tracked, git commit works. If they are not, the error message you saw is returned.

In the case of git commit -m "formatting js" httpdocs/dir1/dir2/*, that is translated as:

git commit -m "formatting js" httpdocs/dir1/dir2/fileirDir1
git commit -m "formatting js" httpdocs/dir1/dir2/fileirDir2
git commit -m "formatting js" httpdocs/dir1/dir2/fileirDir3

That will include files which are explicitly ignored (not tracked at all), including bak_admin.abc.php.
Even if the .gitignore is correctly set, the shell (not git) will pass bak_admin.abc.php to the git commit command, resulting in

error: pathspec 'httpdocs/dir1/dir2/bak_admin.abc.php' did not match any file(s) known to git.

Solution: do not use '*'

git commit -m "formatting js" httpdocs/
git commit -m "formatting js" httpdocs/dir1/dir2/


Leave a Ansewer