Skip to main content

Command Palette

Search for a command to run...

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

Updated
3 min read
Inside Git: How It Works and the Role of the .git Folder

Hey Everyone,

In the previous blog, I talked about Git, what it is, and what it does. If you haven’t checked it out yet, give it a read for a better introduction. But in short, GIT is a version control system that saves snapshots to keep the history of the project, which will help you to keep track of the project, whether you are working solo or as a whole team.

🙋🏼‍♀️ Wait a second, you said that it stores the snapshots. How it does stores questionq❓❓❓

Good question.

Normally, when we have to store something, there are different ways such as using SQL(Structured Query Language)/ NoSQL(Not Only SQL), that will be on the central cloud server. That could work, but there is a big issue - as the codebase grows, the storage cost also grows. And as it is central than every developer will be talking with it, which will then make it slow, expensive, and inconsistent if offline.

Git solves this more smartly.

Git doesn’t rely on the central database to manage project history. Instead:

  • Every developer has their own full copy of the repository

  • Project history (snapshots) is stored locally

  • You can commit changes, branch, and explore without the internet

Ok, Thats good, but what abot .git folder? Yes, so as I told you that git store it locally that means when we run the below command in the project terminal

git init

The message below appears on the terminal

Initialized empty Git repository in /path/to/your/project/.git/

A new hidden .git folder has been created in your project folder.

All the things saved in this .git folder, like

  • all your snapshots

  • commit history

  • branches

Without this folder, there is no Git repository.

Now what is stored in these snapshots???

Git breaks your project into three main object types:

  1. Blob - It represents the content of the file

  2. Tree- It stores the filename, structures

  3. Commit- It represents whatthe project looks like in this current moment

Git does not store full copies of your entire project with every commit.

Instead:

  • Blobs get reused if the file does not change. Only modified files produce new blobs.

  • Trees reference the blobs, and commits reference the trees

.git folder is hidden by default because there is no need to directly interact with the.git folder. Actually, it's best to avoid touching it. The repository may be broken if you change or remove anything from it. All the history commits, tree etc will be gone forever.

And yeah, that is what is inside the .git folder. 😊

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!