How to Ignore Files in Git Without Touching .gitignore
Keep your shared repositories clean and your local workflow private by leveraging Git's built-in local and global exclusion systems.
Every developer is intimately familiar with .gitignore. It is the standard way to prevent build artifacts, dependency directories, and environment variables from slipping into version control. But because .gitignore is checked into the repository and shared with the entire team, it is not always the right tool for every job.
Sometimes, you need to ignore files that are unique to your local workflow—such as a personal scratchpad, a custom debugging script, or operating system artifacts like macOS's .DS_Store. Forcing these into a shared .gitignore clutters the repository for everyone else.
Fortunately, Git provides three distinct levels of file exclusion. Beyond the standard .gitignore, you can leverage per-repository local excludes and machine-wide global ignores to keep your workspace clean without disrupting your team.
The Local, Untracked Option: .git/info/exclude
Every Git repository contains a hidden .git directory. Inside this directory lies a lesser-known file: .git/info/exclude.
Like .gitignore, this file uses the same pattern-matching syntax to ignore files. However, because it lives inside the local .git folder, changes to it are never committed or pushed to the remote repository.
This makes .git/info/exclude the perfect home for files that are highly specific to your personal workflow within a single project. For instance, if you keep a local notes.txt file in the root of a project to jot down ideas or track your daily progress, adding notes.txt to .git/info/exclude ensures it remains untracked without forcing your teammates to adopt your personal ignore rules.
The Machine-Wide Shield: ~/.config/git/ignore
If you find yourself ignoring the same files across every single project on your machine, updating individual repositories becomes tedious. This is where global ignores come in.
By default, Git looks for a global ignore file located at ~/.config/git/ignore in your home directory. Any pattern added here is ignored across every Git repository on your computer.
This is the ideal place to handle system-specific or editor-specific files. For example, macOS users can add .DS_Store to this global file to prevent these metadata files from ever showing up in git status, regardless of the project they are working on.
If you prefer to store your global ignore rules elsewhere—for instance, if you want to keep them in a dotfiles repository—you can easily customize this path. To set a custom global ignore file like ~/.gitignore_global, run the following command in your terminal:
git config --global core.excludesFile ~/.gitignore_global
If you ever need to revert this setting and return to the default path, you can unset the configuration:
git config --global --unset core.excludesFile
Debugging the Ignore Stack with git check-ignore
With three different levels of ignore files active at once, it can occasionally be difficult to track down exactly why a specific file is being ignored. If a file is not showing up when you expect it to, you do not have to guess which rule is responsible.
Git includes a built-in debugging tool specifically for this purpose: git check-ignore. By running this command with the -v (verbose) flag, Git will tell you exactly which file and line number triggered the ignore rule.
For example, to check why a .DS_Store file is being ignored, run:
git check-ignore -v .DS_Store
Depending on where the rule is defined, the command will output the source file, the line number, and the matching pattern.
If ignored by the repository's shared .gitignore:
.gitignore:1:.DS_Store .DS_Store
If ignored by the local exclude file:
.git/info/exclude:7:.DS_Store .DS_Store
If ignored by the default global ignore file:
/Users/nelson/.config/git/ignore:2:.DS_Store .DS_Store
If ignored by a custom global ignore file:
/Users/nelson/.gitignore_global:1:.DS_Store .DS_Store
If the file is not being ignored by any active rule, the command will simply exit with no output.
By mastering these three tiers of Git ignores, you can keep your shared .gitignore files clean and focused on project-specific needs, while keeping your personal workflow files and OS-level clutter safely out of sight.
Sources & further reading
- .gitignore Isn't the Only Way to Ignore Files in Git — nelson.cloud
Lenn writes about cloud platforms, Kubernetes internals, and the infrastructure decisions that quietly make or break engineering organizations. Based in Berlin's vibrant tech scene, they have a talent for turning dense platform-engineering topics into prose that people actually finish reading.
Discussion 0
No comments yet
Be the first to weigh in.