1

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?

Porcupine
  • 5,885
  • 2
  • 19
  • 28

2 Answers2

2

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).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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/**
Porcupine
  • 5,885
  • 2
  • 19
  • 28