DEV Community

Cover image for Git Quick Tip: Using a Global Gitignore
Sean Kegel
Sean Kegel

Posted on • Originally published at seankegel.com

Git Quick Tip: Using a Global Gitignore

Did you know Git supports a global .gitignore file that can be used across all of your local repositories?

Getting Started

To get started, let's create a file named ignore in the ~/.config/git folder. You may need to create this folder if it does not already exist.

mkdir -p ~/.config/git && touch ~/.config/git/ignore
Enter fullscreen mode Exit fullscreen mode

In the file, we can add some patterns to ignore across all files.

# Ignore files generated by operating systems
.DS_Store
Thumbs.db
Enter fullscreen mode Exit fullscreen mode

Git should automatically load this file. The patterns in the global file are merged with any other .gitignore files in your repositories.

If you wanted to put the file in another location, you would need to update the Git configuration to point to the file. For example, if I had a ~/Code/ folder where I store all of my projects, I could create my file there: ~/Code/.gitignore_global. Then, I can run the following command to update my Git configuration.

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

What Should I Add to My Global Gitignore?

What other types of files can be ignored globally? A lot of developers like to ignore the default editor/IDE folders like .vscode and .idea. This would depend on your project though, some projects commit some of the files in these folders.

You can also ignore package folders like node_modules and vendor for PHP Composer.

Generated files can also usually be ignored globally. These files usually exist in the build/ or dist/ folders.

Another great ignore to add is .env. These are used in projects of all types and usually contain sensitive information like API keys and passwords, so it's nice to have some extra protection to make sure you don't accidentally commit the file.

The global .gitignore can also support things specific to your workflow. For example, when working in JetBrains IDEs, I love using scratch files to store notes and pseudo code I might have when coding. If I am using something like Vim or VSCode though, then my scratch files aren't supported. So instead, I add .scratch/ to me global .gitignore and then I can create markdown files or whatever else I might want there without it being committed. I also use .http to store JetBrains HTTP requests if I don't want them committed to the project. Read my post about the PhpStorm HTTP client and requests to learn more.

Gitignore Example

Below is my current global .gitignore:

# OS files
.DS_Store
.AppleDouble
.LSOverride
Thumbs.db
ehthumbs.db
Desktop.ini

# Editor Files
*.swp
*.swo
*.swn
*.swm
*.swl
*.swk
*.bak
*.backup
*.orig
*.ref
*~

# Logs
*.log
log.txt

# Generated files
build/
dist/

# Dependencies
node_modules/
vendor/

# Sensitive files
*.env
*.key
*.pem
*.credentials
*.password
*.secret

# Scratch files
.scratch/

# PhpStorm HTTP Requests
.http/
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using a global .gitignore is helpful to make sure you aren't committing files that shouldn't be committed. Most of these items should already be in the project's .gitignore, however, that might not always be the case.

Also, I don't like adding items to a project's .gitignore that don't apply to all developers, like my .http/ or .scratch. This is where a global .gitignore shines as it lets you add items relevant to your workflows.

For more information, you can refer to the GitHub documentation which has some great content including templates and links to the Git documentation.

Let me know in the comments if you have any other helpful items to add to the global .gitignore. I would love to hear about any custom workflows you might have that having the global ignore helps support. Thanks for reading!

Top comments (0)