Git Reset Explained: When to Use --soft, --mixed, and --hard
This guide walks through Git's reset command, detailing the differences between --soft, --mixed, and --hard modes, when each is appropriate, and how they affect commits, the index, and the working tree for commits that haven't been pushed.
Commit but not pushed – stay calm
If a local commit hasn't been pushed, you can safely undo it because the change hasn't entered the public history.
Soft reset – the gentlest undo
Command: git reset --soft HEAD~1 This removes the most recent commit while keeping the code and the staged (added) state intact. After running it, the commit disappears, the files remain unchanged, and the index stays staged, allowing you to amend the commit message or add more changes and recommit.
Mixed reset – undo commit and unstage
Command (default mode): git reset --mixed HEAD~1 or simply git reset HEAD~1 The commit is removed, the index is cleared, and the working tree files stay modified. You must re‑stage the files you want to include in the new commit, e.g.:
git add file1
git commit -m "New commit message"Hard reset – the most dangerous
Command: git reset --hard HEAD~1 This deletes the commit, clears the index, and rolls back the working‑tree files to the previous version. Both the commit history and the code changes are lost, so use it only when you are certain the changes are no longer needed and you have verified the current state with git status.
Quick comparison
soft= remove commit only<br> mixed = remove commit + unstage<br> hard = remove commit + unstage + revert code
Choosing the right mode
If you only want to discard the last commit, use git reset --soft HEAD~1.
If you need to re‑select which files to commit, use git reset HEAD~1 (mixed).
If you want to discard all changes completely, and are absolutely sure, use git reset --hard HEAD~1 after checking git status.
Summary
The window before pushing is the easiest to recover from. soft keeps code and staged changes; mixed keeps code but clears the index; hard also reverts the code.
Use hard with caution; verify the current changes before running it.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code of Duty
"Code of Duty" — Every line of code has its own mission. We avoid shortcuts and quick fixes, focusing on authentic coding reflections and the joys and challenges of technical growth. The journey of learning matters more than any destination. Join us as we humbly forge ahead in the world of code.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
