Beginner LevelGit & GitHub
What is the difference between git reset --soft, --mixed, and --hard?
❓ Interview Prompt: Compare git reset modes (--soft, --mixed default, --hard) and their impact on Working Directory and Staging Area.
'git reset <commit>' moves the current branch HEAD pointer backwards to a specified target commit. The three reset flags control what happens to your staged and unstaged changes:
1. '--soft' (Preserves Staging Area & Working Directory):
•
Moves HEAD to target commit.
•
All changes from undone commits remain staged in the Index, ready to be recommitted immediately.
2. '--mixed' (Default — Preserves Working Directory Only):
•
Moves HEAD and resets the Staging Area Index.
•
Un-stages all changes, but leaves physical files in your Working Directory intact as unstaged modifications.
3. '--hard' (DANGEROUS — Discards Everything):
•
Moves HEAD, resets the Staging Area Index, and permanently overwrites Working Directory files back to the target commit state! Uncommitted local changes are lost forever.
Code Implementation Example
bash
# Undo last commit, keeping changes staged for recommit
git reset --soft HEAD~1
# Undo last commit, un-staging changes but keeping files on disk (default)
git reset --mixed HEAD~1
# DANGEROUS: Wipe last commit and discard all uncommitted changes on disk!
git reset --hard HEAD~1💡Comparing git reset --soft, --mixed, and --hard modes.
🎯 Interview Key Takeaway
Use --soft to restage commits, --mixed to unstage files safely, and --hard ONLY when discarding all local changes intentionally.
#git reset#--soft#--mixed#--hard#Beginner
Back to all Git & GitHub Questions→