Use a global gitignore file, please!

posted
2025-04-11

Here’s a quick tip that I find myself repeating, so I thought I’d write this up somewhere I can reliably link to.

Many Git repositories have a .gitignore file at the root of the repository. Generally this is used to exclude build outputs from ending up in source control, however often these files also accumulate file and directory names related to various editors (e.g. .vscode, .idea) and operating system caches (.DS_Store, Thumbs.db). The more people work on a repository, the longer these files tend to get.

It doesn’t have to be this way. Git has a global gitignore file where you should ignore files that relate to your tools. The default path of this file is $XDG_CONFIG_HOME/git/ignore, and following the XDG Base Directory Specification, this means the file is generally at ~/.config/git/ignore. You’ll need to create this file if it doesn’t exist.

To give you an example, currently my global gitignore file is:

*~
.#*
.DS_Store
[Tt][Aa][Gg][Ss]
/NOTES.org

In this file, I’m excluding:

  1. common names of backup files
  2. .DS_Store in case I’ve rsynced something from macOS
  3. Ctags-like index files (case-insensitively)
  4. the file I keep my notes related to a repository.

Using a global gitignore file, you free up .gitignore files in source control to list only those files that are related to the project (e.g. build outputs). Even better, you’ll only need to configure the global file once, since it’ll apply to all repositories on your machine.

Should you need one, there’s also a repository-specific gitignore file that doesn’t get tracked in source control at .git/info/exclude.