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
2 min read

How Git Works Internally

Most people use Git daily but don’t know what actually happens inside Git.
This short guide builds a clear mental model — no command memorization.

What Is the .git Folder?

  • Created when you run git init

  • This folder is the Git repository

  • Stores:

    • History

    • Commits

    • Branches

    • Staging area

    • All Git data

👉 Delete .git = Git is gone

Inside the .git Folder (High Level)

https://humbletoolsmith.com/img/posts/a-look-inside-the-_git-folder/Git%20Folder%20Internals.png

https://git-scm.com/book/en/v2/images/data-model-3.png

Key parts only:

  • objects/ → All data (files, commits)

  • index → Staging area

  • refs/ → Branch pointers

  • HEAD → Current branch

Think of .git as Git’s internal database.

Git Stores Data Using Hashes

  • Git tracks content, not files

  • Uses SHA-1 hash

  • Same content → same hash

  • Small change → new hash

✅ Ensures data integrity
✅ Makes Git fast and reliable

Git Objects (Core Idea)

Git stores everything as objects.

1️⃣ Blob (File Content)

  • Stores file data only

  • No filename

  • No folder info

2️⃣ Tree (Folder Structure)

  • Stores filenames

  • Connects files (blobs) & folders (trees)

3️⃣ Commit (Snapshot)

  • Points to one tree

  • Stores message, author, time

  • Links to previous commit

Commit–Tree–Blob Relationship

https://www.kdgregory.com/images/blog/git-object-diagram.png

Commit → Tree → Blob
  • Commit = snapshot

  • Tree = structure

  • Blob = content


How Git Tracks Changes

  • Git does not store diffs

  • Each commit is a snapshot

  • Unchanged files reuse the same blob

💡 Saves space without duplicating data


What Happens During git add

https://www.edureka.co/blog/wp-content/uploads/2016/11/Git-Architechture-Git-Tutorial-Edureka-2.png

  • Reads file content

  • Creates a blob

  • Stores it in .git/objects

  • Updates the index (staging area)

👉 No commit yet


What Happens During git commit

  • Reads staging area

  • Creates a tree

  • Creates a commit object

  • Moves branch pointer forward

👉 Snapshot is now permanent


Branches & HEAD (One-Line Model)

  • Branch = pointer to commit

  • HEAD = pointer to branch

  • Commits don’t move, pointers do


Final Mental Model (Remember This)

  • Git = snapshot system

  • .git = real repository

  • Everything = hash

  • Commits → Trees → Blobs

  • Branches = labels

More from this blog

C

CHAIAURCODE-->WEBCOHORT

13 posts