🧹 Daily Git Workflow: Undoing Uncommitted Changes
The Magic Eraser for Your Code
Imagine you’re drawing a beautiful picture. You make a mistake—maybe you drew a crooked line or used the wrong color. What do you do? You grab an eraser! Git has magic erasers too. They help you fix mistakes before you save your picture to the gallery.
🎯 The Big Picture
Think of Git like a photo booth with a camera:
- Working Directory = You’re getting ready (trying on costumes, making faces)
- Staging Area = You’ve posed and the camera is about to click
- Commit = The photo is taken and saved forever!
Today, we learn how to fix things before the photo is saved. Let’s explore four magic erasers:
graph TD A[🎨 Working Directory] -->|Made a mistake?| B{What kind?} B -->|Want to throw away changes| C[🗑️ Discard] B -->|Oops, staged too early| D[📦 Unstage] B -->|Need old version back| E[⏪ Restore] B -->|Need to pause work| F[📋 Stash]
1️⃣ Discarding Local Changes
What Is It?
You changed a file, but now you hate it. You want the file to go back to how it was in your last save (commit).
Real-Life Example: You’re coloring a cat, but accidentally colored it purple. You want the black-and-white cat back!
The Magic Command
git checkout -- filename.txt
Or the newer, clearer way:
git restore filename.txt
Step-by-Step Story
- You edit
story.txtand add: “The cat flew to the moon” 🐱🚀 - Wait… cats can’t fly! That’s silly.
- Run:
git restore story.txt - Poof! The file is back to normal.
⚠️ Warning!
This eraser is PERMANENT. Once you discard, your changes are gone forever. No undo button!
2️⃣ Unstaging Files
What Is It?
You put clothes in your suitcase (staging area), but realized you packed your teddy bear by mistake. You want to take it out before you zip up the suitcase.
Real-Life Example: You’re at the photo booth, ready to pose. But wait—you’re still wearing your silly hat! Take it off before the photo clicks!
The Magic Command
git reset HEAD filename.txt
Or the newer way:
git restore --staged filename.txt
Step-by-Step Story
- You change
homework.txt - You run
git add homework.txt(put it in staging) - Oops! That’s the wrong homework!
- Run:
git restore --staged homework.txt - The file is OUT of staging, but your changes are still there
🎯 Key Difference
| Action | What Happens |
|---|---|
git restore file |
Throws away your changes completely |
git restore --staged file |
Keeps changes, just removes from staging |
Think of it like this:
- Discard = Throw the drawing in the trash 🗑️
- Unstage = Take drawing off the display shelf, keep it in your hand ✋
3️⃣ Restoring Files
What Is It?
Bring back a file exactly as it was in a previous commit. Like a time machine for your code!
Real-Life Example: Your toy broke today. But you have a magic camera that took pictures of it when it was new. You press a button, and POOF—the toy is new again!
The Magic Commands
Restore from last commit:
git restore filename.txt
Restore from a specific commit:
git restore --source=abc123 filename.txt
(Replace abc123 with the commit ID)
Step-by-Step Story
- Last week,
recipe.txthad a yummy cookie recipe 🍪 - Today, you accidentally replaced it with “blah blah blah”
- Find the old commit:
git log --oneline - You see:
a1b2c3d Add cookie recipe - Run:
git restore --source=a1b2c3d recipe.txt - Your cookie recipe is back! 🎉
🔍 Quick Diagram
graph TD A[📝 File Now] -->|git restore --source=abc123| B[📝 File from Past] C[🕐 Commit abc123] -->|Contains old version| B
4️⃣ Stashing Changes
What Is It?
Imagine you’re building a LEGO castle. Mom calls you for dinner. You can’t leave pieces everywhere! So you put them in a special box, eat dinner, then come back and continue.
Stash = Your special box for unfinished work
When to Use It
- Boss: “Stop! Fix this bug NOW!”
- You: “But I’m in the middle of something…”
- Git Stash: “I’ll hold your work. Go fix the bug. Come back later!”
The Magic Commands
Save your work to the box:
git stash
Give it a name (so you remember what it is):
git stash push -m "Working on login page"
See all your saved boxes:
git stash list
Get your work back:
git stash pop
Get work back but keep it saved too:
git stash apply
Step-by-Step Story
- You’re editing
game.jsto add a cool feature - Suddenly: “Emergency! The app is crashing!”
- Run:
git stash push -m "cool feature half done" - Your changes disappear (safely stored!)
- Fix the crash, commit it
- Run:
git stash pop - Your half-done feature is back! Continue working 🎮
🗂️ Managing Multiple Stashes
# See all stashes
git stash list
# Shows:
# stash@{0}: On main: cool feature
# stash@{1}: On main: login fix
# Apply specific one
git stash apply stash@{1}
# Delete a stash
git stash drop stash@{0}
# Delete ALL stashes
git stash clear
🎨 Visual Summary
graph TD subgraph "Your Workspace" A[📝 Changed File] end A -->|git restore| B[🗑️ Changes Gone Forever] A -->|git add| C[📦 Staged] C -->|git restore --staged| A A -->|git stash| D[📋 Saved for Later] D -->|git stash pop| A
🧠 Remember These!
| I Want To… | Command |
|---|---|
| Throw away changes | git restore file.txt |
| Unstage a file | git restore --staged file.txt |
| Get old version | git restore --source=abc123 file.txt |
| Pause my work | git stash |
| Continue my work | git stash pop |
🌟 Pro Tips
-
Check before discarding!
git diff filename.txtSee what you’re about to throw away.
-
Stash includes untracked files:
git stash -uThis saves new files too!
-
See what’s in a stash:
git stash show -p stash@{0}
🎯 Quick Decision Guide
“What should I do?”
- “I hate these changes, delete them!” →
git restore - “I added too early, but keep changes” →
git restore --staged - “I need the old version back” →
git restore --source=<commit> - “I need to switch tasks, save for later” →
git stash
🎉 You Did It!
You now have four magic erasers in your Git toolkit:
- 🗑️ Discard - Throw away mistakes
- 📦 Unstage - Remove from staging, keep changes
- ⏪ Restore - Time travel to old versions
- 📋 Stash - Pause and save for later
Remember: These only work for uncommitted changes. Once you commit, that’s a different story (for another lesson)!
Now go practice! Make some changes, then undo them. It’s the best way to learn! 🚀