tech

Chapter 2: Git's Internal World — The Three States & Architecture

Understand Git's working directory, staging area, local repository, and the snapshot architecture behind everyday Git commands.

Chapter 2: Git's Internal World — The Three States & Architecture

Chapter 2: Git’s Internal World — The Three States & Architecture

Many developers get confused because they treat Git like a backup tool (like Google Drive). It is not. To command Git with confidence, you need to understand its three-stage pipeline and how it tracks snapshots under the hood.


📸 The Real-World Analogy: The Photography Studio

Imagine you run a professional photography studio. You want to take a family photo book.

┌─────────────────────────────────────────────────────────────┐
│ 1. The Stage          │  2. The Camera Frame │  3. The Album│
│                       │                      │              │
│  (Working Directory)  │    (Staging Area)    │ (Repository) │
│                       │                      │              │
│   Actors moving,      │   People posed,      │  Photo printed│
│   getting dressed.    │   framed, ready.     │  permanently.│
└───────────────────────┴──────────────────────┴──────────────┘
  1. The Stage (Working Directory): This is the physical floor of your studio. Family members are running around, changing outfits, putting on makeup, and moving chairs. This is chaotic and in-progress.
  2. The Camera Frame (Staging Area): You don’t want a photo of the chaos. You ask the parents and kids to sit on the couch. You look through the lens. Only the people in your viewfinder are “staged.” If a cousin is still in the dressing room, they are not in the frame.
  3. The Photo Album (Local Repository): You press the shutter button. Click! The camera captures exactly what was in the frame at that split second. The printed photo is placed in a photo album. You cannot change that photo now; it is a permanent record of that moment in time.

🛠️ The Three Local Zones

Let’s translate this photography studio directly into Git terminology:

                  git add               git commit
Working Directory ───────► Staging Area ──────────► Local Repository
  (Your workspace)          (Index draft)            (The .git Database)

1. The Working Directory

This is your project folder on your computer. When you open VS Code and write code, you are editing files in the Working Directory.

  • Git continuously scans this folder. If you edit a file, Git notices and labels it as Modified.
  • If you create a new file, Git labels it as Untracked (meaning Git doesn’t know about it yet).

2. The Staging Area (The Index)

The Staging Area is a binary index file located inside your .git folder. It is a preparation zone.

  • When you run git add index.html, you copy the current version of index.html into the Staging Area.
  • This allows you to build a commit incrementally. You can make changes to 10 files, but only add 2 of them to the Staging Area for your next commit.

3. The Local Repository (.git)

This is the heart of Git. When you run git init, Git creates a hidden directory named .git in your project folder.

  • This hidden folder contains the entire history database of your project.
  • Inside it are compressed file contents, history metadata, commit logs, and configuration settings.
  • When you commit, Git seals the Staging Area contents and writes it into this database.

💾 What Actually is a Commit?

When you type:

git commit -m "docs: Fix spelling mistake"

Git performs the following operations:

  1. Creates a Tree Object: It looks at the files in the Staging Area and groups them into a folder structure called a “tree.”
  2. Generates Metadata: It records:
    • Who wrote the code (your configured name and email).
    • The exact timestamp.
    • A commit message explaining why the change was made.
    • A reference to the parent commit (the previous commit in the timeline).
  3. Computes the ID (SHA-1 Hash): It hashes the tree, parent commit, and metadata to generate a unique 40-character commit ID (like d6a5f3...).
  4. Moves HEAD: Git moves the HEAD pointer to this new commit hash.

What is HEAD?

HEAD is a pointer that indicates your current active location in Git’s history timeline. Think of it as the “You Are Here” pin on a mall map. By default, HEAD points to the tip of your current active branch.


🔎 Under the Hood: The Hidden .git Folder

If you look inside the hidden .git/ folder, you will find these key files:

.git/
├── HEAD          # Points to the branch you currently have checked out (e.g., ref: refs/heads/main)
├── config        # Project-specific configuration settings
├── index         # The binary file representing the Staging Area
└── objects/      # The database. Stores all file contents (blobs), trees, and commits
  • Blobs (Binary Large Objects): When you run git add, Git compresses the content of your file and stores it as a “blob” in the objects/ directory. Git identifies files by their content, not their name! If two files in different folders contain the exact same code, Git only stores one blob.
  • Trees: Represent directory structures. They map filenames to the blob hashes.
  • Commits: Point to a specific tree and contain the commit description and author details.

💡 Summary of Chapter 2

  • Working Directory: Your active file workspace.
  • Staging Area: The draft room where you prepare the exact content for your next save point.
  • Local Repository: The hidden .git folder containing your compressed historical database.
  • A Commit is an immutable snapshot of your staged files, complete with author info, parent link, and a unique hash ID.
  • HEAD is your position pointer, showing which commit or branch you are currently looking at.

Now that we know how files are saved locally, let’s explore how branches work and how they relate to remote servers like GitHub.


👉 Go to Chapter 3: Branches & Remote Repositories

Related