0
0
Gitdevops~10 mins

How Git stores objects - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - How Git stores objects
Create file or change
Generate SHA-1 hash of content
Compress content with zlib
Store compressed object in .git/objects/xx/
Index object by SHA-1 prefix
Use object in commits, trees, blobs
Retrieve by SHA-1 when needed
Git takes file content, creates a unique hash, compresses it, and stores it in a special folder indexed by that hash.
Execution Sample
Git
echo "Hello" > file.txt
sha1=$(git hash-object file.txt)
mkdir -p .git/objects/${sha1:0:2}
cp file.txt .git/objects/${sha1:0:2}/${sha1:2}
This simulates how Git hashes a file and stores it in the objects folder by splitting the SHA-1 hash.
Process Table
StepActionInput/StateOutput/State Change
1Create filefile.txt content: 'Hello'File created with content 'Hello'
2Generate SHA-1 hashContent 'Hello'SHA-1 hash: e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
3Compress contentContent 'Hello'Compressed data ready for storage
4Create directory.git/objects/e6Directory created for first 2 chars of hash
5Store objectCompressed dataStored in .git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
6Index objectStored objectObject indexed by SHA-1 for retrieval
7Use objectObject indexedUsed in commits, trees, blobs
8Retrieve objectSHA-1 hashOriginal content retrieved by hash
💡 Process ends after object is stored and indexed for retrieval by SHA-1 hash
Status Tracker
VariableStartAfter Step 2After Step 5Final
file.txt contentnone'Hello''Hello''Hello'
SHA-1 hashnonee69de29bb2d1d6434b8b29ae775ad8c2e48c5391e69de29bb2d1d6434b8b29ae775ad8c2e48c5391e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
Compressed datanonenonecompressed 'Hello'compressed 'Hello'
Storage pathnonenone.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
Key Moments - 3 Insights
Why does Git split the SHA-1 hash into two parts for storage?
Git uses the first two characters of the SHA-1 hash as a folder name to avoid too many files in one directory, making storage efficient and fast (see Step 4 and 5 in execution_table).
Is the stored object the original file content?
No, Git compresses the content before storing it to save space, so the stored object is compressed data (see Step 3 and 5 in execution_table).
How does Git find the object later?
Git uses the full SHA-1 hash to locate the object by combining the folder (first 2 chars) and file name (remaining chars) to retrieve and decompress it (see Step 8 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the SHA-1 hash generated at Step 2?
A9de29bb2d1d6434b8b29ae775ad8c2e48c5391e6
Be69de29bb2d1d6434b8b29ae775ad8c2e48c5391
CHello
Dcompressed 'Hello'
💡 Hint
Check the 'Output/State Change' column at Step 2 in the execution_table.
At which step is the object compressed before storage?
AStep 2
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'Action' column describing compression in the execution_table.
If the SHA-1 hash started with 'ab', where would Git store the object?
A.git/objects/ab/...
B.git/objects/e6/...
C.git/objects/ba/...
D.git/objects/aa/...
💡 Hint
Refer to Step 4 and 5 in execution_table about directory naming from SHA-1 prefix.
Concept Snapshot
Git stores objects by:
- Creating a SHA-1 hash of file content
- Compressing the content
- Splitting hash: first 2 chars as folder, rest as filename
- Saving compressed object in .git/objects/
- Using hash to retrieve content later
Full Transcript
Git stores data by first creating a unique SHA-1 hash from the file content. This hash acts like a fingerprint. Then Git compresses the content to save space. It splits the hash into two parts: the first two characters become a folder name inside .git/objects, and the remaining characters become the file name inside that folder. The compressed content is saved there. Later, Git uses the full hash to find and decompress the object when needed. This process helps Git store data efficiently and retrieve it quickly.