How to use a global .gitignore
file
Today I learnt …
There are files that you essentially never want to commit to a Git repo. Files like the annoying .DS_Store
that MacOS sprinkles liberally across your hard drive, for example. Or your personal IDE configuration (Visual Studio Code’s .vscode/
or JetBrains’s .idea/
). Or local environment secrets in .env
. The list goes on.
You can always include these files in each repo’s top-level .gitignore
— a special file that tells Git which files you don’t want to track — but it’s easy to forget and hard to maintain across multiple projects. And it turns out there’s an easier way: Git allows you to define a global .gitignore
file that lists the files that should be ignored in all your repos, old and new.
First, create a file named .gitignore
in your home directory. (The file can be anywhere and named anything, but let’s stick with ~/.gitignore
). Edit it so that it contains all the files you want to exclude from all your repos:
.DS_Store
.vscode/
.env
.envrc
All that’s left is to tell Git where it can find your global config:
If you want to add or remove any files from your global exclusions list, just edit ~/.gitgnore
and Git will use the updated list.
More details on what you can do with a .gitignore
, global or local, in the Git documentation.