I was recently reviewing a number of projects on GitHub and I was amused by some details about people that can be inferred by the content of a project’s .gitignore file. I could identify the owner’s base OS and their preferred IDE – even some of the specific development tools or system plugins they have installed.

If I worked for a marketing company, this could be a source of data to be mined to sell expensive alternate software. After all, between your GitHub identity and the .gitignore content, targeted contact lists could be derived. I hope that no marketing executive reads this otherwise I apologize in advance!

Projects should have .gitignore rules committed as part of the project. This is of course functionally correct. However each individual developer might want to consider setting up their own system-wide ignore rules, so that project-specific files remain project-specific. You can then add rules to ignore the configuration files from your preferred IDE that no one else in the project uses.

There are many very valid reasons why you should do this:

  • The project leads don’t have to do this for each project or change it when you as a contributor move to a new IDE.
  • You can set-up your system to function they way you want without it impacting the project or others.
  • Some potentially unsafe* commands such as ‘git add -A‘ become safer as you will not commit sensitive or user specific files.
  • If you’re sending pull requests to Open Source projects, they will not be declined due files that should not be present.
  • Reduce the need to duplicate rules between many projects for non-project files.

Okay this sounds good, how do I do that?

Firstly let’s create a file on your system that will hold these system-wide ignore rules.

Then add the appropriate rules that you want to be ignored across all projects. Below is the content of my own .gitignore-global file

Into this file you should add matches for things like system cache files or system and IDE configuration files.

Once you have this file created you will need to edit your .gitconfig file to add this additional configuration file. This can be done by editing the file manually or by running the command below:

That’s it! You now have system-wide git ignore rules that will prevent you from needing to duplicate these rules into each project. This alone is a good enough reason to do this; the others are just a bonus.

* By unsafe I mean, if you don’t appreciate what the command or flag does. I often see newcomers to Git use this shortcut with unexpected results.