如果你有类似这样的文件结构?
foo
├── bar
│ ├── file1.coffee
│ ├── file2.coffee
│ └── file3.coffee
├── file1.js
├── file2.js
└── file3.js
如何忽略foo
目录下的一级文件(file1.js
, file2.js
, file3.js
) 但不要忽略 bar
目录中的所有内容(file1.coffee
、file2.coffee
、file3.coffee
)
只要结构保持相同/相似,但文件名和扩展名当然会有所不同。
是否可以在每个目录没有自己的 .gitignore 和白名单的情况下执行此操作?文档似乎没有提到如何实现这一点。
最佳答案
使用 *
作为第一条规则也会忽略文件夹,并且:
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)
所以你需要在忽略第一级后将文件夹列入白名单。
然后将白名单文件夹下的所有内容列入白名单:
*
!*/
!*/**
或者,使用 Sven Marnach的变体,使用 /*
仅针对第一级的文件和文件夹:
/*
!*/
请注意,使用 git 2.9.x/2.10(2016 年年中?),如果文件的父目录被排除在外,则可能会重新包含该文件 if there is no wildcard in the path re-included .
Nguyễn Thái Ngọc Duy (pclouds
)正在尝试添加此功能:
- commit 506d8f1对于 git v2.7.0,在 commit 76b620d 中还原git v2.8.0-rc0
- commit 5e57f9c git v2.8.0-rc0,...在 commit 5cee3493 中恢复(!) git 2.8.0-rc4.
但是,由于重新收录的条件之一是:
The directory part in the re-include rules must be literal (i.e. no wildcards)
除非我们只讨论 foo 和 bar 文件夹,否则这在这里无论如何都行不通:
foo/
!foo/bar
关于git - .gitignore 如何忽略目录中的一级文件而不是子文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667449/