Set .gitignore Globally, Ignore Files like a Bad Ass

Posted Jun 26, 2014
Tagged

Git is an amazing version control program, no matter what Linus says about it. When using version control you want to ignore files that don't need to be tracked. That's where the .gitignore file comes in handy. In this inaugural "Just the Tip", we'll show you how to set .gitignore globally.

When starting a development project one of the first things you do is set up git and start tracking your files and adding the annoying ass files to your .gitignore. Mac loving yuppie? Add the obligatory .DS_Store. Bad ass vim user? Then the usual .swp & .swo or, bonus tip for vim users, *.sw? knocks them all out. SASS aficionado? Then add *.sass-cache for sure.

Here’s a gist from Github with a bunch of suggestions for .gitgnore files.

But rather than add the ignore rules on every project, which is annoying, and if you’re on a team forces everyone to adopt them, you can set your .gitignore globally by running the following commands and adding a .gitignore file to the appropriate directory:

git config --global core.excludesfile ~/.gitignore

git config --global core.excludesfile "%USERPROFILE%\.gitignore"

After running the command in the terminal you add a .gitignore file to your home directory if it’s not there already. If you want to reinforce the fact that it’s global you can name it ~/.gitignore_global (you’ll have to tweak the config command to that name).

To find out what your current .gitignore file is set to, run this command:

git config --get core.excludesfile

Keep in mind that if you already commited files you wanted to ignore you’ll have to remove them manually with something like:

git rm --cached .DS_Store