Undo Unstaged Changes
Restore throws away changes you have not staged. It returns the file to its last saved state.
See the change first
One line is new. Read it before you throw it away.
$ git diffThrow it away
Restore prints nothing. Status shows a clean tree.
$ git restore notes.txt
$ git statusThe change is gone
The diff prints nothing now. No difference means no change is left.
$ git diffExample
$ git diff
$ git restore notes.txt
$ git statusKey points
- Restore with a file name throws away the file’s unstaged changes.
- The file returns to the last snapshot.
- Staged changes are safe. Restore only touches unstaged ones.
- There is no undo for restore. The thrown-away lines are gone.
- Check the diff first, so you know what you are throwing away.
- Status confirms the tree is clean afterwards.
Here, the diff shows one new line. Restore throws it away. Status shows a clean tree.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does git restore throw away?