Skip to content
rajin

12Selective staging and restoring changes

core25–30 minHandbook pp. 29–30 Not started

Warm-up · from “Understanding status and diff”

Which diff previews the next normal commit?

No notes: write what you remember, then check. Recalling before reading is what makes it stick.

The problem

A file includes two unrelated edits, one ready for review and one experimental. You can commit just the ready portion.

After this lesson you can

  • Use selective staging.

  • Unstage without losing edits.

  • Distinguish restore sources and destinations.

Key terms

Full glossary

Say what each term means, then open it to check.

  • Index · from lesson 9

    Prepared path-to-object entries, plus metadata and conflict stages.

  • Commit · from lesson 7

    Object identifying a root tree, parent(s), metadata and message.

  • Tree · from lesson 6

    Directory snapshot mapping names, modes and object IDs.

  • HEAD · preview, covered in lesson 14

    Current symbolic branch reference or detached commit identity.

Visual mental model

  1. HEAD
  2. Indexrestore --staged
  3. Working treerestore

Build the mental model

Use git add -p to select hunks interactively. Git can stage part of a file because the Index represents its own version of the file. Inspect git diff --staged before committing. If a hunk combines unrelated edits, split it where possible (s), edit carefully (e), or separate edits in the working tree first. Choices depend on the interactive prompt.

SimulationUnstage is not discard

A simplified model for building intuition. It does not read your sandbox; use the terminal for real behaviour.

Under the hood

Use git restore --staged PATH to reset the Index entry from HEAD by default, preserving working changes. Plain git restore PATH restores working content from the Index by default. With --source=COMMIT and --staged or --worktree you can choose explicit sources and destinations. Plain restore can discard unstaged work; inspect before running it. Before the first commit, unstaging can require git rm --cached instead because HEAD does not resolve.

Common mistake · important nuance

Restore does not always mean recover lost data; it can overwrite current content. Run destructive examples only in disposable labs.

Move between parts freely. Completion counts only what you verify and answer.