How can I add an empty directory to a Git repository?
我如何将一个空目录(不包含文件)添加到Git存储库?
Another way to make a directory stay empty (in the repository) is to create a .gitignore
file inside that directory that contains four lines:
# Ignore everything in this directory
*
# Except this file
!.gitignore
Then you don't have to get the order right the way that you have to do in m104's solution.
Making @GreenAsJade's comment persistent:
I think it's worth noting that this solution does precisely what the question asked for, but is not perhaps what many people looking at this question will have been looking for. This solution guarantees that the directory remains empty. It says "I truly never want files checked in here". As opposed to "I don't have any files to check in here, yet, but I need the directory here, files may be coming later".
You can't. See the Git FAQ.
Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.
Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.
You can say " git add <dir>
" and it will add files in there.
If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.
在目录中创建一个名为.gitkeep
的空文件,并添加该文件。
上一篇: 一个“和”git add“。
下一篇: 我如何将一个空目录添加到Git存储库?