Preparing for Launch
Before a spacecraft launches, cargo must be carefully loaded into the staging bay. In Git, the staging area (also called the "index") is where you prepare files for commit. You decide what launches together.
The Git Add Command
To stage a file:
git add filename.txt
This tells Git: "Include this file's changes in my next commit."
Why Have a Staging Area?
The staging area is Git's secret weapon. It lets you:
- Commit only some changes, not all
- Review what you're about to commit
- Build logical, focused commits
- Stage parts of files separately
Imagine fixing three bugs in different files. You can stage and commit each fix separately, creating clear history instead of one messy "fixed stuff" commit.
Common Add Patterns
Add Specific File
git add README.md
Add Multiple Files
git add file1.txt file2.txt file3.txt
Add All Changes
git add .
The . means "current directory and all subdirectories." Use cautiously—make sure you want to stage everything!
Add All Files of a Type
git add *.js # All JavaScript files
git add src/*.py # All Python files in src/
Interactive Staging
For fine-grained control:
git add -p
This shows each change and asks if you want to stage it. Perfect for staging parts of files.
Unstaging Files
Made a mistake? Unstage with:
git reset HEAD filename.txt
Or in newer Git versions:
git restore --staged filename.txt
Viewing Staged Changes
See what's staged:
git diff --staged
This shows exactly what will be in your next commit.
The Three States Revisited
Files flow through states:
- Working Directory: You edit files here
- Staging Area: You prepare files for commit with
git add - Repository: You save snapshots with
git commit
This three-stage process gives you control over what gets committed and when.
Best Practices
- Stage related changes together: If you fixed the navigation bug, stage only navigation files
- Review before staging: Use
git statusandgit difffirst - Stage frequently: Prepare commits as you work, not all at once
- Be intentional: Don't blindly
git add .without checking
Practice Exercise
- Create three files:
touch nav.js fuel.js comm.js - Check status:
git status - Stage one file:
git add nav.js - Check status again—see the difference?
- Stage the rest:
git add fuel.js comm.js - Final status check:
git status
Common Mistakes
Forgetting to Add
You change files but forget git add. Your commit will be empty or incomplete!
Adding Too Much
Using git add . without checking can stage unwanted files (logs, build files, secrets).
Not Reviewing
Always check what you're staging. Use git status and git diff --staged.
Next: Making Your First Commit
Your staging bay is loaded. Files are ready for launch. Time to make your first commit and create a permanent snapshot in the mission logs.