如何使用Git跟踪目录而不是其文件?

如何使用Git跟踪目录而不是其文件?,git,directory,gitignore,Git,Directory,Gitignore,我最近开始使用Git,但有一件事我遇到了麻烦。如何跟踪目录而不跟踪其内容 例如,我工作的站点允许上传。我想跟踪uploads目录,以便在分支时创建它,等等,但显然不是其中的文件(在develop branch中的测试文件或master中的真实文件) 在我的.gitignore中,我有以下内容: uploads/*.* 上传/** 也尝试过(忽略整个目录): 上传/ 此目录还可能包含子目录(uploads/thumbs/uploads/videos/),我希望能够跟踪这些子目录,但不能跟踪它们的文

我最近开始使用Git,但有一件事我遇到了麻烦。如何跟踪目录而不跟踪其内容

例如,我工作的站点允许上传。我想跟踪uploads目录,以便在分支时创建它,等等,但显然不是其中的文件(在develop branch中的测试文件或master中的真实文件)

在我的.gitignore中,我有以下内容:

uploads/*.* 上传/** 也尝试过(忽略整个目录):

上传/ 此目录还可能包含子目录(uploads/thumbs/uploads/videos/),我希望能够跟踪这些子目录,但不能跟踪它们的文件


这在Git中是可能的吗?我到处搜索都没有找到答案。

Git不跟踪目录,它跟踪文件,所以要实现这一点,您需要跟踪至少一个文件。因此,假设您的
.gitignore
文件如下所示:

upload/*
您可以这样做:

$ touch upload/.placeholder
$ git add -f upload/.placeholder
如果您忘记了
-f
,您将看到:

$ git add upload/.placeholder The following paths are ignored by one of your .gitignore files: upload Use -f if you really want to add them. fatal: no files added # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: upload/.placeholder # 显然,你可以这样做:

$ touch upload/images/.placeholder
$ git add -f upload/images/.placeholder
这是我写的


在目录中添加一个.gitignore。

我找到的最佳答案是在上载文件夹中包含一个包含此内容的.gitignore文件

# Ignore everything in this directory
*
# Except this file
!.gitignore

这里有迄今为止最好的解决方案:

1) 创建一个.gitignore文件

2) 写在里面:

*
*/
!.gitignore
3) 将.gitignore文件添加到所需的文件夹中


来源:

为了只跟踪目录而不跟踪文件,我做了以下操作。感谢@PeterFarmer对git只跟踪文件的评论,我已经能够保留所有目录,不包括下面描述的文件

# exclude everything in every folder
/data/**/*.*

# include only .gitkeep files
!/data/**/*.gitkeep
将其添加到.gitignore文件将完成此工作。下面是我的文件夹结构

data/
├── processed
│   ├── dataset1.csv
│   └── dataset2.csv
├── raw
│   ├── raw_dataset1.json
└── test
    ├── subfolder
    │   └── dataset2.csv
    └── reviews.csv
当我做
git添加时git状态
,git只识别文件夹,而不识别文件

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   data/processed/.gitkeep
        new file:   data/raw/.gitkeep
        new file:   data/test/.gitkeep
        new file:   data/test/subfolder/.gitkeep
要提交的更改:
(使用“git重置磁头…”取消分级)
修改:.gitignore
新文件:data/processed/.gitkeep
新文件:data/raw/.gitkeep
新文件:data/test/.gitkeep
新文件:data/test/subfolder/.gitkeep
请记住,对于.gitignore文件,请执行以下操作:

前置斜杠只查找根目录

/迪尔

双星号查找零个或多个目录

/**/


Git只跟踪文件。您可以做的是使用git钩子并跟踪将包含目录列表(使用钩子自动生成)的文件。虽然我不完全确定这是否可能。更具体的是:Git跟踪内容,而(对于Git)空目录不是内容。不管你同意与否,你都可以争论,但作为另一个“解决办法”,你可以考虑在需要时在build/install/上创建目录。但是:我也使用“
.placeholder
-file”-解决方案;)我没有使用.placeholder,而是使用.gitignore文件。这不仅解决了目录中的排除问题,而且还充当了占位符本身。谢谢,这正是我所需要的。占位符的一个常见约定是gitignore文件。@Jefromi确实,正如我对Abizern的评论,我从来没有考虑过使用
.gitignore
文件作为占位符。Oops,Git不跟踪目录,当他们告诉你在UNIX中一切都是文件时,它跟踪文件听起来很有趣,甚至一个目录就是一个文件。很好的小文章和注释!从未考虑过使用.gitignore作为占位符文件…谢谢。通过写一篇帖子和获取评论,我学到了一些东西。现在使用
似乎有一种流行的命名惯例。gitkeep
现在刚听到“哈哈”的声音,我希望我能在6个不同的时间投票谢谢!我正在使用这种方法,它非常有用这是最优雅的解决方案
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   data/processed/.gitkeep
        new file:   data/raw/.gitkeep
        new file:   data/test/.gitkeep
        new file:   data/test/subfolder/.gitkeep