Lee Olayvar
Lee Olayvar

Reputation: 3810

Not ignore a file in a sub directory?

I have the following .gitignore file

*.pyc

project/app/dist/*
project/bin/*
project/etc/*
project/var/*

!README.txt

So far so good, most of my README.txt files are not being ignored, just like I want it to happen except for project/ect/downloads/README.txt. That file, is being ignored. Why is this? And How can I fix this?

If I remember correctly, I can simply add it to my project manually, but I'd like to learn what I am doing incorrectly in ignoring the file.

Upvotes: 5

Views: 3415

Answers (2)

Chris Johnsen
Chris Johnsen

Reputation: 225027

Git will not search project/etc/downloads/ for any files because you have configured it to ignore project/etc/*. The “unanchored” !README.txt pattern is only good for negating the exclusion of entries in directories that will be searched (i.e. directories that have not themselves been excluded).

If you exclude project/etc/, Git will never search that directory, so negated exclusions can never apply to its contents (not even one so explicit as !project/etc/important-file!—maybe this could be considered a bug?).

However, if you exclude project/etc/*, Git will search that directory, and negated exclusions can apply to its contents. A person might realize that the second pattern will always match all files and assign the same meaning to both patterns, but Git, but Git treats them differently.

You might consider project/etc/ and project/etc/* to mean the same thing, but Git treats them differently because it does not “realize” that the second one will match everything in the directory.

So, to get !README.txt to apply in projects/etc/downloads/ you will have to unignore the directory but ignore its contents before the !README pattern:

project/etc/*

# "unignore, but ignore the immediate contents of" project/etc/downloads
# so that subsequent negated patterns can apply to the immediate contents
!project/etc/downloads/
project/etc/downloads/*

!README.txt

Upvotes: 16

William Pursell
William Pursell

Reputation: 212654

In the .gitignore file, after /project/etc/*, add the 2 lines:

!/project/etc/downloads/
/project/etc/downloads/*

Upvotes: 3

Related Questions