Skip to main content

Command Palette

Search for a command to run...

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

Updated
3 min read
Inside Git: How it works and the role of .git folder

Git is one of the most widely used tools in software development. Many developers learn git by memorizing the commands without truly understanding what git is doing behind the scenes.

This article explains how git works internally. We will focus on the .git folder, git objects, and how git keep tracks of our changes.

How Git Works Internally (The Big Picture)

At its core, Git is not just a set of commands - it is a database for our project.

Instead of storing “changes between files,” Git stores:

  • Snapshots of project state

  • Indexed by hashes

  • Connected through objects

Every commit, branch, and tag is simply a reference to objects inside the .git directory.

If you understand the .git folder, you understand Git.

Understanding the .git Folder

The .git directory is the entire Git repository. Your working directory is just files. .git is where Git stores everything it knows about the project.

If the .git folder is deleted, the project instantly loses all version control history.

Key Responsibilities of .git

  • Store all versions of tracked files

  • Maintain commit history

  • Track branches and tags

Simplified .git Directory Structure

.git/
├── objects/     # All Git objects (data store)
├── refs/        # Branch and tag pointers
├── HEAD         # Pointer to current branch
├── index        # Staging area
├── config       # Repository configuration

Commit — A Snapshot in Time

A commit ties everything together.

A commit contains:

  • A reference to a tree (project snapshot)

  • Author and timestamp

  • Commit message

  • Reference to parent commit(s)

A commit is not a diff.
It is a complete snapshot of the project state.

How Git Tracks Changes

Git does not track changes line-by-line like traditional version control systems.

Instead:

  1. Git stores snapshots

  2. Unchanged files reuse existing blobs

  3. Only new content creates new objects


What Happens Internally During git add

https://phoenixnap.com/kb/wp-content/uploads/2021/09/git-workflow.png

https://miro.medium.com/1%2AdiRLm1S5hkVoh5qeArND0Q.png

When you run:

git add index.html

Internally:

  1. Git reads index.html

  2. Creates a file from its content

  3. Stores the file in .git/objects

  4. Updates the index (staging area):

Key Insight

git add does not create a commit.
It updates the staging snapshot.


What Happens Internally During git commit

https://git-scm.com/book/en/v2/images/data-model-3.png

When you run:

git commit -m "Initial commit"

Internally:

  1. Git reads the index

  2. Builds a tree representing the directory structure

  3. Creates a commit pointing to that tree

  4. Updates the current branch pointer

  5. Moves HEAD to the new commit

Why This Understanding Matters

When you understand Git internally:

  • Merge conflicts become logical

  • Rebasing stops feeling dangerous

  • Detached HEAD is no longer confusing

  • Git errors become debuggable, not scary

You stop memorizing commands—and start reasoning about state.

1 views