Git Quick Tip: Using a Global Gitignore

Search for a command to run...

No comments yet. Be the first to comment.
Powerful Database Features You Didn't Know PhpStorm Had

Something I recently came across while working on a Laravel app was an unoptimized factory. I’ve seen this many times and have even been guilty of it. Let’s say we have three models, a User, a Team, and a Project. A team can have a team owner (teams....

Learn to cast a JSON column to a DTO in Laravel Eloquent

Configure a Neovim development environment using LazyVim for PHP and Laravel.

In this article, I want to go over how to use tappable scopes in Laravel. I’ve used similar patterns in Java Spring Boot, but never really considered using it in Laravel until I read Unorthodox Eloquent by Muhammed Sari which is an excellent guide to...

Did you know Git supports a global .gitignore file that can be used across all of your local repositories?
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
In the file, we can add some patterns to ignore across all files.
# Ignore files generated by operating systems
.DS_Store
Thumbs.db
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
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.
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/
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!