Looking back and undoing · lesson 3 of 7
Commands and ideas in this lesson
Agit restore filethe file back as it was staged or committed
Bgit restore --staged filefrom lesson 1.5
CDanger badgesSafe, Careful, Can lose work

Undoing changes you haven't committed

Last updated

Sometimes an edit is just wrong, and you want the file back as it was at the last commit. git restore does that. In this lesson you'll use it, see exactly which area each form touches, and learn the one thing about it that can't be undone.

By the end of this lesson you can

  • Throw away an unstaged change with git restore
  • Unstage a change with git restore --staged, without losing it
  • Explain why a restored change is gone for good

Two kinds of restore

git restore copies a file back from one area into another. Which one depends on --staged:

CommandCopiesSo that
git restore --staged adobo.mdthe last commit's version into the staging areathe change is unstaged; your file is untouched
git restore adobo.mdthe staging area's version into your working folderyour edits to the file are thrown away
$ git status -s
 M adobo.md
M  pancit.md
?? halo-halo.md

Working folder

Staging area

pancit.mdmodified

Repository

10 commits on main

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 4 Ana’s desk: pancit.md has a staged change, adobo.md an unstaged one, and halo-halo.md is new.

In git status -s, the first column is the staging area and the second the working folder: M is a staged change, M an unstaged one, ?? untracked.

Danger badges

Every command in this course comes with a badge saying whether it can lose work:

  • Safe: only adds or reads; nothing is lost: only adds or reads; nothing is lost. git status, git log, git add, git restore --staged.
  • Careful: changes files or history, but Git can usually get it back: changes files or history, but Git can usually get it back. You'll meet these from lesson 2.4.
  • Can lose work: can throw away work Git never saved, or rewrite shared history: can throw away work Git never saved.

git restore fileCan lose work: can throw away work Git never saved, or rewrite shared history is the first of these. An edit you never staged or committed exists only in the file. Git never saw it, so when restore overwrites it, there's nothing anywhere to get it back from. Lesson 2.7's safety net can't help either, because it only remembers commits.

Tidy Ana's desk

Ana changed the vinegar in the adobo by mistake, and the pancit change shouldn't be staged yet. Keep the pancit change and her halo-halo notes; throw away the vinegar change.

Git sandbox
  1. Unstage the pancit change, keeping it
  2. Throw away the vinegar change in adobo.md
  3. Keep halo-halo.md

Working folder

Staging area

pancit.mdmodified

Repository

10 commits on main

You are in ~/kusina. Some changes are staged, some aren’t, and halo-halo.md is new.

Commit graph

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

Spot the trap: git restore . on an afternoon's work

git restore . restores every file in the folder. It's the quickest way to throw away a whole afternoon.

$ git status -s
 M adobo.md
 M lumpia.md
 M pancit.md

Working folder

Staging area

nothing staged

Repository

10 commits on main

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 3 An afternoon of work: Ana has corrected three recipes and hasn’t committed yet.

Name the file you mean. And if you're about to throw away changes you might want, run git stash first: the changes are then saved, and you can throw the stash away later.

Check what you learned

Which command takes a change out of the staging area without changing the file?
Ana typed a note into lumpia.md, never staged it, then ran git restore lumpia.md. Can Git get the note back?
Which badge does git restore --staged get?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git