简体   繁体   中英

.gitignore How to ignore first level files in a directory but not sub-files?

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.

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 (ie no wildcards)

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

foo/
!foo/bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM