📘 Scenario Setup
You have the following Git commit history and some uncommitted changes:
- A commit - "A code"
- B commit - "B code"
- C commit - "C code"
- D commit - "D code"
--> Working directory has "E code" (uncommitted changes)
🔁 git reset --soft <commit-B>
-
What it does:
- Moves the HEAD pointer back to commit B
- Keeps your current working directory as is
- C and D are removed from commit history but remain in your code
- All changes (C, D, E) are staged (ready to be recommitted)
✅ Good for: Squashing commits or redoing commit history
🗂️ Result:
-
E code
,D code
,C code
= still in files and staged - Git only shows
A -> B
in the commit log
💣 git reset --hard <commit-B>
-
What it does:
- Moves the HEAD pointer back to commit B
- Deletes all changes made after commit B from both the history and your code
- Everything from
C
,D
, andE
will be wiped from disk
⚠️ Warning: This is destructive and not recoverable unless you use git reflog
🗂️ Result:
- Only
A code
andB code
remain - All changes after B are lost
🧠 Summary Table
Reset Type | Do code C/D/E remain in files? | Do commits after B stay in history? |
---|---|---|
git reset --soft |
✅ Yes | ❌ No |
git reset (default = --mixed ) |
✅ Yes (unstaged) | ❌ No |
git reset --hard |
❌ No (code deleted) | ❌ No |
💡 Pro Tip
- Use
git log
to find the commit hash you want to reset to - Use
git reflog
if you need to recover accidentally lost commits after a--hard
reset
📌 Stay safe — commit often and back up before using --hard
!
If you found this helpful, consider supporting my work at ☕ Buy Me a Coffee.
Top comments (2)
So good - I've definitely nuked stuff with git reset before, and this makes it way clearer. Appreciate the real-world examples!
Clear and super helpful breakdown of git reset types! The examples and summary table make it easy to understand.