More Git answers

by Danny
Published 2 min read

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.


_10
printf '.DS_Store\n.idea/\n*.swp\n' >> ~/.gitignore_global
_10
git 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:

  • .gitignore files 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
_14
Thumbs.db
_14
Desktop.ini
_14
_14
# Editors
_14
.idea/
_14
*.swp
_14
*~

Then tell Git where it is:


_10
git config --global core.excludesFile ~/.gitignore_global
_10
git 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:


_10
mkdir -p ~/.config/git
_10
echo ".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:


_10
git 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:


_10
git rm -r --cached --ignore-unmatch .DS_Store '**/.DS_Store'
_10
git 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 with git config --global core.excludesFile.
  • Expecting the rule to remove files already committed. Ignoring only affects untracked files; use git rm --cached first.
  • Relying on your global file for project essentials like .env. Teammates without the same global file will commit them.
  • Setting core.excludesFile in a repository's local config instead of with --global, so it only works in that one repository.
#

Was this resource helpful?