使用多个.gitignore文件的最佳做法 [英] Best practice for using multiple .gitignore files

查看:784
本文介绍了使用多个.gitignore文件的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


.gitignore 文件集合noreferrer> https://github.com/github/gitignore 。每个忽略文件都有一个扩展名 .gitignore ,例如Java.gitignore,vim.gitignore。当我将这些文件直接放入工作目录时,它们不起作用。

是否需要创建单个文件 .gitignore 并将组成文件合并到它中?



因此,当使用 - global .gitignore 文件,它似乎配置指向单个文件。例如,如果我想将所有 .gitignore 文件应用于Global /目录,我应该手动将它们合并到单个文件中吗?如果是这样,这个额外的工作是保持这个合并文件与官方集合保持同步。

重申,我承认我们可以使用不同的 .gitignore 在不同目录中的文件,但它不是我想要的。我想在一个目录中应用多个 .gitignore 文件。

解决方案

澄清:项目gitignore文件被称为 .gitignore






背景:
$ b $ a .gitignore 指的是它所在的目录,它可能是顶级或后代.git存储库的目录,即.git /目录。

可以有多个 .gitignore 任何子目录中的文件,但最佳实践是在给定的项目根目录中有一个一个 .gitignore ,并具有该文件引用子目录如有必要,例如图像/每年/最近否则,要知道哪个.gitignore文件要查找以找到被忽略的内容是非常棘手的。鉴于您可以使用模式作为文件名,可能会非常棘手!



我还建议使用全局来避免 .gitignore 文件,它适用于您计算机上的所有项目,尽管您可能会保留一个模板用于新项目。这里主要考虑的是你的 .gitignore 与其他开发者(可能存在也可能不存在)不同,所以结果是不确定的。这种情况的例外之一是使用IDE文件的全局 .gitignore 文件,这些文件我不希望在我打开的任何项目中使用,因此我使用全局 .gitigore 与.idea / files(rubyMine)的条目。




您列出的模板意图是通常您正在为特定语言的给定文件编写代码。鉴于此,基于该语言的模板通常就足够了。

如果代码库中有多种语言,那么您需要将多个 .gitignore 的s对于那些可以以多种方式完成的语言,比如:

  cat .gitignore1 .gitignore2> .gitignore#if .gitignore尚不存在
cat .gitignore1>> .gitignore#如果它已存在,则添加到它
粘贴.gitignore1 .gitignore#如果它已存在,则添加到它






Hot(ish)off press(2014年夏季): b
$ b

Gitignorer是一个简单的工具,帮助创建.gitignore文件。它从github.com/github/gitignore中提取特定的(指定的).gitignore模板,将通用文件排除在外,将它们混合在一起,并将它们保存到当前目录中的.gitignore中。



示例用法:

  gitignorer创建c java python 

Gitignorer目前可在AUR上 https://aur.archlinux.org/packages/gitignorer/ 和GitHub的 https:/ /github.com/zachlatta/gitignorer





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.

Is it necessary to create a single file .gitignore and merge the constituent files into it?

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.

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.

解决方案

To clarify: a projects gitignore file is called .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.

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!

I also recommend avoiding using a global .gitignore file which applies to all projects 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).


The intent of the templates you see listed is that normally you are writing the code for a given file in 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:

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):

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.

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


这篇关于使用多个.gitignore文件的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆