Introduction
Most developers learn Git through commands like git commit, git push, or git merge, but they often rely on memorization or online guides to get things done. Understanding Git internal design how it stores data, tracks history, and ensures integrity can turn confusing errors into predictable outcomes. Instead of just memorizing commands, you can reason about Git.
What Happens When You Run git init
Running git init does one main thing: it creates a hidden .git directory in your project root. This folder is Git’s database and control center—everything Git needs to manage your project is stored here. Without it, you’re just working with plain files.
Understanding the .git Folder
The .git directory contains several key components:
- HEAD – A pointer to the current branch or commit you’re on.
- objects/ – Git’s object database, storing all blobs, trees, commits, and tags.
-
refs/ – References to commits: branches (under
refs/heads), tags (refs/tags), and more. - config – Project-specific Git configuration.
-
index – The staging area, a binary file that tracks what will go into the next commit.
What Happens During git add
When you run git add file.txt:
- Git compresses the file’s content and creates a blob object.
- The blob’s hash (e.g.,
a1b2c3...) is computed from its content. - The blob is written to
.git/objects/. - The file’s hash and path are recorded in the index (staging area).
The staging area is essentially a list of what will be part of the next tree object.
What Happens During git commit
When you commit:
- Git creates a tree object from the current index, representing the project structure.
- A commit object is created containing:
- The tree’s hash
- Parent commit hash(es)
- Author, committer, timestamp
- Commit message
- The commit object is stored in
.git/objects/. - The current branch reference (e.g.,
refs/heads/main) is updated to point to the new commit hash.

Top comments (0)