Back
Git can ignore files in three places, not only in .gitignore
SiTech AI Team2 წთ. საკითხავი

Git can ignore files in three places, not only in .gitignore

A blog post walks through the three levels Git offers for ignoring files: the committed .gitignore, the per-repository .git/info/exclude and the machine-wide global ignore file.

Most people who work with Git know exactly one way to keep files out of version control: add them to .gitignore. A post on the nelson.cloud blog points out that Git actually supports three levels of ignore rules, and each of them solves a different problem.

Project level: .gitignore

The familiar option is the .gitignore file at the root of a repository. It is checked into Git together with the code, so every contributor gets the same rules. Files listed there are left out whenever Git commands run — which is why it stays the right place for build output, dependencies and editor artefacts that the whole team wants ignored.

Repository level: .git/info/exclude

The second file is .git/info/exclude. It lives inside the .git directory of every repository, but changes to it are never committed. On a fresh repository it usually holds a few comment lines. It is meant for personal exceptions that apply to one repository only — for example a notes.txt that is useful to you but has no place in the project. Adding such entries here keeps .gitignore tidy and avoids publishing preferences nobody else shares.

Machine level: ~/.config/git/ignore

The third level is global. The file ~/.config/git/ignore sits in the home directory and applies to every repository on the machine; it belongs to no project. Operating-system noise is the classic use case — on macOS that means .DS_Store. The location is configurable: git config --global core.excludesFile ~/.gitignore_global points Git at a different file, and git config --global --unset core.excludesFile restores the default.

Which file is ignoring a path

When a file is unexpectedly invisible to Git, git check-ignore -v prints the exact rule that matched. For .DS_Store the output might be .gitignore:1:.DS_Store, .git/info/exclude:7:.DS_Store or ~/.config/git/ignore:2:.DS_Store, depending on which of the three levels is responsible. If nothing ignores the file, the command prints nothing at all. The original post was discussed on Hacker News.

SSiTech

SiTech — AI-powered web development

We build fast, modern websites and bring AI into real business workflows. Have a project or a question? We'd love to help.