Three places your changes live
Quick recall from lesson 2-1: pancakes.txt is sitting in the repo as an untracked file. How does it get into a commit? Through Git's most misunderstood idea, the staging area.
Every Git repository has three zones:
- The working directory: the ordinary files you see and edit. Your scratchpad.
- The staging area (also called the index): the list of exactly what will go into the next commit. Nothing enters a commit without being staged first — you can picture it as a waiting room between editing and saving.
- The repository: the
.gitdatabase of permanent snapshots from lesson 1-2.
Why the middle step? Because you rarely want to save everything you touched. Maybe you fixed a bug and also left debugging junk in another file. Staging lets you pick the bug fix, commit it cleanly, and deal with the junk later.
git add in action
git add <file> puts a file's current contents into the staging area. Watch how git status changes:
$ git add pancakes.txt
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: pancakes.txtpancakes.txt moved from "Untracked files" to "Changes to be committed". That phrase is the staging area's real meaning: the list of changes the next commit will contain.
Useful forms:
git add pancakes.txt waffles.txtstages several filesgit add .stages everything changed in the current folder and below (convenient, but checkgit statusfirst so junk doesn't sneak in)
Staging copies contents, it doesn't link the file
git add copies the file's contents as they are at that exact moment into the staging area. That rule explains a moment that confuses every beginner: edit pancakes.txt again after staging it, and git status lists the file in two places at once — under "Changes to be committed" (the older, staged version) and under "Changes not staged for commit" (your newer edit).
Commit now and only the staged version is saved; the newer edit stays in your working directory, uncommitted. If you want the latest version in the commit, run git add pancakes.txt again to overwrite the staged copy with the current contents. Nothing is wrong when you see a file listed twice — it just means "you edited this after staging it."
Practice the add flow. In this simulated repo, pancakes.txt already exists but Git isn't tracking it yet.
Step 1/3: Check what Git sees.
Quiz
What does git add pancakes.txt actually do?
Quiz
You run git add cake.txt, then add one more line to cake.txt, then run git commit -m "Update cake". Which version of the file is in the commit?
Problem
You edited two files, soup.txt and TODO.txt, but only the soup change belongs in the next commit. Fill in the blank: git ___ soup.txt