The .git directory structure - Time & Space Complexity
We want to understand how the time to access or update data inside the .git directory changes as the project grows.
How does the structure inside .git affect the speed of git commands?
Analyze the time complexity of git accessing objects stored in the .git directory.
# Example commands showing object storage
$ ls .git/objects
$ cat .git/objects/ab/cdef1234...
$ git cat-file -p <object-hash>
This shows how git stores objects in folders named by the first two hash characters, then files named by the rest.
Look at how git finds objects inside the .git directory.
- Primary operation: Searching for an object file inside a two-level folder structure.
- How many times: One folder lookup, then one file lookup per object access.
As the number of objects grows, git adds more files inside many folders.
| Input Size (number of objects) | Approx. Operations |
|---|---|
| 10 | 2 lookups (folder + file) |
| 1000 | 2 lookups (folder + file) |
| 100000 | 2 lookups (folder + file) |
Pattern observation: The number of lookups stays the same regardless of how many objects exist.
Time Complexity: O(1)
This means git finds any object in the .git directory in constant time, no matter how many objects there are.
[X] Wrong: "Finding an object takes longer as the project grows because there are more files."
[OK] Correct: Git uses a two-level folder system that keeps lookups quick by limiting files per folder, so access time stays constant.
Understanding how git organizes data helps you explain efficient storage and retrieval, a useful skill for many software tools.
What if git stored all objects in a single folder instead of splitting by hash prefixes? How would the time complexity change?