Undoing Uncommitted Changes

Loading concept...

🧹 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:

  1. Working Directory = You’re getting ready (trying on costumes, making faces)
  2. Staging Area = You’ve posed and the camera is about to click
  3. 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

  1. You edit story.txt and add: “The cat flew to the moon” 🐱🚀
  2. Wait… cats can’t fly! That’s silly.
  3. Run: git restore story.txt
  4. 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

  1. You change homework.txt
  2. You run git add homework.txt (put it in staging)
  3. Oops! That’s the wrong homework!
  4. Run: git restore --staged homework.txt
  5. 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

  1. Last week, recipe.txt had a yummy cookie recipe 🍪
  2. Today, you accidentally replaced it with “blah blah blah”
  3. Find the old commit: git log --oneline
  4. You see: a1b2c3d Add cookie recipe
  5. Run: git restore --source=a1b2c3d recipe.txt
  6. 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

  1. You’re editing game.js to add a cool feature
  2. Suddenly: “Emergency! The app is crashing!”
  3. Run: git stash push -m "cool feature half done"
  4. Your changes disappear (safely stored!)
  5. Fix the crash, commit it
  6. Run: git stash pop
  7. 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

  1. Check before discarding!

    git diff filename.txt
    

    See what you’re about to throw away.

  2. Stash includes untracked files:

    git stash -u
    

    This saves new files too!

  3. 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:

  1. 🗑️ Discard - Throw away mistakes
  2. 📦 Unstage - Remove from staging, keep changes
  3. Restore - Time travel to old versions
  4. 📋 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! 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.