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"