87

我在项目的根目录中有数据目录。它有图像目录和一些文件。这是示例:

data
├── images
│   ├── image1.jpg
│   ├── image2.jpg
│   └── image3.jpg 
├── results.csv
└── r.txt

在 gitignore 中写什么,忽略 data/ 目录中的文件(即 results.csv 和 r.txt)和 images/ 目录中的文件(image.jpg、image2.jpg、image3.jpg)?

当我提交它时,存储库中的文件夹结构应该是:

data/
└── images/

所以,我只想提交空文件夹结构。

4

9 回答 9

128

Just add a file .gitkeep in every folder you want committed.

On windows do so by right clicking when in the folder and select: Git bash from here. Then type: touch .gitkeep

于 2013-11-11T20:08:11.173 回答
102

在 Git 中,您不能提交空文件夹,因为 Git 实际上并不保存文件夹,只保存文件。如果您实际上希望它们为“空”(即您没有可提交的内容),则必须在这些目录中创建一些占位符文件。

于 2013-01-26T20:28:40.690 回答
55

这很简单。

告诉.gitignore忽略除.gitignore要保留的文件夹之外的所有内容。放入.gitignore要保留在存储库中的文件夹中。

最上面的内容.gitignore

# ignore everything except .gitignore and folders that I care about:
*
!images*
!.gitignore

在嵌套images文件夹中,这是您的.gitignore

# ignore everything except .gitignore
*
!.gitignore

请注意,您必须在 .gitignore 中拼出您不想在 .gitignore 所在的文件夹中被忽略的文件夹的名称。否则,它们显然会被忽略。

显然,您在存储库中的文件夹不会是空的,因为每个文件夹中都会有.gitignore,但是可以忽略该部分,对吧。:)

于 2013-01-26T23:50:04.523 回答
38

Recursively create .gitkeep files

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;
于 2015-07-06T15:38:08.557 回答
7

Traditionally whenever I've wanted to commit and empty directory structure, I create the structure and then in the leaf directories place an empty file called empty.txt.

Then when I put stuff in that's ready to commit, I can simply remove the empty.txt file and commit the real files.

i.e.

  • data/
    • images/
      • empty.txt
于 2013-01-26T23:55:01.250 回答
4

您可以使用 进行空提​​交git commit --allow-empty,但这将不允许您提交空文件夹结构,因为 git 不知道或不关心文件夹作为对象本身 - 只是它们包含的文件。

于 2013-01-26T20:42:49.770 回答
4

Consider also just doing mkdir -p data/images in your Makefile, if the directory needs to be there during build.

If that's not good enough, just create an empty file in data/images and ignore data.

touch data/images/.gitignore
git add data/images/.gitignore
git commit -m "Add empty .gitignore to keep data/images around"
echo data >> .gitignore
git add .gitignore
git commit -m "Add data to .gitignore"
于 2013-01-27T06:56:41.720 回答
1

According to their FAQ, GIT doesn't track empty directories.

Git FAQ

However, there are workarounds based on your needs and your project requirements.

Basically if you want to track an empty directory you can place a .gitkeep file in there. The file can be blank and it will just work. This is Gits way of tracking an empty directory.

Another option is to provide documentation for the directory. You can just add a readme file in it describing its expected usage. Git will track the folder because it has a file in it and you have now provided documentation to you and/or whoever else might be using the source code.

If you are building a web app you may find it useful to just add an index.html file which may contain a permission denied message if the folder is only accessible through the app. Codeigniter does this with all their directories.

于 2013-11-14T21:59:51.477 回答
1

Simply add file named as .keep in images folder.you can now stage and commit and also able to add folder to version control.

Create a empty file in images folder $ touch .keep

$ git status

On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add ..." to include in what will be committed)

    images/

nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -m "adding empty folder"

于 2017-10-06T06:40:14.553 回答