git reset soft vs mixed vs hard - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When using git reset, it is helpful to understand how the command's work grows as the number of commits or changes increases.
We want to see how the different reset modes affect the amount of work Git does internally.
Analyze the time complexity of these git reset commands:
git reset --soft HEAD~3
git reset --mixed HEAD~3
git reset --hard HEAD~3
These commands move the current branch pointer back by 3 commits, changing the state of the index and working directory differently.
Look at what Git does internally for each reset mode:
- Primary operation: Git updates commit pointers and resets files in the index and/or working directory.
- How many times: For each commit and each file changed in those commits, Git may update the index or working directory.
The input size here is the number of commits and files affected by the reset.
| Input Size (n commits) | Approx. Operations |
|---|---|
| 10 | Updates for 10 commits and their files |
| 100 | Updates for 100 commits and their files |
| 1000 | Updates for 1000 commits and their files |
Pattern observation: The work grows roughly in proportion to the number of commits and files being reset.
Time Complexity: O(n)
This means the time Git takes grows linearly with the number of commits and files it needs to reset.
[X] Wrong: "All git reset modes take the same time regardless of files or commits."
[OK] Correct: Different reset modes update different parts (index, working directory), so the amount of work depends on how many files and commits are involved.
Understanding how git reset works under the hood helps you explain your choices clearly and shows you grasp how tools manage data efficiently.
What if we reset only one file instead of all files in the commits? How would the time complexity change?
Practice
git reset --soft do to your changes after undoing a commit?Solution
Step 1: Understand
This option moves the HEAD pointer back but keeps all changes staged.git reset --softeffectStep 2: Compare with other reset types
Unlike mixed or hard, soft reset does not unstage or delete changes.Final Answer:
It keeps the changes staged and ready to commit again. -> Option CQuick Check:
soft reset = keep staged changes [OK]
- Confusing soft with mixed reset
- Thinking soft deletes changes
- Assuming soft unstages changes
Solution
Step 1: Identify the reset option for unstaging changes
The--mixedoption resets the commit and unstages changes but keeps them in the folder.Step 2: Verify syntax correctness
All commands useHEAD~1to move one commit back;--mixedis the default and correct option here.Final Answer:
git reset --mixed HEAD~1 -> Option DQuick Check:
mixed reset = unstage but keep changes [OK]
- Using --soft when unstaging is needed
- Using --hard which deletes changes
- Using invalid --keep option
git reset --hard HEAD~1?Solution
Step 1: Understand
This option resets the commit and deletes all changes from both staging and working folder.--hardreset effectStep 2: Confirm no changes remain
After hard reset, the working folder matches the commit pointed by HEAD, so changes are lost.Final Answer:
The commit is undone, changes are deleted from folder and staging. -> Option BQuick Check:
hard reset = delete changes from folder and staging [OK]
- Thinking hard reset keeps changes staged
- Confusing hard with mixed reset
- Assuming changes are saved in stash automatically
git reset --soft HEAD~1 but your changes disappeared from staging. What is the likely cause?Solution
Step 1: Analyze why changes are unstaged
Soft reset keeps changes staged; if changes disappeared from staging, mixed reset was likely used.Step 2: Check command confusion
Mixed reset unstages changes but keeps them in folder, matching the symptom.Final Answer:
You rangit reset --mixedinstead of soft. -> Option AQuick Check:
Mixed reset unstages changes, soft keeps staged [OK]
- Confusing soft and mixed reset effects
- Assuming Git version lacks --soft support
- Thinking reset deletes changes automatically
Solution
Step 1: Undo commit and unstage changes
git reset --mixed HEAD~1moves HEAD back, unstages changes but keeps them in folder.Step 2: Selectively stage files
Usegit add <files>to stage only desired files after unstaging.Final Answer:
git reset --mixed HEAD~1; git add <files> -> Option AQuick Check:
Mixed reset + selective add stages chosen files [OK]
- Using soft reset which keeps all changes staged
- Using hard reset which deletes changes
- Trying to unstage files after soft reset
