Learn what Git actually writes to the.git folder when you commit. You will create a small repo, find the blob, tree and commit objects, and see how git gc packs history into a smaller packfile. It suits beginners who know basic Git commands and want to understand storage.
YouTube video: How Git Actually Stores Your Code ?
Caption availability is unknown. Check the CC control in the YouTube player.
Choose a chapter (11)
- Video length
- 12 min 45 s
- Time to do it
- 20 minutes
- Difficulty
- Medium
- Steps
- 8
What you need
Steps
Open a terminal and make a test folder. Run git init inside it, then set your name and email with git config user.name and git config user.email so commits work.
Create a file called hello.txt with one line of text. Run git add hello.txt and git commit -m "first commit". This writes three objects: a blob, a tree and a commit.
Run git cat-file -p HEAD to see the commit. It points to a tree and a parent. Copy the tree hash and run git cat-file -p on it to see the file name and blob hash.
Run git cat-file -p on the blob hash to print the file contents. Run git hash-object hello.txt to confirm the same hash is calculated from the content alone.
Look inside the.git folder. Objects sit in.git/objects as loose files named by hash. Open.git/refs/heads/main to see the branch reference, about 41 bytes in a SHA-1 repo.
Copy hello.txt to copy.txt and commit it. Check git count-objects -v: no new blob appears because identical content reuses the same object.
Make 20 small one-line edits to a roughly 1 MB file, committing each time. Run git count-objects -v to watch loose objects grow, then run git gc and check the size again.
Run git verify-pack -v on the new.pack file in.git/objects/pack to list base objects and deltas. Most blobs will be stored as small deltas against a base version.
Tips and common mistakes
Run git gc only on a clean working tree, since it rewrites loose objects into packfiles and you do not want uncommitted work in the way.
Hashes come from content, so renaming a file without changing it creates no new blob. Git infers renames in logs rather than storing them.
Rebasing creates new commits with new hashes because the parent changes, so old commits stay in the object store until garbage collection.
Questions
Does Git store full snapshots or only changes?
Both ideas are partly right. Each commit points to a full tree of blobs, but on disk Git packs similar blobs as deltas against a base version to save space.
Why did my file copies not create new objects?
Git names objects by a hash of their content. Identical content produces the same hash, so Git reuses the existing blob instead of writing a duplicate.
Is it safe to run git gc?
Yes, for normal repositories. It compresses loose objects into packfiles and removes unreachable ones, and committed history is kept.
The notes are drafted automatically from video details and general task knowledge. How the guide was prepared
Report a correction


