The three areas
Working directory → staging area → repository. Where your changes actually live.
1 Learn the idea
Ever wondered why git makes you add before you commit? It is because your changes pass through three places, and most everyday commands just move files between them.
The three areas
- Working directory — the real files in your folder. You edit here. Git watches, but does not save anything yet.
- Staging area (also called the index) — the draft of your next commit. It is a full snapshot too, not just a list of changes.
- Repository — the commits.
HEADpoints at the one you are standing on; its snapshot is what the other two are compared against.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git status
All three areas agree: the working directory, the stage and the last commit hold the same snapshot. "Working tree clean".
$ edit index.html "<h1>Hello, friend!</h1>" $ edit style.css "h1 { color: tomato }"We edit two files. Only the working directory changes — the stage and the repository still hold the old versions.
$ git status
git statuscompares the areas for you: two files are modified but "not staged for commit".$ git diff
Plain
git diffshows working directory vs. staging area: what you changed but have not staged yet.$ git add index.html
We stage just
index.html. Watch it move onto the stage —style.cssstays behind in the working directory.$ git diff --staged
git diff --stagedshows staging area vs. last commit: exactly what the next commit will contain.$ git restore style.css
We decide the tomato colour was a bad idea.
git restore <file>overwrites the working copy with the staged version. Careful: those edits are gone, not saved anywhere.$ git commit -m "Friendlier greeting"
Click. The commit is made from the stage, not from your folder. All three areas agree again.
$ echo "remember the footer" > todo.txt $ git add todo.txt $ git restore --staged todo.txt
Staged something by accident?
git restore --staged <file>takes it off the stage but keeps your file.todo.txtis back to untracked.$ git status
Untracked files are ones git has never been told about. They are never committed until you
git addthem.
git add <file>git restore --staged <file>git restore <file>git commit
- working directory → stage
- last commit → stage (unstage)
- stage → working directory (discard edits!)
- stage → new commit in the repository
git statusWhich files differ between the three areasgit diff [--staged]Unstaged changes (or, with --staged, what the next commit holds)git restore [--staged] <file>Discard edits (or unstage, keeping the file)git commit -am "msg"Stage all tracked changes and commit3 Check your understanding
Look at the three areas. If you run git commit -m "Update" right now, what ends up in the new commit?
You run git add app.js, then edit app.js again, then git commit. What gets committed?
git status says "Changes to be committed", but git diff prints nothing. Why?
Which command permanently throws away your uncommitted edits to notes.md?
4 Practice for real
The focused commit
You built a search feature in app.js, but also left a red debug border in style.css and a debug.log file. Make one commit containing only the app.js change, discard the style.css debug edit, and leave debug.log untracked (do not delete it).