Git
.gitignore
DS_Store files
Programming
File Management

.gitignore all the .DS_Store files in every folder and subfolder

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Add a single line to your .gitignore file at the repository root and Git will ignore every .DS_Store file in every directory, no matter how deeply nested:

 
**/.DS_Store

If .DS_Store files were already tracked before you added this rule, you also need to remove them from the index. The .gitignore file only prevents untracked files from being added; it does not retroactively untrack files that Git already knows about.

What Are .DS_Store Files?

.DS_Store (Desktop Services Store) is a hidden file macOS Finder creates automatically in every directory you open. It stores folder-specific view preferences: icon positions, background color, column widths, sort order, and similar metadata. The file is binary and is regenerated by Finder whenever you change how a folder is displayed.

These files serve no purpose outside macOS. They clutter diffs, create unnecessary merge conflicts, and expose minor details about your local directory layout to anyone who clones the repository.

Adding .DS_Store to .gitignore

Step 1: Edit .gitignore

Open or create the .gitignore file in the root of your repository and add the pattern:

bash
echo "**/.DS_Store" >> .gitignore

The **/ prefix is a globstar pattern that matches any number of directory levels. Without it, .DS_Store would only be ignored at the repository root level.

Step 2: Remove Already-Tracked .DS_Store Files

If any .DS_Store files were committed before the ignore rule existed, remove them from the index without deleting them from disk:

bash
1# Remove all tracked .DS_Store files from the index
2find . -name ".DS_Store" -exec git rm --cached {} +
3
4# Commit the removal
5git commit -m "Remove tracked .DS_Store files"

The --cached flag tells git rm to remove the file from the staging area only. The actual file stays on your filesystem so Finder continues to work normally.

Step 3: Verify

Confirm that Git now ignores the files:

bash
1# This should show nothing related to .DS_Store
2git status
3
4# Double-check that the pattern is active
5git check-ignore -v .DS_Store

Using a Global .gitignore Instead

Rather than adding .DS_Store to every project, you can configure a global ignore file that applies to all repositories on your machine:

bash
1# Create or append to a global gitignore
2echo "**/.DS_Store" >> ~/.gitignore_global
3
4# Tell Git to use it
5git config --global core.excludesfile ~/.gitignore_global

This is the preferred approach for OS-specific artifacts. The project .gitignore should contain project-specific patterns (build output, dependency directories, environment files), while the global gitignore handles files that are artifacts of your personal development environment.

What Belongs in Each File

FilePurposeExample Patterns
.gitignore (project)Project-specific build artifactsnode_modules/, dist/, .env
~/.gitignore_global (user)OS and editor artifacts.DS_Store, Thumbs.db, *.swp
.git/info/exclude (local)Repo-specific personal ignoresscratch.txt, local-notes/

Common Patterns to Include Alongside .DS_Store

If you are cleaning up OS artifacts, consider adding these related patterns to your global gitignore:

bash
1# macOS
2**/.DS_Store
3**/.AppleDouble
4**/.LSOverride
5Icon?
6
7# Windows
8Thumbs.db
9ehthumbs.db
10Desktop.ini
11
12# Linux
13*~
14.directory
15
16# Editors
17*.swp
18*.swo
19.idea/
20.vscode/settings.json

Removing .DS_Store Files From the Entire Git History

If .DS_Store files were committed many times and you want to remove them from the entire history (not just the current tree), use git filter-repo:

bash
1# Install git-filter-repo if not already present
2pip install git-filter-repo
3
4# Remove .DS_Store from all commits
5git filter-repo --path .DS_Store --invert-paths

This rewrites history, so coordinate with your team before running it on a shared repository. Force-pushing rewritten history breaks clones that others have already pulled.

For less drastic cleanup, the BFG Repo-Cleaner is another option:

bash
java -jar bfg.jar --delete-files .DS_Store
git reflog expire --expire=now --all && git gc --prune=now --aggressive

Verifying Your .gitignore Rules

After setting up ignore patterns, you can test whether Git recognizes them correctly using git check-ignore:

bash
1# Check if a specific file is ignored and by which rule
2git check-ignore -v path/to/some/.DS_Store
3
4# Test multiple paths at once
5find . -name ".DS_Store" -exec git check-ignore -v {} +

The -v flag shows which .gitignore file and which line matched, which is invaluable when debugging why a file is or is not being ignored. If the output is empty, the file is not covered by any ignore rule.

Preventing Finder From Creating .DS_Store on Network Volumes

macOS can be told to stop creating .DS_Store files on network-mounted volumes:

bash
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE

This does not affect local drives, so .DS_Store files will still appear in local directories. There is no supported way to prevent Finder from creating them locally without disabling Finder itself.

Common Pitfalls

Adding .DS_Store to .gitignore but forgetting to remove already-tracked files. The ignore rule only applies to untracked files. If .DS_Store was committed before the rule existed, Git continues tracking it. You must run git rm --cached to untrack it.

Using .DS_Store without the **/ prefix. A bare .DS_Store in .gitignore only matches at the directory level where .gitignore lives. Subdirectories are not covered. Always use **/.DS_Store for recursive matching.

Putting OS-specific patterns in the project .gitignore. Team members on Linux and Windows do not generate .DS_Store files. Keeping OS artifacts in a global gitignore avoids cluttering the project configuration with patterns that only apply to your operating system.

Running git filter-repo on a shared branch without coordination. History rewriting changes every commit hash. Anyone who cloned the repository before the rewrite will have diverging histories. Only rewrite history when the team agrees, and never on shared branches without notice.

Forgetting that .gitignore is itself a tracked file. Changes to .gitignore must be committed and pushed for the team to benefit from the new patterns. A local-only .gitignore modification does not propagate.

Summary

  • Add **/.DS_Store to your .gitignore to ignore these files in every directory recursively.
  • Run git rm --cached to untrack any .DS_Store files that were committed before the ignore rule.
  • Prefer a global ~/.gitignore_global for OS and editor artifacts so every repository on your machine is covered automatically.
  • Use git filter-repo or BFG to remove .DS_Store from the entire commit history when a clean history matters.
  • The project .gitignore should focus on project-specific artifacts like build output, dependencies, and environment files.

Course illustration
Course illustration

All Rights Reserved.