5

If you had a file structure something like?

foo
├── bar
│   ├── file1.coffee
│   ├── file2.coffee
│   └── file3.coffee
├── file1.js
├── file2.js
└── file3.js

How to ignore first level files in the foo directory (file1.js, file2.js, file3.js) BUT NOT IGNORE everything in the bar directory (file1.coffee, file2.coffee, file3.coffee)

Providing the structure stays the same/ similar, but file names and extensions will of course be different.

Is it possible to do this without each directory having it's own .gitignore and whitelisting? The documentation doesn't seem to mention how to achieve this.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64

1 Answers1

7

Using * as a first rule would ignore folders as well, and:

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.?+, see below)

So you need to white-list folders after ignoring the first level.
And then you white-list everything below those white-listed folders:

*
!*/
!*/**

Or, using Sven Marnach's variation, using /* which targets only the files and folders of the first level:

/*
!*/

Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

However, since one of the condition to re-inclusion was:

The directory part in the re-include rules must be literal (i.e. no wildcards)

That would not have worked here anyway, unless we are talking just about foo and bar folders:

foo/
!foo/bar
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250