Simplify Your Workflow with Global .gitignore: Code Happily, Ignore Globally!
To my surprise, i came to know about that there is a stuff called as global ignore in git. In this blog i will jot down notes on setting up global .gitignore file.
Git allows you to ignore certain files and directories using a .gitignore file. However, if you frequently work on multiple projects, you might want to configure a global .gitignore file to avoid repeating the same ignore rules across repositories.
Why Use a Global .gitignore?
A global .gitignore file is beneficial when you want to exclude common files across all Git repositories on your system. Some examples include,
- OS-specific files (e.g., macOS
.DS_Store, WindowsThumbs.db) - Editor/IDE-specific files (e.g.,
.vscode/,.idea/) - Common compiled files (e.g.,
*.pyc,node_modules/,dist/) - Personal configurations that should not be committed (e.g.,
.envfiles) - Or you can just define a format like *.ignore.* , which you can use to ignore any file needed.
Setting Up a Global .gitignore File
1. Create the Global .gitignore File
Run the following command to create a global .gitignore file in your home directory:
touch ~/.gitignore_global
You can use any path, but ~/.gitignore_global is commonly used.
2. Configure Git to Use the Global .gitignore
Tell Git to use this file globally by running
git config --global core.excludesFile ~/.gitignore_global
3. Add Rules to the Global .gitignore
Edit the ~/.gitignore_global file using a text editor
nano ~/.gitignore_global
Example content
# Ignore system files .DS_Store Thumbs.db # Ignore editor/IDE settings .vscode/ .idea/ *.suo *.ntvs* *.njsproj *.sln *.sw? # Ignore compiled files *.o *.pyc *.class node_modules/ dist/ # Ignore environment files .env .env.local
Save the file and exit the editor.
4. Verify the Global Ignore Configuration
To check if Git recognizes your global .gitignore file, run
git config --global core.excludesFile
It should return the path to your global ignore file, e.g., ~/.gitignore_global.
Managing the Global .gitignore
- To update the file, edit
~/.gitignore_globaland add/remove rules as needed. - If you ever want to remove the global
.gitignoresetting, run:git config --global --unset core.excludesFile - To list all global configurations, use:
git config --global --list
Setting up a global .gitignore file is a simple yet powerful way to maintain cleaner repositories and avoid committing unnecessary files. By following these steps, you can streamline your workflow across multiple Git projects efficiently.
Originally published on parottasalna.com.