10 Apr-2016

Remove .DS_Store files from git

This tutorial will show you how to remove those annoying Mac OSX .DS_Store files from a Git repository.

.DS_Store is a file that stores custom attributes of its containing folder, such as the position of icons or the choice of a background image. The name is an abbreviation of Desktop Services Store, reflecting its purpose.

The file .DS_Store is created in any directory (folder) accessed by the Finder application, even on remote file systems mounted from servers that share files, for example via Server Message Block (SMB) protocol or the Apple Filing Protocol (AFP).

The problem with .DS_Store files is that they impose additional burden on revision control process: They are frequently changed and therefore appear in commits, unless specifically excluded.

In this tutorial, I will show you how to remove the .DS_Store files from any existing repository and make them banish.

First: Untrack all .DS_Store files

You will have to find all the .DS_Store files in your repository and untrack them. To do so, issue the following commands:

cd /path/to/root/of/repository
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Second, edit .gitignore

You will have to configure your git repository to ignore any changes in any .DS_Store file and not track them, or include them in future commits.

To do so, you will have to add .DS_Store to the .gitignore file in the root of your repository. Issue the following command:

echo '.DS_Store' >> .gitignore

Commit and push your changes

Now, you can commit and push the changes to your repository. Issue the followng commands:

git add .gitignore
git commit -m 'Annoying .DS_Store banished!'
git push

Happy coding ;-)