victorwoo victorwoo - 1 year ago 71
Git Question

Best practice for using multiple .gitignore files

There exists a collection of useful

files at Every ignores file there has an extension
, 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
and merge the constituent files into it?

Accordingly, when using a
file, it seems the config points to a single file. If for instance I want to apply all
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
files in different directories, but it is not what I want. I want to apply multiple
files in a single directory.

Answer Source

To clarify: a projects gitignore file is called .gitignore


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, 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 and on GitHub at