Git忽略除某个目录的所有子目录中的某种类型的所有文件?

标签 git gitignore

我正在尝试制作一个 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 addgit 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/

相关文章:

git - 如何避免将特定文件与 git-pull merge ?

php - 我应该使用 Git 来部署 Web 应用程序吗?

git - 为什么 git pull 要求输入密码,尽管之前已经设置过?

Git clone fatal error ,用户凭据正确

git - 如何提交没有更改和新消息?

git - 为什么不 gitignore .gitignore?

git - 尽管我的 .gitignore 为何我的配置文件仍被 checkin ?

git - 我如何告诉 git 忽略以波浪号开头的文件?

windows - .gitignore 任何级别的 NuGet 包文件夹,但包括任何级别的 .targets 文件

Git 预提交 Hook 在 GitHub for mac 中失败(在命令行上工作)