Git mental model (snapshots not diffs) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how Git handles data internally when you save changes.
Specifically, how the number of files affects Git's work when it stores snapshots.
Analyze the time complexity of this Git command sequence.
git add .
git commit -m "Save snapshot"
This code adds all current files to the staging area and then commits them as a snapshot.
Look for repeated work Git does when creating a snapshot.
- Primary operation: Git reads each file to create a snapshot of its content.
- How many times: Once per file in the project at commit time.
As the number of files grows, Git reads more files to build the snapshot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Reads 10 files |
| 100 files | Reads 100 files |
| 1000 files | Reads 1000 files |
Pattern observation: The work grows directly with the number of files.
Time Complexity: O(n)
This means Git's work to save a snapshot grows linearly with the number of files.
[X] Wrong: "Git stores only the changes, so commit time is always the same no matter how many files."
[OK] Correct: Git actually saves a snapshot of all files at commit time, so more files mean more work.
Understanding how Git handles snapshots helps you explain version control efficiency clearly and confidently.
"What if Git stored only diffs instead of snapshots? How would the time complexity change when committing many files?"
Practice
git commit?Solution
Step 1: Understand what
It records the current state of your project files as a snapshot.git commitdoesStep 2: Differentiate snapshot from changes
Unlike some systems, Git saves the whole snapshot, not just the changes.Final Answer:
A snapshot of all your files at that moment -> Option DQuick Check:
Git saves snapshots, not diffs [OK]
- Thinking Git saves only changes (diffs)
- Confusing commit with backup
- Believing commit saves command history
Solution
Step 1: Recall Git commands for saving work
The command to save a snapshot isgit commit.Step 2: Verify other options
Commands likegit snapshot,git save, andgit backupdo not exist in Git.Final Answer:
git commit -> Option CQuick Check:
git commitcreates snapshots [OK]
git commit to save snapshots [OK]- Using non-existent commands like git save
- Confusing commit with backup commands
- Trying git snapshot which is invalid
echo "Hello" > file.txt git add file.txt git commit -m "First snapshot" echo "World" >> file.txt git add file.txt git commit -m "Second snapshot"
What does the second commit snapshot contain?
Solution
Step 1: Understand what each commit saves
Each commit saves a full snapshot of the file at that time, not just changes.Step 2: Analyze the second commit content
After appending "World", the second commit snapshot includes both "Hello" and "World" lines in file.txt.Final Answer:
The entire file.txt with both "Hello" and "World" lines -> Option BQuick Check:
Git snapshots save full file content [OK]
- Thinking commit saves only new lines
- Confusing snapshots with diffs
- Assuming commit saves partial file
git commit but Git says "nothing to commit, working tree clean". What is the most likely reason?Solution
Step 1: Understand the message meaning
"Nothing to commit, working tree clean" means no changes are detected compared to last commit.Step 2: Check if files were changed
If no files changed, Git has no new snapshot to save, so commit does nothing.Final Answer:
There are no changes since the last snapshot -> Option AQuick Check:
No changes = no new snapshot [OK]
- Assuming you must always run git add before commit even if no changes
- Thinking repository is corrupted
- Restarting computer unnecessarily
git commit?Solution
Step 1: Recall Git snapshot model
Git saves a full snapshot of the project at commit time, not just diffs.Step 2: Understand Git's storage optimization
Internally, Git reuses unchanged files from previous commits to save space efficiently.Final Answer:
Git saves a full snapshot of all files, but reuses unchanged files from previous commits internally -> Option AQuick Check:
Snapshots with internal reuse = Git saves a full snapshot of all files, but reuses unchanged files from previous commits internally [OK]
- Thinking Git saves only diffs
- Believing commit saves nothing until push
- Assuming changed files are saved as full copies only
