Working directory state in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how checking the working directory state in git grows as the number of files changes.
How does git's work to find changes scale when more files exist?
Analyze the time complexity of the following git command.
git status --short
This command shows which files in the working directory are changed, added, or deleted compared to the last commit.
Git checks each file in the working directory to see if it differs from the last commit.
- Primary operation: Comparing each file's current state to its committed state.
- How many times: Once for every file in the directory.
As the number of files grows, git must check more files one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file checks |
| 100 | 100 file checks |
| 1000 | 1000 file checks |
Pattern observation: The work grows directly with the number of files.
Time Complexity: O(n)
This means the time to check the working directory grows in a straight line with the number of files.
[X] Wrong: "Git checks only changed files, so time is constant no matter how many files exist."
[OK] Correct: Git must look at every file to know if it changed, so more files mean more checks.
Understanding how git checks file changes 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 unchanged files? How would that affect the time complexity?"
Practice
git status command show you about your working directory?Solution
Step 1: Understand the purpose of
This command checks the current state of the working directory and staging area.git statusStep 2: Identify what
It lists new files, modified files, and files staged for commit, helping you track changes.git statusreportsFinal Answer:
It shows which files are new, modified, or staged for commit. -> Option AQuick Check:
Working directory changes = git status output [OK]
- Confusing git status with git commit
- Thinking git status deletes files
- Assuming git status changes files automatically
app.js for commit?Solution
Step 1: Identify the command to stage files
Thegit addcommand is used to add files to the staging area.Step 2: Confirm the correct syntax for staging a specific file
Usinggit add app.jsstages the file namedapp.jsfor the next commit.Final Answer:
git add app.js -> Option CQuick Check:
Stage files = git add [OK]
- Using git commit to stage files
- Trying to use git status to stage
- Using git push before commit
git status show about index.html?
echo 'Hello' > index.html git add index.html echo 'World' >> index.html git status
Solution
Step 1: Analyze the commands on index.html
First, 'Hello' is written and the file is staged withgit add. Then 'World' is appended, modifying the file after staging.Step 2: Understand git status output
Git will showindex.htmlas staged (with 'Hello') but also as modified (unstaged changes with 'World').Final Answer:
index.html is staged and has unstaged changes. -> Option AQuick Check:
Modified after staging = staged + unstaged changes [OK]
- Assuming staging updates automatically after file change
- Thinking file is untracked after git add
- Confusing staged with committed
git add README.md but git status still shows README.md under 'Changes not staged for commit'. What is the likely cause?Solution
Step 1: Understand git add and file modification
Runninggit addstages the current file state. If the file changes after, those changes are unstaged.Step 2: Interpret git status showing unstaged changes
If README.md appears under 'Changes not staged for commit', it means it was modified after staging.Final Answer:
You modified README.md after running git add. -> Option BQuick Check:
Modify after add = unstaged changes shown [OK]
- Assuming git add stages future changes automatically
- Thinking .gitignore affects already tracked files
- Confusing committed files with staged files
secret.txt. How can you remove it from the staging area without deleting the file from your working directory?Solution
Step 1: Understand the difference between unstaging and deleting
To remove a file from staging but keep it in the working directory, you must unstage it.Step 2: Identify the correct command to unstage a file
git reset secret.txtremoves the file from the staging area without deleting it from disk.Final Answer:
git reset secret.txt -> Option DQuick Check:
Unstage file = git reset filename [OK]
- Using git rm deletes the file from disk
- Using git checkout resets file content, not staging
- Using git clean deletes untracked files
