git add — Staging Changes
The git add command moves changes from your working directory into the staging area (also called the "index"). Only staged changes are included in the next commit.
Why a Staging Area?
The staging area lets you selectively choose what goes into each commit. You might have changed 5 files, but only want to commit 2 of them right now.
Working Directory → git add → Staging Area → git commit → Repository
(edit) (prepare) (save)
Common Patterns
git add file.txt— Stage a specific filegit add file1.txt file2.txt— Stage multiple filesgit add .— Stage all changes in the current directorygit add -A— Stage all changes (including deletions)
Try it!
The repository is already initialized. Create two files and stage them both:
- Create files:
touch file1.txt file2.txt - Stage them:
git add file1.txt file2.txt(or usegit add .) - Verify with
git status— both should appear as "new file" under staged changes
Common Mistake
Using git add . without checking first — This stages everything, including files you might not want to commit (like log files, build artifacts, or secrets). Always run git status first!