Inside Git: How It Works and the Role of the .git Folder
How Git Works Internally?
- Git does not track changes like a traditional “file editor.” Instead, it stores snapshots of your project over time. Every time you commit, Git saves the exact state of your files and links it to previous snapshots.
Understanding the .git Folder
The
.gitfolder is the heart of a Git repository. It is created when you rungit init.The
.gitfolder contains the entire commit history, all Git objects such as files, folders, and commits, along with branch information and configuration data; if this folder is deleted, the project becomes a normal folder and all version history is permanently lost, which is why the.gitdirectory can be thought of as Git’s database where everything is stored and managed.

Git Objects: Blob, Tree, Commit
Blob (Binary Large Object)
Git stores only the contents of files, not their filenames or folder structure; it saves this data as blobs, where each blob represents file data, and if two different files have identical content, Git stores just one blob and reuses it for both.
Tree
A tree represents a directory in Git; it stores file names and references that link to blobs (file contents) or other trees, effectively capturing the folder structure.
Commit
A commit is a reference to a tree that represents a snapshot of the project at a point in time, includes a reference to the previous commit, and stores metadata such as the author, date, and commit message.

What Happens Internally During git add
When you run git add:
Git reads the file content
Creates a blob object
Stores it inside the
.gitfolderAdds a reference to the staging area
Think of staging as telling Git what you want in the next snapshot.
What Happens Internally During git commit
When you run git commit:
Git creates a tree object from staged files
Git creates a commit object
The commit links to:
The tree (current snapshot)
The previous commit
HEADmoves to the new commit

How Git Tracks Changes
Git does not store differences (diffs); instead, it stores complete snapshots efficiently by reusing existing blobs for unchanged files and creating new objects only for new or modified content, which makes Git fast and space-efficient.
How Git Uses Hashes for Integrity
Every Git object is identified by a SHA-1 hash (e.g., a3f5c9...).
Git uses hashes to:
Identify objects uniquely
Detect file corruption
Prevent accidental changes
If file content changes, the hash changes — guaranteeing data integrity.
Mental Model to Remember Git
Think of Git as:
A content-addressed database
Storing snapshots, not edits
Using hashes as permanent IDs
Linking commits like a chain