Understanding Git Internals: The Purpose of .git Folder
An Overview of Git Internals Structure and Workflow

Ever wondered how Git works internally. What exactly gets created when you run git init,
and what changes occur inside Git when you execute commands like git add?
Lets explores what happen behind the scenes of so-called .git folder?
The .git Folder
When you run the git init in the terminal , it results in creation of a .git folder which is generally hidden. This folder various key components responsible for the tracking and storing of changes made.
refs: Short for "references." This stores pointers to commit objects (like where your branches and tags are).config: Contains project-specific settings (like your remote URLs)HEAD: A file that points to the branch you are currently on.index: Also known as the Staging Area. It’s a binary file that lists what will go into your next commit.objects: The heart of the database. This is where all your file content and commit history are stored.hooks: They are event-driven scripts that Git executes when you run specific commands likecommit,push, ormerge.info: This is used to store repository-specific information and configuration, primarily for excluding certain patterns and housing supplementary objects.
Git Internal/git object
Git primary operates by using three primary objects.
Blob
It generally stores the raw content of the file. If it contain identical files , the content produce the same hash blob, disabling duplication.
Tree
It represents a directory structure. It contains references (hashes) to blobs and others trees
along with filenames and permissions.
Commit
Captures a complete snapshot of the project at a point in time. It points to a root tree, includes metadata (author, timestamp, message), and references parent commits to form a history chain.
What Happens During git add and git commit?
Let’s deep dive into the internal flow of .git folder
Step 1: git add
When you stage a file, Git:
Takes the current content of the file and then Hashes it to Creates a Blob in the objects/ folder.
Updates the index (staging area) to map the filename to that new Blob hash.
Step 2: git commit
When you commit, Git:
Creates a Tree object that represents the current state of the index and a Commit object that points to that Tree and the previous commit (the parent). Moves the pointer of your current branch (in refs/heads/) to this new Commit hash.
Why it’s important
Understanding Git is easier once you realize it is mostly about pointers and snapshots.
A branch is just a file that points to a commit. Switching branches simply moves that pointer.
When you run git add, Git already stores your data as objects, even before you commit.
And Git doesn’t rebuild your project by replaying every change—it just checks out the snapshot stored in the commit’s tree.
Conclusion
Git is a content-addressed key-value store where objects are indexed by SHA-1 hashes.
The version control features are built on top of this storage model.
Understanding .git reveals how Git actually works.

