Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not ignore a file in a sub directory?

Tags:

git

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.

like image 247
Lee Olayvar Avatar asked Jul 03 '10 02:07

Lee Olayvar


People also ask

Does Gitignore work in subfolders?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I ignore a file without adding to Gitignore?

You just need to add the file to . git/info/exclude in the same way you would add it to . gitignore .

What does sub directory mean?

Definition of subdirectory : an organizational directory on a computer that is located within another directory : subfolder The file you are looking for should have an extension of .EXE.


1 Answers

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
like image 96
Chris Johnsen Avatar answered Oct 22 '22 05:10

Chris Johnsen