What is the difference between tracked, untracked, and ignored files in Git?
Understand the difference between tracked, untracked, and ignored files in Git, how git add and .gitignore work, and how to keep your repo clean.
Expected Interview Answer
Tracked files are those Git already knows about (staged or committed at least once); untracked files exist in your working directory but have never been added to Git; ignored files are untracked files Git is told to deliberately skip via .gitignore.
Tracked files are under version control, so Git reports their modifications, deletions, and staging. Untracked files are new to the working tree and appear in git status until you add them. Ignored files are a subset Git intentionally excludes — matched by patterns in .gitignore — so they never clutter status or get committed, though an already-tracked file is not ignored just by adding it to .gitignore.
- Tracking gives files full version history
- Knowing untracked files prevents forgetting to add work
- Ignoring keeps build artifacts and secrets out of the repo
- Cleaner git status focused on real changes
- Prevents accidental commits of large or sensitive files
AI Mentor Explanation
Tracked files are the players named on the official team sheet whose every run is recorded. Untracked files are net practice players hanging around who are not yet on any sheet. Ignored files are spectators the scorers are explicitly told to disregard — present at the ground, but deliberately never entered into the match record.
Step-by-Step Explanation
Step 1
See all three states
Run git status to view tracked changes, a list of untracked files, and the exclusion of ignored ones.
Step 2
Track a file
Run git add <file> to move an untracked file into the staging area so Git begins tracking it.
Step 3
Create ignore rules
Add patterns like node_modules/ or .env to .gitignore so matching untracked files are skipped.
Step 4
Check why a file is ignored
Use git check-ignore -v <file> to see which .gitignore rule and line matched it.
Step 5
Stop tracking but keep locally
Run git rm --cached <file> so an already-tracked file becomes untracked and .gitignore can then exclude it.
What Interviewer Expects
- Clear definitions of tracked, untracked, and ignored
- Knowing git add promotes untracked to tracked
- Understanding .gitignore only affects untracked files
- Knowing already-tracked files need git rm --cached to be ignored
- Awareness of git status showing these states
Common Mistakes
- Thinking adding a tracked file to .gitignore stops tracking it
- Assuming ignored files are the same as deleted files
- Forgetting untracked files are not backed up until added
- Committing secrets or build artifacts by not ignoring them
Best Answer (HR Friendly)
“Tracked files are the ones Git is already watching and saving history for; untracked files are new ones Git has noticed but is not yet saving; and ignored files are ones you have told Git to deliberately leave alone, like temporary or sensitive files. This keeps the project history clean and focused on the code that matters.”
Code Example
# See tracked changes, untracked files, and (hidden) ignored files
git status
# Also list ignored files explicitly
git status --ignored
# Promote an untracked file to tracked
git add app.js
# Tell Git to ignore build output and secrets
printf 'node_modules/\n.env\ndist/\n' >> .gitignore
# Find out which rule ignores a file
git check-ignore -v .env
# Stop tracking a file already in the repo, keep it on disk
git rm --cached config.local.jsonFollow-up Questions
- Why does adding a committed file to .gitignore not stop it being tracked?
- How do you make Git ignore a file only on your machine without editing .gitignore?
- What does git clean do and how is it dangerous?
- How can you see exactly which .gitignore rule matched a file?
MCQ Practice
1. A file has never been git added. What is its state?
A file Git has not been told to track is untracked until you run git add on it.
2. You add an already-committed file's name to .gitignore. What happens?
.gitignore only prevents untracked files from being added; use git rm --cached to untrack an existing file.
3. Which command shows why a file is ignored?
git check-ignore -v reports the .gitignore file and line number of the matching rule.
Flash Cards
Tracked file — A file Git already knows about (staged or committed at least once) and reports changes for.
Untracked file — A file in the working directory Git has never been told to track; shows in git status until git added.
Ignored file — An untracked file matched by a .gitignore pattern so Git deliberately skips it.
Ignore an already-tracked file — Adding it to .gitignore is not enough; run git rm --cached <file> first, then commit.