Git restore Command
In this page:
Discarding Working Directory Changes
git restore <file> reverts an unwanted edit back to the file's last committed state, discarding your local, unstaged changes -- it's the modern, more explicit replacement for using git checkout to throw away edits.
Example: Discarding Working Directory Changes
git restore file.txt
Unstaging a File
If a file is already staged with git add but you've changed your mind about including it in the next commit, git restore --staged <file> unstages it while leaving your actual edits in the working directory untouched.
Example: Unstaging a File
git restore --staged file.txt
Restoring from a Specific Commit
git restore --source=<commit> <file> pulls a file's contents from any specific point in history, not just the last commit, letting you recover an older version without checking out that whole commit.
Example: Restoring from a Specific Commit
git restore --source=a1b2c3d file.txt
Unstaging and Restoring Together
Combining --staged and --source in one command both unstages a file and resets its working copy to a past commit's version in a single step, rather than doing it as two separate commands.
Example: Unstaging and Restoring Together
git restore --staged --source=a1b2c3d file.txt
Patch Mode Restoration
git restore -p <file> opens an interactive, hunk-by-hunk prompt so you can discard only some of a file's changes and keep the rest -- useful when a file mixes an unwanted experiment with a fix you actually want to keep.
Example: Patch Mode Restoration
git restore -p file.txt
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)