Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’2 min read

What is .git Folder?

When you run git init, Git creates a hidden .git folder.

This folder stores all history, commits, and data.
If it’s deleted β†’ your repo is gone.

.git = Git’s database

Inside .git (Simplified)

.git/
β”‚
β”œβ”€β”€ objects/     β†’ Stores all file data (blobs, trees, commits)
β”œβ”€β”€ refs/        β†’ Branch pointers
β”œβ”€β”€ HEAD         β†’ Current branch
β”œβ”€β”€ index        β†’ Staging area
β”œβ”€β”€ config       β†’ Repo settings

Mental Model

.git = Database where Git stores everythin

1. Blob (File Content)

A blob stores:

βœ… Only file data
❌ No filename

Example:

hello.txt β†’ "Hi World"

Git saves:

Blob = "Hi World"

If 2 files have same content β†’ Git stores only ONE blob (saves space )


πŸ”Ή 2. Tree (Folder Structure)

A tree stores:

βœ… Folder info
βœ… File names
βœ… Links to blobs

Example:

project/
 β”œβ”€ app.js
 └─ index.html

Tree connects:

Tree
 β”œβ”€β”€ app.js β†’ Blob
 └── index.html β†’ Blob

Think of Tree = Folder snapshot πŸ“


πŸ”Ή 3. Commit (Snapshot Record πŸ“Έ)

A commit stores:

βœ… Pointer to Tree
βœ… Author
βœ… Date
βœ… Message
βœ… Parent commit

Commit
   ↓
  Tree
   ↓
 Blobs

Each commit = Full snapshot of project

How Git Tracks Changes

Git doesn’t save only differences.

It saves snapshots of files.
Same files = reused (no extra space).

So Git is fast and efficient.

What Happens in git add?

git add file.txt

βœ” Creates a blob
βœ” Puts it in staging area (index)

Files β†’ Staging Area

What Happens in git commit?

git commit -m "msg"

βœ” Creates tree
βœ” Creates commit
βœ” Moves branch forward

Staging β†’ Commit β†’ Branch

How Git Uses Hashes

Every object has a unique hash.

πŸ‘‰ If data changes β†’ hash changes
πŸ‘‰ Keeps Git secure and reliable πŸ”

Summary

  • .git = main storage

  • Blob = file

  • Tree = folder

  • Commit = snapshot

  • add = prepare

  • commit = save