CodeRigor

FREE LESSON

Staging, diff, ignore & selective restore

Distinguish staged and unstaged changes, exclude local files, and restore only the state you intend.

A useful commit contains one coherent change, even when your working directory contains several. The index makes that separation possible. Think in terms of three versions of each tracked path: the version in HEAD, the version in the index, and the version in the working tree. A file can have both staged and unstaged changes at once.

Compare the right pair

git diff compares the working tree with the index. git diff --cached compares the index with HEAD; this is the usual final review of the next commit. git diff HEAD compares the working tree with HEAD and therefore includes both categories of tracked edits. Untracked file contents do not appear in an ordinary diff. In short status output the first column describes the index relative to HEAD and the second describes the working tree relative to the index; ?? marks an untracked path.

Three comparisons
start / 3
Three comparisons

Undo staging without undoing editing

If a README draft was staged accidentally, use git restore --staged -- README.md. For that form, the default source is HEAD: the index returns to the committed version while the working file remains edited. By contrast, git restore -- README.md restores the working file from the index and can discard an unstaged edit. Adding both --staged --worktree changes both destinations. Always name the source and destination in your explanation before selecting the command.

Git commands
git diff --cached
git restore --staged -- README.md
git diff -- README.md
git add app.cpp
git diff --cached
git commit -m "Check the bounds before indexing"
Unstage without discarding
start / 4
Unstage without discarding

Stage at the right size

Named paths are often the clearest unit. Outside these noninteractive labs, git add -p lets you review and stage selected hunks, and git restore -p lets you select changes to restore. A commit need not contain every edit in one file. After partial staging, review both diffs and test the state you intend to publish; tests run on your working files, which may include unstaged fixes missing from the staged snapshot.

Ignore patterns belong in .gitignore when they describe shared generated paths, such as build outputs. Repository-local personal exclusions can go in .git/info/exclude. These rules normally keep untracked paths out of discovery; they do not make an already tracked file untracked. git check-ignore -v path explains a matching rule. To stop tracking an appropriate generated file while keeping a local copy, git rm --cached path changes the index; review and commit that change. Ignore rules are not secret removal: data already committed remains in history.

Ignore is not history erasure
start / 4
Ignore is not history erasure

Preserve the unrelated work

In this lab the README draft is valuable and must survive. A clean index at the end does not imply a clean working tree. Your successful state has a committed app fix, an unstaged README draft, and no remaining staged changes. Run sample helps distinguish those outcomes before Submit checks multiple initial contents.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Review the object/reference model and core workflows here. The authored lesson and linked Git manual cover the current command forms and advanced details used in this lab.