DEV Community

Cover image for Inside Git: How its works and the role of the .git folder
Debashis Das
Debashis Das

Posted on

Inside Git: How its works and the role of the .git folder

Inside Git: How its works and the role of the .git folder

When most beginners learn Git, they focus on the commands like:

git add
git commit
git push
Enter fullscreen mode Exit fullscreen mode

But Git is not magic
Behind the scene Git is just files, folders and hashes working together in a very smart way.

To truly understand Git, you need to understand what happens inside the .git folder.

This article will help you to build a mental model of Git, not just memorize commands.


1. How Git works internally (Big picture)

At a high level, Git does only three main things:

  1. It stores snapshot of your project
  2. It track changes using hashes
  3. It connects snapshots together

Important thing to remember:

Git does not track changes line by line
Git tracks snapshots of files

Every time you commit, Git saves a snapshot of your project's state


2. Understanding the .git Folder

When you run:

git init
Enter fullscreen mode Exit fullscreen mode

Git creates a hidden folder called .git/:

.git/
Enter fullscreen mode Exit fullscreen mode

This folder is the heart of Git.

πŸ”‘ Key idea

If .git folder is deleted, your project is no longer a Git repository and Git cannot track your code.


What is inside the .git folder?

Conceptually it contains:

  • Git configuration
  • Commit history
  • Branch information
  • All version of your files

You usually don't open this folder -- but understanding it makes Git click.


πŸ—‚ Diagram: Structure of the .git Directory

Typical important folders inside .git:

.git/
β”œβ”€β”€ objects/
β”œβ”€β”€ refs/
β”œβ”€β”€ HEAD
β”œβ”€β”€ index
└── config
Enter fullscreen mode Exit fullscreen mode

Let’s understand only what matters for beginners πŸ‘‡


3. Git Objects: Blob, Tree, Commit

Git stores everything as objects.
There are three main object types.


1️⃣ Blob (File Content)
  • A blob store the content of a file.
  • File name is not store here
  • Same content = same hash

Example:

hello.txt β†’ "Hello World"
Enter fullscreen mode Exit fullscreen mode

Git store "Hello World" as a blob object.


2️⃣ Tree (Folder Structure)
  • A tree represents a directory
  • Its maps:
    • file names β†’ blob hashes
    • folder names β†’ other tree hashes

Think of a tree as:

"This folder contains these files and subfolders"


3️⃣ Commit (Snapshot + History)
  • A commit object contains:
  • Reference to tree
  • Reference to the parent commit
  • Author information
  • Commit message
  • Timestamp

A commit answers:

"What did the project look like at this moment?"


🧩 Diagram: Relationship Between Commit, Tree, and Blob

This diagram helps readers visualize:

Commit
  ↓
 Tree
  ↓
Blob (file content)
Enter fullscreen mode Exit fullscreen mode

4. How Git Tracks Changes

Git does not modify files directly when you commit.

Instead of it:

  1. Store file content as blob
  2. Connects them using tree
  3. Saves a commit pointing to the tree

If a file does not change:

  • Git reuses the old blob
  • No duplicate storage

That's why Git is:

  • Fast
  • Efficient
  • Reliable

5. What happen during git add

Let's say you edit a file:

app.py
Enter fullscreen mode Exit fullscreen mode

Now run:

git add app.py
Enter fullscreen mode Exit fullscreen mode

Internally Git does this:

  1. Reads the file content
  2. Create a blob object
  3. Store it in:

    .git/objects/
    
  4. Updates the index (staging area)
    Important mental model:

git add does not create commit
It prepares data not the next commit


6. What Happens During git commit

Now you run:

git commit -m "Add login feature"
Enter fullscreen mode Exit fullscreen mode

Git now:

  1. Reads the staging area
  2. Creates a tree object
  3. Link to the previous commit
  4. Moves HEAD to the new commit

πŸ”„ Diagram: Internal Flow of git add and git commit
Working Directory
      ↓ git add
Staging Area (index)
      ↓ git commit
Commit (snapshot)
Enter fullscreen mode Exit fullscreen mode

7.How Git Uses Hashes for Integrity

Git uses SHA-1 hashes (40 character string).

Example:

e83c5163316f89bfbde7d9ab23ca2e25604af290
Enter fullscreen mode Exit fullscreen mode

Git uses hashes to:

  • Identify uniquely
  • Detect file corruption
  • Ensure data integrity

Why this is powerful?

If:

  • File content changes β†’ hash changes
  • Commit data changes β†’ hash changes

So Git can detect tampering or corruption instantly.

This makes Git extremely trustworthy.


8. Building the Right Mental Model of Git

Instead of thinking:
❌ "Git is a set of commands"

Think this way:
βœ… "Git is a content-addressable database of snapshots"

Commands like init, add, commit, log, status, branch and checkout are just ways to interact with that database.


Final Thoughts

Understand Git internally gives you confidence.

Now you know:

  • Why the .git folder exists
  • How Git stores files
  • What commits actually are
  • Why Git never loses history

Once this mental model is clear:

  • Git errors feel less scary
  • Commands make more sense
  • You stop memorizing and start understanding

Git is not magic, it's just a very well designed.πŸš€

Top comments (0)