Undo: clean up without losing work
Discard a bad edit, unstage a file, revert a published commit, or shelve work for later.
Lesson 8 of 9 in Git9 min1 exercise
Undo in Git depends on where the change is. Pick the row that matches the situation, and prefer the least destructive option.
- Unstaged edit you want gone →
git restore notes.txt - Staged file you want out of the snapshot →
git restore --staged notes.txt - Local commit not shared yet →
git reset --soft HEAD~1keeps the changes - Published commit →
git revert <hash>makes a new commit that undoes it - Half-finished work →
git stashthengit stash poplater
git restore notes.txt # throw the edit away
git restore --staged notes.txt # keep the edit, unstage it
git revert 1a2b3c4 # new commit that reverses an old one
git stash # shelve everything for later
git stash pop # bring it backExercises
Checked in your browser · hints and solutions included
01
Exercise
Throw away one edit
You made a mess of notes.txt but have not staged it. Discard the unstaged changes in that file.
terminal
↵