Setting Up a Global gitignore (core.excludesFile)
How to set core.excludesFile so that files from your personal environment, such as macOS .DS_Store and editor files, are ignored in every repository, and what the default path is.
Files such as .DS_Store, Thumbs.db, .idea/workspace.xml and *.swp exist because of your computer and your editor, not the project. The global gitignore is where you write these rules once instead of repeating them in every repository.
Default path
Even without any configuration, git reads the following file as a global ignore list.
$XDG_CONFIG_HOME/git/ignore$HOME/.config/git/ignoreifXDG_CONFIG_HOMEis empty or not set
In other words, on most macOS and Linux systems you only need to create the ~/.config/git/ignore file.
mkdir -p ~/.config/git
printf '.DS_Store\n*.swp\n.idea/workspace.xml\n' >> ~/.config/git/ignore
To use another file: core.excludesFile
To use a file at a path of your choice, set core.excludesFile. Once this is set, git reads that file instead of the default path.
# macOS / Linux
git config --global core.excludesFile ~/.gitignore_global
# Windows (PowerShell)
git config --global core.excludesFile "$env:USERPROFILE\.gitignore_global"
Check that the setting took effect with the following command.
git config --global --get core.excludesFile
If nothing is printed, the setting is not configured, and the default path above is used.
What belongs in the global file
What fits globally are personal files that appear the same way no matter which project you work on.
| Type | Examples | Recommended templates |
|---|---|---|
| Operating system | .DS_Store, ._*, Thumbs.db, desktop.ini, *~ | macOS, Windows, Linux |
| Personal editor/IDE state | *.swp, .idea/workspace.xml, personal files in .vscode/* | Vim, JetBrains, VisualStudioCode |
| Personal tools | .envrc (direnv), personal notes folders | Write your own |
If you pick only templates such as macOS and JetBrains in the generator and copy the result, you can use it directly as the contents of your global file.
Conversely, keep build output, dependency folders and secret files in the project .gitignore rather than the global one. Those rules must apply to everyone who clones the repository.
Limits of the global approach
A global gitignore applies only to your computer. If a teammate has not set up a global config, their .DS_Store can still be committed. That is why the GitHub template repository describes both options: putting the Global templates in your global file or merging them into project templates.
In practice, the following guideline works well.
- For personal projects, or when team rules can tell everyone to set up a global config, keep them global.
- When you cannot control contributors, as in open source, also put at least
.DS_StoreandThumbs.dbin the project .gitignore.
Precedence
The global file has the lowest precedence of the four rule sources. Even if you ignore *.log globally, if the project .gitignore has !keep.log, the project wins. If you are unsure which rule applied, run git check-ignore -v <path>. If the path of the global file appears at the beginning of the output, a global rule made the decision.