Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for using multiple .gitignore files

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.

like image 636
victorwoo Avatar asked Apr 23 '12 03:04

victorwoo


People also ask

Can you nest Gitignore files?

You can nest gitignores in a project to have per directory ignore files. e.g. Now a/b/. gitignore can handle any ignoring in subdirectory b .

Where should my Gitignore file be?

gitignore is located in the root directory of your repo. / will ignore directories with the name.

Is it OK to have multiple Gitignore files?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Should Gitignore files be committed?

gitignore file must be edited and committed by hand when you have new files that you wish to ignore. . gitignore files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored.


1 Answers

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


like image 141
Michael Durrant Avatar answered Sep 27 '22 22:09

Michael Durrant