How to Remove DS_Store Files from Git Repository
If you use git on the Mac platform, you likely have come across DS_Store files in a git repository. These can be annoying and clutter a git repo, and you may even run into unnecessary conflicts due to .DS_Store files being maintained in git repos, where you may see error messages like “The files .DS_Store and .DS_Store had a tree conflict”. Because .DS_Store files are not needed by the vast majority of git repositories and generally can just cause issues, it’s reasonable to want to remove .DS_Store files from git repositories.
First, if you’re wondering what a DS_Store file is in the first place, it’s basically a file that contains folder-specific metadata on a Mac. Thus, a .DS_Store file includes everything from metadata about view type, file/folder size, thumbnail information, folder layout, and any Finder level customizations to a particular folder on the Mac. Since .DS_Store files are prefixed with a . in the filename, they are invisible to the Finder (effectively making them hidden files), but will show in the command line, and with command line tools or repository managers like git that will default to include every single file, including hidden files, in a directory.
While .DS_Store files are obviously useful for the Finder, they are not useful for having in a git repository. Let’s review how you can remove all DS_Store files from a git repo, and how to prevent then from being stored in future git repositories as well.
How to Remove Existing .DS_Store Files from Git Repository
- Open the Terminal, and navigate to the git repo directory you wish to clear of .DS_Store files
- Enter the following command string:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
That will find .DS_Store files and remove them.
If you wish to go even further, you can actually remove all DS_Store files from a Mac using another variation of the find command, but be aware by doing so you will lose any and all Finder window and folder customizations, thus it’s not particularly recommended to go that route.
How to Ignore .DS_Store Files in git
Let’s now go a step further, and prevent git from maintaining and committing DS_Store files in the first place, and instead ignore them completely. This is done by modifying (or creating) the .gitignore file in the active repo directory:
echo .DS_Store >> .gitignore
Now you’ll want to add the .gitignore file to your repository, and commit the change.
git add .gitignore
git commit -m 'Ignoring .DS_Store Files'
–
For what it’s worth, it’s not just git that can run into issues with .DS_Store file creation or clutter, or that may benefit from ignoring them, you can also exclude DS_Store files from zip archives created on a Mac, which can help to reduce clutter (and prevent machine specific metadata) from being kept in a zip file. And you may find that .DS_Store files are obnoxious with network environments, but a defaults command can prevent DS_Store file creation for network volumes if needed.