If you use Git on macOS, you have probably seen .DS_Store files appear in your repositories. These files are created automatically by macOS Finder and are not related to your code. They often get accidentally committed and cause unnecessary noise in pull requests.
In this post, you will learn how to ignore .DS_Store files globally in Git so you never have to worry about them again.
What Is a .DS_Store File
.DS_Store stands for Desktop Services Store. macOS uses this file to save folder settings such as:
• Icon positions
• Folder view options
• Window sizes
These files are created automatically and appear in many directories. They should never be tracked in Git repositories.
Why Ignoring .DS_Store Is Important
Ignoring .DS_Store files helps you:
• Keep your repositories clean
• Avoid unnecessary commits
• Reduce noise in code reviews
• Prevent merge conflicts
Instead of adding .DS_Store to every project’s .gitignore, it is better to ignore it globally on your machine.
Step 1. Create a Global Git Ignore File
Git allows you to define a global ignore file that applies to all repositories on your system.
Open your terminal and run:
touch ~/.gitignore_global
This creates a file named .gitignore_global in your home directory.
Step 2. Add .DS_Store to the Global Ignore File
Now add .DS_Store to this file:
echo ".DS_Store" >> ~/.gitignore_global
To confirm it was added correctly, run:
cat ~/.gitignore_global
You should see:
.DS_Store
Step 3. Tell Git to Use the Global Ignore File
Next, configure Git to use this file for all repositories:
git config --global core.excludesfile ~/.gitignore_global
This command updates your global Git configuration.
Step 4. Verify the Configuration
To make sure Git is using the global ignore file, run:
git config --global --get core.excludesfile
The output should look like this:
/Users/your-username/.gitignore_global
If you see this path, the setup is complete.
What If .DS_Store Is Already Tracked
If a .DS_Store file was already committed in a repository, the global ignore will not remove it automatically.
To remove it from a specific repository:
git rm --cached .DS_Store
git commit -m "Remove .DS_Store"
After this, Git will stop tracking the file and will ignore it in the future.
Final Thoughts
Setting up a global Git ignore file is a small change that saves a lot of time and frustration. Once configured, you never need to think about .DS_Store files again.
This approach works for any other files you want to ignore globally, such as editor backups or system files.
Clean repositories lead to better collaboration and happier developers.
Happy Coding!
Top comments (0)