[!NOTE] Git is not just “Saving”. It is a three-stage process of Editing, Staging, and Committing.
The Workflow: Moving Data
Git manages three distinct areas (or “trees”) on your local machine. Understanding how data moves between them is critical for mastering Git.
1. Working Directory (Sandbox)
- What it is: The actual files on your hard drive that you can see and edit.
- State: Files can be modified, deleted, or created here without Git knowing.
- Analogy: The Set of a movie. You can move props around, change lighting, and mess things up. Nothing is recorded yet.
2. Staging Area / Index (Preparation)
- What it is: A binary file (located at
.git/index) that stores a list of file names and file stats. It represents the next commit. - State: When you run
git add, you are copying the file from the Working Directory to the Index. - Analogy: The Camera Viewfinder. You frame the shot exactly how you want it. You might decide to leave some messy props (untracked files) out of the frame.
3. Repository / HEAD (History)
- What it is: The hidden
.gitdirectory where Git stores all your commits and objects. - State: When you run
git commit, you permanently save the snapshot from the Staging Area to the Repository. - Analogy: The Photo Album. Once the photo is taken (committed), it is preserved forever.
Visualizing the Flow
Interactive: Staging Area Simulator
Interact with the file states to understand the lifecycle.
Working Directory
📄 file.txt (v1)
Staging Area
(empty)
Repository
Deep Dive: The Index File
The Index is one of the most important concepts in Git. It is a binary file that acts as a cache of your working directory.
When you run git status, Git does this:
- Compares the Working Directory file stats (timestamp, size) with the Index.
- Compares the Index entry with the HEAD commit.
- If Index $\neq$ HEAD → “Changes to be committed” (Staged)
- If Working Directory $\neq$ Index → “Changes not staged for commit” (Modified)