git .gitignore 和 .gitkeep 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7229885/
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 11:43:17  来源:igfitidea点击:

What are the differences between .gitignore and .gitkeep?

gitgitignore

提问by Matty

What are the differences between .gitignoreand .gitkeep? Are they the same thing with a different name, or do they both serve a different function?

.gitignore和之间有什么区别.gitkeep?它们是名称不同的同一个东西,还是它们都有不同的功能?

I don't seem to be able to find much documentation on .gitkeep.

我似乎无法在.gitkeep.

回答by Wooble

.gitkeepisn't documented, because it's not a feature of Git.

.gitkeep没有记录,因为它不是 Git 的一个特性。

Git cannot add a completely empty directory. People who want to track empty directories in Git have created the convention of putting files called .gitkeepin these directories. The file could be called anything; Git assigns no special significance to this name.

Git不能添加一个完全空的目录。想要在 Git 中跟踪空目录的人已经创建了将文件.gitkeep放入这些目录中的约定。该文件可以被称为任何东西;Git 没有赋予此名称任何特殊意义。

There is a competing convention of adding a .gitignorefile 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; .gitignoreis also used to list files that should be ignored by Git when looking for untracked files.

.gitignore文件添加到空目录以跟踪它们是一种相互竞争的约定,但有些人认为这令人困惑,因为目标是保留空目录,而不是忽略它们;.gitignore还用于列出 Git 在查找未跟踪文件时应忽略的文件。

回答by sjas

.gitkeepis just a placeholder. A dummy file, so Git will not forget about the directory, since Git tracks only files.

.gitkeep只是一个占位符。一个虚拟文件,所以 Git 不会忘记目录,因为 Git 只跟踪文件。



If you want an empty directory and make sure it stays 'clean' for Git, create a .gitignorecontaining 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 .txtfiles:

如果您希望 Git 只能看到一种类型的文件,这里是一个如何过滤除 .gitignore 和所有.txt文件之外的所有文件的示例:

# .gitignore to keep just .txt files
###################################

# Filter everything...
*

# ... except the .gitignore...
!.gitignore

# ... and all text files.
!*.txt

('#' indicates comments.)

('#' 表示注释。)

回答by Jim Munro

.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 repository, .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 repository.

由于 Git 删除或不向存储库添加空目录,因此 .gitkeep 是一种在存储库中保留空目录的黑客(我不认为它被正式命名为 Git 的一部分)。

Just do a touch /path/to/emptydirectory/.gitkeepto add the file, and Git will now be able to maintain this directory in the repository.

只需touch /path/to/emptydirectory/.gitkeep添加文件,Git 现在就可以在存储库中维护这个目录。