Joe Gilmore

1 min read

Stop .DS_Store appearing in Git changes

How to stop macOS .DS_Store files appearing in Git changes, even when they're already in your .gitignore.

Stop .DS_Store appearing in Git changes

Dear .DS_Store, Nobody Invited You to GitHub

How to stop macOS .DS_Store files appearing in Git changes, even when they're already in your .gitignore.

There are difficult problems in software development.

Memory leaks. Race conditions. Mysterious production crashes at 3 AM.

And then there's .DS_Store.

A tiny, completely unremarkable macOS file that somehow manages to be more persistent than a salesperson who's discovered you own a business.

You've added it to .gitignore. You've checked twice. You've even discarded its changes in VS Code.

And yet, there it is again.

Hello, you little bastard.

Why Won't This Thing Just Go Away?

Here's what's happening.

At some point, probably during that glorious first commit when you enthusiastically ran:

git add .

...you accidentally invited .DS_Store into your repository.

And Git, being the loyal little version-control system that it is, has remembered that invitation ever since.

The problem is that .gitignore only ignores files Git isn't already tracking.

So adding this:

.DS_Store

...does absolutely nothing for a .DS_Store file that's already committed.

Meanwhile, macOS Finder keeps updating these files whenever you browse folders.

Git sees the changes.

VS Code shows the changes.

You discard the changes.

Finder updates the file again.

Congratulations. You've invented the world's most boring infinite loop.

The Fix (Approximately 4 Seconds)

Open your terminal in the repository and run:

git rm --cached .DS_Store git commit -m "Stop tracking .DS_Store"

That's it.

The --cached flag tells Git to stop tracking the file without deleting it from your Mac.

Your .gitignore can finally do the job you hired it for.

Got .DS_Store Files Hiding in Subfolders?

Of course you have. Why would there only be one?

To remove all tracked .DS_Store files throughout your repository, run:

git ls-files -z | grep -zE '(^|/)\.DS_Store$' | xargs -0 -r git rm --cached --

This works on systems with GNU xargs, such as Linux.

On macOS, use this version instead:

git ls-files -z | grep -zE '(^|/)\.DS_Store$' | xargs -0 git rm --cached --

Then commit:

git commit -m "Evict all .DS_Store files"

Make sure your .gitignore contains:

.DS_Store

And just like that, your repository is free of Apple's little desktop confetti.

One Last Thing

If you're starting a new repository, add .DS_Store to your .gitignore before your first commit.

Or don't.

I'm not your dad.

But I'll probably see you back here in six months when that tiny grey file starts waving at you from the Source Control panel again.

Happy coding. 🍻