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
3 min readView as Markdown

What is Git?

Git is a Distributed version control system (DVCS) that is used to track every changes in our source code and other files. In simple words, Git help developer by saving the current versions of their work so we can easily look back at previous versions or undo mistakes .Git helps developers collaboratively work with other developers.

what the .git folder is and why it exists

When we run “git init“ , Git creates a hidden directory called .git . The .git folder is Git's database and control center. Every Git repository contains this hidden directory at its root, which stores everything Git needs to track your project's history such as history, commits, branches, tags.

Unlike many systems that track file changes, Git takes a snapshot of your entire project each time you commit. The .git folder stores these snapshots efficiently.

Structure of the .git directory

Git Objects: Blob, Tree, Commit

Git stores everything as a object.There are three core object types:-

Blob (Binary Large Object):-

A blob stores the content of a file. It does not store the filename and any metadata. If two files have identical content, Git stores only one blob.

eg:- “Hello World“ → Blob (hash: a1b2c3…..)

Tree:-

It represents a folder.Maps fileNames → blobs or other trees.

Tree

├── index.html → Blob

├── app.js → Blob

└── src/ → Tree

Commit:-

A commit stores:-

i)A reference to a tree (root directory)

ii)A reference to a parent commit.

iii)Author, message, timestamp.

A commit is a snapshot of the entire project, not a diff.

Relationship between commits, trees, and blobs

Think it like this, Blob = file content, Tree = folder Structure, Commit = snapshot + metadata.

How git track changes: The internal Flow

What Happens Internally During git add

When we run “git add file.txt“, internally Git does the followings:-

  1. Reads the content of file.txt

  2. Creates a blob object.

  3. Stores it in .git/objects

  4. Updates the index (staging Area) to point to that blob.

    What Happens Internally During git commit

    When we run “git commit -m “message“ “, internally Git does the followings:-

    1. Reads the index.

    2. Creates tree objects from staged files.

    3. Creates a commit object pointing to the root tree

    4. Moves the branch pointer to the new commit

      Here nothing is copied—only references are updated.

      Explain how Git uses hashes to ensure integrity

      Every Git object is identified by a SHA-1/SHA-256 hash based on object content and object type.

      eg:- hash = SHA(content + type)

      Why hashes Matter

      If content changes → hash changes.

      Commits cannot be silently altered.

      Entire history is tamper-evident.

Mental Model

Git stores snapshots of our project, connected by hashes.

Files = blobs

Folder = Trees

Snapshots = commits

Branches = movable pointer

“git adds“ stages blobs.

“git commits“ create trees and commit.