Restoring and Unstaging Files

Modern Git provides git restore for restoring working-tree files and removing changes from the staging area. git restore file.js discards unstaged modifications to that file, while git restore --staged file.js removes it from the staging area without discarding its working-tree changes.

Discarded Changes Can Be Lost

git restore file.js can permanently discard uncommitted modifications to that file. Inspect the diff first if the changes matter.

Restore and Unstage
git restore app.js
git restore --staged app.js
git restore --source=HEAD~1 app.js
💡 The first command restores the working file from the index. The second removes it from staging. The third restores the file from another commit.
Key Takeaways
  • git restore --staged unstages without discarding the working copy.
  • git restore can discard working-tree changes.
  • Use explicit source commits when restoring historical versions.
Knowledge Check

What does `git restore --staged file.js` do?