How to get rid of .DS_Store and node_modules in git repositories
How to get rid of .DS_Store files in git repositories
So you’ve built the project of your dreams. Your front-end is crisp. Your backend is airtight.
You key in git status to stage your masterpiece for commit, when you see this:
What in code hell is this file? You don’t remember making this file and certainly don’t want to push it to your otherwise pristine project.
First off — what is this .DS_Store file?
It stands for Desktop Services Store and it holds meta information about your folder’s thumbnails, settings, etc.
.DS_Store files are created any time you navigate to a file or folder from the Finder on a Mac.
How to get rid of them
If you have already accidentally committed it to your repository, use:
git rm –cached .DS_Store
This will get rid of it from the current directory and then you just need to add .DS_Store to your root .gitignore file. Anything in your .gitignore file will be, as the name suggests, ignored by git. You generally want git to ignore anything that is generated for you (node_modules) or is secret (.env files).
If you have .DS_Store files littered all over your project, copy and paste this to find and remove all of the files from the repository at once:
find . -name .DS_Store -print0 | xargs -0 git gm –ignore-unmatch
How to always ignore them
Chances are you likely never want to include these files in this project or any other one moving forward.
Let’s make a global .gitignore file:
echo .DS_Store >> ~/.gitignore_global
And let git know that you want to use this file for all of your repositories:
git config –global core.excludesfile ~/.gitignore_global
And that’s it! .DS_Store is out of your programming life. Deuces!