Skip to main content

Command Palette

Search for a command to run...

Inside Git : How it works & the role of the .git folder

Published
3 min read

Most of us treat Git like a black box: remember the Git command like Git add,Git commit, Git push and that's it and we think this is it.

But we don't actually understand the working of Git, like where does the file go? How Git track our every changes?

So in this article, we will explore:

  • What is .Git folder & why it exist

  • Break down of git object

  • Internal working of git command

  • How git uses hashes to ensure integrity

What is .git Folders & why does it exist?

The .git folder is the heart of a git repository. It is the hidden directory that uses to store all the information necessary for managing your projects version control history.

Git need a place to →

  1. Store snapshot of files

  2. Track relationship between commit

  3. Ensure data integrity using hashes

  4. Move between version instantly

The .git folder exist so git can do all of this locally, without relying on a server.

BREAK DOWN OF GIT OBJECT

let understand the git data model , ai it's core git is a content-addressable filesystem . It store the data as objects and each object is identified by SHA-1 hash of it's content

When we add a file to git , it give us hexadecimal sha-1 hash of the file and this hashes become 5he file unique address in Git Database.

Git organizer's everything into object Stored in it's Database. There are four types of object, but three are essential for understanding how git works:

  • Blob

  • Trees

  • Commit

So, let's understand what is blob , a blob( binary large object) store raw contents of a file . Just the contents. No filename, no permission, no timestamps, no Metadata of any kind.

When we add a README.md to git, it creates a blob containing the file text. That blob has no idea it came from the file called Readme.md it is simply a chunk of bytes with a SHA-1 hashes computed from those bytes.

When we run

Git add file.txt

What git does is

  • Compressed the file content

  • Hashes it using SHA-1

  • Stores it as a blob in .git/objects

Now ,let's understand the tree , tree represent a directory. It contains pointer to blobs(file) and other trees (sub directory) , along with the filename and permission

The tree acts as a directory listing. It says “there is a file called README.md, and its contents are stored in blob 5dd01c17.” The blob stores the data; the tree gives it a name and location.

If you rename a file without changing its contents, Git creates a new tree (with the new filename) pointing to the same blob. The actual file data is not duplicated.

Now we have blobs for content and trees for structure. But version control needs more: who made changes, when, and why. That is what commits provide.

Now , let's see the about the commit

Commits: Recording History

A commit captures a complete snapshot of your project at a specific moment in time. Every commit contains:

A pointer to a tree (the root directory at that moment)

Pointers to parent commits (the history leading to this point)

Author information (who wrote the changes)

Committer information (who applied the changes)

Timestamps

A commit message explaining the changess

When you run git commit, Git creates a new commit object. This commit points to a tree representing your project’s current state. The parent pointer links back to the previous commit, forming a chain. This chain is your project’s history.