Aiki x Developer

git

Removing .DS_Store Files From Git Repository & Always Ignore .DS_Store Files

June 25th, 2020

git

Hiya!

I'm Arisa, a freelance Full-Stack Developer and living in Germany.

Sometimes, I accidentally pushed .DS_Store files in my Git repository although I know it can be prevented by creating .gitignore file 🤦‍♀️.

Yea... It happens when I'm in a rush 😇.

But solving the problems and make things as efficient as possible is the job for all developers.

Here's the way to delete all .DS_Store files in your Git repository and telling Git to ignore them from now on.

Removing all of your .DS_Store files in a Git repo

We should know what .DS_Store file is before taking an action.

It stands for Desktop Services Store.

And it holds meta information about our folder’s thumbnails, settings, etc.

This file is gonna be created always when we navigate files and folders from the Finder in Macs.

It means, this is a Mac users issue only.

If you wanna just remove one single .DS_Store, just hit this command.

$ git rm --cached .DS_Store

But if you find several .DS_Store files in your repository, hit this command instead.

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

Or this.

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

And push as usual to your Git repo.

Then Hooray🎉!

You succeed to wipe all .DS_Store off and your repo looks much clean!

Like KonMari method 🧙‍♀️.

But this just solves the problem existing .DS_Store files in Git repos.

Not solving the problem yet for the future.

I'm pretty sure I'll do this same stupid rushed push again by forgetting to add .gitignore.

Here's the solution for yourself in the future.

Telling Git to ignore .DS_Store always from now on

There's the way to tell your Git to ignore .DS_Store at any time from now on.

All you need to do is just create a file and write in there to ignore annoying .DS_Store at any time.

So, hit the command like this.

Make sure you run in a root directory.

$ echo .DS_Store >> ~/.gitignore_global

At this point, you created a global .gitignore file.

You've already wrote which file to ignore always.

All what is left is telling your Git to use this global .gitignore file always.

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

Done ✨

No more being frustrated by the .DS_Store files!

Summary

It's super easy to remove all .DS_Store files in your Git repos and telling your Git to ignore all .DS_Store files always.

Also, it keeps your repo cleaner.

Simple and clean is the best thing in programming.

Hope my article is healpful!

Ciao!

Created by Arisa Fukuzaki, © 2019