Git/
Lesson

You've initialized your repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. and created some files. Now Git notices them as "untracked", but that's not enough. Before Git will save a snapshot of your work, you need to explicitly tell it which changes to include. That's what staging is for.

The staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save. explained

Think of committing like taking a photograph. You don't just snap a picture of everything in the room, you first arrange the scene, adjust the lighting, and decide exactly what should be in the frame. Staging is your chance to compose that perfect shot.

When you run git add, you're saying: "Hey Git, include these changes in my next commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.." The changes move from your working directory into the staging area (sometimes called the "indexWhat is index?A data structure the database maintains alongside a table so it can find rows by specific columns quickly instead of scanning everything."), where they wait patiently for you to commit.

Why have a staging area at all? Imagine you're working on a feature and you fix a typo while you're at it. The staging area lets you commit the typo fix separately from the feature work, keeping your history clean and logical. Without it, every commit would be a messy grab-bag of unrelated changes.
02

Staging specific files

The most explicit way to stage is to name files individually:

# Stage a single file
git add index.html

# Stage multiple specific files
git add index.html style.css app.js

This is the safest approach when you want precise control. You see exactly what's going into your commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed., and there's no chance of accidentally including a file you didn't mean to.

AI pitfall
AI assistants love suggesting git add . after every change. This is dangerous, it stages everything, including files you may not want to commit (debug logs, temporary test files, files with hardcoded API keys). Build the habit of staging files by name. It takes two extra seconds but saves you from embarrassing commits.

When to stage individually

  • You're working on multiple unrelated changes
  • You want to make small, focused commits
  • You're not sure what's changed and want to review file by file
  • You're fixing a bug while working on a feature and want separate commits
03

Staging everything at once

When you know you want to commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. all your changes, use the dot shortcut:

# Stage all changes in the current directory and subdirectories
git add .

This stages:

  • New files (previously untracked)
  • Modified files (changes to existing tracked files)
  • Deleted files (if you've removed tracked files)

Important distinction
git add . stages changes from your current directory downward. If you're in src/components/ and run it, files in your project root won't be staged. Use git add -A to stage everything regardless of where you are in the repo.
04

Checking what you've staged

Always verify before committing. Run git status to see exactly what's happening:

git status

The output is color-coded and organized:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   index.html       ← Staged, ready to commit (green)
        modified:   style.css        ← Staged modifications (green)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
        modified:   app.js           ← Modified but not staged (red)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        config.js                     ← Git doesn't know about this yet (red)
StatusColorMeaning
Changes to be committedGreenStaged and ready for the next commit
Changes not stagedRedModified but won't be in the next commit
Untracked filesRedNew files Git isn't tracking yet
05

Fixing mistakes: how to unstage

Changed your mind? Maybe you staged a file by accident, or you noticed an error and want to fix it before committing. You can pull changes back out of the staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save.:

# Unstage a specific file (but keep the changes in your working directory)
git restore --staged filename.txt

# Unstage everything
git restore --staged .

After unstaging, the changes still exist in your working directory, they're just not marked for the next commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. anymore. You can edit them further or re-stage them later.

The old way (still works)

You might see tutorials using git reset HEAD filename. This does the same thing as git restore --staged, but the newer restore command is clearer about what it does. Both work, but restore is the modern approach.

06

Leveling up: interactive staging

Once you're comfortable with basic staging, try the interactive mode:

git add -p

This goes through your changes chunk by chunk, asking for each one:

diff --git a/app.js b/app.js
index 1234..5678 100644
--- a/app.js
+++ b/app.js
@@ -10,5 +10,5 @@ function calculate() {
-  return x + y
+  return x * y
 }

Stage this hunk [y,n,q,a,d,s,e,?]?

Press y to stage this chunk, n to skip it, or ? to see all options. This is especially useful after applying large AI-generated code blocks, AI often changes more than you asked for, and git add -p lets you accept only the hunks you actually want.

07

Your staging workflow

Here's the pattern you'll use hundreds of times:

# 1. Make changes to your files (edit in your editor)

# 2. Check what changed
git status

# 3. Stage the changes you want to commit
git add filename1 filename2
# OR
git add .

# 4. Verify what's staged
git status

# 5. Commit (we'll cover this next)
git commit -m "Your message"

Remember: staging is your friend. Take the time to craft clean, logical commits. Your future self (and your teammates) will thank you when they can actually understand your project history.

08

Quick reference

CommandWhat it doesWhen to use it
git add filenameStage a specific filePrecise, focused commits
git add .Stage everything in current dirAll changes belong together
git add -AStage everything in the repoFrom any subdirectory
git add -pStage chunks interactivelyMixed changes in one file
git restore --staged fileUnstage a fileAccidentally staged something
javascript
# Stage a specific file
git add README.md

# Stage all files in current directory
git add .

# Check what is staged
git status

# Unstage a file
git restore --staged README.md