tags:

views:

4013

answers:

6

How can I remove those annoying Mac OS X .DS_Store files from a Git repository?

A: 

This will work:

find . -name *.DS_Store -type f -exec git-rm {} \;
John Topley
The asterisk should not be in there.
Aristotle Pagaltzis
+5  A: 

delete them using git-rm, and then add .DS_Store to .gitignore to stop them getting added again. You can also use blueharvest to stop them getting created all together

Nathan Reed
BlueHarvest: http://www.zeroonetwenty.com/blueharvest/
Pat Notz
+17  A: 

Remove existing files from the repository:

find . -name .DS_Store -print0 | xargs -0 git-rm

Add the line

.DS_Store

to the file .gitignore, which can be found at the top level of your repository (or created if it isn't there already). Then

git add .gitignore
git commit -m ".DS_Store banished!"
benzado
You could also use the exec option to findfind . -name .DS_Store -exec git-rm {} \;It would be one less command.
Milhous
It's really trivial, but using `-exec` will launch `git-rm` once for every .DS_Store file, while `xargs` will put all the paths on one command line. Mostly I prefer `xargs` because I don't have to worry about escaping a lot of special characters.
benzado
This should use .git/info/exclude, not .gitignore. See http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally/1753078#1753078 for an explanation of the difference between the two.
Andrew Grimm
@Andrew: I don't agree. It would never be useful for a .DS_Store file to be checked in, so it makes more sense as a repository-wide, all-users setting, rather than something each Mac user needs to remember to set on her own machine.
benzado
+9  A: 

In some situations you may also want to ignore some files globally. For me, .DS_Store is one of them. Here's how:

git config --global core.excludesfile = /Users/mat/.gitignore

(Or any file of your choice)

Then edit the file just like a repo's .gitignore. Note that I think you have to use an absolute path.

webmat
A: 

I found that the following line from snipplr does best on wiping all .DS_Store, including one that has local modifications.

find . -depth -name '.DS_Store' -exec git-rm --cached '{}' \; -print

--cached option, keeps your local .DS_Store since it gonna be reproduced anyway.

And just like mentioned all above, add .DS_Store to .gitignore file on the root of your project. Then it will be no longer in your sight (of repos).

manat
A: 

I had to change git-rm to git rm in the above to get it to work:

find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print

David Kahn