我正在尝试制作一个 gitignore 文件,它将忽略所有 .jar 文件,除非它们位于名为 libs 的文件夹中。这是我的基本文件结构:
-.gitignore
-libs/
-goodFile.jar
-someFolder/
-subFolder/
-alsoGood.jar
-otherCode/
-fileToExclude.jar
-otherOtherCode/
-otherSubfolder/
-alsoExclude.jar
我目前在 .gitignore 中尝试过:
*.jar
!libs
!libs/
!libs/*
!libs/**
!libs/**/
!libs/**/*.jar
!libs/*.jar
可以单独使用,也可以组合使用,甚至可以一起使用。它们都不起作用。我发现这样做的唯一方法是将另一个 .gitignore 文件放入 libs/
(我希望避免)或使用 !libs/*/*/*.jar
子目录的每个可能级别的行。有没有办法让它忽略除 libs 中的所有 jar?
最佳答案
怎么样:
*.jar
!libs/**/*.jar
顺序很重要。
编辑
我使用了您的项目结构,并在执行 git add
和 git status
$ git stat
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: libs/goodFile.jar
new file: libs/someFolder/subFolder/alsoGood.jar
new file: libs/someFolder/subFolder/test/anotherFolder/test.jar
$ cat .gitignore
*.jar
!libs/**/*.jar
关于Git忽略除某个目录的所有子目录中的某种类型的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537528/