DEV Community

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

Posted on

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

Most if the developers uses Git everyday doing - git init , git add , git commit but very few of them knows how the git actually handle all these commit internally

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

What is Git? ( Quick Recap )

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

If you want to know further 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 permananetly stores snapshot

Everytime you commit , Git :

  • Takes a snapshot of all tracked files

  • Stores it safely inside .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 your root folder. Believe me, it’s there β€” you just can’t see it.

πŸ‘‰This folder is the entire brain of Git
If its deleted, your project is no longer a Git repository

What's inside the .git Folder ?

Lets see whats inside our Git folder πŸ‘‡

Git inside

πŸ‘‰ You can also see this just simply 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 lastest 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 working directory and 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 whats inside our commit that makes Git easy to track changes and mark the changes.

git commit

Now understand when you run

// Initialize 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 to 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 yo the root tree
  2. Pointer to 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)