# How Git Works Internally  in (.git Folder)

## Let’s Start With a Simple Story

Imagine you have a **magic school bag**.

Inside that bag:

* Every homework version you ever wrote is saved
    
* Nothing is ever lost
    
* You can go back to *any day* and see what you wrote
    
* Even if you tear a page, the bag remembers it
    

**That magic bag is the** `.git` folder.

You don’t see it normally, but **Git lives inside it**.

## What Is the `.git` Folder?

When you run:

```plaintext
git init
```

Git creates a **hidden folder** called:

```plaintext
.git
```

### Very Important Rule

> **Your project files are outside.  
> Git’s brain is inside** `.git`.

Your code = school notebook  
`.git` folder = teacher’s record book

## Why Does the `.git` Folder Exist?

The `.git` folder exists to answer these questions:

* What files do you have?
    
* What did they look like yesterday?
    
* Who changed them?
    
* When were they changed?
    
* Can we go back safely?
    

Without `.git`, Git knows **nothing**.

## What’s Inside the `.git` Folder? (Simple View)

Think of `.git` like a **library**.

Inside it, Git stores:

* File content
    
* Folder structure
    
* History
    
* Labels for versions
    

You don’t need to open it daily—but understanding it makes Git feel easy.

## Git Objects: Blob, Tree, Commit (Very Simple)

Git stores everything as **objects**.  
Only **three main types** matter.

Let’s explain them using a **toy box example**.

### 1\. Blob (The Toy)

A **Blob** stores **file content only**.

* It does NOT know the file name
    
* It does NOT know the folder
    
* It only knows: “Here is the content”
    

Example:

```plaintext
Hello World
```

That text becomes a **blob**.

Same content = same blob (even if file name changes)

### 2\. Tree (The Box)

A **Tree** is like a **box that arranges toys**.

* It knows file names
    
* It knows folder structure
    
* It points to blobs
    

Example:

```plaintext
project/
 ├── index.html
 └── style.css
```

Tree says:

* index.html → blob A
    
* style.css → blob B
    

---

### 3\. Commit (The Photo)

A **Commit** is a **photo of the whole project**.

It contains:

* A tree (folder structure)
    
* A message (“Added homepage”)
    
* Who made it
    
* Time
    
* Parent commit (previous photo)
    

Commit = snapshot in time

### Relationship Between Commit, Tree, and Blob

Think like this:

```plaintext
Commit
  ↓
 Tree
  ↓
 Blobs (file contents)
```

Git does NOT save files again and again  
Git saves **connections between objects**

That’s why Git is fast and smart.

## What Happens Internally During `git add`?

You write or change a file.

Git says:

> “Okay, I will prepare this.”

### Internally:

1. Git reads file content
    
2. Converts it into a **blob**
    
3. Stores it inside `.git/objects`
    
4. Marks it as **staged**
    

Git is saying:

> “I know this content now.”

## What Happens Internally During `git commit`?

Now you say:

```plaintext
git commit
```

### Internally:

1. Git creates a **tree** (folder map)
    
2. Git creates a **commit object**
    
3. Commit points to:
    
    * Tree
        
    * Parent commit
        
4. HEAD moves to this new commit
    

Git is saying:

> “This version is now official.”

## How Git Tracks Changes (Smart Trick)

Git does **not** track files.

⚠️ Important:

> **Git tracks content, not file names.**

If content doesn’t change:

* Git reuses the same blob
    
* No extra storage
    

This is why Git is:

* Fast
    
* Space efficient
    
* Reliable
    

## How Git Uses Hashes (Magic Fingerprints)

Every Git object gets a **hash**.

A hash is like:

* A fingerprint
    
* A barcode
    
* A secret code
    

Example:

```plaintext
a3f5c9e4...
```

### Why Hashes Matter

* If content changes → hash changes
    
* If hash changes → Git knows something is wrong
    
* No corruption possible
    

Git becomes **trustworthy by design**

## Mental Model (Remember This Forever)

Think of Git like this:

* Blob = content
    
* Tree = structure
    
* Commit = snapshot
    
* `.git` = brain
    
* Hash = fingerprint
    

**Git is not magic.  
Git is organized memory.**

## Final Thought for Students

> You don’t need to memorize commands.  
> You need to understand the story.

Once the story is clear:

* Commands make sense
    
* Fear disappears
    
* Confidence grows
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768627979363/89bb2320-4faf-4aea-9fcc-fac5f6362995.png align="center")
