git 忽略除某些子目录中的文件以外的所有内容

git ignore everything except files in certain subdirectories

我刚刚创建了一个这样的文件夹:

.
├── config
│   ├── subfolder
│   │   └── config.xml
│   └── subfolder2
│       └── config.xml
├── .gitignore
├── log2.txt
└── log.txt

我在此文件夹中输入了 git init。我想忽略除 .gitignore 和所有 config.xml 文件之外的所有内容。

这些是我试过的.gitignore

1:

*
!.gitignore
!**/config.xml

2:

*
!.gitignore
!config/*/config.xml

3:

*
!.gitignore
!**/config.xml

git status 显示 .gitignore 是唯一具有上述任一 gitignore 设置的未跟踪文件。

我希望所有 config.xml 文件也都在未跟踪文件列表中。

我已阅读 atlassian 上的教程。而且我认为也许是以下规则使我无法忽略除子目录下某些文件之外的所有内容。

我在 windows 10 操作系统中使用 git version 2.11.0.windows.1。有人知道为什么我的 .gitignore 文件没有按我预期的方式工作吗?

已克隆您的存储库。

有了这个 .gitignore:

*
!.gitignore
!*/*/config.xml

它忽略了除 .gitignore 和配置文件之外的所有内容:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:   config/subfolder/config.xml

no changes added to commit (use "git add" and/or "git commit -a")
.
├── config
│   ├── subfolder
│   │   ├── config.xml
│   │   └── ignoringthis
│   └── subfolder2
│       └── config.xml
├── log3.txt
└── newdir

这是你想要的吗?

感谢 torek 的 ,我终于想出了以下解决方案:

/**
!/**/
!.gitignore
!**/config.xml