如何使用 Git 跟踪目录而不是它们的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5091017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:10:38  来源:igfitidea点击:

How to track directories but not their files with Git?

gitdirectorygitignore

提问by Ben

I've recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

我最近开始使用 Git,但在做一件事时遇到了麻烦。如何在不跟踪目录内容的情况下跟踪目录?

For example the site I'm working on allows uploads. I want to track the uploads directory so that it is created when branching, etc. but obviously not the files within it (test files whilst in develop branch or the real files in master).

例如,我正在处理的网站允许上传。我想跟踪上传目录,以便在分支等时创建它,但显然不是其中的文件(开发分支中的测试文件或 master 中的真实文件)。

In my .gitignore I have the following:

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

uploads/*.*

Have also tried (which ignores the whole directory):

也试过(忽略整个目录):

uploads/

This directory may also contain sub directories (uploads/thumbs/ uploads/videos/) I would like to be able to track these but not their files.

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

Is this possible with Git? I've searched everywhere without finding an answer.

这可以用 Git 实现吗?我到处搜索都没有找到答案。

回答by Peter Farmer

Git doesn't track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignorefile looks something like this:

Git 不跟踪目录,它跟踪文件,因此要实现这一点,您至少需要跟踪一个文件。所以假设你的.gitignore文件看起来像这样:

upload/*

You can do this:

你可以这样做:

$ touch upload/.placeholder
$ git add -f upload/.placeholder

If you forget the -fyou'll see:

如果你忘记了,-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

Then when you do git statusyou'll see:

然后当你这样做时,git status你会看到:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#   new file:   upload/.placeholder
#

Obviously you can then do:

显然你可以这样做:

$ touch upload/images/.placeholder
$ git add -f upload/images/.placeholder

回答by Abizern

I wrote about this here.

在这里写了这个。

Add a .gitignore within the directory.

在目录中添加一个 .gitignore。

回答by Gonzalo Cao

Best answerd I've found is to include a .gitignore file in your upload folder with this content

我发现的最佳答案是在包含此内容的上传文件夹中包含一个 .gitignore 文件

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

Here you have How can I add an empty directory to a Git repository?

在这里,您有如何将空目录添加到 Git 存储库?

回答by mejiamanuel57

The best solution so far:

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

1) Create a .gitignorefile

1) 创建一个.gitignore文件

2) Write inside:

2)写在里面:

*
*/
!.gitignore

3) Add the .gitignore file to the folder that you want.

3) 将 .gitignore 文件添加到您想要的文件夹中。

Source: https://stackoverflow.com/a/5581995/2958543

来源:https: //stackoverflow.com/a/5581995/2958543

回答by metinsenturk

In order to track only directories but not the files, I did the following. Thanks to the @PeterFarmer's comment on git tracking only files, I've been able to keep all directories excluding the files as described in below.

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

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

# include only .gitkeep files
!/data/**/*.gitkeep

Adding this to the .gitignore file will do the work. The following is my folder structure.

将此添加到 .gitignore 文件将完成这项工作。以下是我的文件夹结构。

data/
├── processed
│   ├── dataset1.csv
│   └── dataset2.csv
├── raw
│   ├── raw_dataset1.json
└── test
    ├── subfolder
    │   └── dataset2.csv
    └── reviews.csv

When I do git add . && git status, git only recognizes folders, but not files.

当我这样做时git add . && git status,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

Keep in mind that the following for your .gitignore files:

请记住,.gitignore 文件的以下内容:

Prepending slash looks only for the root directory.

前置斜杠仅查找根目录。

/dir

/目录

Double asterisk looks for zero or more directories.

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

/**/

/**/