If you’re a macOS user working with Git, you’ve likely encountered .DS_Store
files. These hidden files, created by Finder to store folder-specific metadata, can sneak into your Git repository and clutter your commits. In this post, I’ll guide you through the steps to remove existing .DS_Store
files from your repository and prevent them from being tracked in the future.
.DS_Store
Files Are a Problem.DS_Store
files are automatically generated by macOS to store information like folder view settings. While harmless locally, they can cause issues in Git repositories:
Let’s dive into how to handle them effectively.
.DS_Store
Files from GitIf .DS_Store
files are already tracked in your repository, you’ll need to remove them from Git’s index without deleting them from your local filesystem (unless you want to).
Check for tracked .DS_Store
files: Run the following command in your repository’s root directory to see if any .DS_Store
files are tracked:
git ls-files | grep .DS_Store
Remove .DS_Store
files from the Git index: Use this command to untrack all .DS_Store
files while keeping them on your disk:
find . -name .DS_Store -exec git rm --cached {} \;
(Optional) Delete .DS_Store
files from the filesystem: If you want to remove .DS_Store
files entirely from your local repository, run:
find . -name .DS_Store -delete
.DS_Store
Files in GitTo prevent .DS_Store
files from being tracked in the future, add them to your .gitignore
file.
Add .DS_Store
to .gitignore
: Open or create a .gitignore
file in your repository’s root directory and add the following line:
.DS_Store
(Optional) Set up a global .gitignore
: If you work across multiple repositories, you can ignore .DS_Store
globally:
echo ".DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
Verify .gitignore
: Double-check that .DS_Store
is listed in your .gitignore
file to ensure Git ignores it moving forward.
.DS_Store
Files (Optional)You can configure macOS to stop creating .DS_Store
files in specific contexts, such as network or USB drives.
Disable .DS_Store
creation on network drives:
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
Disable .DS_Store
creation on USB drives:
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
Revert these settings (if needed): To re-enable .DS_Store
creation, use:
defaults delete com.apple.desktopservices DSDontWriteNetworkStores
defaults delete com.apple.desktopservices DSDontWriteUSBStores
After removing .DS_Store
files and updating .gitignore
, commit your changes:
git add .gitignore
git commit -m "Remove .DS_Store files and update .gitignore"
If you’re working with a remote repository, push the changes:
git push origin <branch-name>
By following these steps, you can keep your Git repository clean from .DS_Store
files and avoid future headaches. Regularly check your .gitignore
file to ensure it’s up to date, especially when collaborating with others on macOS.