Search code examples
gitgitignore

git check-ignore output empty but still being ignored


I have a folder logs/ which is being ignored:

$ $ git status -s --ignored logs
!! logs/

But, when I run:

git check-ignore -v logs/

I get null output, which seems to indicate that there is nothing ignoring it.

I have nothing staged:

$ git status --untracked-files=no
On branch master
nothing to commit (use -u to show untracked files)

So why is this folder being ignored?

$ git --version
git version 2.10.2

Solution

  • This happens when the ignoring is due to ignoring the file(s) inside the directory, rather than the directory itself. I made a logs/ directory and put some things in it and got the same result you got:

    $ git status -s --ignored logs
    !! logs/
    

    Here is what I put inside logs:

    $ ls -A logs
    foo
    

    Here is why it's ignored:

    $ grep foo .gitignore
    foo
    

    But here git check-ignore -v says nothing about logs/foo:

    $ git check-ignore -v logs/
    

    Why

    Once the directory is "empty" (because all files in it are ignored), it is no longer interesting to Git, since Git does not record directories in commits. But in this case, it's not the directory itself being ignored; it's the summarizing from git status that limits you to seeing just the directory here.

    Hence, when you ask Git "why is logs/ ignored", the answer is, sort of, that it's not ignored, it's just the contents that are ignored. (I think we could say that this is a bug in git check-ignore.)

    How see what's going on

    Add -uall (or --untracked=all) to see the actual ignored files:

    $ git status -s --ignored -uall logs
    !! logs/foo
    $ git check-ignore -v logs/foo
    .gitignore:34:logs/foo  logs/foo
    

    and now you can find out why the files are being ignored, using their (now no longer summarized away) paths.

    (NB: I may have changed the .gitignore contents several times above during the cut and paste, which I did piecemeal: at various times I had logs/foo and foo in .gitignore, both of which made the directory "become empty". So repeating the experiment might give slightly different output. Nonetheless, the key is using -uall to find the file names.)