Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

.gitignore

A plain-text file that tells Git which untracked files and directories to leave out of normal status output and skip when staging files with `git add .`.

What belongs in .gitignore?

A project directory contains more than the files the team wants to version. Running a build might create a dist/ directory; running tests might produce logs or coverage reports. These files can stay on disk without cluttering git status or getting staged by git add .. Put shared ignore rules in .gitignore and commit the file so everyone gets the same defaults when they clone the repository.

How Git reads the patterns

Write one pattern per line. Blank lines are ignored, and a line starting with # is a comment. The patterns are globs, not regular expressions: * matches zero or more characters within one part of a path, without crossing a /. A trailing slash restricts a match to directories. A leading slash anchors a pattern to the directory containing that .gitignore, which is not necessarily the repository root. A .gitignore in a subdirectory applies only within that subtree and can override rules inherited from above.

Within one file, the last matching rule wins. Start a rule with ! to make an exception to an earlier ignore rule. One common trap: an exception for a file cannot bring it back if its parent directory is itself ignored. To keep one file in a directory, ignore the directory's contents rather than the directory itself, then add an exception for the file.

Example: a root-level .gitignore

# Build output at the repository root
/dist/

# Logs anywhere in the project
*.log

# Local environment files, but keep the template
.env*
!.env.example

The leading slash means /dist/ matches the top-level build directory but not apps/web/dist/. The log rule matches both debug.log and logs/server.log. The last line makes .env.example eligible to be staged; that template should contain placeholder values, not real credentials.

Shared rules vs. personal rules

Keep project-wide rules in the committed .gitignore. For a personal rule that only applies to your local copy of one repository, use .git/info/exclude. For rules you want across repositories, such as ignoring editor backup files, configure a file through core.excludesFile.

Interview question: why is Git still tracking the file?

Adding a path to .gitignore does not untrack it. If Git already tracks .env, edits can still appear in git status and be included in a commit. To stop tracking it without deleting your working copy, add the ignore rule, run git rm --cached -- .env, and commit both changes. That does not remove the file from earlier commits. If a real credential was committed, revoke or rotate it rather than relying on the ignore rule.

For the opposite problem—an untracked file is missing from git status—run git check-ignore -v -- path/to/file. If a rule matches, the output identifies the source file, line number, and pattern.