How to add a global .gitignore

Every developer has done it at least once: accidentally committed a .DS_Store file, a .idea/ folder, or some other machine-specific artifact into a shared repository. A per-project .gitignore helps, but you have to repeat yourself in every repo. A global .gitignore fixes that: it is a single file that Git applies to every repository on your machine.

How Git decides what to ignore

Git evaluates ignore rules from three sources, in order of specificity:

  1. .gitignore files inside the repository (committed, shared with the team)
  2. .git/info/exclude inside the repository (local, not committed)
  3. The file pointed to by core.excludesFile in your global Git config (applies to every repo on the machine)

The global exclude file is the right place for rules that are specific to your machine or tooling, not to a project. See the official gitignore documentation for the full resolution order.

Step 1: Create the file

By convention the file is named .gitignore_global and lives in your home directory, but you can place it anywhere and call it anything.

Linux / macOS

touch ~/.gitignore_global

Windows (PowerShell)

New-Item -ItemType File -Path "$env:USERPROFILE\.gitignore_global"
PowerShell output of New-Item creating .gitignore_global

Step 2: Tell Git about it

Register the file with Git via the core.excludesFile config key.

Shortcut: when core.excludesFile is not set, Git already reads $XDG_CONFIG_HOME/git/ignore, falling back to ~/.config/git/ignore (%USERPROFILE%\.config\git\ignore on Windows). Put your rules in that file and you can skip this step entirely. The explicit setting below is useful when you want the file somewhere else or want it to be obvious in your .gitconfig.

Linux / macOS

git config --global core.excludesFile ~/.gitignore_global

Windows (PowerShell)

git config --global core.excludesFile "$env:USERPROFILE\.gitignore_global"

This writes one line into ~/.gitconfig (Linux/macOS) or %USERPROFILE%\.gitconfig (Windows):

[core]
    excludesFile = /home/you/.gitignore_global

You can verify it was written correctly:

Linux / macOS

git config --global core.excludesFile

Windows (PowerShell)

git config --global core.excludesFile

Both should print the path you set.

Step 3: Populate the file

Anything your operating system, editor, or toolchain generates belongs here. Files the project itself produces do not.

A practical starting point:

# macOS
.DS_Store
.AppleDouble
.LSOverride

# Windows
Thumbs.db
ehthumbs.db
desktop.ini

# Linux
*~
.nfs*


# Vim / Neovim
*.swp
*.swo
Session.vim

# Emacs
\#*\#
.\#*

# direnv
.envrc

Leave version-manager files such as .tool-versions (asdf, mise) or mise.toml out of this list. Many teams commit them on purpose to pin tool versions. Ignoring them globally hides them from git status, and you never notice when a colleague’s change to them is missing from your commit.

Rules use the same glob syntax as a regular .gitignore. Blank lines and lines starting with # are ignored.

Generating rules automatically

Maintaining this list by hand gets tedious. gitignore.io (also accessible via API) generates tailored ignore rules for any combination of operating systems, editors, and languages. For example, to get rules for macOS + Windows + JetBrains + Go in the terminal:

Linux / macOS

curl -sL "https://www.toptal.com/developers/gitignore/api/macos,windows,jetbrains+all,go" \
  >> ~/.gitignore_global

Windows (PowerShell)

Invoke-WebRequest -Uri "https://www.toptal.com/developers/gitignore/api/macos,windows,jetbrains+all,go" `
  -UseBasicParsing | Select-Object -ExpandProperty Content `
  | Add-Content -Path "$env:USERPROFILE\.gitignore_global"

The GitHub team also maintains a curated collection of templates at github/gitignore. Each file there covers one language or tool and is a reliable reference.

Verify it works

Create a throwaway file that matches a rule and confirm Git ignores it:

Linux / macOS

cd /tmp && git init test-repo && cd test-repo
touch .DS_Store
git status

Windows (PowerShell)

$null = New-Item -ItemType Directory -Path "$env:TEMP\test-repo"; cd "$env:TEMP\test-repo"
git init
New-Item -ItemType File -Path "Thumbs.db"
git status

git status should report a clean working tree, because the file is invisible to Git.

PowerShell output showing git status with no files tracked

Caveats

  • Sources have a fixed precedence. Patterns in a repository’s .gitignore files beat .git/info/exclude, which beats core.excludesFile. So a !negation in a project’s .gitignore re-includes a file your global file ignores. Within a single source, the last matching pattern wins, even if an earlier one is more specific.
  • Already-tracked files are not affected. If a file is already committed to a repository, adding it to any .gitignore (including the global one) will not untrack it. You need git rm --cached <file> first.
  • Team members need their own setup. The global exclude file is purely local; it is never committed. If a pattern should apply to everyone on a project, put it in the project’s .gitignore instead.

Conclusion

You set up a global .gitignore once, and from then on it keeps OS, editor, and tool artifacts out of the commits in every repository on your machine. Keep it focused on machine- and tool-specific artifacts, and leave project-specific rules to the per-repository .gitignore.