When working with Git, it’s often necessary to ignore certain files and directories that are specific to your development environment or operating system. This can include IDE settings, operating system files, and more. Instead of adding these patterns to each project’s .gitignore file, you can set up a global .gitignore file that applies to all repositories on your system. Here’s how you can do it.

Steps to Create and Configure a Global .gitignore File

1. Create the Global .gitignore File

First, create a global .gitignore file. You can place this file anywhere you like, but a common location is in your home directory.

1
touch ~/.gitignore_global

2. Add Patterns to the Global .gitignore File

Open the .gitignore_global file in a text editor and add the patterns for files you want to ignore globally. For example:

# Ignore Mac system files
.DS_Store

# Ignore Windows system files
Thumbs.db
desktop.ini

# Ignore IDE/editor settings
.idea/
.vscode/
*.suo
*.user
*.userosscache
*.sln.docstates

# Ignore node_modules globally
node_modules/

3. Configure Git to Use the Global .gitignore File

Tell Git to use this global .gitignore file by running the following command:

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

4. Verify the Configuration

To verify that Git is using the global .gitignore file, you can check the configuration with:

1
git config --global core.excludesfile

This should output the path to your global .gitignore file (e.g., /Users/yourusername/.gitignore_global).

Example of a Global .gitignore File

Here’s an example of what a global .gitignore file might look like:

# Ignore Mac system files
.DS_Store

# Ignore Windows system files
Thumbs.db
desktop.ini

# Ignore IDE/editor settings
.idea/
.vscode/
*.suo
*.user
*.userosscache
*.sln.docstates

# Ignore node_modules globally
node_modules/

# Ignore log files
*.log

# Ignore temporary files
*.tmp
*.swp
*~

Summary

  • Create a Global .gitignore File: Create a file (e.g., ~/.gitignore_global) to store global ignore patterns.
  • Add Ignore Patterns: Edit the file to include patterns for files you want to ignore across all repositories.
  • Configure Git: Run git config --global core.excludesfile ~/.gitignore_global to set up Git to use the global ignore file.
  • Verify: Check the configuration to ensure Git is using the correct global .gitignore file.

By setting up a global .gitignore file, you can avoid adding common ignore patterns to every single repository, keeping your project-specific .gitignore files clean and focused on project-specific needs.

Happy coding!


[alex_rocha] by alex

🇧🇷Senior Software Developer