如何使用 .gitignore 仅跟踪特定文件?

How do I track only specific files using .gitignore?

我应该在我的 Git 存储库的 .gitignore 中放入什么来排除那里的所有文件和目录 除了 特定的文件和目录。例如,如果我在一个 repo 中有大量文件和目录,并且只想 Git 跟踪 fileX.fileYdirectoryZ,但完全忽略其他所有内容, 我的 .gitignore?

应该是什么

您应该忽略内容;但不包括文件夹。
这允许您排除文件。

/**
!/**/
!fileX

那是因为:

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

检查 .gitignore 的效果 git checkignore -v

git check-ignore -v -- afile