本文翻译自:What are the differences between .gitignore and .gitkeep?
What are the differences between .gitignore
and .gitkeep
? .gitignore
和.gitkeep
什么.gitkeep
? Are they the same thing with a different name, or do they both serve a different function? 它们是不同名称的同一个东西,还是它们都起着不同的作用? I don't seem to be able to find much documentation on .gitkeep
. 我似乎无法在.gitkeep
上找到太多文档。
#1楼
参考:https://stackoom.com/question/UKp3/gitignore和-gitkeep有什么区别
#2楼
.gitkeep
is just a placeholder. .gitkeep
只是一个占位符。 A dummy file, so git will not forget about the directory, since git tracks only files. 一个虚拟文件,所以git不会忘记该目录,因为git只跟踪文件。
If you want an empty dir and make sure it stays 'clean' for git, create a .gitignore
containing the following lines within: 如果你想要一个空目录并确保它对git保持“干净”,请创建一个包含以下行的.gitignore
:
# .gitignore sample
###################
# ignore all files in this dir...
*
# ... except for this one.
!.gitignore
If you desire to have only one type of files being visible to git, here is an example how to filter everything out, except .gitignore and all .txt
files: 如果你希望git只能看到一种类型的文件,下面是一个如何过滤所有文件的例子,除了.gitignore和所有.txt
文件:
# .gitignore to keep just .txt files
###################################
# filter everything...
*
# ... except the .gitignore...
!.gitignore
# ... and all text files.
!*.txt
('#' indicates comments.) ('#'表示评论。)
#3楼
.gitkeep
isn't documented, because it's not a feature of Git. .gitkeep
没有记录,因为它不是Git的一个功能。
Git cannot add a completely empty directory . Git 无法添加一个完全空的目录 。 People who want to track empty directories in Git have created the convention of putting files called .gitkeep
in these directories. 想要在Git中跟踪空目录的人已经创建了将这些名为.gitkeep
文件放在这些目录中的约定。 The file could be called anything; 该文件可以被称为任何东西; Git assigns no special significance to this name. Git对此名称没有特别的意义。
There is a competing convention of adding a .gitignore
file to the empty directories to get them tracked, but some people see this as confusing since the goal is to keep the empty directories, not ignore them; 有一个竞争的惯例是将.gitignore
文件添加到空目录中以跟踪它们,但有些人认为这令人困惑,因为目标是保留空目录,而不是忽略它们; .gitignore
is also used to list files that should be ignored by Git when looking for untracked files. .gitignore
还用于列出Git在查找未跟踪文件时应忽略的文件。
#4楼
.gitignore
is a text file comprising a list of files in your directory that git will ignore or not add/update in the repository. 是一个文本文件,包含目录中的文件列表,git将忽略或不在存储库中添加/更新。
.gitkeep
Since git removes or doesn't add empty directories to a repo .gitkeep is sort of a hack (I don't think it's officially named as a part of git) to keep empty directories in the repo. 由于git删除或不添加空目录到repo .gitkeep是一种黑客(我不认为它被正式命名为git的一部分)在repo中保留空目录。
just do a touch /path/to/emptydirectory/.gitkeep
to add the file and git will now be able to maintain this directory in the repository. 只需touch /path/to/emptydirectory/.gitkeep
即可添加文件,git现在可以在存储库中维护此目录。