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:
.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:
- Set git's core.excludesfile setting to a file of your choosing, with
$ git config --global core.excludesfile ~/.gitignore<br />
$ echo *.DS_Store >> ~/.gitignore<br />
(Adapted from this SO answer.)
- You can avoid adding a .gitignore file on a per-repo basis by adding
*.DS_Storeto one repository's
.git/info/excludefile (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.]
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
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
It could also beL
If you wanted to you add this to your .gitignore
That would take care of all instances above and also would take care of instances like
So you no longer have to add multiple lines. A single line will do the job