Removing .DS_Store files from Git

Published on
Removing .DS_Store files from Git

There is nothing more frustrating to a developer than looking at their brand new repository, pushed with a fresh new project. Then you see one. Then two…three…more!

Those pesky .DS_Store files start popping up everywhere, and you realize you forgot to add them to your .gitignore file. Let me show you a quick command you can run to clear them out.

Run this command to find all instances of .DS_Store files in your repository and in your git history.

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

The next thing you’ll want to do is add .DS_Store to your .gitignore file immediately. That will protect you from yourself and other developers who work on MacOS.

Never Have This Happen Again

Wouldn’t it be nice if you forgot to add .gitignore to your .gitignore file in the future, that you would still be protected? You can protected yourself against this by creating a global gitignore file.

Add a .gitignore_global file to your Home Directory by running this command.

touch ~/.gitignore_global

Now open that file in your favorite text editor and put in some common file patterns that you’ll most likely want to remain out of your Git history.

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
.Trashes
Thumbs.db

The last thing we need to do is tell Git about our global gitignore file. Do this by running this command.

git config --global core.excludesfile ~/.gitignore_global