How Git Works
Now let’s focus on how Git actually works behind the scenes.
This blog is the foundation of everything that follows.
Please read it carefully — even multiple times if needed — because all future Git concepts build on this flow. 🚀
We’ll cover:
- The three areas in Git
- How files move between these areas
- Why the staging area is necessary
- Examples shown with visuals
Git’s Three Key Areas
Your project folder may look like a normal folder, but when Git is initialized, it is divided into three areas:
- Working Directory → Where you create, edit, or delete files.
- Staging Area (Index) → Where files wait before being committed.
- Local Repository → Where snapshots (commits) of your project are stored.
👉 Visual representation:
Example: Day 1 (Adding Files)
Imagine you have two files:
file1.txt
file2.txt
Initially, they are only in the working directory. Git does not yet know about them.
Once they are prepared (moved to staging), Git can take a snapshot and store it in the repository.
Example: Day 2 (Updating a File)
On the next day, you update file2.txt.
👉 First, the updated version moves to the staging area. 👉 Then Git stores a new snapshot in the repository as Commit 2.
Notice: file1.txt is not duplicated again. It is reused from Commit 1.
Only the updated file2.txt is stored in Commit 2.
Example: Day 3 (Adding a New File)
Now, you create a new file: file3.txt.
👉 It first enters the staging area. 👉 Then it becomes part of Commit 3 along with file1 and file2.
What If a File is Deleted?
Suppose by mistake you delete file3.txt from your working directory. 👉 Don’t worry! It still exists in:
- The staging area (if staged before deletion)
- The local repository (Commit 3)
This means you can restore the file anytime from staging or from a previous commit.
Why Do We Need the Staging Area?
You may wonder: Why not move files directly from working directory to repository?
Here’s why the staging area is important:
- Suppose you have 10 files.
- You only want to commit 5 of them.
- Without the staging area, all 10 files would go into the repository.
- With the staging area, you can choose exactly which files to include.
👉 That’s the power of the staging area.
Summary
- Git has three areas: Working Directory → Staging Area → Repository.
- Files first move into staging, and only then into the repository.
- Commits are snapshots of the staged files.
- Deleted files can be restored from staging or repository.
- Staging area exists to give you fine control over commits.
🚀 Don’t Miss Out!
If you enjoyed this blog and want to learn Git step by step with me, then you’ll love what’s coming next.
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉