0
0
Gitdevops~5 mins

git reset soft vs mixed vs hard - CLI Comparison

Choose your learning style9 modes available
Introduction
Sometimes you want to undo changes in your project. Git reset helps you move back to an earlier state. It has three ways to do this: soft, mixed, and hard. Each one changes your files and history differently.
When you want to undo a commit but keep your changes to fix or edit them before committing again.
When you want to undo a commit and unstage the changes but keep them in your files to work on.
When you want to completely undo a commit and remove all changes from your files and staging area.
When you accidentally committed too early and want to adjust your last commit without losing work.
When you want to discard all recent changes and return your project to a clean state.
Commands
This moves the last commit out of the history but keeps all your changes staged, so you can edit or recommit easily.
Terminal
git reset --soft HEAD~1
Expected OutputExpected
No output (command runs silently)
--soft - Moves HEAD to the previous commit but keeps changes staged.
This moves the last commit out of history and unstages the changes, but your files still have the changes to edit or review.
Terminal
git reset --mixed HEAD~1
Expected OutputExpected
No output (command runs silently)
--mixed - Moves HEAD and unstages changes but keeps them in files.
This moves the last commit out of history and removes all changes from both staging and files, making your project exactly like the previous commit.
Terminal
git reset --hard HEAD~1
Expected OutputExpected
HEAD is now at <commit-hash> <commit-message>
--hard - Moves HEAD and discards all changes in staging and files.
Check the current state of your files and staging area after reset to see what changed.
Terminal
git status
Expected OutputExpected
On branch main Your branch is behind 'origin/main' by 1 commit and can be fast-forwarded. nothing to commit, working tree clean
Key Concept

If you remember nothing else, remember: soft reset keeps changes staged, mixed reset unstages changes but keeps them in files, and hard reset removes all changes completely.

Common Mistakes
Using git reset --hard without realizing it deletes all unstaged changes.
This causes loss of work because changes in files and staging are removed permanently.
Use git reset --soft or --mixed if you want to keep your changes for editing.
Confusing --soft and --mixed and expecting changes to be unstaged after --soft reset.
Soft reset keeps changes staged, so they appear ready to commit, which can confuse users expecting them unstaged.
Use --mixed if you want changes unstaged but still in files.
Running git reset without specifying a mode and expecting it to keep changes staged.
By default, git reset uses --mixed, which unstages changes, so staged changes are lost.
Always specify --soft if you want to keep changes staged.
Summary
git reset --soft moves HEAD back but keeps changes staged for easy recommit.
git reset --mixed moves HEAD back and unstages changes but keeps them in files.
git reset --hard moves HEAD back and removes all changes from staging and files.