π 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.