忽略 .gitignore 中的所有内容并添加子文件夹不起作用

Ignoring everything in .gitignore and adding subfolder does not work

我有以下.gitignore:

# Ignore everything
/*
!/.gitignore

# Readd folders:
#!/mainfolder/ # this works!
!/mainfolder/subfolder/
!/mainfolder/subfolder/*

我想忽略一切,但是subfolder/。添加行 !/mainfolder/ 有效,因此 /mainfolder 不再被忽略。但我只想添加 subfolder/ 下面的内容,而 subfolder/ 的第二行和第三行不起作用。

我在网上找到了几个链接,表明这是正确的方法(例如 this one)。那我做错了什么?!

编辑:我在 Windows 10 上使用来自 SourceTree 的 Git 2.17.1。我已经从集成的 MINGW64 bash.

测试了这些结果

您需要先将文件夹列入白名单,然后是文件:

*
!.gitignore
!/mainfolder/
/mainfolder/*
!/mainfolder/subfolder/
!/mainfolder/subfolder/**

对于任何仍然被忽略的文件,检查原因:

git check-ignore -v -- path/to/ignored/file

would it be possible to extend the answer on how you would do this for a deeper subfolder? E.g. how would I ignore everything except ´deepsubfolder´(?): !/mainfolder/subfolder/deepsubfolde

类似于“”:

*
!.gitignore
!*/
!**/deepsubfolder/**