Git Restore --staged: Unstage Without Losing Edits
Unstage a tracked modification
Unstaging changes the next snapshot, not the working file. In an independent unstage lab, Tea is committed, Rice is staged, and Milk is the current working content. With --staged, the default source becomes HEAD.
git status --shortgit restore --staged -- notes.txtgit status --shortcat notes.txtgit diff --cached -- notes.txtStatus becomes space-M: the index matches HEAD (Tea), the working file is Milk, and the staged diff for this file is empty. The separately staged Rice version is no longer in the index; save it first if valuable.
Unstage a newly added file
If a file does not exist in HEAD, unstaging its addition removes its index entry but leaves its working copy. Continue in the same fixture with a new draft.
printf "Draft\n" > draft.txtgit add -- draft.txtgit restore --staged -- draft.txtgit status --shortcat draft.txtdraft.txt becomes untracked (??) and still contains Draft. The unrelated notes.txt edit remains.
Use a path-specific reset when needed
In repositories with an existing HEAD, git reset HEAD -- notes.txt is an older way to reset the index entry to HEAD. It is not the same as commit-level reset. Restage Milk, then use the path form.
HEAD does not move and Milk is still in the working file. Before the first commit HEAD is not a valid source; for a newly staged file use the appropriate index-only removal (introduced in Chapter 5), after checking what is staged.
Practice
Stage two files, unstage only one, and explain which will be included in the next commit. Use a disposable repository.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Restore --staged: Unstage Without Losing Edits — knowledge check
Reason about undo operations, affected states and preservation of work.
What does restore --staged normally update?
What happens to a newly staged file when unstaged?
HEAD Tea, index Rice, working Milk. After unstaging?
Which older command unstages one tracked file with existing HEAD?
What is a risk when the index differs from the working file?