Undo without losing work · Lesson 9 of 15
Restore and unstage
Restore a working file or remove it from staging without moving history.
git restore file copies the staged version back to the working tree, discarding unstaged edits. git restore --staged file removes a change from staging while keeping it in the file. Neither moves HEAD or deletes commits.
Last commit
Staging
Working tree
restore copies backward
git init -q -b main echo "safe" > note.txt git add note.txt git commit -q -m "Save note" echo "mistake" > note.txt git status -s git restore note.txt cat note.txt
If you added something by mistake, use restore --staged first; the edit remains in the working tree. --source other-branch file retrieves one file from a chosen reference without switching the whole branch.
Quick question
Remove a file from staging but keep its edit. Use?
Exercise
Commit good, change the file to bad, restore it, then print it.
Expected output: good
git init -q -b main echo "good" > app.txt git add app.txt git commit -q -m "Good" echo "bad" > app.txt cat app.txt
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.