git restore
Discard edits in a file or unstage it
What it does
Without flags, git restore brings a file back to its staged version, or to the last commit if it was never staged. Put simply, it throws away edits that have not been through git add.
With --staged it works on the index instead: the file leaves the index and your edits stay in it. This undoes git add. The command arrived in git 2.23; before that the same was done with git checkout -- <file> and git reset <file>.
Careful. git restore <file> without --staged erases uncommitted edits in the file. They cannot be brought back: git never saved them.
Syntax
git restore <file>
git restore --staged <file>Examples
Discard the unstaged edits in src/app.js
git restore src/app.jssame as:
git checkout -- src/app.jsrestore— return the file to its version in the indexsrc/app.js— file
Unstage src/app.js but keep the edits in the file
git restore --staged src/app.jssame as:
git reset HEAD src/app.js,git reset src/app.jsrestore— restore--staged— only the index, leave the file alonesrc/app.js— file
Related commands
- git add — Stage changes so they go into the next commit
- git status — See what changed in the folder and what is already staged
- git reset — Move the branch to another commit, for example to undo the last commits