How can I Remove .DS_Store files from a Git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To remove .DS_Store files from a Git repository, you need to do two things: stop tracking the ones already committed and tell Git to ignore future ones. Ignoring alone is not enough, because .gitignore does not automatically untrack files that are already in the index.
That is why the cleanup is a two-step process rather than a one-line magic fix.
Add .DS_Store to .gitignore
Start by making sure new .DS_Store files will not be added again.
Then stage the ignore file:
This prevents future accidental tracking, but it does not remove existing tracked .DS_Store files.
Remove Existing .DS_Store Files from Git Tracking
To untrack the files that are already in the repository, remove them from the Git index.
The important flag here is --cached. It tells Git to remove the files from version control without necessarily deleting them from your working tree first.
After that, commit the cleanup:
At this point, the repository stops tracking those files and future .DS_Store files should be ignored.
If You Want to Delete the Files Locally Too
Sometimes you want the files gone from the working tree as well, not just from Git tracking.
This is optional. The main Git cleanup step is untracking them from the index. Local deletion is just a hygiene step for your machine.
A Safer Way to Inspect Before Removing
If you want to see which .DS_Store files exist before changing anything, list them first.
That is a good idea in large repositories, especially if you want to sanity-check the scope before running a removal command.
Consider a Global Git Ignore Too
Because .DS_Store is a macOS-wide nuisance rather than a project-specific file, many developers also add it to a global Git ignore.
This helps across all repositories on your machine. It does not replace the repository-level cleanup if .DS_Store files are already committed, but it does reduce the chance of repeating the problem in new projects.
Why .gitignore Alone Does Not Fix It
A common source of confusion is editing .gitignore and then noticing that .DS_Store still shows up in Git status or history. That happens because Git only uses ignore rules for untracked files.
Once a file is already tracked, Git assumes you meant to track it until you explicitly remove it from the index.
Common Pitfalls
- Adding
.DS_Storeto.gitignoreand expecting already tracked files to disappear automatically. - Using
git rmwithout--cachedwhen you meant to stop tracking rather than delete from disk. - Forgetting to commit the cleanup after removing the files from the index.
- Cleaning the current repository but not adding a global ignore, so the same problem returns elsewhere.
- Running broad file-removal commands without first checking what they will affect.
Summary
- Add
.DS_Storeto.gitignoreso new files are ignored. - Use
git rm --cachedto stop tracking.DS_Storefiles that are already in the repository. - Commit the cleanup so the repository history reflects the removal.
- Delete the files locally too if you want a cleaner working tree.
- Consider a global Git ignore on macOS to keep
.DS_Storeout of future repositories.

