Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Git & GitHub: The Three Stages (Working Directory, Staging Area, Repository) – Zero-Fluff Study Guide**
Source: https://www.fatskills.com/web-development/chapter/tech-git-github-the-three-stages-working-directory-staging-area-repository-zero-fluff-study-guide

TECH **Git & GitHub: The Three Stages (Working Directory, Staging Area, Repository) – Zero-Fluff Study Guide**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Git & GitHub: The Three Stages (Working Directory, Staging Area, Repository) – Zero-Fluff Study Guide


1. What This Is & Why It Matters

Git’s three-stage workflow (Working Directory → Staging Area → Repository) is the backbone of version control. If you don’t understand this, you’ll: - Accidentally commit broken code (e.g., debug logs, half-finished features).
- Lose work because you didn’t stage changes before switching branches.
- Struggle with merge conflicts because you don’t know how Git tracks changes.

Real-world scenario:
You’re a DevOps engineer working on a Kubernetes deployment. You modify a YAML file, test it locally, but forget to stage it before committing. Later, you deploy the wrong version to production—because Git didn’t track your changes. Understanding the three stages prevents this.


2. Core Concepts & Components

  • ? Working Directory (WD)
  • Definition: Your local filesystem where you edit files.
  • Production insight: If you modify a file but don’t stage it, Git won’t track it—even if you commit.

  • ? Staging Area (Index)

  • Definition: A "snapshot" of changes you plan to commit.
  • Production insight: Use git add -p to selectively stage changes (e.g., exclude debug logs).

  • ?️ Repository (Repo)

  • Definition: The .git directory where commits are stored.
  • Production insight: If you git commit without staging, nothing new gets saved.

  • ? git status

  • Definition: Shows which files are in WD, staged, or committed.
  • Production insight: Run this before every commit to avoid surprises.

  • git add

  • Definition: Moves changes from WD → Staging Area.
  • Production insight: git add . stages all changes (including unwanted files—use .gitignore).

  • git commit

  • Definition: Moves staged changes → Repo.
  • Production insight: Always write clear commit messages (e.g., fix: login API 500 error).

  • ? git restore / git reset

  • Definition: Undo changes in WD or Staging Area.
  • Production insight: git restore --staged <file> unstages a file without losing changes.


3. Step-by-Step Hands-On: Fixing a Bug Without Breaking Production

Prerequisites:
- Git installed (git --version).
- A local repo (or clone one: git clone https://github.com/your-repo.git).

Task: Fix a bug in app.js without committing debug logs.

Step 1: Check Current State

git status

Expected output:


On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
modified: app.js

Step 2: Edit app.js (Add Debug Logs)

echo "console.log('DEBUG: Fixing login bug');" >> app.js

Verify:


cat app.js

Expected output:


console.log('User logged in');
console.log('DEBUG: Fixing login bug');  # ← Unwanted debug log

Step 3: Stage Only the Bug Fix (Not Debug Logs)

git add -p app.js

Git prompts:


Stage this hunk [y,n,q,a,d,s,e,?]? s  # Split into smaller chunks

Select only the bug fix (not the debug log):


- console.log('User logged in');
+ console.log('User logged in (fixed)');

Press y to stage, n to skip debug log.

Step 4: Verify Staging Area

git status

Expected output:


Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
modified: app.js Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory)
modified: app.js

✅ Only the bug fix is staged.

Step 5: Commit the Fix

git commit -m "fix: login API 500 error"

Verify:


git log -1

Expected output:


commit abc1234 (HEAD -> main)
Author: You <[email protected]>
Date:   Mon Jan 1 12:00:00 2024 +0000
fix: login API 500 error

Step 6: Discard Unwanted Debug Logs

git restore app.js

Verify:


cat app.js

Expected output:


console.log('User logged in (fixed)');  # Debug log is gone


4. ? Production-Ready Best Practices


Security

  • Never stage secrets (e.g., .env files). Use git-secrets or .gitignore.
  • Use git add -p to review changes before staging.

Reliability

  • Commit often, push frequently (but keep commits small and logical).
  • Use git stash to save WIP changes before switching branches.

Maintainability

  • Write atomic commits (one logical change per commit).
  • Use .gitignore to exclude build files (node_modules/, .DS_Store).

Observability

  • Run git status before every commit to avoid surprises.
  • Use git diff --staged to review staged changes.


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Committing debug logs Production logs fill up with console.log('DEBUG'). Use git add -p to exclude unwanted changes.
Forgetting to stage changes git commit says "nothing to commit" even though files were modified. Run git status first.
Using git add . blindly Accidentally staging node_modules/ or .env. Use .gitignore and git add -p.
Not checking git status Committing half-finished work. Make it a habit to check before committing.
Losing unstaged changes git reset --hard wipes working directory. Use git stash to save WIP changes.


6. ? Exam/Certification Focus


Typical Question Patterns

  1. "What happens if you modify a file but don’t stage it before committing?"
  2. Answer: The change won’t be included in the commit.

  3. "How do you unstage a file without losing changes?"

  4. Answer: git restore --staged <file> (or git reset HEAD <file> in older Git).

  5. "What’s the difference between git restore and git reset?"

  6. Answer:
    • git restore → Discards changes in WD or unstages.
    • git reset → Moves branch pointers (can rewrite history).

⚠️ Trap Distinctions

  • git add . vs git add -u
  • . → Stages all changes (including new files).
  • -u → Stages only modified/deleted files (not new ones).

  • git commit -a vs git add + git commit

  • -a → Stages all tracked files (skips untracked files).
  • git add → Lets you selectively stage changes.


7. ? Hands-On Challenge

Challenge:
You modified config.yml but accidentally staged a typo. Unstage the file without losing changes, fix the typo, then stage and commit.

Solution:


git restore --staged config.yml  # Unstage
nano config.yml                  # Fix typo
git add config.yml               # Stage
git commit -m "fix: typo in config"

Why it works:
- git restore --staged keeps changes in WD.
- git add stages the corrected file.


8. ? Rapid-Reference Crib Sheet

Command Purpose Example
git status Check WD/Staging/Repo state git status
git add <file> Stage a file git add app.js
git add -p Stage changes interactively git add -p
git restore --staged <file> Unstage a file git restore --staged app.js
git restore <file> Discard WD changes git restore app.js
git commit -m "msg" Commit staged changes git commit -m "fix: login bug"
git diff See unstaged changes git diff
git diff --staged See staged changes git diff --staged
git stash Save WIP changes git stash
git stash pop Restore WIP changes git stash pop
⚠️ git reset --hard DANGER: Wipes WD + Staging Avoid unless intentional


9. ? Where to Go Next


Final Tip:
The three stages are your safety net. Treat them like a checklist: 1. Edit (WD) 2. Stage (git add) 3. Review (git diff --staged) 4. Commit (git commit)

Master this, and you’ll never lose work or commit garbage again. ?



ADVERTISEMENT