Search code examples
gitgithubgitlabgitignore

Git - Unignore any folder located anywhere inside repository that recursively allows everything


How to unignore folder with a particular name (e.g. Sync) that can be located anywhere inside the repository and Recursively allow everything inside it, violating every other rule of gitignore?


Solution

  • The solution proposed (!/**/Sync/**/*) would not work alone, because It is not possible to re-include a file if a parent directory of that file is excluded.

    So if you have a .gitignore with:

    *
    !.gitignore
    

    You must whitelist folders first (!*/) and then exclude files (!**/Sync/**, no need for **/*)

    The end result would be a .gitignore like:

    *
    !.gitignore
    !*/
    !**/Sync/**
    

    Don't forget to check with any file which .gitignore rule applies with:

    git check-ignore -v -- aFile
    

    Note: / would be needed (for !**/Sync/**) if you wanted to anchor the whitelist from the top of your repository.
    If not, no / needed: this would be relative to wherever your .gitignore is).

    From gitignore man page:

    If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).