git - 我怎样才能 gitignore 目录结构中的文件但保持目录结构?

标签 git

假设我有一个像这样的目录结构:

app/
  uploads/
    .gitkeep
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

我想保留目录结构但不包含其中的文件(显然 .gitkeep 除外)。文档说:

A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

所以,我希望这样可以解决问题:

/app/uploads/**
!/app/uploads/**/.gitkeep

但是这不起作用。没有添加任何子目录。

最佳答案

我使用的结构如下所示:

app/
  .gitignore
  uploads/
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

我的 app/.gitignore 文件的内容:

uploads/**    # Ignore everything in 'uploads' folder
!uploads/**/  # exclude subdirectories
!.gitkeep     # and .gitkeep files.

关于git - 我怎样才能 gitignore 目录结构中的文件但保持目录结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22082534/

相关文章:

git - 如何贡献给别人的存储库?

git - 非常大的 TFS 存储库——来自子文件夹的 <git tfs branch>

Git:使用 key 文件但不使用 ~/.ssh/config 通过 ssh 访问远程存储库

git - 从 GitHub 历史记录中删除了敏感文件,但我仍然可以访问它

windows - 在 Windows 上使用引号名称的 Git 标签

git - 如何查看 fork 的 github 项目的差异

linux - 压缩时排除选定的子目录

Git BFG 追溯启用 LFS - protected 提交问题

git - JGit:git submodule foreach 命令

git - 致命的 : I don't handle protocol