Search code examples
gitversion-controlgitignore

How to add an exception to a deeply nested file in .gitignore?


We are trying to ignore a folder and add an exception to a specific file, within that folder, which is nested several levels deep.

Here is how i am doing it right now (works, but is complicated):

/ignored_folder/*
!/ignored_folder/foo/
/ignored_folder/foo/*
!/ignored_folder/foo/exception_file.txt

Is there a way to simplify this, that would work for any depth? This configuration does not make exception_file.txt an ignore exception (does not work):

/ignored_folder/*
!/ignored_folder/foo/exception_file.txt

Solution

  • Ignoring folders means not looking inside them at all, and ignored_folder/* matches folder names inside the (now-misleadingly-named) ignored_folder. You want

    ignored_folder/**
    !ignored_folder/**/
    

    That's "recursively ignore all files inside ignored_folder, but don't skip over nested folders, look in side them anyway because I might want to cherry-pick individual files inside them".

    And then after that you can casually add

    !ignored_folder/foo/exception_file.txt
    

    edit: but tttt I'd start with @TheIceBar's method and ignore or squelch any warnings.