Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding git reset: soft vs mixed vs hard
📖 Scenario: You are working on a project using Git for version control. Sometimes you want to undo changes but keep some parts of your work safe. Git offers different ways to reset your work: soft, mixed, and hard. Each one affects your files and commits differently.
🎯 Goal: Learn how to use git reset --soft, git reset --mixed, and git reset --hard to undo commits in different ways.
📋 What You'll Learn
Understand what a Git commit is
Know how to check the current commit history
Learn how to reset commits with different options
💡 Why This Matters
🌍 Real World
Developers often need to undo commits without losing work. Knowing how to use different reset options helps manage code safely.
💼 Career
Understanding git reset options is essential for software developers, DevOps engineers, and anyone working with version control to fix mistakes and manage code history.
Progress0 / 4 steps
1
Set up a Git repository with three commits
Initialize a Git repository and create three commits named commit1, commit2, and commit3 by creating files file1.txt, file2.txt, and file3.txt respectively. Use git init, git add, and git commit -m commands exactly as shown.
Git
Hint
Use git init to start the repo. Then create files and commit them one by one with the exact commit messages.
2
Set a variable for the commit before the last one
Create a variable called target_commit that stores the commit hash of the second commit (the one with message commit2) using git rev-parse HEAD~1.
Git
Hint
Use git rev-parse HEAD~1 to get the commit before the last one and save it in target_commit.
3
Reset the last commit using git reset --soft
Use git reset --soft $target_commit to move the HEAD to the second commit but keep the changes from the last commit staged.
Git
Hint
Use git reset --soft with the variable target_commit to undo the last commit but keep changes staged.
4
Show the current Git status after soft reset
Run git status to display the current state of the repository after the soft reset.
Git
Hint
Use git status to see that the last commit's changes are still staged after the soft reset.
Practice
(1/5)
1. What does git reset --soft do to your changes after undoing a commit?
easy
A. It unstages the changes but keeps them in the folder.
B. It removes the changes from both staging and folder.
C. It keeps the changes staged and ready to commit again.
D. It deletes the commit and all changes permanently.
Solution
Step 1: Understand git reset --soft effect
This option moves the HEAD pointer back but keeps all changes staged.
Step 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 C
Quick Check:
soft reset = keep staged changes [OK]
Hint: Soft reset keeps changes staged for quick recommit [OK]
Common Mistakes:
Confusing soft with mixed reset
Thinking soft deletes changes
Assuming soft unstages changes
2. Which of the following is the correct syntax to undo the last commit but keep changes unstaged?
easy
A. git reset --keep HEAD~1
B. git reset --soft HEAD~1
C. git reset --hard HEAD~1
D. git reset --mixed HEAD~1
Solution
Step 1: Identify the reset option for unstaging changes
The --mixed option resets the commit and unstages changes but keeps them in the folder.
Step 2: Verify syntax correctness
All commands use HEAD~1 to move one commit back; --mixed is the default and correct option here.
Final Answer:
git reset --mixed HEAD~1 -> Option D
Quick Check:
mixed reset = unstage but keep changes [OK]
Hint: Mixed reset unstages but keeps changes in folder [OK]
Common Mistakes:
Using --soft when unstaging is needed
Using --hard which deletes changes
Using invalid --keep option
3. Given you committed changes but want to undo the commit and remove all changes from your working folder, what will be the result of git reset --hard HEAD~1?
medium
A. The commit is undone, changes remain staged.
B. The commit is undone, changes are deleted from folder and staging.
C. The commit is undone, changes remain unstaged in folder.
D. The commit is undone, changes are saved in stash.
Solution
Step 1: Understand --hard reset effect
This option resets the commit and deletes all changes from both staging and working folder.
Step 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 B
Quick Check:
hard reset = delete changes from folder and staging [OK]
Hint: Hard reset deletes changes from folder and staging [OK]
Common Mistakes:
Thinking hard reset keeps changes staged
Confusing hard with mixed reset
Assuming changes are saved in stash automatically
4. You ran git reset --soft HEAD~1 but your changes disappeared from staging. What is the likely cause?
medium
A. You ran git reset --mixed instead of soft.
B. You committed new changes after reset.
C. Your Git version does not support --soft option.
D. You actually ran git reset --hard by mistake.
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 ran git reset --mixed instead of soft. -> Option A
Hint: Unstaged changes? Check if mixed reset was used instead of soft [OK]
Common Mistakes:
Confusing soft and mixed reset effects
Assuming Git version lacks --soft support
Thinking reset deletes changes automatically
5. You committed changes but realize you want to undo the commit, keep the changes unstaged, and then selectively stage some files. Which command sequence achieves this?
hard
A. git reset --mixed HEAD~1; git add <files>
B. git reset --soft HEAD~1; git add <files>
C. git reset --hard HEAD~1; git add <files>
D. git reset --soft HEAD~1; git reset HEAD <files>
Solution
Step 1: Undo commit and unstage changes
git reset --mixed HEAD~1 moves HEAD back, unstages changes but keeps them in folder.
Step 2: Selectively stage files
Use git add <files> to stage only desired files after unstaging.
Final Answer:
git reset --mixed HEAD~1; git add <files> -> Option A