Using M4 to Compose Your .gitignore File

Chris Wanstrath has a cool project to collect folks’ .gitignore files.

Chris breaks these down by platform and language/environment, which is great. What’s less great is you can’t pass multiple files to core.excludesfile, so you can’t easily compose them.

The ideal situation would to extend git to support multiple ignore files, but that’s a lot of pain (mostly because git isn’t on github itself).

So I wrote .gitignore.m4:

dnl cd && m4 -I ~/code/ext/gitignore ~/.gitignore.m4 > ~/.gitignore

.metadata
bin
dist
tmp

include(`Global/OSX.gitignore')

include(`C++.gitignore')

include(`Objective-C.gitignore')
build
*~.nib

include(`Autotools.gitignore')

This is my first time using M4 in anger, and apparently that dn1 is a way to make a single-line comment. I know, weird.

Anyway, that comment contains the invocation you need to run to (re)generate your composited .gitignore:

cd && m4 -I ~/code/ext/gitignore ~/.gitignore.m4 > ~/.gitignore

Here’s the breakdown:

  • First we switch to the users home directory, where .gitignores usually live.

  • That -I ~/code/ext/gitignore tells M4 to add ~/code/ext/gitignore to its file search path. Change this part to where-ever you git-clone’d Chris’s gitignore repo on your local machine.

  • The rest just reads the template file and overwrites its output to ~/.gitignore.

git m4 github gitignore Jul 23 2011