Git 排除所有文件夹中与名称匹配的所有文件,除了名为 Views 的文件夹

Git exclude all files that match a name in all folders expect folders named Views

我想从目录中的每个文件夹中排除所有名为 Web.config 的文件 期望父文件夹命名为 'Views'

前目录:

-Root

  -ApiProject
      -Web.config (should exclude)
      -SomeOther.file (should allow)
      -SubFolder     (should allow)
          -one.file     (should allow)
          -two.file     (should allow)
  -MvcProject
      -Web.config (should exclude)
      -Views
          -Web.config (should allow)

我已经尝试通过 .gitignore 文件中的以下内容,但似乎没有用。未添加 Views 文件夹中的 Web.config

[Ww]eb.config

!**/观看次数/[Ww]eb.config

应该是

**/[Ww]eb.config
!**/Views/[Ww]eb.config

**/[Ww]eb.config 使 Git 忽略任何文件夹中名为 Web.configweb.config 的文件,并且 !**/Views/[Ww]eb.config 覆盖以前的模式以不忽略这些文件,如果它们位于 Views 文件夹中。

另外请记住,gitignore 仅适用于当前未跟踪的文件:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

How to make Git “forget” about a file that was tracked but is now in .gitignore?