ChaiCodechaicode webdev cohort 2026GitHubGit

Inside Git: How It Works and the Role of the .git Folder

Explore the inner workings of Git, the role of the .git folder, and how repositories manage data, changes, and integrity with hashes

Kenil Patel
Kenil PatelFull Stack Engineer

When we use Git, we usually interact with commands like git add, git commit, and git push.
But have you ever wondered what actually happens inside Git when you run these commands?

This article will help you understand:

  • What the .git folder really is

  • How Git stores your code internally

  • What are Blob, Tree, and Commit objects

  • How Git tracks changes

  • How hashes ensure data integrity

The goal is not to memorize commands but to build a mental model of how Git works internally.


1. What is the .git Folder and Why Does It Exist?

When you run:

git init

Git creates a hidden folder named .git in your project directory.

This .git folder is the brain of Git.
It contains everything Git needs to track your project:

  • History of commits

  • Information about branches

  • Objects (files and snapshots)

  • Configuration

  • References (HEAD, branches, tags)

Without the .git folder, your project is just a normal folder.
With .git, it becomes a Git repository.

So we can say:

Your entire Git repository lives inside the .git folder.


2. Structure of the .git Directory (Conceptual)

Inside .git, you will find folders like:

  • objects/ → stores all data (files and commits)

  • refs/ → stores branch and tag references

  • HEAD → points to the current branch

  • index → staging area

  • config → repository configuration

  • logs/ → history of changes

You normally never edit these manually, but understanding them helps you understand Git deeply.


3. Git Objects: Blob, Tree, Commit

Git stores everything as objects.
There are three main types of objects:

1. Blob (Binary Large Object)

A blob stores the content of a file.

  • It does NOT store filename

  • It only stores file data

  • Example: content of index.html

So if two files have the same content, Git stores only one blob.


2. Tree

A tree represents a directory.

It stores:

  • File names

  • Folder structure

  • References to blobs and other trees

Think of it like:

A tree = folder snapshot


3. Commit

A commit stores:

  • Reference to a tree (project snapshot)

  • Author info

  • Message

  • Parent commit (previous history)

So a commit connects everything:

Commit → Tree → Blobs

This is how Git builds a chain of history.


4. How Git Tracks Changes

Git does NOT track changes line-by-line like traditional systems.
Instead, Git stores snapshots.

Each commit is a snapshot of your entire project at that time.

If a file does not change:

  • Git reuses the same blob object

  • No duplication happens

This makes Git:

  • Fast

  • Memory efficient

  • Reliable


5. What Happens Internally During git add?

When you run:

git add file.txt

Internally Git does the following:

  1. Reads the content of file.txt

  2. Creates a blob object

  3. Generates a hash for that content

  4. Stores it inside .git/objects

  5. Adds reference to the staging area (index file)

So:

git add = move changes from working directory to staging area

It does NOT create a commit yet.
It just prepares data.


6. What Happens Internally During git commit?

When you run:

git commit -m "My first commit"

Git performs these steps:

  1. Reads staged blobs from index

  2. Creates a tree object (directory structure)

  3. Creates a commit object

  4. Stores commit inside .git/objects

  5. Updates branch reference

  6. Moves HEAD to new commit

So:

git commit = create a permanent snapshot in history


7. How Git Uses Hashes to Ensure Integrity

Git uses SHA-1 hash (or SHA-256 in newer versions).

Every object (blob, tree, commit) is identified by a hash like:

a3f5c2e9d4...

This hash depends on:

  • Object content

  • Object type

  • Metadata

If even one character changes, the hash changes completely.

This gives Git:

  • Data integrity

  • Tamper detection

  • Unique identification

So Git can always verify:

"Has this data been modified?"


8. Relationship Between Commits, Trees, and Blobs

Conceptually:

  • Blob → file content

  • Tree → folder structure

  • Commit → snapshot + metadata

Flow looks like:

Commit
  ↓
 Tree
  ↓
 Blobs

Each commit points to a tree, and each tree points to blobs.


9. Mental Model of Git (Simple)

Instead of thinking:

Git = commands

Think like this:

  • .git = database

  • Blob = file data

  • Tree = folder snapshot

  • Commit = project snapshot

  • Hash = ID card

  • Branch = pointer to commit

  • HEAD = current pointer

Git is basically a content-addressable file system with history.


10. Internal Flow of git add and git commit

git add flow:

Working Directory → Staging Area (index) → Objects (blob)

git commit flow:

Staging Area → Tree → Commit → Branch → HEAD update

This explains why Git has three areas:

  • Working directory

  • Staging area

  • Repository


Conclusion

Understanding Git internally helps you:

  • Debug problems better

  • Use Git confidently

  • Avoid memorizing commands blindly

  • Build a strong mental model

The .git folder is not magic.
It is just a well-organized database of objects connected by hashes.

Once you understand:

  • Blob

  • Tree

  • Commit

  • Hash

  • Staging area

Git becomes logical instead of confusing.