DEV Community

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

Posted on

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

Fellow coders, I'm excited to deep dive into git's ineternals, what is the look and feel of git?

Git can feel like megical blackbox which stores the project’s details in memory. Let’s build

something awesome together — no rote learning.

We'll cover:

How Git works internally (the big picture).

What the .git folder is and why it matters.

Git's core objects: blobs, trees, and commits.

How Git tracks changes (with a peek at git add and git commit).

By the end, you'll visualize Git like a tree of snapshots, not a list of commands. Ready? Let's roll.

Why Does Git Exist?

Git exists because we need to track the changes of the software ,we are working. The story begins with

Linus Tovalds, the creator of Linux , He was working on linux kernel. Linus and the linux community were using a proprietary version control system called Bitkeeper(a Centralised version control system). However, this tool contains the limitations and was not open source, which conflicted with the open source philosophy of Linux community.

Linus became frustrated with these limitations and the dependency on a proprietary tool. In 2005, when the free use of Bitkeeper was revoked, linus decided to create new version control syste that would be fast ,efficient and open source. He wanted the system that can track the large linux version and support distributed workflows. This ,git was born. It was designed to be a destributed version control system, allowing multiple developers to collaborate on single project.

This frustration and need for better solution led the development of git,which become most powerful version control system. The other reason of frustration, he doesn’t decide to pay ,he was frustrated with slow, centralized version control systems like CVS or SVN.

Step 1- Understanding the .git folder: Your project’s secret vault

Git doesn’t mess with your project’s files , it keeps separate code files — .py files, .js ,.

tsx and .java.
Your metadata (commit history, branch details and changes) stored inside the .git folder.

Have you ever wondered what happens when we type the git init command in the terminal? It's fascinating! A hidden folder called .git is created, and it holds all the project's metadata. Isn't that intriguing?.

What's Inside the .git Folder?

Inside the, .git folder lies the heart of your git repositoty, a hidden store that stores all essential metadata and history of the project.
While I was working on project just iniialized a git repo using the git init command. It will create the hidden

git folder , open it up,the pro tip type command — ls -a that you will show you the hidden folder on your terminal. While I was working on the project , then discovered the key components:

1-Head: This file pointed to the current branch Bhupesh was working on. It was like a google lens, guides you latest details of the project.

2-config: Bhupesh found a configuration file that contains settings mandatory to his repository. Such as remote repository urls and user information. It was control panel for git operations.

3- objects: Object folder inside .git stores the information — commit as a unique object, that is 40 character SHA-1 hash

4- refs: Bhupesh , when he is working on his projects , he founds the references to branches.If we open the file then we will found the name of the branch.

5-logs: We store every actions which we took on repo ,you can check your logs, using git log.

6-Index: This file acts as staging area, keeping track of changes Bhupesh was preparing to commit.

what happens internally during git add and git commit?
when we run the git add app.js ,so this action redirects our changes to staging area , staging area is a temperatary memory location, also called the index. Here the changes gathered before the commit.

Git commit watches the staging area and this is a procedure which is performed with help of git command

git commit -m “moving to permanent memory“ , git commit .

There are three stages of git:

1- Working Directory— The folder where you actively make changes to your files. It’s your playground where you edit,add or delete files.

2- Staging Area- After making changes , you use the git add command to move these changes to the staging area. Think it is similar to your table where you keep your books after obtaining from the library.
So , understand it , after a time you return your book to library and this is temperatary memory where you keep the changes, if you switch the branch without commit ,you may loose the changes.

3-Repository (Commit History):

Once you are stisfied with the changes in the staging area, you run git commit -m “your project details wll be there: just like documented read me md ,what changes you have made in the project.“

You use the git commit to save the changes permanently in the repository, this creates a selfie/snapsot of your project and you can refer back to later. (.git/objects): Permanent history.

how Git uses hashes to ensure integrity

Git uses hashes , Explain how Git uses hashes to ensure integrity. It presents the each file and commit with help of unique hash code. Hash is generated based on the content of the object, even small change in repo presents the completely different hash. This technique allows git to detect any unauthorized changes to the code. If content of file is changes then we get the different hash which shows the content of data is modified.
You can think it just like HashCode generation algorithm in collection framework in Java.

The similarity is basically that both in Git and in Java, a hash code is used as a kind of unique identifier. In Git, when you make a commit, the hash code that’s generated is like a unique fingerprint based on the commit’s content and metadata. Similarly, in Java, a hash code is a unique number used to identify an object, so you can tell it apart from other objects.

In short, both are ways of giving something a unique ID so it can be recognized uniquely.

Your Git Mental Model:

Imagine you are writing a book . Each chapter you write like commit in git. When you finish a chapter ,you take a snapshot of it and store in a safe place (permanent storage) — a cupboard or wardrobe.

If you decided to make any changes to your chapter , you need to navigate to that page(branch) , update it add this page number for example 815 to staging area and once you are ready, you take another snapshot (commit) and add it to your collection of chapters(the repository).

Now think of .git folder is a secret diary which you store the important details about your project. Object folder stores the commit information about the project and config folder stores the auther information inside the project.

how Git uses hashes to ensure integrity

Git uses hashes , Explain how Git uses hashes to ensure integrity. It presents the each file and commit with help of unique hash code. Hash is generated based on the content of the object, even small change in repo presents the completely different hash. This technique allows git to detect any unauthorized changes to the code. If content of file is changes then we get the different hash which shows the content of data is modified.
You can think it just like HashCode generation algorithm in collection framework in Java.

The similarity is basically that both in Git and in Java, a hash code is used as a kind of unique identifier. In Git, when you make a commit, the hash code that’s generated is like a unique fingerprint based on the commit’s content and metadata. Similarly, in Java, a hash code is a unique number used to identify an object, so you can tell it apart from other objects.

In short, both are ways of giving something a unique ID so it can be recognized uniquely.

Your Git Mental Model:

Imagine you are writing a book . Each chapter you write like commit in git. When you finish a chapter ,you take a snapshot of it and store in a safe place (permanent storage) — a cupboard or wardrobe.

If you decided to make any changes to your chapter , you need to navigate to that page(branch) , update it add this page number for example 815 to staging area and once you are ready, you take another snapshot (commit) and add it to your collection of chapters(the repository).

Now think of .git folder is a secret diary which you store the important details about your project. Object folder stores the commit information about the project and config folder stores the auther information inside the project.

Summary:

Git is a tool that helps track changes in software projects.

Purpose of Git: Created by Linus Torvalds to manage changes in the Linux kernel, Git is a fast, efficient, and open-source version control system.

.git Folder: This hidden folder stores all the important information about your project, like commit history and branch details.

Key Components:

HEAD: Points to the current branch you're working on.

Config: Contains settings like remote repository URLs and user info.

Objects: Stores commits as unique objects with a 40-character hash.

Refs: Holds references to branches.

Logs: Records actions taken on the repository.

Index: Acts as a staging area for changes before committing.

Stages of Git:

Working Directory: Where you make changes to files.

Staging Area: Temporary space where changes are prepared for commit.

Repository: Permanent storage of committed changes.

Hashes: Git uses unique hash codes to ensure the integrity of files and commits, detecting unauthorized changes.

Think of Git as a way to take snapshots of your project, allowing you to track and manage changes over time.

Top comments (0)