Skip to main content

Command Palette

Search for a command to run...

Inside Git

How It Works and the Role of the .git Folder

Updated
•4 min read•View as Markdown
Inside Git

At this point, I hope you have a basic idea of what Git is and why Git is used. If not, feel free to read this blog (http://bit.ly/49MMZnr) to refresh your memory.

So, as we know, Git tracks changes in files, works locally on your machine, and keeps a full history of your project.

Now the question is: how is this happening behind the scenes? Where are those changes saved? Is Git storing the data in a database or in text files? In a simple way, we can store data either in a database or in files. It mainly depends on how convenient and efficient it is to store and manage that data.

Git chose to store everything as files instead of using a database. This makes Git fast, lightweight, and easy to use on any machine without extra setup.

To manage all this data, Git creates a hidden folder called .git inside your project. Now, how do you create this hidden folder 😳 ?

# just type the below command inside your project 
git init
# it will create .git folder inside your project.

Understanding the .git Folder

What is the .git folder ?

The .git folder is the brain of Git.

  • It stores entire history

  • It stores commits, branches, tags

  • It stores everything related to version control - your file changes, commit history, and other metadata.

Whenever you make a commit, Git saves a snapshot of your project inside this .git folder. That is how Git remembers every every version of your project and allows you to go back in time whenever needed.

If you delete .git:

❌ Your project becomes a normal folder

❌ No history

❌ No Commits

❌ No Git

High level structure of .git folder

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

Important Parts

File / FolderWhat is does
objects/Stores all Git data (commits, files, folders)
refs/Stores branch pointers
HEADTells Git which branch you are on
indexStaging area (what will be committed next)

👉 90% of GIt’s power lives in objects/


Git Objects: Blob, Tree, Commit (core of Git)

Git stores data using 3 main object types.

1️⃣ Blob - File Content

A blob store:

  • The content of a file

  • Not the file name

  • Git saves the content as a blob and gives it a HashID

2️⃣ Tree - Folder Structure

A tree represents:

  • A directory

  • File names

  • Folder structure

Tree Says:

This folder contains these files and subfolders.

3️⃣ Commit - Snapshot + Metadata

A commit stores:

  • References to a tree (project structure)

  • Author / Date time

  • Message

  • Parent Commit

A Commit answers:

What did the project look like at this moment ?


Relationship between Commit, Tree, and blob

This of Git like this:

Commit
  ↓
Tree (folders)
  ↓
Blobs (file contents)

Visual Diagram

Commit
 ├── Tree (root)
 │    ├── file1.txt → Blob
 │    ├── file2.txt → Blob
 │    └── src/
 │         └── app.js → Blob

How Git tracks changes

Git never saves differences first, instead

  • it saves full content

  • Uses hashes to detect duplicates

Internal flow

git add

When you run

git add file.txt

Git does NOT create a commit. Instead:

  • Git reads file content

  • Creates a blob object

  • Stores it in .git/objects

  • Updates the index (staging area)

Mental model

Working Directory → Staging Area

git commit

When you run:

git commit -m "add file.txt"

Internally:

  • Git creates a tree from the index (staging area)

  • Creates a commit object

  • Commit points to the tree

  • Branch pointer moves forward

Flow diagram

Index → Tree → Commit → Branch

Git Hashes

Git uses SHA-1 hashes (40-character IDs). Example:

e83c5163316f89bfbde7d9ab23ca2e25604af290

Why Hashes Matter

✔ Ensure data integrity
✔ Same content = same hash
✔ Any change = new hash

If even one character changes, hash changes completely.

👉 This makes Git trustworthy and secure


Why Git Is So Reliable

Git guarantees:

  • No silent corruption

  • No missing history

  • No accidental overwrites

Because:
✔ Everything is hashed
✔ Everything is connected
✔ History is immutable

Simple Summary

  • .git is the heart of Git

  • Git stores objects, not files

  • Blob = file content

  • Tree = folder structure

  • Commit = snapshot

  • git add → staging

  • git commit → snapshot

  • Hashes ensure safety and integrity


Thank you :) Happy Coding.