DEV Community

Cover image for Git Reset Cheat Sheet: Soft vs Hard (With Example)
davinceleecode
davinceleecode Subscriber

Posted on

Git Reset Cheat Sheet: Soft vs Hard (With Example)

📘 Scenario Setup

You have the following Git commit history and some uncommitted changes:

  1. A commit - "A code"
  2. B commit - "B code"
  3. C commit - "C code"
  4. 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, and E will be wiped from disk

⚠️ Warning: This is destructive and not recoverable unless you use git reflog

🗂️ Result:

  • Only A code and B 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)

Collapse
 
nevodavid profile image
Nevo David

So good - I've definitely nuked stuff with git reset before, and this makes it way clearer. Appreciate the real-world examples!

Collapse
 
shifa_2 profile image
Shifa

Clear and super helpful breakdown of git reset types! The examples and summary table make it easy to understand.