Git tip: keep your personal business out of .gitignore files

Chances are most of the Git repositories you work with contain a .gitignore file. This tells Git the files and directories that you want kept out of the repository.

The .gitignore file is usually programming language or framework specific, and is a great way to keep the repository clean and free of things like log files and build artefacts.

What it‘s less good for, however, is ignoring files that are specific to your particular operating system, IDE, or environment. Firstly, it‘s a pain to have to add your own system-specific exclusions to the .gitignore file of every repository you work with, but also it makes for a messy .gitignore file, especially when there are many developers all with their own particular set-up.

Thankfully there is a simple solution: maintain your own global ignore file which will apply to all the repositories you work with on your computer.

Configuring a global Git ignore file

From your terminal or command line, run the following to configure Git with a global ignore file called .gitignore_global in your home directory:

  $ git config --global core.excludesfile ~/.gitignore_global

Now you can fill it with all the system-specific exclusions you like, without polluting the ignore files of the repositories you work with.

What to add to your global Git ignore file

My global ignore file contains operating system specific things like macOS’ .DS_Store file, but also things specific to my IDE, for example the .tags file generated by Ctags.

For a comprehensive library of operating system and editor specific exclusions check out the community-maintanted github/gitignore repository on GitHub.

There is a third way…

Sometimes you may have a file you want to exclude from a repository that doesn’t belong in the repository’s ignore file, but also doesn’t belong in your global ignore file. In those probably rare cases, you can list the exclusion in a file in your local repository called .git/info/exclude.

Want more juicy Git tips like this straight to your inbox?

You'll get an email whenever I have a fresh insight or tip to share. Zero spam, and you can unsubscribe whenever you like with a single click.

More articles on Git

Authored by Published by