CHAPTER 5 · 1 lesson · 13 exercises
Undoing Changes and Resetting
Discard uncommitted edits with restore, unstage changes, and undo commits with reset and revert.
Everything so far has been about creating commits. But Git's greatest value is being able to safely undo mistakes.
When you know you can go back, you can code fearlessly. Delete files, experiment with new ideas, rewrite whole modules — whatever you do, you can always restore committed states.
The rule for undoing in Git: first decide which area you want to restore.
- Working tree (uncommitted edits) → discard with
git restore - Staging area (staged files) → unstage with
git restore --staged - Amend the latest commit →
git commit --amend - Commit history → undo with
git resetorgit revert
The golden distinction between reset and revert:
Not pushed yet → reset. Already pushed to remote → revert.
Reset deletes commits from history, which corrupts other teammates' branches if already pushed. Revert creates a new commit that inverts the changes, safely preserving public history.
git reset --hard permanently wipes uncommitted changes. Practice it here in a safe sandbox.
Lessons in this chapter
restore / reset / revert
- 05Undoing changes (restore / reset / revert)Use restore, amend, reset, and revert to roll back changes across your working tree, staging area, and commit history.Go to the exercises