提交空文件夹结构(使用 git)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14541253/
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
Commit empty folder structure (with git)
提问by Иван Бишевац
I have data directory in project's root. It has images directory and some files. Here is example:
我在项目的根目录中有数据目录。它有图像目录和一些文件。这是示例:
data
├── images
│ ├── image1.jpg
│ ├── image2.jpg
│ └── image3.jpg
├── results.csv
└── r.txt
What to write in gitignore, to ignore files from data/ directory (that is results.csv and r.txt) and files from images/ directory (image.jpg, image2.jpg, image3.jpg)?
在 gitignore 中写什么,忽略来自 data/ 目录(即 results.csv 和 r.txt)的文件和来自 images/ 目录(image.jpg、image2.jpg、image3.jpg)的文件?
When I commit it, folder structure in repository should be:
当我提交时,存储库中的文件夹结构应该是:
data/
└── images/
So, I just want empty folder structure to be commited.
所以,我只想提交空文件夹结构。
回答by Joost van der Laan
Just add a file .gitkeep
in every folder you want committed.
只需.gitkeep
在您要提交的每个文件夹中添加一个文件。
On windows do so by right clicking when in the folder and select: Git bash from here. Then type: touch .gitkeep
在 Windows 上,通过在文件夹中右键单击并选择:Git bash from here 来执行此操作。然后输入:touch .gitkeep
回答by Nevik Rehnel
In Git, you cannot commit empty folders, because Git does not actually save folders, only files. You'll have to create some placeholder file inside those directories if you actually want them to be "empty" (i.e. you have no committable content).
在 Git 中,你不能提交空文件夹,因为 Git 实际上并不保存文件夹,只保存文件。如果您确实希望它们为“空”(即您没有可提交的内容),则必须在这些目录中创建一些占位符文件。
回答by ddotsenko
This is easy.
这很简单。
tell .gitignore
to ignore everything except .gitignore
and the folders you want to keep. Put .gitignore
into folders that you want to keep in the repo.
告诉.gitignore
忽略所有内容,除了.gitignore
要保留的文件夹。放入.gitignore
要保留在 repo 中的文件夹中。
Contents of the top-most .gitignore
:
最上面的内容.gitignore
:
# ignore everything except .gitignore and folders that I care about:
*
!images*
!.gitignore
In the nested images
folder this is your .gitignore
:
在嵌套images
文件夹中,这是您的.gitignore
:
# ignore everything except .gitignore
*
!.gitignore
Note, you must spell out in the .gitignore the names of the folders you don't want to be ignored in the folder where that .gitignore is located. Otherwise they are, obviously, ignored.
请注意,您必须在 .gitignore 中拼出您不想在 .gitignore 所在文件夹中忽略的文件夹的名称。否则,显然,它们将被忽略。
Your folders in the repo will, obviously, NOT be empty, as each one will have .gitignore
in it, but that part can be ignored, right. :)
显然,您在 repo 中的文件夹不会是空的,因为每个文件夹中都会有.gitignore
,但可以忽略该部分,对。:)
回答by emj365
Recursively create .gitkeep files
递归创建 .gitkeep 文件
find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;
回答by Karl Nicoll
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
.
传统上,每当我想要提交和清空目录结构时,我都会创建结构,然后在叶目录中放置一个名为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.
然后,当我将准备提交的内容放入其中时,我可以简单地删除empty.txt
文件并提交真实文件。
i.e.
IE
- data/
- images/
- empty.txt
- images/
- 数据/
- 图片/
- 空的.txt
- 图片/
回答by Kalle Pokki
Consider also just doing mkdir -p data/images
in your Makefile, if the directory needs to be there during build.
mkdir -p data/images
如果在构建期间目录需要在那里,也可以考虑只在你的 Makefile 中做。
If that's not good enough, just create an empty file in data/images and ignore data.
如果这还不够好,只需在 data/images 中创建一个空文件并忽略数据。
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"
回答by David Culp
You can make an empty commit with git commit --allow-empty
, but that will not allow you to commit an empty folder structure as git does not know or care about folders as objects themselves -- just the files they contain.
您可以使用 进行空提交git commit --allow-empty
,但这将不允许您提交空文件夹结构,因为 git 不知道或不关心文件夹作为对象本身——只是它们包含的文件。
回答by Nayagam
Simply add file named as .keep in images folder.you can now stage and commit and also able to add folder to version control.
只需在图像文件夹中添加名为 .keep 的文件。您现在可以暂存和提交,还可以将文件夹添加到版本控制。
Create a empty file in images folder
$ touch .keep
在图像文件夹中创建一个空文件
$ touch .keep
$ git status
$ 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 add .
$ git commit -m "adding empty folder"
$ git commit -m "adding empty folder"
回答by JoeMoe1984
According to their FAQ, GIT doesn't track empty directories.
根据他们的常见问题解答,GIT 不跟踪空目录。
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.
基本上,如果您想跟踪一个空目录,您可以.gitkeep
在其中放置一个文件。该文件可以是空白的,它会正常工作。这是 Gits 跟踪空目录的方式。
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.
另一种选择是为目录提供文档。您可以在其中添加一个自述文件,描述其预期用途。Git 将跟踪该文件夹,因为其中有一个文件,并且您现在已经向您和/或其他可能使用源代码的人提供了文档。
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.
如果您正在构建一个 Web 应用程序,您可能会发现添加一个 index.html 文件很有用,如果该文件夹只能通过该应用程序访问,该文件可能包含一条权限被拒绝的消息。Codeigniter 对其所有目录执行此操作。