By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
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.
Production insight: If you modify a file but don’t stage it, Git won’t track it—even if you commit.
? Staging Area (Index)
Production insight: Use git add -p to selectively stage changes (e.g., exclude debug logs).
git add -p
?️ Repository (Repo)
.git
Production insight: If you git commit without staging, nothing new gets saved.
git commit
? git status
git status
Production insight: Run this before every commit to avoid surprises.
➕ git add
git add
Production insight: git add . stages all changes (including unwanted files—use .gitignore).
git add .
.gitignore
✅ git commit
Production insight: Always write clear commit messages (e.g., fix: login API 500 error).
fix: login API 500 error
? git restore / git reset
git restore
git reset
git restore --staged <file>
Prerequisites:- Git installed (git --version).- A local repo (or clone one: git clone https://github.com/your-repo.git).
git --version
git clone https://github.com/your-repo.git
Task: Fix a bug in app.js without committing debug logs.
app.js
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
echo "console.log('DEBUG: Fixing login bug');" >> app.js
Verify:
cat app.js
console.log('User logged in'); console.log('DEBUG: Fixing login bug'); # ← Unwanted debug log
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.
y
n
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.
git commit -m "fix: login API 500 error"
git log -1
commit abc1234 (HEAD -> main) Author: You <[email protected]> Date: Mon Jan 1 12:00:00 2024 +0000 fix: login API 500 error
git restore app.js
console.log('User logged in (fixed)'); # Debug log is gone
.env
git-secrets
git stash
node_modules/
.DS_Store
git diff --staged
console.log('DEBUG')
git reset --hard
Answer: The change won’t be included in the commit.
"How do you unstage a file without losing changes?"
Answer: git restore --staged <file> (or git reset HEAD <file> in older Git).
git reset HEAD <file>
"What’s the difference between git restore and git reset?"
git add -u
.
-u → Stages only modified/deleted files (not new ones).
-u
git commit -a vs git add + git commit
git commit -a
git add + git commit
-a
Challenge:You modified config.yml but accidentally staged a typo. Unstage the file without losing changes, fix the typo, then stage and commit.
config.yml
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.
git restore --staged
git add <file>
git add app.js
git restore --staged app.js
git restore <file>
git commit -m "msg"
git commit -m "fix: login bug"
git diff
git stash pop
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. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.