First steps · lesson 5 of 8
Commands and ideas in this lesson
Agit diffchanges not staged yet
Bgit diff --stagedwhat the next commit will hold
Cgit add -pstage part of a file
Dgit restore --stagedtake a file out of the staging area

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.

Git sandbox
  1. See the two changes in adobo.md with git diff
  2. Stage only the salt change with git add -p adobo.md
  3. Commit it, with a message like "Say how much salt goes in the adobo"
  4. Commit the rice change on its own
  5. 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

  1. 1c17fad HEAD →main Add a README
  2. 9c53888 Add leche flan recipe
  3. 1f359a2 Add sinigang recipe
  4. 601c654 Add 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.

Spot the trapAna

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.

Git sandbox
  1. Stage everything with git add .
  2. Unstage pancit.md without 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

You staged the salt change and then edited another line of adobo.md. What does git diff --staged show?
What happens to your edits in the file when you run git restore --staged adobo.md?
Why make the salt change and the rice change separate commits?

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.

All sources for Simple Git