Restoring and Checking Out Files
Sometimes you want to discard changes to a file and go back to the last committed version. Git provides `git restore` (modern) and `git checkout --` (older) for this purpose.
Be careful: restoring a file discards your uncommitted changes permanently. Always check `git status` and `git diff` first to make sure you actually want to lose those edits.
git restore <file>
git restore --staged <file>Discarding working directory changes
Run `git restore filename` to discard uncommitted changes in the working directory and revert the file to its last committed state.
Unstaging a file
If you accidentally staged a file, run `git restore --staged filename` to move it back to unstaged (without losing the changes themselves).
git restore app.jsDiscards uncommitted changes in app.js, restoring it to the last commit.
git add app.js
git restore --staged app.jsUnstages app.js while keeping the actual edits in the working directory.
Key points
- git restore discards uncommitted changes to a file.
- git restore --staged unstages a file without losing edits.
- The older git checkout -- filename does the same as git restore.
- Restoring changes is permanent and cannot be undone easily.
