从特定文件夹中包含通常排除的文件

Include generally excluded file from specific folder

在我的项目中,我在项目子文件夹中生成了文件 Widget.js,因此我 git- 使用此规则忽略它们:

widgets/*/Widget.js

所以它会忽略像这样的文件:

但是现在,在一个特定的文件夹中,我想将此文件包含到存储库中。例如:文件

应该包括在内。

是否可以根据需要设置规则?

使用!再次添加此文件 https://git-scm.com/docs/gitignore#_pattern_format.

只需添加

!widgets/include_1/Widget.js

之后

 widgets/*/Widget.js

首先取消忽略目录,然后取消忽略 .gitignore 中的文件:

widgets/*/Widget.js
!widgets/include_1/
!widgets/include_1/Widget.js

如果只是单个文件,只强制添加文件而不触及 .gitignore 规则可能会更容易。 .gitignore 仅适用于未跟踪的文件,因此一旦您添加文件一次,它将保持跟踪状态:

git add -f widgets/include_1/Widget.js

Documentation:

-f

--force

Allow adding otherwise ignored files.