Note: Despite having been previously marked as a duplicate of .gitignore exclude folder but include specific subfolder and How to use .gitignore to ignore everything in a directory except one file?, this is NOT a duplicate of those questions. The important issue is auto-detection of new files in supposedly non-ignored directories, and why that's not happening. The content of this question illustrates that I already understand the questions answered at those other links.
If I add a new directory, subdirectory, and file to the root of a project like this:
foo/
bar/
baz.txt
After this, git status -u
reports:
Untracked files:
(use "git add <file>..." to include in what will be committed)
foo/bar/baz.txt
Not surprisingly, if I add then line /foo
to the end of my .gitignore
file, the untracked file is no longer reported.
What does surprise me, however, is if I follow /foo
with !/foo/bar
, the file baz.txt
is still ignored, even though I have negated the ignore pattern for the bar
directory.
/foo
!/foo/bar
What am I missing? Why doesn't negating the exclusion of a subdirectory restore automatic detection of new, unversioned files in that subdirectory?
Even when I specifically re-include the file itself like this:
/foo
!/foo/bar
!/foo/bar/baz.txt
...git status -u
provides me no warning that there's a new file which hasn't been added to version control.
I realize that I can explicitly add any new file to version control, despite what .gitignore
rules in or rules out. What I want is the safety of Git discovering files that I might have forgotten to add, finding them and warning me about them.
For the record, I'm using git 2.23.0 on macOS.