忽略除 .gitignore 中一个目录之外的所有内容

Ignore everything except one directory in .gitignore

我的目录结构如下:

/app
/src
    /MyProject
        /FirstProject
            /Controller
                /file-1-1.php
                /file-1-2.php
                /file-1-3.php
            /Resources
                /view-1.html.twig
                /view-2.html.twig
        /SecondProject
            /Controller
                /file-2-1.php
                /file-2-2.php
                /file-2-3.php
            /Resources
                /view-3.html.twig
                /view-4.html.twig
/vendor
/web

我想忽略我存储库中的所有文件/src/MyProject/SecondProject/Resources 中的文件除外。 我尝试了很多方法但没有成功。 GitHub 应用程序未检测到我想要的目录。通常此应用程序不检测任何文件或检测所有文件,所以我很困惑。

我试过了:

# IGNORE:
app/
src/
vendor/
web/

# ALLOW:
  # my first attempt:
!src/MyProject/SecondProject/Resources/*
  # second attempt:
!src/MyProject/SecondProject/Resources/**/
  # third attempt:
!src/MyProject/SecondProject/Resources/**/*

其他问题还没找到答案!

这个 .gitignore 将实际工作:

/app
/vendor
/web
/src/**/**
!src/**/
!/src/MyProject/SecondProject/Resources/**

mipadi's answer 指的是在 git 2.7.0 中引入的机制,并且...在 2.7.1 中逆转!

剩下的唯一规则是:

It is not possible to re-include a file if a parent directory of that file is excluded.

因此需要先将 src 子文件夹列入白名单,然后再将 Resources 内容列入白名单。

我用以下方法测试了上面的内容:

> git --version
git version 2.7.1.windows.1