git 使用多个 .gitignore 文件的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10274424/
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
Best practice for using multiple .gitignore files
提问by victorwoo
There exists a collection of useful .gitignore
files at
https://github.com/github/gitignore. Every ignores file there has an extension .gitignore
, e.g. Java.gitignore, vim.gitignore. When I put these files directly into a workdir, they don't work.
https://github.com/github/gitignore 上有一系列有用的.gitignore
文件
。每个忽略文件都有一个扩展名,例如 Java.gitignore、vim.gitignore。当我将这些文件直接放入工作目录时,它们不起作用。.gitignore
Is it necessary to create a single file .gitignore
and merge the constituent files into it?
是否有必要创建单个文件.gitignore
并将组成文件合并到其中?
Accordingly, when using a --global
.gitignore
file, it seems the config points to a single file. If for instance I want to apply all .gitignore
files in a Global/ directory, should I manually merge them into a single file? If so, it's extra work to keep this merged file up to date with the "official" collection.
因此,在使用--global
.gitignore
文件时,配置似乎指向单个文件。例如,如果我想应用.gitignore
Global/ 目录中的所有文件,我应该手动将它们合并到一个文件中吗?如果是这样,则需要额外的工作来使此合并文件与“官方”集合保持同步。
To reiterate, I acknowledge that we can use different .gitignore
files in different directories, but it is not what I want. I want to apply multiple .gitignore
files in a single directory.
重申一下,我承认我们可以.gitignore
在不同的目录中使用不同的文件,但这不是我想要的。我想.gitignore
在一个目录中应用多个文件。
采纳答案by Michael Durrant
To clarify: a projects gitignore file is called .gitignore
澄清:一个项目 gitignore 文件被称为 .gitignore
Background:
背景:
A .gitignore
refers to the directory that it's in, which is either the top level or descendent of a directory with a .git repository, i.e. a ".git/" directory.
A.gitignore
指的是它所在的目录,它是具有 .git 存储库的目录的顶级或后代,即“.git/”目录。
There can be multiple .gitignore
files in any sub directories but the Best Practice is to have one.gitignore
in a given projects root and have that file reference sub-directories as necessary, e.g. images/yearly/recent Otherwise it is be tricky to know "which" .gitignore file to look at to find something that's being ignored. Given that you can use patterns as file names that could be pretty tricky!
可以有多个.gitignore
文件,在任何子目录,但最好的做法是有一个.gitignore
在给定的项目根,并有文件引用子目录必要的,例如图像/年/最近否则是会非常棘手知道“其中”。 gitignore 文件以查找被忽略的内容。鉴于您可以使用模式作为文件名,这可能非常棘手!
I also recommend avoidingusing a global .gitignore
file which applies to allprojects on your machine, although you might keep a template around for using with new projects. The main consideration here is that your .gitignore
will be different from other developers (which may or may not exist) and so the result is undetermined. One example of an exception to this is using a global .gitignore
file for IDE files that I don't want in any project that I open on my machine so I use a global .gitigore
with an entry for .idea/ files (rubyMine).
我还建议避免使用.gitignore
适用于您机器上所有项目的全局文件,尽管您可能会保留一个模板用于新项目。这里的主要考虑是您的.gitignore
意愿与其他开发人员(可能存在也可能不存在)不同,因此结果是不确定的。一个例外的例子是使用全局.gitignore
文件作为 IDE 文件,我不希望在我的机器上打开的任何项目中使用全局文件,因此我使用全局.gitigore
文件和 .idea/ 文件 (rubyMine) 的条目。
The intent of the templates you see listed is that normally you are writing the code for a given filein a specific language. Given this, a template that is based on the language is frequently sufficient.
您看到列出的模板的目的是,通常您正在使用特定语言为给定文件编写代码。鉴于此,基于该语言的模板通常就足够了。
If there are multiple languages in the code base, then used you will need to combine multiple .gitignore
's for those languages, which can be done in a multitude of ways such as:
如果代码库中有多种语言,那么 used 您需要.gitignore
为这些语言组合多个's,这可以通过多种方式完成,例如:
cat .gitignore1 .gitignore2 > .gitignore # if .gitignore doesn't exist yet
cat .gitignore1 >> .gitignore # Add to it if it already exists
paste .gitignore1 .gitignore # Add to it if it already exists
Hot(ish) off the press (summer 2014):
热门(ish)关闭媒体(2014 年夏季):
Gitignorer is a simple utility that aids in the creation of .gitignore files. It pulls specific (specified) .gitignore templates, with common files to exclude, from github.com/github/gitignore, mashes them together, and saves them to a .gitignore in the current directory.
Gitignorer 是一个简单的实用程序,可帮助创建 .gitignore 文件。它从 github.com/github/gitignore 中提取特定(指定).gitignore 模板以及要排除的通用文件,将它们混合在一起,并将它们保存到当前目录中的 .gitignore 中。
Example usage:
用法示例:
gitignorer create c java python
Gitignorer is currently available in the AUR over at https://aur.archlinux.org/packages/gitignorer/and on GitHub at https://github.com/zachlatta/gitignorer
Gitignorer 目前在 AUR 中的https://aur.archlinux.org/packages/gitignorer/和 GitHub 上的https://github.com/zachlatta/gitignorer可用