Stash, Undo, and Recovery
Level: Intermediate
ℹ️ What You'll Learn
- Stash changes temporarily
- Undo uncommitted changes
- Undo commits
- Recover lost commits
- When to use which
Stash (Save Work Temporarily)
Interrupt workflow, switch branch:
# Working on feature, interrupted
git stash
# Changes saved, working directory clean
# Switch branch to fix urgent bug
git checkout main
git checkout -b bugfix/critical
# Fix bug, commit, test
git checkout feature/exam
git stash pop
# Resume feature work
List stashes:
git stash list
# stash@{0}: WIP on feature/exam
# stash@{1}: WIP on feature/fees
Apply specific stash:
git stash apply stash@{1}
# Keep in stash list
git stash pop
# Apply and remove
Undo Uncommitted Changes
Discard local edits:
# Revert one file
git checkout StudentService.cs
# Revert all changes
git checkout .
# or
git reset --hard
SMS: Mistake in StudentService, revert to last commit.
Undo Commit (Not Yet Pushed)
# Keep changes, undo commit
git reset --soft HEAD~1
# Changes stay staged
git reset --mixed HEAD~1
# Changes unstaged
git reset --hard HEAD~1
# Discard completely
Never git reset --hard on public branch.
Undo Commit (Already Pushed)
Safe method - use git revert:
git revert abc123
# Creates new commit undoing abc123
All other commits remain.
Recover Lost Commit
Accidentally deleted branch with commits:
git reflog
# Shows all local actions
# Find lost commit
0a1b2c3 HEAD@{5}: commit: Add exam results
git checkout -b recovery 0a1b2c3
# Restore lost work
Undo Last Commit (Not Pushed)
git reset --soft HEAD~1
# Undo, keep changes staged
git commit -m "Fixed message"
# Recommit with correct message
Key Takeaways
- Stash = save work temporarily
- Reset = undo commits (local only)
- Revert = undo commits (safe for pushed)
- Reflog = recover lost work
💡 Safety First
Never force push to shared branches. Use revert for public branches.
⚠️ Destructive Commands
git reset --hard= LOSSgit push --force= DANGER- Only use on local branches
🤖Use AI to Learn Faster
Use ChatGPT, Claude, or Copilot to go deeper on Git Undo. Try these prompts:
"When do I use stash?""What's the difference between reset and revert?""How do I recover lost commits?""Quiz me on undo"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.
nexcoding.in
Have questions on your tech stack, ongoing projects, or need one-to-one training?