Git Remove .DS_Store

Β· 238 words Β· 2 minute read

Every developer using a Mac has found a .DS_Store file on their git. Nothing to be alarmed, this is a harmless file. However, it can be annoying when it shows up on every folder.

What is .DS_Store? πŸ”—

.DS_Store is a hidden metadata file for MacOS. The file contains information specific to the folder it’s in: folder color, icon size, window size, file order, etc. The file is created the first time any View Options is selected for a folder, but it’s hidden using the . prefix. However, it can be seen in the terminal using the ls -la command. And of course, it also shows up in Git repositories.

Remove .DS_Store from Git πŸ”—

Step 1: Checkout to a new branch πŸ”—

It is best to move to a new branch to be safe.

git checkout -b destroy-ds-store

Step 2: Find and remove .DS_Store from Git πŸ”—

The following terminal command finds all previously committed .DS_Store on the git repository, and removes them.

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

This can be test run using the --dry-run or -n flag after rm:

find . -name .DS_Store -print0 | xargs -0 git rm --dry-run --ignore-unmatch

Step 3: Add .DS_Store to .gitignore πŸ”—

Add **/.DS_Store to the .gitignore file.

echo "**/.DS_Store" >> .gitignore

The ** part tells Git that .DS_Store files should be ignored in every folder of your project.

Step 4: Commit changes πŸ”—

git commit -am "DS_Store destroyed"