Course contentsShow
← Software Engineering
Lesson 47 of 1,9332. Version Control with GitFree lesson

Staging Changes with 'git add'

Adding files to the staging area, staging partial changes, and understanding the index.

Staging Changes with 'git add'

What you'll learn: How to selectively prepare changes for your next commit using Git's staging area.

Understanding the Staging Area

Remember the three states we just learned? The staging area (also called the "index") is your preparation zone between your working directory and the repository. Think of it like packing a suitcase before a trip—you don't throw everything from your closet into your luggage. Instead, you carefully select which items to pack. Similarly, git add lets you choose exactly which changes should be included in your next commit.

The Basic Command

To stage a file, use:

git add filename.txt

This tells Git: "I want this file's current changes included in my next commit."

Staging Multiple Files

You can stage several files at once:

git add file1.txt file2.txt
git add *.js
git add .

The dot (.) stages all changes in the current directory—use this carefully!

Why Stage Selectively?

Staging lets you group related changes into logical commits. Maybe you fixed a bug in one file and added a new feature in another. You'd stage and commit the bug fix separately from the feature. This keeps your project history clean and understandable.

Checking What's Staged

Use git status to see what's staged (ready to commit) versus what's modified but unstaged:

git status

You'll see files listed under "Changes to be committed" (staged) and "Changes not staged for commit" (modified but not added yet).

Key Takeaway: git add moves changes from your working directory into the staging area, letting you carefully curate what goes into each commit—think of it as preparing your snapshot before taking the picture.