Git: How to set up a global ignore file
Have you ever accidentally committed a bunch of junk created by your OS, like
Thumbs.db files from Windows, or
.DS_Store files from macOS? Or, have you joined a project, and for one of your first commits, added rules to the
.gitignore file for your text editor’s project files? If so, this post is for you! You can avoid such pain or busywork by making a global ignore file.
Your global ignore file applies to all repositories. By adding some patterns there, you don’t need to do the multiplicative work of adding patterns to each new repository you work on. (Or take the risk of forgetting and accidentally committing some files.)
In this post we’ll cover creating a global Git ignore, and adding patterns for files from your OS, text editor, and other tools.
core.excludesFile option configures where Git looks for your global ignore file—but normally you won’t need to set the option. It defaults to
$HOME/.config/git/ignore, which is a standard place for tool-specific configuration within your home directory.
(The Linux-specific env var
$XDG_CONFIG_HOME takes precedence over the
$HOME/.config prefix, if set. But since
$XDG_CONFIG_HOME defaults to
$HOME/.config, this will normally result in the same directory being used.)
You can ensure your
.config/git directory exists (using
$ mkdir -p $HOME/.config/git
Then, create the file with your editor:
$ $EDITOR $HOME/.config/git/ignore
Now to add some patterns…
(They use the same format as repo-specifc
Whichever OS you use, it probably adds some small cache files in directories. For example, I use macOS which adds a
.DS_store file to pretty much every directory viewed with its file explorer. These files are never useful to track in Git.
GitHub have a repository of ignore file templates. Their OS-specific templates are a good starting place for your global ignore file. Pick the right one and copy-and-paste its contents:
Most teams use a mix of text editors, and even you all use the same, you probably don’t want to share configuration. So, it’s good to add some global patterns to avoid committing your text editor’s specific project files and directories.
Personally, I use Sublime Text, which uses two files for projects. I use these two patterns to ignore them:
# Sublime Text *.sublime-project *.sublime-workspace
Easy peasy lemon squeezy.
If you need some inspiration for your editor patterns, GitHub’s ignore template repo may again be useful—see its “Global” directory. Beware though, the templates seem to have varying opinions on what patterns to include. For example, the VS Code template currently starts:
.vscode/* !.vscode/settings.json ...
! signifies an “unignore” pattern, so the second line here makes the
.vscode/settings.json file commitable. This seems wrong to me—different team members may want to use different VS Code settings.
I’d instead recommend a single pattern to exclude
.vscode entirely, and add such an unignore pattern in projects where members do want to share configuration. So yeah, caveat yourself, emptor.
You may also find it useful to add some patterns to exclude files generated by your programming language, or other tools. Your project repos should include such patterns in their
.gitignore files, so all team members can benefit. But, adding a few language-specific patterns to your global ignore file can help prevent accidental additions in temporary test repos, one-off contributions, etc.
I use Python heavily, so I added these patterns to my global ignore file:
# Python *.pyc venv .tox
Respectively, these choice patterns ignore Python’s byte code files, virtual environments, and environments made by the tox testing tool.
Follow suite as you see fit.
Learn how to make your tests run quickly in my book Speed Up Your Django Tests.
One summary email a week, no spam, I pinky promise.
- Git: How to automatically stash while rebasing or merging
- Git: How to enable autocorrect
- Git: How to automatically create upstream branches