Git 忽略子文件夹但不忽略文件夹中的文件

Git ignore subfolders but not the files in a folder

我环顾四周,就是找不到与我相同的场景。我有:

--public
      |--img
           |--logo (folder)
           |--post_image (folder)
           |--banner.jpg
           |--icon.ico
           |--image1.jpg
           |-........etc

我需要忽略 logopost_image 文件夹,但我想保留 img 文件夹中的每个文件。我该怎么做?

现在我有 **/public/img 但我觉得这不是正确的方法!

您可以将 .gitignore 文件放入任何子目录,它不必位于根目录。在您的情况下,public/img 目录中的 .gitignore 文件的内容为:

logo
post_image

会做这份工作。

对于其他方法,您可能需要查看:

  • gitignore all files of extension in directory
  • Git-ignore certain files in sub-directories, but not all

我发现这更简单(至少在有很多子文件夹的情况下):

public/img/*/

这将排除 public/img/ 中的所有子文件夹,但不会排除此文件夹中的文件。

请尝试以下选项。

public/img/*

!public/img/logo/

!public/img/post_image/

git-ignore