如何使 gitignore 排除 2 个文件夹中存在的特定 js 文件,而忽略所有其他 js 文件?

How to make gitignore exclude a specific js file present in 2 folders, whereas ignoring all other js files?

文件结构:

我想忽略所有 .js 文件,除了 file.js(它存在于两个同名文件夹中)。所以,在我的 gitignore 中,我尝试了以下操作:

选项 1:

!*file.js
*.js

选项 2:

!/*/file.js
*.js

选项 3:

!/Folder1/file.js
!/Folder2/file.js
*.js

但始终只有 Folder1 中的 file.js 包含在所有 3 个选项中,而 Folder2 中的 file.js 始终被忽略。两个文件夹中的其他js文件被完美忽略。我在这里做错了什么?

您希望全局 *.js 位于顶部。通过将 *.js 放在底部,它会覆盖它上面的排除项。

*.js
!Folder1/file.js
!Folder2/file.fs