.gitignore: Telling Git What to Ignore
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.
Interview question
You've committed a file to your Git repository. Later, you decide this file should not be tracked. What is the correct way to stop Git from tracking it using .gitignore?
- a.Git will ignore future changes to the file if its pattern is in .gitignore, but keep the existing version in history.
- b.Add the file's pattern to .gitignore, and Git will automatically untrack it.
- c.Use git rm --cached <file> and then add the file's pattern to .gitignore.Correct
- d.Delete the file from your local directory, then add its pattern to .gitignore.
Why? this is the answer
The card states that .gitignore only works on untracked files. To stop tracking a file already committed, you must first remove it from the index using git rm --cached <file>, then add its pattern to .gitignore to prevent it from being tracked again. Simply adding it to .gitignore (option B) will not affect files already under version control.
Just read this? Test yourself on what you have been reading.
Read the original → git-scm.com
- #git
- #version control
- #devops
- #configuration
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on git — each one lists the topics its interview covers.
See open roles