Git Reset Explained — Soft, Mixed, and Hard

When working with Git, sometimes you commit something by mistake and want to move back in history.
This is where the git reset command comes into play.

In this blog, we’ll understand:

  • What git reset does.
  • The difference between soft, mixed, and hard reset.
  • When to use each one.
  • How Git moves files between working directory, staging area (index), and commits.

Project Setup

For demonstration, let’s say we have this repo:

git log --oneline
a3c5d12 (HEAD -> main) third commit (file1, file2, file3)
62c559b second commit (file1, file2)
1f92b10 first commit (file1)

📂 Files after third commit:

file1.txt
file2.txt
file3.txt

👉 Visual representation:



1. git reset --soft

git reset --soft HEAD~1
  • Moves the branch pointer one step back (to the second commit).
  • Does not touch staging or working directory.
  • Files from the third commit remain staged.

✅ Use this when you just want to redo the last commit message or squash commits.


2. git reset --mixed (default)

git reset --mixed HEAD~1
# same as
git reset HEAD~1
  • Moves the branch pointer back.
  • Unstages changes from the last commit (they remain in the working directory).
  • Staging area is reset to match the target commit.

👉 After reset:

  • file3.txt will still exist in the working directory.
  • But it won’t be staged.

✅ Use this when you want to edit files before recommitting.


3. git reset --hard

git reset --hard HEAD~1
  • Moves the branch pointer back.
  • Wipes out changes from staging and working directory.
  • Your repo looks exactly as it did at that commit.

⚠️ Warning: This is destructive. Once reset, you lose changes unless you had them saved elsewhere.


Quick Comparison

ModeBranch PointerStaging Area (Index)Working DirectoryUse Case
SoftMoved backKeeps changes stagedKeeps changesRedo commits without losing staged changes
MixedMoved backReset to commitKeeps changesDefault, unstage changes but keep work
HardMoved backReset to commitReset to commitDiscard everything after a commit

Summary

  • --soft → only pointer moves, changes remain staged.
  • --mixed (default) → pointer moves, changes unstaged but still in working directory.
  • --hard → pointer moves, everything reset (staging + working directory).

Use soft and mixed for safe adjustments, and hard only when you are sure you want to discard changes.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉