DEV Community

Cover image for Gitting happier with a global .gitignore
Dom Habersack
Dom Habersack

Posted on • Originally published at islovely.co

Gitting happier with a global .gitignore

In most teams, we’re free to choose whatever editors and tools we want to get our work done. Everybody uses the OS and editor of their choice, and we push everything to a shared repository. Some use VS Code on macOS, others use Sublime Text on Windows, yet others kick it oldschool with vim on Linux.

All these individual choices create file noise. Operating systems add files like .DS_Store to keep track of folder preferences. Editors keep your workspace settings in hidden files as well. We don’t normally want to add those system-specific files to our project repositories.

Let say Josh’s config file for VS Code says he likes to use two spaces for indentation. Becky uses the same editor, but she prefers four spaces for indentation. If both were to commit their configurations, they’d run into merge conflicts all the time.

To avoid this, we can add the troublesome files and directories to our project’s .gitignore file. Files and directories listed in there are not committed to the repository at all. Josh and Becky can keep using VS Code, but their settings never leave their computer.

This gets tedious when every developer’s individual settings end up in a project’s .gitignore. The entries that really matter get lost in a sea of one-off configuration files. Instead of this approach, every person can maintain their own global .gitignore. It works like a regular .gitignore in a project, but applies to this person’s machine globally. These settings apply system-wide, without having to list those files again per project.

If you happen to use VS Code, you can add its hidden files to your global .gitignore. That way, they will NEVER be added to any of the repositories you contribute to. That puts developers in charge of keeping their system-specific files out of repositories.

To set this up, create a global .gitignore file somewhere on your system. The name doesn’t matter; I call mine .gitignore_global so I can tell it apart from regular .gitignore files. You can then set that file to be your global “excludes file” with this command:

git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

In this snippet, replace ~/.gitignore_global with the path to your system-wide .gitignore. Tada, you’re now happily ignoring everything that is listed in this file in all repositories on your entire computer!

– Dom

Top comments (6)

Collapse
 
3rror profile image
Gianluca Andreotti

There is also a repository with a lot of pre-made .gitignore.
github.com/github/gitignore

For example, here there are some rules specific for macOS: github.com/github/gitignore/blob/m...

Collapse
 
jwkicklighter profile image
Jordan Kicklighter
Collapse
 
domhabersack profile image
Dom Habersack

Nice, these are excellent starting points. Thanks for sharing!

Collapse
 
miguelmj profile image
MiguelMJ

Wow! I didn't know this! I'm creating mine right now. No more .directory and other rubbish. Thanks, Dom!

Collapse
 
domhabersack profile image
Dom Habersack

Fantastic! I always ask my coworkers to use the global version instead of the per-repository gitignores so they can take care of their own rubbish.

Collapse
 
chris_lu_f1a9fd9cfc9 profile image
Chris L*****u

Great. Now how do I push this file to all my developers around the world?