Sub topic 10
Personal and global ignore rules
This chapter assumes you can write basic .gitignore patterns and know the difference between tracked and untracked files. It covers two other places to put ignore rules, so that personal clutter stays out of the shared .gitignore.
Why not everything belongs in .gitignore
A .gitignore file is committed, so everyone who clones the repository gets the same rules. That suits things every contributor produces, such as node_modules/ or build output.
Some files are specific to you:
- Editor settings, such as
.idea/or*.swp. - Operating system files, such as
.DS_StoreorThumbs.db. - Scratch notes or local test scripts that only you use.
Adding these to the shared file clutters it with rules that are irrelevant to your teammates. Git has two other places for them.
The three sources
| Source | Scope | Shared with others? |
|---|---|---|
| `.gitignore` | One repository, committed | Yes |
| `.git/info/exclude` | One repository, your clone only | No |
| `core.excludesFile` | Every repository on your machine | No |
Per-repository personal rules: .git/info/exclude
The file .git/info/exclude uses the same syntax as .gitignore. It lives inside the .git directory, so Git never tracks it and git clone never copies it. Use it for a rule that applies to one project but that only you need.
echo "scratch/" >> .git/info/exclude
echo "notes-to-self.md" >> .git/info/excludeIf the .git/info directory does not exist, create it with mkdir -p .git/info first. Repositories made by git init usually have it already.
Machine-wide rules: core.excludesFile
The setting core.excludesFile points to a file whose patterns apply to every repository you use. This is the right place for editor and operating system clutter.
git config --global core.excludesFile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_globalThe first command writes the setting to your ~/.gitconfig. The second adds a pattern to the file it names. The file can have any name. .gitignore_global is only a convention.
If you never set core.excludesFile, Git falls back to $XDG_CONFIG_HOME/git/ignore. When XDG_CONFIG_HOME is unset, that path is ~/.config/git/ignore. You can create that file and skip the configuration step.
# ~/.gitignore_global
.DS_Store
Thumbs.db
.idea/
.vscode/
*.swp
*~How the sources combine
Git checks several sources for each path. When more than one has a matching pattern, the one with higher precedence wins. From highest to lowest:
- Patterns given on the command line, for commands that accept them.
.gitignorefiles in the working tree. A file in a deeper directory overrides one closer to the root..git/info/exclude.- The file named by
core.excludesFile.
Because a negation in a higher source beats a match in a lower one, a project .gitignore can re-include something your global file ignores. If your global file ignores *.log and the project has !debug.log in its .gitignore, debug.log is not ignored.
Finding which source matched
When a file is ignored and you do not know why, git check-ignore -v names the source file, line number, and pattern.
git check-ignore -v .DS_StoreA match in your global file prints a line such as /home/you/.gitignore_global:1:.DS_Store .DS_Store. A match in a repository exclude file prints .git/info/exclude as the source. This is the quickest way to tell a personal rule from a shared one.
Common mistakes
- Putting personal rules in the shared file. Teammates then carry your editor's patterns forever. Move them to your global file.
- Relying on global rules for project files. Your global file does not travel with the repository. If a build artifact must be ignored for everyone, put it in
.gitignore. - Forgetting the setting on a new machine.
core.excludesFileis stored in~/.gitconfig, so a fresh setup needs it again.
Exercise
You use an editor that creates .myeditor/ folders, and you want to ignore them in all your repositories without changing any project's .gitignore. Which commands do you run, and how do you confirm the rule works?
Show answer
Run git config --global core.excludesFile ~/.gitignore_global, then echo ".myeditor/" >> ~/.gitignore_global. In a repository containing a .myeditor/ directory, run git check-ignore -v .myeditor/. The output should name ~/.gitignore_global as the source of the matching pattern.