Safety & Recovery · 8 min read · Beginner
Undoing Changes, Stash & Revert
Fix mistakes confidently with git restore, git stash, git reset, and git revert without losing work.
What you'll learn
- Temporarily shelve unfinished edits with git stash and git stash pop
- Discard unstaged edits safely with git restore
- Choose git revert over git reset on shared branches
Every developer makes mistakes in Git. Knowing which undo command matches your situation prevents panic and data loss.
Choosing the Right Undo Tool
| Situation | Recommended Command | Safe on Shared Branch? |
|---|---|---|
| Need to switch branches mid-edit | git stash / git stash pop | Yes |
| Discard unstaged edits in a file | git restore file.js | Yes |
Undo a commit already pushed to main | git revert <hash> | Yes (adds an inverse commit) |
| Move a local unpushed commit back to staging | git reset --soft HEAD~1 | Local only |
Fill-in-the-Blank Exercise
Safe Recovery Commands
Temporarily save uncommitted work, and later safely undo a pushed commit on main.
git
git a1b2c3dTry It Yourself (Interactive Git Terminal)
GitInspect History Before Undoing
main
(main) $ git status
On branch main Untracked files: index.html, styles.css
Check Your Understanding
Score: 0 / 1
1. Which command safely undoes a buggy commit that has already been pushed to main?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.