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:
| Command | Copies | So that |
|---|---|---|
git restore --staged adobo.md | the last commit's version into the staging area | the change is unstaged; your file is untouched |
git restore adobo.md | the staging area's version into your working folder | your edits to the file are thrown away |
$ git status -s M adobo.md M pancit.md ?? halo-halo.md
Working folder
Staging area
pancit.mdmodifiedRepository
10 commits on main
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
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.
- Unstage the pancit change, keeping it
- Throw away the vinegar change in adobo.md
- Keep halo-halo.md
Working folder
Staging area
pancit.mdmodifiedRepository
10 commits on main
You are in ~/kusina. Some changes are staged, some aren’t, and halo-halo.md is new.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
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
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.
- Git documentation: git-restore: --staged, --worktree and --source
- Pro Git: Undoing Things with git restore: unstaging and unmodifying, and the warning that it's dangerous
- Git documentation: git-status: the hints git status prints for each kind of undo