Programblings

Rambling about programming and life as a programmer - by Mathieu Martin

Git global ignores

22nd October 2008

I don’t know about you, but for me, using git is so low-friction that I use it basically for everything where I may need a powerful undo button. In other words, I don’t use it only for team software development projects.

For example, I’ve frequently used it in the past to keep track of the modifications I make to an article I work on for few days. God knows I write a lot of these. There’s a reason I called this blog Programblings ;-)

To be honest, I’m using git as I write even this short article.

I also use it for trivial one evening coding projects. As soon as I spend more than an hour on code, whatever it is, I’ll usually create a local git repo for it.

One of the annoying things I realized when creating repositories more and more often, is that I always ended up ignoring the same files. Over and over again. Boring.

Fortunately for me, git can be configured to take into consideration a global ignore file. Heck, I can even create a system-wide ignore file if I want (check out git config’s doc for more info).

Configure your personal ignore file

I like to stick to conventions so I call my file .gitignore, and I put it in my home directory. But that’s up to you, really.

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

Note that there’s one little gotcha to be aware of. If you prefer to edit the .gitconfig file directly (or if you use a weird shell), git expects an absolute path. In the example above, bash converted the ~ shorthand to my home directory.

Now I just add ignore globs to it like any other project level (directory level, really) git ignore file.

echo .DS_Store >> ~/.gitignore

Once I’ve ignored all my favorite useless files, I can get cracking and never worry about them again.

I still have to ignore files

When I create a new repository on which people may actually contribute, I’ll still create a proper ignore file, however. Otherwise I’d convey the message that I consider contributors as slaves who only deserve the boring work of creating ignore files. Since I’m pure of heart, that’s not how I roll.

5 Responses to “Git global ignores”

  1. Pierre Olivier Martel Says:

    Very useful tip! When you say you also use git for your personal documents like blog drafts, how do you setup git? Do you have one git repository at the root of your Documents folder to just keep track of everything?

  2. Brian Ericson Says:

    Neat! (I should really just read “git config –help” top-to-bottom.)

    If you want to add something to your local repo’s .gitignore, you can create an alias which will make it easier:

    git config −−global alias.ignore ‘!f () { local ignore_file=$( git rev-parse −−show-cdup ).gitignore arg; for arg in $*; do echo $arg >> $ignore_file; done; sort -u -o $ignore_file $ignore_file; }; f’

    Git will do tab-completion on the alias (provided you use git’s bash-completion, “git ig” will complete to “git ignore” for you), each argument will be appended to the .gitignore file (without you having to figure out the correct parent path), and the file will then be sorted and duplicates tossed.

  3. Mathieu Martin Says:

    @Pierre Olivier

    I have the habit of working in a directory per project (be it an article or a mini code project). So I create one repo per project.

    Note that I don’t necessarily push them to a remote server. I’m only talking about using git as a powerful local undo button :-)

    @Brian

    Wow, cool trick :-) My bash skills are not high enough for me to pretend I understand all of that little script. But I’ll definitely give it a try. Thanks!

  4. Mathieu Martin Says:

    @Brian

    I edited your comment to properly show the double dashes.

    I hate Wordpress for that ;-)

  5. Alok Says:

    Good tip.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>