tezvyn:

.gitignore: Telling Git What to Ignore

AI-drafted, machine-checkedSource: git-scm.combeginner

A .gitignore file is a deny-list for your repository, telling Git which files and directories to intentionally leave untracked. Use it to keep build artifacts, log files, and local configs out of your commit history.

WHY IT EXISTS: Git is designed to track every file change in a project directory. However, many files are generated during development—like build artifacts, logs, or editor settings—that shouldn't be part of the project's shared history. A .gitignore file provides a mechanism to tell Git to ignore these files from the start.

THE MENTAL MODEL: Think of .gitignore as a set of rules for Git's "do not track" list. When you run git status or git add ., Git first checks these rules. If a file matches a pattern in .gitignore, Git pretends it doesn't exist, keeping your working directory clean and preventing temporary or machine-specific files from being committed.

HOW IT WORKS: Git processes ignore rules from multiple sources in a specific order of precedence. First, patterns from the command line. Second, it looks for .gitignore files in the current directory and all parent directories up to the project root; patterns in a subdirectory's .gitignore override those in a parent. Third, it checks the repository-specific $GIT_DIR/info/exclude file. Finally, it checks the global ignore file defined by your Git configuration. The last matching pattern for a given file wins.

WHEN TO USE IT: Use a project's .gitignore file for patterns that should be shared with everyone who clones the repository. This includes build output directories (/build), dependency caches (node_modules), and log files (.log). For personal rules that apply to all your projects, like editor-specific files (.vscode/, .swp), use a global ignore file. For project-specific but personal rules, use $GIT_DIR/info/exclude.

WHEN NOT TO USE IT: Do not use .gitignore to ignore a file that is already being tracked by Git. It only works on untracked files. To stop tracking a file you've already committed, you must first remove it from the index using git rm --cached <file>. Also, do not rely on .gitignore as a security mechanism to hide secrets; the files still exist on the filesystem.

ONE CANONICAL EXAMPLE: A typical Node.js project's .gitignore would include lines like: node_modules/ to ignore the entire dependency folder; *.log to ignore any file ending in .log; .env to ignore environment variable files containing secrets; and dist/ to ignore the compiled output directory. A line starting with # is a comment, like # Ignore build artifacts.

Read the original → git-scm.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.