What is a .gitignore file and how does it work?
Learn what a .gitignore file does, how its glob patterns work, why it only affects untracked files, and how to stop tracking a file already committed.
Expected Interview Answer
A .gitignore file is a plain-text list of file and folder patterns that tells Git which untracked files it should never stage, commit, or show as untracked, so build artifacts, dependencies, and secrets stay out of version control automatically.
Each line is a glob-style pattern — like node_modules/, *.log, or .env — matched against paths relative to the .gitignore's location. Git checks these patterns whenever it scans the working directory, so ignored files never clutter git status or get added by git add . by accident. It only affects untracked files though: once a file is already tracked, adding it to .gitignore does not stop Git from tracking further changes to it, you must first git rm --cached it.
- Keeps generated files and dependencies out of commits
- Prevents accidentally committing secrets like .env files
- Keeps git status output clean and readable
- Works automatically with git add . and wildcards
- Can be scoped globally, per-repo, or per-directory
AI Mentor Explanation
A .gitignore file is like the ground staff's list of items never allowed onto the pitch — spare studs, drink bottles, personal phones. Anything on that list simply stays in the dressing room and never enters the match-day count. But if a banned item was already logged as official kit before the rule existed, the list alone won't remove it — someone must strike it off the inventory.
Step-by-Step Explanation
Step 1
Create the file
Add a .gitignore file at the repo root (or any subdirectory) listing patterns, one per line.
Step 2
Write glob patterns
Patterns like *.log, node_modules/, or /dist match files and folders relative to the .gitignore's location.
Step 3
Git checks it automatically
Every git status or git add . scan skips paths matching those patterns, so they never appear as untracked.
Step 4
Already-tracked files are unaffected
If a file was committed before being added to .gitignore, Git keeps tracking its changes regardless of the new rule.
Step 5
Untrack it explicitly if needed
Run git rm --cached <file> to stop tracking a file that .gitignore should have caught, then commit that removal.
What Interviewer Expects
- Explains .gitignore only affects untracked files
- Knows glob-style pattern syntax (*, /, trailing slash for directories)
- Understands git rm --cached is needed to untrack an already-committed file
- Mentions common uses: node_modules, build output, .env, IDE files
- Knows global gitignore (core.excludesFile) versus per-repo files
Common Mistakes
- Expecting .gitignore to remove files that are already tracked
- Forgetting the trailing slash needed to match directories only
- Committing secrets before adding them to .gitignore
- Not knowing about a global .gitignore for editor/OS files
Best Answer (HR Friendly)
“.gitignore is a simple list telling Git which files to never track automatically, like temporary files, downloaded dependencies, or secret configuration. This keeps the project history clean and prevents sensitive files from ever being uploaded by mistake.”
Code Example
# .gitignore
node_modules/
*.log
.env
/dist
# If .env was committed before adding it here, remove it from tracking
git rm --cached .env
git commit -m "Stop tracking .env"Follow-up Questions
- How do you set up a global .gitignore for all your repositories?
- What does the trailing slash in a .gitignore pattern mean?
- How do you force-add a file that .gitignore is excluding?
- What is the difference between .gitignore and .git/info/exclude?
- How do you remove a secret from Git history entirely, not just future commits?
MCQ Practice
1. What does a .gitignore file control?
.gitignore lists patterns for files Git should never stage or report as untracked; it has no effect on commits or branches.
2. What happens if a file is already tracked before being added to .gitignore?
.gitignore only prevents untracked files from being staged; an already-tracked file must be untracked with git rm --cached.
3. Which command stops Git from tracking a file without deleting it from disk?
git rm --cached removes the file from the index (stops tracking) while leaving the actual file untouched on disk.
Flash Cards
What does .gitignore do? — Lists patterns for untracked files/folders that Git should never stage or show in status.
Does .gitignore remove already-tracked files? — No — you must run git rm --cached to untrack a file that was committed before being ignored.
Give an example .gitignore pattern for a directory. — node_modules/ — the trailing slash restricts the match to directories.
Where can you set a .gitignore that applies to all your repos? — A global gitignore file configured via git config --global core.excludesFile.