Git File Ignoring Strategies: Beyond .gitignore

Git allows developers to ignore files at three distinct levels of scope: project-wide, repository-specific (local), and user-global. Using the correct level ensures that environment-specific files, such as IDE configurations or OS-generated artifacts, do not clutter the version control history or the shared .gitignore file.

Project-Level Ignoring with .gitignore

The .gitignore file is the standard method for ignoring files that should be excluded from version control for all contributors to a project. Because this file is checked into the repository, it ensures a consistent ignore list across all environments.

Best use case: Use .gitignore for project-specific artifacts such as build outputs, dependency folders (e.g., node_modules), and environment variable templates.

Repository-Specific Local Ignoring with .git/info/exclude

For files that need to be ignored only in a specific local clone of a repository, Git provides the .git/info/exclude file. Unlike .gitignore, this file is located within the .git directory and is not tracked by version control, meaning changes to it are never pushed to other collaborators.

Best use case: Use .git/info/exclude for personal notes, local scripts, or custom Makefiles that are unique to your personal workflow and should not be shared with the team.

User-Global Ignoring with ~/.config/git/ignore

To ignore files across every Git repository on a machine, developers can use a global ignore file. By default, this is located at ~/.config/git/ignore.

Best use case: Use global ignores for OS-generated files (e.g., .DS_Store on macOS) or IDE-specific files that are common to all your projects.

Customizing the Global Ignore Path

If you prefer a different location for your global ignore file, you can configure it using the following command:

git config --global core.excludesFile ~/.gitignore_global

To revert to the default setting, run:

git config --global --unset core.excludesFile

Debugging Ignored Files with git check-ignore

When a file is being ignored and you are unsure which configuration is responsible, the git check-ignore command identifies the source of the ignore rule.

Running git check-ignore -v <filename> will output the source file and the line number of the rule. For example:

  • Project-level: .gitignore:1:.DS_Store .DS_Store
  • Local-level: .git/info/exclude:7:.DS_Store .DS_Store
  • Global-level: /Users/username/.config/git/ignore:2:.DS_Store .DS_Store

Advanced Ignoring and Tracking Strategies

Beyond the standard ignore files, Git offers several advanced methods for handling files that are already tracked or need special treatment.

Handling Tracked Files

If a file is already tracked by Git but you want to ignore local changes to it, you can use the update-index command:

  • Assume Unchanged: git update-index --assume-unchanged <file> tells Git to stop tracking changes to the file, which is useful for local configuration files that are tracked in the repository.
  • Skip Worktree: git update-index --skip-worktree <file> is often preferred for files that are intended to be modified locally and should not be committed back to the repository.

Reducing Diff Noise

Using a .gitattributes file in the root of a project, you can specify that Git should ignore the diffs of certain files while still tracking them. For example, adding package-lock.json -diff to .gitattributes prevents massive, machine-generated diffs from appearing during git diff operations.

Local Scratch Directories

Some developers use a "scratch" directory strategy by creating a folder (e.g., /scratch) and placing a .gitignore file inside it containing only *. This ensures that everything within that folder is ignored by Git, regardless of where it is placed in the project structure.

Summary of Git Ignore Scopes

Scope File Path Tracked by Git? Recommended Use
Project .gitignore Yes Build artifacts, dependencies
Local .git/info/exclude No Personal notes, local scripts
Global ~/.config/git/ignore No OS/IDE files (e.g., .DS_Store)

Sources