Git Internal Architecture

 

Git Internal Architecture: Working Tree, Index, and HEAD 

If you’ve ever wondered how Git really works internally, understanding these three core areas will completely change how you use Git:

  • Working Tree (Working Directory)

  • Index (Staging Area)

  • HEAD (Current Commit Reference)

Mastering these concepts will help you understand:

  • Why git add is necessary

  • How commits actually work

  • What happens during git reset

  • Why merge conflicts occur

  • What “detached HEAD” means

 1. What is the Working Tree in Git?

The Working Tree (also called Working Directory) is the actual project folder on your computer — the files you see and edit.

When you:

  • Create a file

  • Modify a file

  • Delete a file

You are changing the Working Tree.

Example

echo "Hello Git" > file.txt

Now file.txt exists in your working directory, but Git is not tracking it yet.

Check the status:

git status

You’ll see:

Untracked files:
  file.txt

At this point:

  • Working Tree → modified

  • Index → unchanged

  • HEAD → unchanged

Key Insight

The Working Tree represents your current development state — not what will be committed yet.

2. What is the Index (Staging Area) in Git?

The Index, also known as the Staging Area, is a temporary area where Git prepares changes before creating a commit.

Think of it as a commit draft.

When you run:

git add file.txt

Git copies the current version of file.txt into the Index.

What Happens Internally?

Now the state becomes:

  • Working Tree → file.txt (latest version)

  • Index → snapshot of file.txt ready for commit

  • HEAD → still previous commit

The Index is where Git builds the exact snapshot that will become your next commit.

Why the Staging Area Exists

This allows you to:

  • Stage only specific files

  • Stage parts of a file (git add -p)

  • Control exactly what goes into a commit

This design makes Git extremely powerful compared to other version control systems.

 3. What is HEAD in Git?

HEAD is a pointer that refers to the current commit you are working on.

Usually, it points to a branch, and that branch points to the latest commit.

Example:

HEAD → main → latest commit

You can check it:

cat .git/HEAD

You’ll see something like:

ref: refs/heads/main

That means HEAD is pointing to the main branch.

How Working Tree, Index, and HEAD Work Together

Let’s walk through a complete example.

Step 1: Initialize a Repository

git init

Step 2: Create a File

echo "Version 1" > app.txt

Current state:

  • Working Tree → Version 1

  • Index → empty

  • HEAD → no commits yet

Step 3: Stage the File

git add app.txt

Now:

  • Working Tree → Version 1

  • Index → Version 1

  • HEAD → still no commit

Step 4: Commit

git commit -m "Initial commit"

Now:

  • Working Tree → clean

  • Index → clean

  • HEAD → points to new commit

Git creates a commit object and moves HEAD forward.

Visual Representation of Git’s Architecture

            git add              git commit
Working Tree --------> Index --------------> HEAD
 (Your Files)         (Staging)            (Commit History)

This is the core internal Git flow.

What Happens When You Modify a File Again?

echo "Version 2" > app.txt

Now:

  • Working Tree → Version 2

  • Index → Version 1

  • HEAD → Version 1

You can compare differences:

Compare Working Tree vs Index:

git diff

Compare Index vs HEAD:

git diff --staged

This separation is extremely powerful.

What is Detached HEAD in Git?

If you checkout a specific commit:

git checkout <commit-hash>

Now HEAD directly points to a commit, not a branch.

This is called Detached HEAD state.

In this state:

HEAD → specific commit

If you make a commit here, it won’t belong to any branch unless you create one.

Where Does Git Store All This?

Inside the hidden .git folder:

.git/
 ├── HEAD
 ├── index
 ├── objects/
 ├── refs/

Important Components

  • HEAD → current branch reference

  • index → staging area snapshot

  • objects/ → stores commits, trees, and blobs

  • refs/ → stores branch pointers

Git is essentially a content-addressable filesystem.

Why Understanding Git Internal Architecture Matters

When you understand Working Tree, Index, and HEAD, you can fully understand:

  • git reset --soft, --mixed, --hard

  • git stash

  • git rebase

  • git merge

  • Conflict resolution

  • Advanced branching strategies

Without this knowledge, Git feels confusing.

With this knowledge, Git becomes predictable.

Final Thoughts

Git does not store changes like traditional version control systems.

Git stores snapshots.

Each commit is a complete snapshot of your project, and the flow between:

Working Tree → Index → HEAD
is the foundation of everything in Git.

If this story helped or motivated you, feel free to visit my profile Linkedin.com and connect.

Everyone starts somewhere — this was my start.


Comments

Popular posts from this blog

My First Stipend-Based Internship💻🚀

How I Fixed the Prisma Error

Design Web Pages using CSS