Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git check-ignore output empty but still being ignored

Tags:

git

gitignore

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
like image 531
Tom Hale Avatar asked Nov 23 '16 11:11

Tom Hale


People also ask

Why is Gitignore being ignored?

Some times, even if you haven't added some files to the repository, git seems to monitor them even after you add them to the . gitignore file. This is a caching issue that can occur and to fix it, you need to clear your cache.

Why is my Git file ignore?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet.

How do I ignore a .gitignore file?

gitignore , can be ignored using explicit repository excludes in your_project_directory/. git/info/exclude . This file will not be shared with other developers, and is specific for that single repository.


1 Answers

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.)

like image 100
torek Avatar answered Sep 23 '22 19:09

torek