git restore
Restore working tree files or staged changes to a previous state
Practical Example
git restore src/index.ts # discard unstaged changes in one file git restore --staged src/index.ts # unstage a file (undo git add) git restore --source=HEAD~1 . # restore all files to how they were 2 commits ago
Command Overview & How It Works
A modern command (introduced in Git 2.23) for restoring files to a known state, replacing the confusing dual use of git checkout. Use git restore <file> to revert a file in your working directory back to the last committed state — discarding uncommitted changes. Use git restore --staged <file> to unstage a file (the inverse of git add) without changing the working directory. Use git restore --source=HEAD~1 <file> to restore a file to how it looked two commits ago. Use git restore . to restore all files in the current directory. Unlike git reset, restore operates at the file level and does not move branch pointers. Unlike git checkout, its behavior is predictable and focused — it always restores files, never switches branches.
Practical Tips
- •Discard uncommitted edits to a single file with git restore <file> — far safer than a blanket reset.
- •Unstage a file with git restore --staged <file>, the inverse of git add.
- •Restore from a specific point with git restore --source=HEAD~1 <file> to bring back an earlier version.