Sub topic 7
Staging Changes with git add
This chapter assumes you have Git installed, can use a terminal, and have already run git init in a project and set your user.name and user.email. You should know that Git keeps a working tree, a staging area, and a repository.
What git add does
git add copies the current content of a file into the staging area. The staging area (also called the index) is the list of changes that will go into your next commit. Git records the file as it is at the moment you run the command. If you edit the file again afterward, the new edits are not staged.
This is why Git separates add from commit. You can change ten files and then commit only the three that belong together.
A worked example
Create a file and check its state.
echo "# Hello Git" > README.md
git status
git add README.md
git statusThe first git status lists README.md under "Untracked files". An untracked file exists in the working tree, but Git has no record of it. After git add, the second git status lists it under "Changes to be committed" as a "new file". It is now staged.
Ways to name what you stage
| Command | What it stages |
|---|---|
| `git add README.md` | One named file |
| `git add docs/ src/` | Everything in those directories |
| `git add '*.md'` | Files matching the pattern, quoted so Git expands it |
| `git add .` | All changes under the current directory |
| `git add -u` | Changes to files Git already tracks, with no new files |
git add . is convenient, but it stages everything, including files you did not mean to include. Read git status before you use it.
Staging after a second edit
A file can be partly staged. Run this after the first commit of README.md:
echo "First line." >> README.md
git add README.md
echo "Second line." >> README.md
git statusgit status now lists README.md twice. It appears under "Changes to be committed" (the first line) and under "Changes not staged for commit" (the second line). A commit now would include only the first line. Run git add README.md again to stage the second.
Common errors
fatal: pathspec 'file' did not match any files: The path is misspelled, or you are in a different directory. Check withlsandgit status.- Nothing staged after
git add .: The files may match a.gitignorerule. Git silently skips ignored files. - Staging a file by accident: Run
git restore --staged <file>. This removes it from the staging area and leaves the file's contents untouched. - Empty directories: Git tracks files, not directories. An empty directory cannot be staged. A common workaround is to put a placeholder file such as
.gitkeepinside it.
Checking what you staged
Before committing, confirm the staged content with git diff --staged. It compares the staging area with the last commit. Plain git diff compares the working tree with the staging area, so it shows only unstaged edits.
Exercise
You run git add notes.txt, then append a new line to notes.txt, then run git commit -m "Update notes". Does the commit contain the new line? What command would you have run to include it?
Show answer
No. git add staged the file as it was before the append, so the commit holds only the earlier content and the new line remains an unstaged change. Running git add notes.txt again before committing would have staged the new line too.
Exercise
What is the difference between git add . and git add -u?
Show answer
git add . stages all changes under the current directory, including new untracked files. git add -u stages only modifications and deletions of files Git already tracks, so new files are left out.