Undoing Committed Changes

Loading concept...

Undoing Committed Changes in Git

The Time Machine in Your Pocket

Imagine you’re building a LEGO castle. You add pieces, stack blocks, and suddenly… you put the wrong piece! Or maybe you stacked too many blocks and the tower looks funny.

Wouldn’t it be amazing if you could:

  • Go back in time and undo your last moves?
  • Erase mistakes like they never happened?
  • Keep the pieces but rearrange them?

Git is exactly that time machine for your code!

Today, we’ll learn how to use Git’s magical “undo” powers to fix mistakes in commits you’ve already made.


🎭 Meet Our Heroes

Think of your Git history like a train with carriages:

🚂 ← [Commit 3] ← [Commit 2] ← [Commit 1] ← [Start]
     (HEAD)

Each carriage (commit) carries your changes. Sometimes you need to:

  • Disconnect carriages (reset)
  • Add a reverse carriage (revert)
  • Fix the last carriage (amend)
  • Find lost carriages (reflog)

🔄 Resetting Commits

What is Reset?

Reset is like saying: “Pretend these commits never happened!”

Imagine you wrote 3 pages in your diary:

  • Page 1: “I ate breakfast”
  • Page 2: “I played outside”
  • Page 3: “I made a mistake”

With reset, you can tear out pages from your diary!

The Three Reset Modes

Git reset has 3 personalities, like 3 different erasers:

graph TD A[git reset] --> B[--soft] A --> C[--mixed] A --> D[--hard] B --> E[Keeps changes<br>ready to commit] C --> F[Keeps changes<br>but unstaged] D --> G[Deletes<br>everything]

🧸 Soft Reset (The Gentle Eraser)

What it does: Removes the commit but keeps all your changes staged and ready.

Analogy: You wrapped a gift, but want to add something inside. You carefully unwrap it, but keep the wrapping paper ready to use again.

git reset --soft HEAD~1

Before:

Commit: "Added login button"
Status: Clean

After:

Commit: Gone!
Changes: Still staged, ready to commit

When to use: You made a commit too early and want to add more changes to it.


🎨 Mixed Reset (The Default Eraser)

What it does: Removes the commit AND unstages changes, but keeps files modified.

Analogy: You drew a picture and framed it. Mixed reset takes it out of the frame, but your drawing is still there on paper.

git reset HEAD~1
# or
git reset --mixed HEAD~1

Before:

Commit: "Fixed header color"
Status: Clean

After:

Commit: Gone!
Changes: Modified but NOT staged

When to use: You want to reorganize what goes into which commit.


💥 Hard Reset (The Nuclear Eraser)

What it does: Removes commit AND deletes all changes completely!

Analogy: You wrote on a paper and then burned it. Gone forever!

git reset --hard HEAD~1

Before:

Commit: "Broke everything"
Files: Modified

After:

Commit: Gone!
Files: Back to previous state
Changes: DELETED FOREVER

When to use: You made terrible changes and want to pretend they never existed.


📊 Reset Comparison Table

Mode Commit Staged Changes Working Files
--soft ❌ Removed ✅ Kept ✅ Kept
--mixed ❌ Removed ❌ Unstaged ✅ Kept
--hard ❌ Removed ❌ Gone ❌ Gone

🔙 Reverting Commits

What is Revert?

Reset erases history. But what if you’ve already shared your commits with friends (pushed to a remote)?

Revert is the polite undo!

Instead of erasing, it creates a NEW commit that does the opposite of the old one.

Analogy: You added 5 cookies to a jar. Instead of pretending you never added them, you take 5 cookies OUT. The jar shows both actions in its history!

graph LR A[Add Feature] --> B[Revert: Remove Feature] B --> C[History shows both!]

How to Revert

git revert abc123

This creates a new commit that undoes everything in commit abc123.

Example:

# Original commit added "Hello World"
# Revert commit removes "Hello World"
git revert HEAD

Before:

Commit 3: "Added broken code"
Commit 2: "Fixed button"
Commit 1: "Initial"

After revert:

Commit 4: "Revert: Added broken code"  ← NEW!
Commit 3: "Added broken code"
Commit 2: "Fixed button"
Commit 1: "Initial"

Reset vs Revert

Feature Reset Revert
Changes history ✅ Erases commits ❌ Adds new commit
Safe for shared code ❌ No ✅ Yes
Keeps record ❌ No ✅ Yes

✏️ Amending Commits

What is Amend?

Ever sent a text message and immediately noticed a typo? Amend is like editing your last message!

Analogy: You sealed an envelope, but forgot to add a photo. Amend lets you open it, add the photo, and reseal it.

git commit --amend

Two Uses of Amend

1. Fix the commit message:

git commit --amend -m "Better message here"

2. Add forgotten files:

# Oops, forgot to add a file!
git add forgotten-file.txt
git commit --amend --no-edit

The --no-edit keeps your original message.

Important Rule!

Only amend commits you haven’t pushed yet!

Why? Because amend creates a NEW commit with a new ID. If your friend already has the old commit, things get confusing!

graph TD A[Make commit] --> B{Pushed?} B -->|No| C[Safe to amend] B -->|Yes| D[DON'T amend!<br>Use revert instead]

🔍 Git Reflog: Your Safety Net

What is Reflog?

Remember when we said reset --hard deletes everything forever?

Well… Git has a secret diary called reflog!

Reflog remembers EVERYTHING you did, even things you “deleted”.

Analogy: Imagine every toy you throw in the trash goes to a magical lost-and-found room. For 90 days, you can go there and get it back!

How to Use Reflog

git reflog

Output:

abc123 HEAD@{0}: reset: moving to HEAD~1
def456 HEAD@{1}: commit: Added feature
ghi789 HEAD@{2}: commit: Fixed bug

Each line shows what you did. The weird codes like abc123 are commit IDs.

Rescuing “Lost” Commits

Oh no! You did reset --hard and lost important work!

Step 1: Find the lost commit

git reflog
# Look for your commit message

Step 2: Rescue it!

git reset --hard def456
# or
git checkout def456

Your “deleted” work is back!

graph TD A[Accidentally deleted commit] --> B[Run git reflog] B --> C[Find commit ID] C --> D[git reset --hard ID] D --> E[Work recovered!]

Reflog Expiry

Reflog entries last about 90 days by default. After that, they’re truly gone. So don’t wait too long to recover lost work!


🎯 Quick Decision Guide

“Which command should I use?”

graph TD A[Want to undo?] --> B{Pushed to remote?} B -->|Yes| C[Use REVERT] B -->|No| D{Keep changes?} D -->|Yes, staged| E[Use SOFT reset] D -->|Yes, unstaged| F[Use MIXED reset] D -->|No, delete all| G[Use HARD reset] A --> H{Just fix last commit?} H -->|Yes| I[Use AMEND]

🌟 Summary

Tool Purpose Analogy
reset --soft Undo commit, keep staged Unwrap gift carefully
reset --mixed Undo commit, unstage Remove from frame
reset --hard Undo everything Burn the paper
revert Create opposite commit Take cookies back
amend Fix last commit Edit sealed letter
reflog Find lost commits Lost-and-found room

🎉 You Did It!

Now you know how to:

  • ✅ Go back in time with reset (soft, mixed, hard)
  • ✅ Safely undo shared commits with revert
  • ✅ Fix your last commit with amend
  • ✅ Rescue “lost” work with reflog

Remember: Git almost never truly deletes anything. With reflog, you have a 90-day safety net. So experiment bravely!

Happy coding, time traveler! 🚀

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.