Search code examples
gittortoisegit

.gitignore into the project root directory is not overriden by .gitignores into children directories


In project root directory I have .gitignore file that ignores everything except gitignore files. Here is the content of the file:

**/*
!.gitignore

Then in implementation/xml/ I have another gitignore file with this content:

!**/*

Then in implementation/xml/ant/ I have created new file named texd-odt-build.xml full path (/implementation/xml/ant/texd-odt-build.xml).

Now the second gitignore file that is two folders deeper underneath the one (gitignore file) residing in the project root directory. Then the second gitignore should override the one in the root right? But using the bash I see that texd-odt-build.xml is still into the ignore list.

To check it out I used this command: git status --ignored. What might be the reason for "children" gitignore not working?


Solution

  • Note: to check if a file is ignored, use git check-ignore:

    git check-ignore -v -- afile
    

    As I mentioned in "How do I add files without dots in them (all extension-less files) to the gitignore file?", there is mainly one rule to remember with .gitignore:

    It is not possible to re-include a file if a parent directory of that file is excluded. ()
    (: unless certain conditions are met in git 2.7+)

    That means, when you exclude everything ('*'), you have to white-list folders, before being able to white-list files.

    # Ignore everything under node_modules
    *
    
    # Except for the subfolders
    !**/
    
    # Except for the .gitignore
    !.gitignore
    

    Then in implementation\xml, you can exclude files from the .gitignore (because its folder and sub-folders are already white-listed)

    implementation\xml\.gitignore:
    
    !*