Globally ignore .DS_Store files in Git

If you’re a macOS developer working with Git, you’ve probably encountered .DS_Store files appearing in your commit history. These system files are created by macOS to store folder-specific metadata, usually related to images, and they have no place in your version control.
While you could add .DS_Store to each project’s .gitignore file, there’s a better solution: configure a global gitignore file that applies to all your Git repositories.
Setting up your global gitignore
Here’s how to configure Git to always ignore .DS_Store files:
Step 1: Create a global gitignore file
First, create a file in your home directory (if you don’t have one already) to store your global ignore patterns:
touch ~/.gitignore_global
Step 2: Add .DS_Store to the file
Open the file in your preferred text editor and add the pattern .DS_Store. Alternatively, use this one line terminal command to do so:
echo ".DS_Store" >> ~/.gitignore_global
Step 3: Configure Git to use the global gitignore file
Tell Git to use this file as your global excludes file:
git config --global core.excludesfile ~/.gitignore_global
That’s it! From now on, Git will automatically ignore .DS_Store files in all your repositories.
Leave a Reply