Staging Changes
The git add command adds changes to the staging area, preparing them for a commit.
The Staging Area
The staging area (also called the "index") is where you prepare changes before committing. This lets you:
- Select specific changes to include in a commit
- Review what will be committed
- Build commits piece by piece
Adding Files
Common Add Commands
Add a Specific File
git add filename.txt
Add All Files
git add .
# or
git add -A
Add by Pattern
git add *.js
git add src/
Add Options
| Command | Description |
|---|---|
git add <file> | Add specific file |
git add . | Add all in current directory |
git add -A | Add all changes everywhere |
git add -p | Add interactively (patch mode) |
git add -u | Add modified/deleted, not new |
Interactive Adding
The -p (patch) flag lets you stage parts of a file:
git add -p filename.txt
Options at each hunk:
y- stage this hunkn- skip this hunks- split into smaller hunksq- quit
Unstaging Files
Remove files from staging:
git reset HEAD filename.txt
# or
git restore --staged filename.txt
Best Practices
- Review before adding: Use
git difffirst - Add related changes together: One feature per commit
- Don't add generated files: Use
.gitignore
Exercise: Stage All Changes
Add all files to the staging area using git add .:

