如何忽略 git 中除文件夹及其所有子文件夹/内容之外的所有内容

How to ignore everything except a folder and all it's subfolders / contents in git

在我的 .gitignore 我有:

*
!file.txt
!folder1
!folder1/
!folder1/*

这几乎可以满足我的要求,除非我在 folder/ 之后添加一个文件夹以外的内容时它不会被提交。例如 folder1/test/folder2/test.txt 将不会被提交。有什么办法可以解决这个问题而不用在我的 .gitignore 中添加无穷无尽的 *?

编辑:

我一直在玩这个,这是一个较短的版本

/*          # ignore everything in root
!/folder1   # except folder1

原版:

试一试

*
!folder1
!folder1/**

来自gitignore documentation

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

...

  • A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

...