DEV Community

Cover image for Inside Git: How It Works and the Role of the .git Folder
Abhimanyu Kumar
Abhimanyu Kumar

Posted on

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

when i run Git commands like git add or git commit, everything feels simple.
But behind the scenes, Git is doing a lot of serious work.

The real magic of Git lives inside the hidden folder called .git.

If you truly want to understand Git, Not just use it you must understand what happens inside this folder.

Let's open Git from the inside.


The Moment Git Comes Alive

Imagine you have a normal project folder.

myProject

Now run this command

  • git init

suddenly , a hidden folder appears That is:

.git/

This single folder turns your project into Git repository.

  • If the .git folder is deleted, Git forgets everything.
  • But your file remain, but Git memory is gone.

This tells one thing:

Git is not the files -- Git is the history stored in .git.

Now, What is the .git Folder Really?

Here we will think like as a Git brain.

It stores:

  • Every commit you have ever made.
  • Every file version.
  • Every branch.
  • Every message.
  • Every pointer.
  • The complete timeline of your project.

Let's Enter the .git folder

Inside .git, you'll see something like this:

Each part has a clear responsibility

HEAD - Where You Are Right Now

Head is a pointer.

it tells Git:

"This is the current commit you are working on."

usually, it points to branch like main.
When you switch branches like main.

When you switch branches or checkout a commit, HEAD moves.
That's how Git always knows your current position in history


objects/ - The heart of Git ❤️

This is the most important directory.

Everything in Git becomes an object and is stored here:

  • File contents
  • Folder Structure
  • Commits
  • Messages

Git uses hashes (SHA-1) to store objects.

That means:

  • Same object -> same hash
  • No duplication
  • Data integrity guaranteed

Git doesn't care about filenames here -- only content.


index -> The Stagging Area

When you run:

git add .

Changes are written to the index file.

This file represents:

"What will got into the next commit"

It’s a buffer between working files and permanent history.

  • That’s why Git allows:
  • Partial commits
  • Clean control
  • Intentional snapshots

refs/ -> Branches & Tags

This folder stores references to commits.

refs/heads/master

This file contains a commit hash.
So a branch is not a copy of code
it’s just a pointer to a commit.
That’s why branches are:

  • Lightweight
  • Fast
  • Cheap to create

logs/ -> Git's Memory Timeline

Every movement of HEAD is logged here.
This is how Git can:

  • Recover lost commits
  • Undo mistakes
  • Support git reflog Even if you “lose” a commit, Git still remembers it — temporarily.

config – Repository Settings

This file stores:

  • Repo-specific configuration
  • Remote URLs
  • User preferences

It’s local to the repository and overrides global settings.


What Happens When You Run Git Commands?

Let’s connect commands to internal behavior.

git add

  • Updates the index
  • Does not create history

git commit

  • creates a new commit object
  • Stores snapshot in objects/
  • Moves branch pointer
  • Updates HEAD

git checkout

  • Moves HEAD
  • Updates working directory

git log

  • Reads commit objects
  • Follows parent links

Why Git Is So Fast

Because:

  • Files are hashed
  • Objects are immutable
  • No duplication
  • Everything is local

Git doesn’t ask a server:

“What changed?”

Git already knows.

Why Deleting .git Is Dangerous

If you delete .git:

  • All commits are gone
  • All branches disappear
  • History is erased

Your files survive — but Git becomes blind.

That’s why .git is sacred.


Git is not magic things
it is:

  • Smart data structures
  • Clear pointers
  • Immutable snapshots
  • Careful tracking

Once you understand .git, Git commands stop feeling scary.

You stop memorizing commands
and start reasoning about behavior.

Top comments (0)