🎓 A1Learner
🔍
0 of 50 lessons completed

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 diff

Throw it away

Restore prints nothing. Status shows a clean tree.

$ git restore notes.txt
$ git status

The change is gone

The diff prints nothing now. No difference means no change is left.

$ git diff

Example

$ git diff
$ git restore notes.txt
$ git status

Key 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?