What does git restore <file> do?
git restore <file> undoes changes in the specified file in the working tree, returning it to the state of the last commit or the staging area.
What exactly the command does
When you run:
bash
git restore file.txtGit:
- removes all uncommitted changes in the file
- replaces the file with the version from the staging area, if the file had been added
- if the file was not in the staging area, it takes the version from the last commit
What git restore <file> does NOT do
- it does not change the commit history
- it does not affect other files
- it does not work with commits
When to use it
- you need to undo local edits in a file
- the file is changed, but there is no need to commit
- you need to quickly return the file to its original state
Important to remember
All unsaved changes in the file will be lost. Before using it, make sure you do not need them.
Key phrasing for an interview
git restore <file>undoes uncommitted changes in a file and returns it to the last saved state, without affecting the commit history.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.