Stop committing .DS_Store, editor folders and other personal clutter in every repository by setting up one global ignore file with core.excludesFile.
The short answer
#Create one ignore file for your machine and point Git at it with core.excludesFile. Every repository on your computer then ignores those patterns, without you adding them to each project's .gitignore.
_10printf '.DS_Store\n.idea/\n*.swp\n' >> ~/.gitignore_global_10git config --global core.excludesFile ~/.gitignore_global
Why it happens
#Some files come from your operating system or editor, not from the project. macOS creates .DS_Store in folders you open in Finder, Windows creates Thumbs.db, and editors create folders such as .idea/ or swap files ending in .swp.
You could add them to every project's .gitignore, but they are about your machine, not the code. Other contributors on different systems have different clutter, and project ignore files fill up with entries that have nothing to do with the project.
Git reads ignore patterns from three places, from most specific to least:
.gitignorefiles in the repository, shared with everyone..git/info/exclude, private to one clone.- The global file set by
core.excludesFile, private to your user account and used in every repository.
How to fix it
#Create the global ignore file
Put your personal patterns in a file. The name does not matter; this example uses ~/.gitignore_global:
_14# ~/.gitignore_global_14_14# macOS_14.DS_Store_14.AppleDouble_14_14# Windows_14Thumbs.db_14Desktop.ini_14_14# Editors_14.idea/_14*.swp_14*~
Then tell Git where it is:
_10git config --global core.excludesFile ~/.gitignore_global_10git config --global core.excludesFile # print the value to check
Or use the default location
If core.excludesFile is not set, Git already looks in $XDG_CONFIG_HOME/git/ignore, which is ~/.config/git/ignore when that variable is not set. Putting your patterns there works without any config:
_10mkdir -p ~/.config/git_10echo ".DS_Store" >> ~/.config/git/ignore
Pick one approach. If core.excludesFile is set, Git uses that file instead of the default one.
Check that a file is ignored
git check-ignore -v shows which file and line ignored a path, which is very useful when a rule does not seem to apply:
_10git check-ignore -v .DS_Store_10# /Users/sam/.gitignore_global:4:.DS_Store .DS_Store
No output means the path is not ignored.
Stop tracking files that were already committed
Ignore rules only apply to untracked files. If .DS_Store files are already in the repository, remove them from the index and commit:
_10git rm -r --cached --ignore-unmatch .DS_Store '**/.DS_Store'_10git commit -m "Stop tracking .DS_Store files"
The files stay on your disk; only Git stops tracking them.
Keep project rules in the project
The global file is for things specific to you. Anything every contributor would generate, such as node_modules/, build output or .env files, belongs in the project's .gitignore so it protects everyone, including CI and new team members.
Common pitfalls
#- Writing the file but never setting
core.excludesFile, or setting it with a typo in the path. Check withgit config --global core.excludesFile. - Expecting the rule to remove files already committed. Ignoring only affects untracked files; use
git rm --cachedfirst. - Relying on your global file for project essentials like
.env. Teammates without the same global file will commit them. - Setting
core.excludesFilein a repository's local config instead of with--global, so it only works in that one repository.
Related reading
#Was this resource helpful?