git restore
git restore reverts file changes in a Git repository. Use it deliberately: restoring a file can discard work that has not been committed.
What you will learn
- How to discard working-tree changes in one file
- How to unstage a file without discarding its edits
- How to restore a file from a chosen commit
Common commands
# Discard unstaged changes in one file
git restore index.html
# Unstage a file but keep its edits
git restore --staged index.html
# Restore from a specific commit
git restore --source=HEAD~1 -- report.txtThe default target is the working tree. With --staged, Git changes the index instead and leaves the working-tree edits in place.
Common mistakes
- Run
git statusand review a diff before restoring. - Do not use a broad path such as
.unless you intend to affect every matching file. - If you need a recoverable history change, consider a new commit or branch instead of deleting uncommitted work.