Untracked vs tracked files in Git - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When working with Git, it is important to understand how Git handles files that are tracked versus untracked.
We want to see how Git's operations grow as the number of files increases.
Analyze the time complexity of the following Git command.
git status
This command shows the state of tracked and untracked files in the repository.
Git checks each file in the working directory to see if it is tracked or untracked.
- Primary operation: Scanning all files in the directory
- How many times: Once per file in the working directory
As the number of files increases, Git must check each one to determine its status.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file checks |
| 100 | 100 file checks |
| 1000 | 1000 file checks |
Pattern observation: The number of operations grows directly with the number of files.
Time Complexity: O(n)
This means Git's work grows in a straight line as you add more files.
[X] Wrong: "Git only checks changed files, so time does not grow with more files."
[OK] Correct: Git scans all files to find untracked ones, so more files mean more checks.
Understanding how Git handles files helps you explain performance in real projects and shows you know how tools work under the hood.
"What if Git used a cache to remember untracked files? How would the time complexity change?"
Practice
untracked in Git?Solution
Step 1: Understand the meaning of untracked files
Untracked files are those that Git has not seen before and are not part of any commit.Step 2: Compare with tracked files
Tracked files are known to Git and included in commits, unlike untracked files.Final Answer:
Git does not know about the file yet and it is not included in commits. -> Option BQuick Check:
Untracked = Not known to Git [OK]
- Confusing untracked with ignored files
- Thinking untracked files are staged
- Assuming untracked files are committed
Solution
Step 1: Identify the command to track files
Thegit addcommand tells Git to start tracking a file by adding it to the staging area.Step 2: Differentiate from other commands
git commitsaves changes,git pushsends commits to remote, andgit statusshows file states.Final Answer:
git add -> Option DQuick Check:
Start tracking = git add [OK]
- Using git commit before adding files
- Confusing git push with tracking
- Thinking git status tracks files
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: trackedfile.txtWhich files are currently tracked by Git?
Solution
Step 1: Analyze the status output sections
The "Untracked files" section lists files Git does not track yet, herenewfile.txt. The "Changes to be committed" section lists tracked files staged for commit, heretrackedfile.txt.Step 2: Determine tracked files
Onlytrackedfile.txtis tracked because it is staged.newfile.txtis untracked.Final Answer:
trackedfile.txt only -> Option AQuick Check:
Tracked files = staged or committed files [OK]
- Assuming untracked files are tracked
- Confusing staged with untracked
- Ignoring the status section labels
git add newfile.txt but git status still shows newfile.txt as untracked. What is the most likely reason?Solution
Step 1: Understand why git add might not track a file
If a file is ignored by Git due to .gitignore rules,git addwill not track it and it remains untracked.Step 2: Eliminate other reasons
Committing is not required to track a file; wrong filename would cause an error; Git tracks all extensions unless ignored.Final Answer:
The file is listed in .gitignore and ignored by Git. -> Option AQuick Check:
Ignored files stay untracked despite git add [OK]
- Thinking commit is needed to track
- Ignoring .gitignore rules
- Assuming Git restricts file types
file1.txt (tracked, modified), file2.txt (untracked), and file3.log (untracked). Your .gitignore contains *.log. You want to commit file2.txt but not the changes in file1.txt or file3.log. What is the correct sequence of commands?Solution
Step 1: Understand .gitignore effect
The pattern*.login .gitignore causesfile3.logto be ignored and untracked, so it won't be added bygit add ..Step 2: Choose commands to add only file2.txt
Usinggit add file2.txtadds only that file without staging changes to trackedfile1.txt. Then commit saves it.git add file3.logis ignored.git add .would addfile2.txtAND stage changes tofile1.txt. Usinggit commit -aonly commits tracked files, so untrackedfile2.txtwon't be included.Final Answer:
git add file2.txt; git commit -m "Add file2" -> Option CQuick Check:
Use git add on untracked file, ignore .log files [OK]
- Using git commit -a to add untracked files
- Adding ignored files by mistake
- Using git add . which also stages changes to tracked files
