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?
2 Answers
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).

- 1,262,500
- 529
- 4,410
- 5,250
Update: In VONC's answer he is correct to mention about !*/
and the slash in the beginning of the pattern.
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:
Inside .gitignore add the following:
*
!.gitignore
!*/
!/**/Sync/**

- 5,885
- 2
- 19
- 28
-
I have edited my answer to explain the/ which is an anchor. Please consider accepting the right answer again – VonC May 19 '18 at 04:35
-
@Vonc Thanks for explaining the slash in the beginning, :) – Porcupine May 19 '18 at 14:44
-
Sure, that was missing indeed. – VonC May 19 '18 at 14:45