.gitignore,排除文件夹中的所有文件...但保留那些带有 .gitkeep 的子文件夹?

.gitignore, exclude all files in a folder... but keep those sub-folders with a .gitkeep inside it?

我想忽略log 文件夹中的所有文件,.gitkeep 文件 除外(我需要这些文件来保留空目录):

- log
  |- foo.log (should be ignored)
  |- folder1 (should be ignored)
  |- folder2 (keep it because contains a .gitkeep file)
     |- .gitkeep

不工作:

/log/*
!/log/*/.gitkeep

使用 ** 也不行:

/log/*
!/log/**/.gitkeep

这可能吗...无需像这样手动排除每个子文件夹?

/log/folder1/*
/log/folder2/*
!/log/folder2/.gitkeep

Unignore 被 * 忽略的目录:

/log/**
!/log/*/
!/log/*/.gitkeep

没有这个 git 甚至不会查看被忽略的子目录。