.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:
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:
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:
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:
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:
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
| File | Purpose | Example Patterns |
.gitignore (project) | Project-specific build artifacts | node_modules/, dist/, .env |
~/.gitignore_global (user) | OS and editor artifacts | .DS_Store, Thumbs.db, *.swp |
.git/info/exclude (local) | Repo-specific personal ignores | scratch.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:
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:
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:
Verifying Your .gitignore Rules
After setting up ignore patterns, you can test whether Git recognizes them correctly using git check-ignore:
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:
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_Storeto your.gitignoreto ignore these files in every directory recursively. - Run
git rm --cachedto untrack any.DS_Storefiles that were committed before the ignore rule. - Prefer a global
~/.gitignore_globalfor OS and editor artifacts so every repository on your machine is covered automatically. - Use
git filter-repoor BFG to remove.DS_Storefrom the entire commit history when a clean history matters. - The project
.gitignoreshould focus on project-specific artifacts like build output, dependencies, and environment files.

