Undoing Changes
Made a mistake? No problem! Git makes it easy to undo changes at any stage.
The Undo Commands
Git has different commands for undoing changes depending on where those changes are:
| Situation | Command |
|---|---|
| Discard changes in working directory | git restore |
| Unstage a staged file | git restore --staged |
| Undo the last commit (keep changes) | git reset --soft HEAD~1 |
| Undo the last commit (discard changes) | git reset --hard HEAD~1 |
Discarding Working Directory Changes
If you've edited a file but want to go back to the last committed version:
git restore index.html
Warning: This permanently discards your changes! The file will revert to its state in the last commit.
Unstaging Files
Accidentally staged a file you didn't mean to? Remove it from staging (but keep your changes):
git restore --staged index.html
This moves the file back from the staging area to the working directory.
The Safety Net
Think of Git's undo features like this:
Working Directory Staging Area Repository
│ │ │
│◄── restore ────────┤ │
│ │◄── restore ──────┤
│ │ --staged │
│◄───────────────────┼── reset --hard ──┤
When to Use Each
git restore- "I messed up this file, bring it back"git restore --staged- "I didn't mean to stage this"git reset --soft- "I want to redo my last commit"git reset --hard- "Delete everything and go back" (careful!)
Try It Out!
Use the interactive simulator to practice undoing changes safely.
Visual Mode: Drag files back from staging, or click "Discard" buttons Command Mode: Practicegit restore and git reset commands
Don't worry - in this sandbox, nothing is permanent!