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

Most of us use Git commands like `git add`, `git commit` and `git push`. We know what to type but we don’t know what actually happens inside.

You don’t know where your code history is actually stored? How git is tracking your history? Or how Git knows what changed?

In this blog i will discuss How git works internally and Role of .git folder.

## Understanding the .git folder

When you run `git init` in a folder have you notices nothing changes in folder Thats because Git creates a hidden folder called .git.

Thid folder is the brain of your project. It contains everything - history, branches, config settings and actual file contens in a compressed format.

**Try this**: Go to your project terminal and run: `ls -a`

You will see `.git`. If you delete this folder, your project forgets everything and It becomes just a normal folder again, No History, No Versions.

Inside `.git`, the most important sub folder is `objects/`. Here all data is stored.

## Git Objects

Git stores data in a very specific way using ‘Objects’. In simple language Git is a key - value store like Disctionay or HashMap.

There are three main types of Objects you must know :

**A. Blob (Binay Large Object)**

**.** This stores content of your file

**.** It stores only the data, not the filename.

**.** If You have two files with the exact same text, Git will store only one Blob.

**B. Tree**

**.** This represnets Your directories.

**.** A Tree object stores - List of filenames and Pointers to Blobs (files) or other Trees (sub-folders).

**.** It connects the content with filename.

**C. Commits**

1. This is the snaphsot you see in history
    
2. A Commit Object stores :
    
    * The Tree (the state of folders at that time).
        
    * Metadata (Author, Timestamp, Message).
        
    * Parrent Commit (The previous commit hash).
        

## How Git Tracks Changes (Internel flow)

When you works on a project you usually run two commands `git add` and `git commit`. Let’s see what happens internally.

**Step 1:** `git add.`

When you run this, Git takes your file content and compresses into a **Blob** and saves it inside .`git/objects`.

It then updates the Staging Area (Index) to say “ Hey, this file is ready to be commited”.

**Step 2:** `git commit -m “message”`

1. Git creates a **Tree Object** representing your current folder structure.
    
2. It creates a **Commit Object** that points to this tree.
    
3. It links this new commit to it’s parent commit (previous commit).
    
4. It updates the `HEAD` pointer to this new commit.
    

## Hash Codes (SHA-1)

You must have seen those long strings like `a1b2c3d4……` next to your commits.

These are SHA-1 Hashes. Git takes the content of an Object (Blob, Tree or Commit) and genereates a unique 40-character ID.

**Integrity**: Even if you change a single comma in file, Hashes changes completely.

This means no one can ulter the code history without breaking the chain. This makes git incredibly secure and reliable.

## Conclusion

Git isn’t a tool for saving work, It is a beatiful system of pointers and snapshots.

* The `.git` folder is the database.
    
* **Blobs** holds data, **Tree** holds structure and **Commits** hold history.
    
* Everything is secured by **Hashes**.
