Remove .DS_Store files from a Git repository

Untrack Finder metadata with a Git pathspec, add a shared ignore rule, and preserve existing global ignore settings.

Best practices
Illustration of Git branches and version history

.DS_Store files store Finder view metadata and usually do not belong in a source repository. Remove tracked copies from the index, then add an ignore rule so routine staging does not include them again.

Find and untrack them

Run from the repository root:

git ls-files -- ':(glob)**/.DS_Store'
git rm --cached --ignore-unmatch -- ':(glob)**/.DS_Store'
printf '%s\n' '.DS_Store' >> .gitignore
git add .gitignore
git diff --cached
git commit -m "Stop tracking Finder metadata"

The quoted Git pathspec matches .DS_Store at the root and in nested directories without shell expansion. It operates on tracked paths, including a tracked file already missing from disk. --cached keeps any existing working-tree copies. Check the ignore file before appending a duplicate rule.

This avoids a filesystem-wide find that can descend into unrelated nested repositories or Git metadata. Publish the commit through the normal branch workflow when ready.

Add a personal ignore rule

If you want the rule across repositories, inspect your existing global ignore configuration first:

git config --global --get core.excludesFile

Add .DS_Store to that file if one is already configured. Otherwise you can choose a file such as:

printf '%s\n' '.DS_Store' >> "$HOME/.gitignore_global"
git config --global core.excludesFile "$HOME/.gitignore_global"

Git also supports a default ignore file under the XDG configuration directory. Setting a different core.excludesFile can replace that source of rules, so preserve any existing personal patterns.

A global rule helps with ordinary staging, but it does not remove tracked files, prevent an explicit git add -f, or override every higher-priority repository rule.

FAQ

Why exclude Finder metadata?

Its machine-specific folder presentation settings create changes that usually have no value to other collaborators. Finder may recreate the files, so deleting them once without an ignore rule does not solve the problem.

Are there similar files on other systems?

Windows may create Thumbs.db or desktop.ini; some Linux desktop tools use .directory. Ignore them only when they are unwanted environment metadata for your project, rather than assuming every similarly named file is disposable.

Should I rewrite history to remove old copies?

Usually a current-version cleanup is enough. If a repository-wide cleanup already requires rewriting history, a tool such as git-filter-repo can remove exact root and nested .DS_Store paths. Review its path patterns and coordinate the rewrite; a broad *.DS_Store match can also select unintended suffix names. Consider sensitive information separately rather than assuming all metadata is harmless.

See git-rm, gitignore, and Git pathspecs.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting