The staging area
Last updated
One commit should hold one change. In this lesson you'll use the staging area to split two unrelated edits to the same recipe into two commits, see exactly what you're about to commit with git diff, and take things back out of the staging area.
By the end of this lesson you can
- Say why the staging area exists: to build each commit from exactly the changes you choose
- See unstaged and staged changes with git diff and git diff --staged
- Stage part of a file with git add -p
- Unstage a file with git restore --staged
Why stage at all
Ana has been editing adobo.md. She wrote down how much salt Lola Remy really uses, and she added "Serve with rice" to the last step. She also typed up a new recipe, pancit.md.
Those are three different changes. If they go into one commit called "Update recipes", nobody reading the history later can tell what changed or why, and if Lola Remy says the salt is wrong, undoing that commit also undoes the rice and the pancit.
The staging area lets you build each commit from exactly the changes that belong together. You stage one change, commit it, stage the next, commit it. One commit, one change.
See what changed: git diff
git diff shows the changes in your working folder that you haven't staged yet. Each changed part is a hunk:
- lines starting with
-were removed; - lines starting with
+were added; - lines starting with a space are unchanged context, to show where the change is;
@@ -10,10 +10,10 @@says where: from line 10, 10 lines before and 10 lines after.
A changed line shows as the old line removed and the new line added. git diff --staged shows the other half: what's staged, which is exactly what git commit would save. Lesson 2.2 goes further into reading diffs.
Two changes, two commits
git add -p (for patch) goes through a file's changes one hunk at a time and asks, for each, whether to stage it. In a real terminal you answer y or n for each; when two changes are close together Git shows them as one hunk, and s splits it. The sandbox shows them already split, with a box to tick.
Make three commits: the salt, then the rice, then the pancit recipe. Use git diff --staged before each commit to check what's going in.
- See the two changes in adobo.md with
git diff - Stage only the salt change with
git add -p adobo.md - Commit it, with a message like
"Say how much salt goes in the adobo" - Commit the rice change on its own
- Commit pancit.md in a third commit
Working folder
Staging area
nothing staged
Repository
4 commits on main
You are in ~/kusina. adobo.md has two unrelated changes, and pancit.md is new.
Commit graph
1c17fadHEAD →main Add a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Taking things back out
Staged something you didn't mean to? git restore --staged takes it out of the staging area:
- the change goes back to being "not staged",
- the file in your working folder isn't touched, so nothing is lost.
Commands that only move things between the staging area and the repository are safe: git restore --stagedSafe: only adds or reads; nothing is lost. Lesson 2.3 shows git restore without --staged, which is a different matter.
git status always tells you the command to use: under "Changes to be committed" it says (use "git restore --staged <file>..." to unstage). When in doubt, read git status.
Spot the trap: git add . sweeps in everything
git add . stages every change in the folder (the . means "this folder and everything in it"). It's quick, and it's how unwanted files get into commits.
Ana has finished the pancit recipe. She also has half-written notes in halo-halo.md that aren’t ready. She types git add . and git commit -m "Add pancit recipe".
What happens if…
Try it: stage everything with git add ., then take pancit.md back out.
- Stage everything with
git add . - Unstage
pancit.mdwithout losing it
Working folder
Staging area
nothing staged
Repository
4 commits on main
You are in ~/kusina. adobo.md has two unrelated changes, and pancit.md is new.
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-add, --patch: choosing hunks to stage, and splitting them with s
- Git documentation: git-diff: comparing the working folder, the index and commits
- Git documentation: git-restore, --staged: restoring the index from HEAD
- Pro Git: Interactive Staging: staging patches