Skip to main content

Command Palette

Search for a command to run...

The Mystery of .Git Folder

Explained using Strict Principal & The Board Exams

Updated
10 min read
The Mystery of .Git Folder
K

An enthusiast who wants to learn new things and try to pass on the same through articles on those topics in simple words.

In the last two articles, first we talked about “What is Version Control and Why it was Required” and then we talked about the most used version control software “The Git” and we saw how to install it and were introduced to some of the most basic commands which one should be aware of while starting with Git.

In this article, we would now move our learning journey ahead and get a little bit deeper and understand what exactly is happening behind the scene when we type all those command like git init, git add and git commit.

For many developers, Git is still a black box. They memorize a few magical incantations (git add ., git commit -m "saved"), cross their fingers, and hope nothing explodes. But when things go wrong and when they get a "detached HEAD" or a "merge conflict"—the magic spells stop working, and panic sets in.

But Git isn't magic. It is a database. And interestingly enough, it functions almost exactly like a strict Indian School Administration. Yes, our very own school, of which we know a lot about.

So, to make it simple, i’ll try and explain you what .git folder is and how it is functioning.

If you remember the discipline of our school days - the "Strong Room" for exam papers, the "Attendance Register" that couldn't be fudged, and the terrified respect for the Principal - you already understand how Git works.

In this guide, we are going to perform open-brain surgery on a repository. We will build "The Git Public School" from scratch to understand exactly what happens inside that hidden .git folder.


Part 1 : The Bhoomi Pujan (git init)

Let's start by buying a plot of land for our project. ( Simply create a project folder after opening VS Code or any editor of your choice)

Open your terminal and create a folder:

mkdir project_school
cd project_school

Right now, this is just an empty plot of land. To turn it into a school, we need to construct the administration block and hire some people for doing the job of administrating.

This is what git init command does.

git init

When you type git init the .git folder gets created with many files and sub-folders. But at present they are all empty. So let us understand whats there inside the admin block.

The Admin Block

When you run that command, Git creates a hidden folder called .git. This is the Admin Block. It is a restricted area - where students (ie you as a developer) are strictly forbidden from entering without permission.

Inside this block, Git constructs the infrastructure:

  1. .git/config (The School Almanac/Diary):

    This contains the rules of the school. It lists the user's name and roll number (email). Every student must carry this ID.

  2. .git/HEAD (The Peon - Ramu Kaka):

    Ramu Kaka is the most important person in the school. He is the only one who knows exactly where the Principal is currently standing. If you ask him, he points to a specific class (Branch) on the notice board.

  3. .git/refs/heads (The Class Signboards):

    These are the signs above the classroom doors, like "Class: Main" or "Class: Feature-X". Right now, the signs are up, but the rooms are empty.

  4. .git/objects/ (The Strong Room):

    This is the terrifying, double-locked vault in the basement. This is where the exam papers go. Once something enters the Strong Room, it can never be modified. It is immutable.

What you see:

  • config, HEAD, description: The core configuration files are created.

  • objects directory: Contains empty info and pack subdirectories. This is where your data will eventually live.

  • refs directory: Contains empty heads and tags subdirectories. This is where your branches and tags will be stored.

Explanation of Files & Folders at this stage:

  • config: A text file containing settings specifically for this repository. By default, it includes core settings like the repository format version and whether it's a "bare" repository. You can edit this with the git config command.

  • HEAD: This is a crucial text file that points to the current branch you are working on. In a new repository, it usually contains the text ref: refs/heads/main .

  • description: A file used by the GitWeb (like Github) program to display a description of the repository. You can usually ignore this unless you are setting up a public web interface for your Git server.

  • hooks/ (folder): This folder contains example scripts (ending in .sample, like pre-commit.sample) that you can use to automate actions at certain points in Git's workflow. At this stage, they are just inactive templates. You can create your own scripts and store them here with name pre-commit but without .sample extention.

  • info/ (folder): Contains an exclude file, which acts like a .gitignore file but is not shared with other collaborators. It’s for your personal ignore rules.

  • objects/ (folder): This is the "database" of your repository. Right now, it's an empty shell. It contains two empty subfolders, info/ and pack/, which will later be used to optimize storage. There are no actual "objects" (Blobs, Trees, or Commits) in it yet.

  • refs/ (folder): This folder will hold the pointers to your branches and tags. At this initial stage, the heads/ (for branches) and tags/ subfolders are completely empty because you haven't created any commits or branches.


Part 2 : Inside the Strong Room (The 3 Objects)

Before we write any code, we must understand the three things allowed inside the Strong Room (.git/objects). Git doesn't store files the way your hard drive does, it stores them as distinct objects.

1. The Photocopy Answer Sheet (The Blob)

In this school, the administration never keeps the original answer sheet.

  • When you submit an answer sheet, the school takes a Xerox copy of the content.

  • This copy is locked in the Strong Room.

  • Crucial Detail: The copy has no name on it. It is anonymous. It is just raw content of what you wrote in the answer sheet.

  • Why? If two students write the exact same answer (e.g., two files with the same text), the school only needs to store one photocopy.

2. The Tied Bundle (The Tree)

You can't have thousands of anonymous papers flying around.

  • The Class Teacher ties all the answer sheets for a specific exam together with a Red Thread.

  • This bundle creates a map : "The file named 'maths.txt' corresponds to Photocopy #a1b2c."

  • This represents your folder structure.

3. The Sealed Envelope (The Commit)

This is the official record.

  • The Principal takes the Tied Bundle, puts it in a Yellow Envelope, and seals it with red wax.

  • He stamps it with the Author's Name (Invigilator), the Date, and a reference to the Previous Envelope (the last exam).


Part 3 : The Exam Submission (git add)

Now lets understand what happens when you type git add. Imagine school is in session and here comes your first exam.

echo "2 + 2 = 4" > maths.txt

At this moment, the paper is lying on your desk (your Workspace/VS Code file). The school administration doesn't know it exists. It is "Untracked."

To submit it, you must hand it to the invigilator. And to do that you have to raise your hand (git add) and call the invigilator to collect your exam sheet.

git add maths.txt

The "Submission" Phase

This is where most beginners get confused. git add is not just "making a list." It is a physical action.

  1. The Xerox (Blob Creation):

    The Invigilator takes your paper, runs to the xerox machine, makes a copy, and locks it in the Strong Room immediately.

    • Technical note: A Blob object is created in .git/objects. It just contains your answers ie code.
  2. The Attendance Register (The Index):

    The Invigilator opens the big, red-bound Attendance Register (The Staging Area).

    • She finds your row and puts a Tick Mark.

    • She writes down the ID code of the photocopy she just locked away.

    • She says: "I have formally received the content for maths.txt."

Key Takeaway : The "Staging Area" (Index) is literally just the Attendance Register. It tracks which students have successfully submitted their papers to the Strong Room for this exam.

GIT ADD Command

Now, The next image shows the changes in the .git folder after you have created the hello.txt file and run the command git add hello.txt. When you type git add hello.txt the changes that occur are as follows:


Part 4 : Result Declaration Day (git commit)

Now that the assignments are collected, the Principal must sign off on them to make them part of permanent history.

git commit -m "Result Math Exam"

Sealing the Record

When you run this command, the Principal (Git) steps in to finalize the batch.

  1. Tying the Bundle (Tree Creation):

    The Principal looks at the Attendance Register. He sees that maths.txt is ticked. He takes all the corresponding photocopies from the Strong Room and ties it into a Bundle (Tree Object).

  2. Sealing the Envelope (Commit Creation):

    He takes that Bundle and slides it into a fresh Yellow Envelope.

    • He writes the message: "Result Math Exam".

    • He stamps the date and time.

    • He seals it with wax. This envelope is now Commit Object #f8a9c...

  3. Ramu Kaka Updates the Board (HEAD Update):

    Finally, Ramu Kaka (the Peon) walks over to the Main Notice Board.

    • He erases the old record.

    • He writes: "The official current record for Class 'Main' is Envelope #f8a9c."

Now, if anyone asks "What is the state of the project?", Ramu Kaka points to that specific Yellow Envelope.

GIT COMMIT Command

And after you type git commit -m "First save" , the changes that happen in the .git folder are :


Before we move to the next part, here is a summary of the three images you've seen:

  1. git init: Created the skeleton .git folder structure.

  2. git add hello.txt: Added the index file (staging area) and a Blob object containing the file's content.

  3. git commit -m "First save": Created a Tree object (snapshot of the directory) and a Commit object (metadata), and updated the main branch pointer.

This sequence perfectly illustrates how Git builds up its internal database piece by piece , same as we discussed in the school analogy. And now have a visual understanding of the "hidden brain" of your repository!


Part 5 : The Integrity (Why you can't cheat)

Why is Git so popular? Because, like a strict Indian school, you cannot cheat.

In a normal folder, you could secretly change a file and no one would know. But in this school, every single photocopy in the Strong Room has a unique ID (SHA-1 Hash) generated from its content.

  • If you try to change 2 + 2 = 4 to 2 + 2 = 5 in the past history...

  • The Photocopy ID changes.

  • So, the Bundle (Tree) that holds it must change.

  • So, the Yellow Envelope (Commit) that holds the bundle must change.

  • So, the Principal's stamp is broken.

The entire chain breaks. You cannot use "Whitener" (correction fluid) on the school records without the Principal finding out.


Summary Cheat Sheet

Technical TermIndian School AnalogyThe Function
.git FolderThe Admin BlockRestricted area. No students allowed.
BlobPhotocopy Answer SheetRaw content. Anonymous. Stored in Strong Room.
TreeRed Thread BundleFolder structure. Maps filenames to Photocopies.
CommitSealed Yellow EnvelopeOfficial record with timestamp and author.
IndexAttendance RegisterThe list of "What is ready to be sealed."
HEADRam Kaka (Peon)The pointer. He knows which Envelope is current.

Hopefully, i was able to explain the .git folder and what it contains. The actual folder looks like this when you run git init for the first time in the project:

Now next time you are confused about the git commands and what they are doing, just imagine Ramu Kaka standing by the notice board, waiting for you to hand him the next Sealed Envelope. It makes the whole process a lot less intimidating.

If you found this article helpful, share it with others and feel free to leave your feedback—I’d love to hear your thoughts!