.gitignore Isn't the only way to ignore files in Git
Beyond .gitignore: Three Ways to Hide Files in Git
Many developers assume that for keeping unwanted files out of their version control. However, Git actually provides three distinct levels of file exclusion..gitignore is the only tool available
"I've been using Git for so long and I just realized you can ignore files at three different levels and not just with .gitignore." — Nelson Figueroa
The Hierarchy of Exclusion
Depending on whether you want a rule to apply to everyone on the project, just yourself in one project, or every project on your computer, you have different options:
Comparison Table
| File Path | Scope | Tracked in Git? | Best Use Case |
|---|---|---|---|
.gitignore | Project-wide | Yes | Standard project ignores (e.g., node_modules) |
.git/info/exclude | Local Repo | No | Personal workflow files (e.g., notes.txt) |
~/.config/git/ignore | Machine-wide | No | OS-specific files (e.g., .DS_Store) |
Detailed Breakdown
1. The Standard: .gitignore
This is the most common method. Any pattern added here is ignored by Git commands and is checked into the repository so that all collaborators share the same ignore rules.
2. The Private Stash: .git/info/exclude
Located inside the .git folder, this file allows you to ignore files without committing those rules to the repo. It is ideal for files that are unique to your specific setup but aren't relevant to other team members.
3. The Global Rulebook: ~/.config/git/ignore
This file resides in your home directory. Anything listed here is ignored across every single repository on your machine. This is the perfect spot for system-generated files like macOS .DS_Store.
Customizing the Global Path:
If you prefer a different location (e.g., ~/.gitignore_global), you can change the configuration:
# Set a custom global ignore file
git config --global core.excludesFile ~/.gitignore_global
# Revert to the default system setting
git config --global --unset core.excludesFile
Verifying Ignore Rules
If you are unsure why a file is being ignored, you can use the check-ignore command. Mathematically, the "Ignored" state can be represented as:
How to debug:
- Identify the file you are questioning.
- Run the verbose check command.
- Analyze the output path.
Example Command:
git check-ignore -v .DS_Store
Possible Outputs:
- From
.gitignore:.gitignore:1:.DS_Store .DS_Store - From
.git/info/exclude:.git/info/exclude:7:.DS_Store .DS_Store - From the default global file:
/Users/nelson/.config/git/ignore:2:.DS_Store .DS_Store - From a custom global file:
/Users/nelson/.gitignore_global:1:.DS_Store .DS_Store
Note: If the file is not being ignored by any of these, the command will return no output.
Related Reading:
- Cloning specific branches (using
--single-branchand--depth). - Extracting contributor emails via
git shortlog. - GitHub email scraping and privacy protection.