Skip to main content

Command Palette

Search for a command to run...

#5 Inside Git: How It Works and the Role of the .git Folder

Updated
3 min read
P
Since I am developer. My blogs are related to Tech and also blogs write during web dev cohort by chai code.

Most developers use Git every day, but few look under the hood. While it feels like magic, Git is actually a very simple content-addressable filesystem. Everything Git knows about your project is stored in one place: the hidden .git folder.

1. What is the .git Folder?

When you run git init, Git creates a hidden directory in your project root. If you delete this folder, your project remains, but your entire version history is gone. It is the "brain" of your repository.

Inside, you'll find:

  • objects/: The database where all your file content and history are stored.

  • refs/: Pointers to commits (like where main or feature branches are).

  • HEAD: A file that points to the branch you are currently working on.

  • index: The binary file that represents your Staging Area.

2. The Three Core Objects

Git doesn't save "changes" (deltas); it saves snapshots. To do this, it uses three main types of objects, all stored in the objects/ folder.

The Blob (Binary Large Object)

A Blob stores the content of a file. It doesn't care about the filename or the date; it only cares about the data inside. If two different files have the exact same text, Git only stores one Blob.

The Tree

A Tree is like a directory. It maps filenames to Blobs. It tells Git: "This folder contains a file named style.css (which is this Blob) and a subfolder named images (which is another Tree)."

The Commit

A Commit is a snapshot of the root Tree. It includes:

  • A pointer to the main Tree object.

  • The author and timestamp.

  • A pointer to the parent commit (creating the "chain" of history).

3. The Power of the Hash (SHA-1)

Git identifies every object using a 40-character SHA-1 hash (e.g., 5d691f...).

  • This hash is generated based solely on the content.

  • If you change even a single comma in a file, the hash changes completely.

  • This ensures data integrity: it is impossible to change a file's content without Git noticing.

4. What Happens Internally During Commands?

When you run git add

  1. Git takes your file content and creates a Blob.

  2. It calculates the SHA-1 hash of that Blob.

  3. It updates the index file (Staging Area) to record that "this filename now points to this new Blob hash."

When you run git commit

  1. Git creates a Tree object representing the state of the index.

  2. It creates a Commit object that points to that Tree.

  3. It moves the HEAD pointer of your current branch to this new commit hash.

5. Building the Mental Model

Instead of thinking of Git as a list of changes, think of it as a Map of Hashes:

  • Commit → points to Tree

  • Tree → points to Blobs

  • Branch → is just a text file containing a Commit Hash

This is why Git is so fast. To switch branches, Git doesn't "calculate" anything; it just swaps the files in your folder to match the hashes listed in the Tree of the branch you're switching to.