git restore

Undo changes you have not committed

Lesson 6 of 20 · 9 min · Module 2: Everyday changes

Not every experiment deserves a commit. Git can put a file back the way it was, or take it out of the next snapshot.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Discard changes to a file with git restore
  • Unstage a file with git restore --staged
  • Use the hints in git status

The idea

git restore file throws away your uncommitted changes to a file and brings back the version from the staging area, which is the last commit unless you staged something. git restore --staged file only takes the file out of the next commit: your edits stay in the file. git status prints these commands as hints, so read it first.

git status
git restore style.css
git restore --staged draft.md

01Discard a change in one file

git restore file replaces the file with the version in the staging area. Use it when an experiment went nowhere.

git restore style.css
git status --short
Output
A  draft.md
M  index.html

02Unstage without losing work

git restore --staged file takes the file out of the next commit and leaves your edits in place. Because draft.md was a new file, it becomes untracked again.

git restore --staged draft.md
git status --short
Output
M  index.html
?? draft.md

03Let git status guide you

git status always suggests the next command: git add to stage, git restore --staged to unstage, git restore to discard. Read the hints before you act.

Your turn

Your folder is a mix. style.css has an experiment you do not want, draft.md was staged by accident, and index.html has a welcome message that is ready. Throw away the change to style.css, unstage draft.md but keep the file, then commit only the welcome message as Add a welcome message.

What the checks look for

  • style.css is back to its last committed version
  • draft.md is still there, but not committed
  • Only the welcome message is committed
Try it in the editor →

Take this with you

git restore discards changes in a file; git restore --staged only unstages it.

Go a little further

Change a file, stage it, then change it again. Predict what git restore --staged and git restore will each do, then try them one at a time.

Learn Git & GitHub for free

20 short lessons with real code and instant checks.

See the whole course →