Staging Changes with git add
You'll learn how to selectively prepare changes for commit using Git's staging area, giving you fine-grained control over what gets versioned.
The Core Idea
Think of staging like packing a suitcase for a trip. Your working directory is your entire closet—everything you might need. The staging area is your suitcase—the specific items you've decided to take. The commit is boarding the plane—once you commit, those staged changes are permanently recorded.
git add moves changes from your working directory into the staging area. This intermediate step lets you craft logical, focused commits even when you've modified multiple files.
Basic Staging Commands
Stage a single file:
git add analysis.py
Stage multiple files:
git add analysis.py visualization.py
Stage all changes in current directory:
git add .
Stage all changes anywhere in repository:
git add --all
Pattern-Based Staging
You can use wildcards to stage files matching patterns:
git add *.csv # Stage all CSV files
git add reports/*.html # Stage all HTML files in reports/
git add data/cleaned_* # Stage files starting with "cleaned_"
Selective Staging with -p
Sometimes you've made multiple logical changes in one file. Use git add -p (patch mode) to stage only specific portions:
git add -p model.py
Git will walk through each change interactively, letting you stage individual "hunks" separately. This creates cleaner, more meaningful commits.
Key Takeaway: git add gives you precise control over what changes enter your next commit—stage everything at once, specific files by pattern, or even individual portions of a file using patch mode, enabling clear version history.