DEV Community

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

Posted on • Edited on

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

Most developers use Git every day, doing - git init , git add, git commit but very few of them know how git actually handles all these commits internally

In this blog, we will look inside Git , how it works internally, and explore the important role of the .git folder

What is Git? (Quick Recap)

Git is a version control system that helps developers track changes, collaborate, and manage project history efficiently.

If you want to learn more about Git you can read ๐Ÿ‘‡ this blog too.

How Git Works (Internal View)

As you know, Git works in three main areas:

1. Working Directory - Your actual project files
2. Staging Area - Where changes are prepared
3. Repository - Where Git permanently stores snapshot

Every time you commit, Git :

  • Takes a snapshot of all tracked files

  • Stores it safely inside the .git directory

  • Links commits together

Git internal

The Heart of Git: The .git Folder

When you run:

git init
Enter fullscreen mode Exit fullscreen mode

Git creates a hidden folder inside the root folder. Believe me, itโ€™s thereโ€”you just canโ€™t see it.

๐Ÿ‘‰This folder is the entire brain of Git.
If it's deleted, your project is no longer a Git repository.

What's inside the .git Folder?

Lets see what's inside our Git folder ๐Ÿ‘‡

Git inside

๐Ÿ‘‰ You can also see this-just do

cd .\.git\
ls
Enter fullscreen mode Exit fullscreen mode

Here are the most important parts:

  1. objects - Where Everything is Stored

Git internal

This folder stores all Git data:

  • Commits

  • Files

  • Directory structures

git objects

Note: git log shows only commit objects, while .git/objects stores commit trees and blobs which is why more directories exist than commits

  1. refs/ - Branch & Tag Pointers

git refs

This folder keeps references to:

  • Branches (refs/heads) :- Points to the latest commit of that branch

  • Tags (refs/tags) :- Tags mark a specific commit

Each branch is just a pointer to a commit hash.

git refs

  1. HEAD โ€” Where you are Right Now

The HEAD file tells Git:

  • Which branch you are on

  • Which commit is currently checked out

git HEAD

  1. index โ€” The Staging Area

This file stores:

  • What you added using git add

  • What will go into the next commit

It acts as a bridge between the working directory and the repository.

  1. config โ€” Repository Settings

Contains:

  • Remote URLs

  • User configurations

  • Repo-specific settings

git config

How a Commit Works Internally

First, we have to look at what's inside our commit that makes Git easy to track and mark changes.

git commit

Now, understand when you run

// Initialize a Git repository
git init

// Stage changes
git add .

// Commit the staged changes
git commit -m "message"
Enter fullscreen mode Exit fullscreen mode

Git does this:

  • Initializing git in your repository

  • git add stores it as a blob object in .git/objects/

  • git commit -m "message" builds a tree object

  • Now Git creates a commit object containing ๐Ÿ‘‡

  1. Pointer to the root tree
  2. Pointer to the parent commit
  3. Author & committer info
  4. Timestamp
  5. Commit message

git commit

Image credit: Photo by freeCodeCamp

Final Thoughts

Git may look simple on the surface, but internally its a powerful database . The .git folder is not just a config folder , its the entire history and structure of your project.

Thanks for reading ! if enjoyed this blog , you can read more on this ๐Ÿ‘‡

Top comments (0)