Accidentally staged a file? git restore --staged moves it back to the working directory without losing your changes. To discard working directory changes entirely and revert a file to the last committed version, use git restore without --staged.
Git
Beginner
7 min read
Unstaging and Restoring Files
Example
# Unstage a file (keep your working directory changes):
git restore --staged index.html
# Unstage everything:
git restore --staged .
# Discard working directory changes to a file
# WARNING: This permanently discards unsaved changes!
git restore index.html
# Discard ALL working directory changes:
git restore .
# Restore a file to its state at a specific commit:
git restore --source=HEAD~2 index.html
# Old syntax (still works, but restore is preferred):
# git reset HEAD index.html <- unstage
# git checkout -- index.html <- discard changes
# Check what you are about to commit:
git status
git diff --staged
# Remove an untracked file (dry run first):
git clean -n # preview what would be removed
git clean -f # actually remove untracked files
git clean -fd # also remove untracked directories