Create a global .gitignore to remove .DS_Store from your life forever

I use git a lot. In fact, I use it for almost individual software project that I start, no matter whether it’s a single script or thousands of assets. Not only do I get revision control, but also I can just git add remote to push the repository to a remote server for peace of mind and ease of replication to other machines (of course, this is no substitute for a proper backup, but it’s extremely useful nonetheless).

Per-project .gitignore

When I set up a new git repository, I’ll typically copy a previous .gitignore from a previous and strip the stuff from it that was specific to the previous project.

By adding a .gitignore file to your git repository, you’re telling git to ignore, on a line-by-line basis, anything matching those patterns. I typically define something like:

These stop Python’s bytecode files, the dreaded OSX .DS_Store files (note: no wildcard needed since that’s what they are always called), and VIM’s swap files that it saves incase recovery is required.

Global (per-user) .gitignore

Of course, I’d much rather not have to re-define these rules for every new repository, since I can’t ever imagine that I’d ever want files matching those specifications to be version-managed in any way. Thankfully, git allows for global .gitignore files that are applied to all repositories.

Create the global .gitignore somewhere on your filesystem. Git doesn’t quite care where. I personally recommend to put it in your home directory:

Then, instruct git to actually use it:

And that’s it. From now all, you’ll never have to remove .DS_Store from your repositories ever again. Until Apple decide, heaven forbid, to rename it one day.

Leave a Reply