Create global .gitignore for user settings


ignored

When it comes to ignoring files in a git repository I do something I think many others have done.  I’ve added user level settings files to my .gitignore because I don’t want them included in my git repository.  You know, the files created by an IDE, operating system, or other applications.  Such as ‘.project’ created by Zend Studio or ‘.idea’ created by PHPStorm, and there are many others.

While it may be acceptable to add them to the .gitignore file in a private repo, should they be ignored in a publicly shared repository?  To answer this let me explain that I believe in “freedom”, and think everyone is entitled to do what they want, as long as it doesn’t hinder someone else’s freedom.  This is important when it comes to code.  Therefore, these files should NOT be ignored in the .gitignore of a public repo because someone creating a clone of the repo may desire to do something different.  So marking these files to be ignored, or not, should totally be a personal decision made by the user.

However, there is a way to have our cake, and eat it too.  We can inform our local instance of git to have a system global .gitignore file.  Therefore while the individual repository has a clean .gitignore file, with only references specific to the project, we can still have our user level ignores in place.  Here is how to do it.

From command line issue the following command: (command is non-Windows because of the file location, I’m not sure what it would be on a Windows machine, so perhaps someone can comment with a Windows friendly command)

$ git config --global core.excludesfile ~/.gitignore_global

This command creates a global setting in our systems git configuration informing git of an excludes file containing additional files to ignore globally.  The file name will be called ‘.gitignore_global’ and will be located in the users home directory.

Adding this –global config setting to git does not create the file for us, so we will still need to create this file in the location we specified. (The home directory in this case.)  Here is what mine looks like:

.idea
.buildpath
.project
.settings
.vagrant
.DS_Store
nbproject
.thumbs

Meanwhile my project level .gitignore might look like:

/vendor
local.config.php

For more on this topic, and perhaps a better explanation, please see https://help.github.com/articles/ignoring-files.


2 responses to “Create global .gitignore for user settings”

  1. Actually, that command works fine on Windows as well. I just tested it, this is what it creates in my .gitconfig file in my user directory:

    [core]
    excludesfile = ~/.gitignore_global

    I confirmed that git does read this new file and tested that git is ignoring patterns listed there.

  2. It’s not a problem to inogre all files without an extension with .gitignore you just have to go about it in reverse. Here’s a .gitignore that I verified does it:# Ignore all files*# Do not inogre files with extensions!*.*# Do not inogre directories!*/If there’s something in addition to that your script is doing, you can avoid the extra commit by adding your script to .git/hooks/pre-commit. Make sure to set permissions to 755. It’ll even cancel your commit if something goes wrong if you set the correct exit code.