git restore Command Guide
Restore working tree files
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Working Dir
git restore <file>Discard changesgit restore .Discard all changesgit restore -p <file>Interactive restoreStaging
git restore --staged <file>Unstage filegit restore --staged .Unstage allSource
--source=HEAD~1From previous commit--source=mainFrom another branchDownloadable Image Preview
Basic Usage
The git restore command (Git 2.23+) restores working tree files. It's a clearer alternative to git checkout for file operations.
# Discard changes in working directory
git restore file.js
# Unstage a file
git restore --staged file.js
# Restore from a specific commit
git restore --source=HEAD~1 file.jsDiscarding Changes
Restore files to their last committed state:
# Discard changes in a single file
git restore file.js
# Discard changes in multiple files
git restore file1.js file2.js
# Discard all changes in working directory
git restore .
# Interactive: choose which changes to discard
git restore -p file.jsgit restore permanently discards uncommitted changes. Make sure you don't need those changes before running this command.Unstaging Files
# Unstage a file (keep working directory changes)
git restore --staged file.js
# Unstage all files
git restore --staged .
# Unstage specific files
git restore --staged src/app.js src/utils.js
# Interactive unstaging
git restore --staged -p file.jsUnstage vs Discard
- --staged: Moves from staging to working dir (keeps changes)
- (no flag): Discards working directory changes
Restoring from Source
# Restore from previous commit
git restore --source=HEAD~1 file.js
# Restore from specific commit
git restore --source=abc1234 file.js
# Restore from another branch
git restore --source=main file.js
# Restore entire directory from commit
git restore --source=v1.0.0 src/
# Restore and unstage (both working dir and staging)
git restore --source=HEAD --staged --worktree file.jsRestore vs Checkout
# Old way (checkout)
git checkout -- file.js # Discard changes
git checkout HEAD file.js # Restore from HEAD
git checkout abc1234 -- file.js # Restore from commit
# New way (restore) - clearer intent
git restore file.js # Discard changes
git restore --source=HEAD file.js
git restore --source=abc1234 file.jsPractical Examples
# Accidentally modified a file - discard changes
git restore file.js
# Added wrong files to staging
git restore --staged *.log
# Get old version of config file
git restore --source=v1.0.0 config.json
# Undo all changes, start fresh
git restore .
git restore --staged .
# Partially discard changes
git restore -p file.js
# Choose 'y' or 'n' for each hunk
# Restore deleted file
git restore deleted-file.jsFrequently Asked Questions
What is the difference between git restore and git checkout?
git restore (Git 2.23+) is specifically for restoring files, making it clearer and safer. git checkout does both branch switching and file restoration, which can be confusing.
How do I unstage a file?
Use "git restore --staged <file>" to unstage a file while keeping the changes in your working directory.
Can I recover files after git restore?
If the changes were never committed, they cannot be recovered. git restore permanently discards uncommitted working directory changes.
How do I restore a file from another branch?
Use "git restore --source=<branch> <file>" to get the version of a file from another branch.
Summary
git restore is the modern, clear way to restore files. Use --staged to unstage, no flag to discard working directory changes.
Quick Reference
git restore <file>- Discard changesgit restore --staged <file>- Unstagegit restore --source=<ref>- From sourcegit restore -p- Interactive
Official Documentation
For authoritative information, refer to the official documentation: