Practical technology learning
Git Restore: Unstage a File While Keeping Its Working Edit
Git can hold a committed snapshot, a staged snapshot and a working file with different contents. This disposable local lab follows one edit through those three places. It demonstrates unstaging rather than changing shared history or publishing code.
Prepare one committed practice file
Create a new local repository with a notes.txt file containing Original note, then commit it. Change the file to Revised note. Keep this fixture separate from shared repositories and choose one exact path so there is no uncertainty about which file the command will affect.
Inspect the staged snapshot
Stage notes.txt and read git diff --cached. It should show the change proposed between HEAD and the index. In this simple fixture, ordinary git diff is empty immediately after staging because the working file and index contain the same revised text.
Restore only the index entry
Run git restore --staged -- notes.txt. With no alternate source, Git restores the selected index entry from HEAD. The working file still contains Revised note. The staged diff is now empty, while the ordinary diff shows the working edit relative to the index.
Keep the destination option visible
The --staged option selects the index as the restore destination. Without a destination option, restore normally targets the working tree instead. That operation can remove an unstaged edit. A command that looks shorter may therefore answer a different question; check the options and path before using it outside the disposable fixture.
Record all three observations
- HEAD contains the last committed practice text.
- The index contains the snapshot proposed for the next commit.
- The working file contains the text currently available to edit.
- Both diff views and the file contents match the intended operation.
Keep the conclusion local
Unstaging this file does not change the existing commit or publish anything. More complex staged and unstaged edits require the same careful separation of snapshots. Preserve the tiny result sheet as a reference when reviewing a later Git workflow.