Stop letting .DS_Store slow you down

I have over 100 git repositories on my Mac, and for almost every one, I sometimes browse the directory structure in the Finder. Once I do that, I inevitably end up with a few pesky .DS_Store files that want to be added to my repo:

Pesky .DS_Store Files in Terminal during Git Status

.DS_Store files don't add anything of value to my code (they just tell Mac OS X about folder display and icons), so I always end up adding them to my own projects' .gitignore files. But when I'm working on other repositories (like Drupal, or a fork from GitHub) I don't want to add a .gitignore if none exists, or mess with the project's existing .gitignore. So what's a coder to do?

There are a couple good solutions:

  1. Set git's core.excludesfile setting to a file of your choosing, with *.DS_Store inside:
    $ git config --global core.excludesfile ~/.gitignore
    $ echo *.DS_Store >> ~/.gitignore

    (Adapted from this SO answer.)
  2. You can avoid adding a .gitignore file on a per-repo basis by adding *.DS_Store to one repository's .git/info/exclude file (the .git folder is an invisible folder in the root of your git repo).

There! No more .DS_Store killing your commit momentum!

[Update: @applemachome told me about a for-pay app, BlueHarvest, that should help with this problem in a more general fashion (it can keep pesky .DS_Store files off of external and shared network volumes, too!). But it's probably overkill for most people.]

Comments

If you're using ZSH for terminal, using the following command does not work: echo *.DS_Store >> ~/.gitignore
Which outputs: zsh: no matches found: *.DS_Store

Instead use the following: echo '*.DS_Store' > ~/.gitignore

Its probably best to also mention the following. .DS_Store files can be found in many folders and just because you have added particular file or directory to a .gitignore or other exclusionary file, does not mean that other files named .DS_Store in other directories will be excluded from git tracking.

The way around this is to do the following

**/*.DS_Store

What that does is starts at the root of the directory and then looks through all subdirectories for any file whose name contains .DS_store. This could be

.DS_Store
/dir/.DS_Store
/dir/dir/.DS_Store
/dir/dir/dir/test/.DS_Store

It could also beL

abc.DS_Store
/dir/abc1.DS_Store
/dir/dir/test/cat.DS_Store

If you wanted to you add this to your .gitignore

**/*.DS_Store*

That would take care of all instances above and also would take care of instances like

.DS_Store.5
dir/cat.DS_Store.5
dir/dir/dir/test/.hello.cat.DS_Store.5

So you no longer have to add multiple lines. A single line will do the job