Stop Committing .DS_Store Files: How to Globally Ignore Them on Mac
If you’ve ever opened a pull request and spotted a sneaky .DS_Store file in the diff, you’re not alone. It’s one of those small, embarrassing things that happen to every Mac developer at least once. The good news? You can fix it globally — once — and never think about it again.
What Is .DS_Store Anyway?
.DS_Store stands for Desktop Services Store. macOS creates these hidden files automatically in every folder you open with Finder. They store metadata like icon positions, folder view preferences, and window sizes — stuff that’s useful to your OS, but completely meaningless to your teammates (especially those on Linux or Windows).
These files have no place in your Git repositories, Docker contexts, or deployment pipelines.
The Wrong Way: Per-Project .gitignore
You might already be adding .DS_Store to individual project .gitignore files. That works, but it has a few problems:
- You have to remember to do it for every new project
- You’re cluttering your project’s
.gitignorewith a Mac-specific concern - Collaborators on other OSes wonder why it’s there
- You’ll still forget on that 2am side-project sprint
There’s a better way.
The Right Way: A Global Git Ignore File
Git supports a global gitignore file — a single file on your machine that applies ignore rules to every repository, regardless of whether it has a .gitignore or not.
Step 1 — Create the Global Gitignore File
touch ~/.gitignore_global
Step 2 — Add .DS_Store (and Friends) to It
cat >> ~/.gitignore_global << 'EOF'
# macOS
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# macOS directory metadata
__MACOSX/
EOF
Here’s what each entry covers:
| Pattern | What it ignores |
|---|---|
.DS_Store | Finder folder metadata |
.DS_Store? | Variants with a trailing character |
._* | Resource fork files created by macOS |
.Spotlight-V100 | Spotlight search index |
.Trashes | Trash folder metadata on external drives |
ehthumbs.db | Windows thumbnail cache (bonus) |
Thumbs.db | Windows thumbnail cache (bonus) |
__MACOSX/ | Folder created when unzipping on macOS |
Step 3 — Register the File with Git
git config --global core.excludesfile ~/.gitignore_global
That’s it. Git will now respect this file across every repo on your machine.
Verify It’s Set
git config --global core.excludesfile
# Output: /Users/yourname/.gitignore_global
Already Committed .DS_Store? Clean It Up
If .DS_Store files are already tracked in an existing repo, simply adding them to .gitignore won’t remove them — Git is already watching them. You need to untrack them first.
Remove from the Entire Repo
# Remove all .DS_Store files from Git tracking (keeps the local file)
find . -name .DS_Store -print0 | xargs -0 git rm --cached --force 2>/dev/null
# Commit the cleanup
git commit -m "chore: remove .DS_Store files from tracking"
One-liner Alternative
git rm -r --cached **/.DS_Store && git commit -m "chore: remove .DS_Store"
Note:
--cachedmeans the file is removed from Git’s index only — it stays on your local filesystem. Without--cached, Git would delete the file entirely.
Bonus: Prevent Finder from Creating Them on Network Shares
If you work with network drives, external SSDs, or shared volumes, Finder will happily scatter .DS_Store files across them too. You can disable this behaviour:
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE
To re-enable it later:
defaults delete com.apple.desktopservices DSDontWriteNetworkStores
This only applies to network volumes, not local drives. macOS will still create
.DS_Storelocally — but your global gitignore has that covered.
Quick Reference
# One-time setup (run this once on any new Mac)
touch ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_global
echo "._*" >> ~/.gitignore_global
echo ".Spotlight-V100" >> ~/.gitignore_global
echo ".Trashes" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
# Clean up an existing repo
find . -name .DS_Store -print0 | xargs -0 git rm --cached --force 2>/dev/null
git commit -m "chore: remove .DS_Store files"
Wrapping Up
Setting up a global gitignore takes about 60 seconds and saves you from a surprisingly common source of noise in your commits and PRs. It’s one of those small developer hygiene habits that pays off every single day.
If you’re setting up a new Mac or onboarding a new developer, make this part of your standard machine setup — right alongside installing Homebrew and configuring your SSH keys.
Found this useful? Check out more tips on noukeosombath.com.