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

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


When you start using Git, most of the time you only run commands like git add, git commit, or git push. Everything feels a bit magical. You type a command, and Git somehow remembers your changes forever.

But have you ever wondered what is actually happening behind the scenes?

In this blog, we’ll look inside Git, understand how it works internally, and learn why the .git folder is the most important part of any Git repository.


What Happens When You Run git init?

When you run the command:

git init


Git creates a hidden folder called .git inside your project directory.

This .git folder is the heart of Git.  

Without it, your project is just a normal folder.  

With it, your project becomes a Git repository.

Everything Git knows about your project is stored inside this folder.


Why the .git Folder Is So Important

The .git folder contains:

- Your complete project history

- All commits you have ever made

- Branch information

- Configuration settings

- References to different versions of your code


If you delete the .git folder, Git will forget everything about your project history.

That’s why you should never manually edit or delete files inside the .git folder unless you really know what you’re doing.


Understanding How Git Tracks Changes

Git does not track files the way many beginners think.

Git does NOT:

- Track changes line by line

- Automatically save every file


Instead, Git works in **three main stages**.


The Three States of a File in Git

Every file in Git lives in one of these three states:

1. Working Directory  

2. Staging Area  

3. Repository (Committed)


1. Working Directory

This is your normal project folder where you write and edit code.

When you change a file, Git knows that the file has been modified, but it does not save the change automatically.

At this stage, the file is only changed on your system.


2. Staging Area

When you run:

git add filename


You move the file to the staging area.

The staging area is like a preview area.  


You are telling Git:

“Yes, I want to save these specific changes.”

This allows you to control exactly what goes into the next commit.


3. Repository (Commit)

When you run:

git commit -m "message"


Git takes everything from the staging area and permanently saves it in the repository.

This saved version is called a commit. Once committed, your changes become part of Git history.


What Is Actually Stored Inside the .git Folder?

Let’s break down some important parts of the .git folder (conceptually).


Objects

Git stores everything as objects.


There are mainly three types:

- Blob objects (file content)

- Tree objects (folder structure)

- Commit objects (snapshots with metadata)


Git does not store differences; it stores snapshots of your project.


HEAD

HEAD is a pointer.


It tells Git:

- Which branch you are currently on

- Which commit you are working with


When you switch branches, HEAD simply points to a different commit.


Branches

Branches are not full copies of your project. They are just lightweight pointers to commits. This is why Git branches are fast and efficient.


How Git Is Different from Other Version Control Systems

Traditional systems store file differences.  

Git stores full snapshots.


Because of this:

- Git is very fast

- Branching is easy

- Merging is powerful

- History is reliable


This design is one of the biggest reasons Git is so popular.


Comments

Popular posts from this blog

Git for Beginners: Basics and Essential Commands

Why Version Control Exists: The Pendrive Problem