How to Remove .DS_Store files from a Git repository?

DS_Store files are automatically created by Mac OS X Finder in browsed directories. These files contain information about system configuration. If you upload them along with other files, the files can be misused to obtain information about your computer. For more information, see Apple security updates.

Sometimes it is annoying to see .DS_Store files added to your git repositories. Every time you do git status you may suddenly find new .DS_Store file in your git repository tree. So how to remove these git .DS_Store files and make your git repo not track them?

How to Remove .DS_Store files from a Git repository

How to remove DS_Store files from tracking

First of all, lets remove existing DS_Store files from the repository:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Next, add the following line to your .gitignore file, which can be found at the top level of your repository. (Or create the file if it isn’t there already):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.DS_Store
.DS_Store
.DS_Store

You can do this easily with this command in the top directory:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo .DS_Store >> .gitignore
echo .DS_Store >> .gitignore
echo .DS_Store >> .gitignore

Then commit the file to the repo:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add .gitignore
git commit -m '.DS_Store banished!'
git add .gitignore git commit -m '.DS_Store banished!'
git add .gitignore
git commit -m '.DS_Store banished!'

Leave a Reply