Git & GitHub Interview Question #52

What is the difference between git reset --soft, --mixed, and --hard?

Undo & Recovery, Tags, Forks, Pull Requests & Code Review Mid-Level Intermediate

Quick Interview Answer

With --soft, the branch moves but the index and working tree remain unchanged, so the undone commit's changes stay staged. With --mixed, the branch and index are reset while the changes remain in the working tree.

Detailed Explanation

With --soft, the branch moves but the index and working tree remain unchanged, so the undone commit's changes stay staged. With --mixed, the branch and index are reset while the changes remain in the working tree.

With --hard, the branch, index, and working tree are reset to the target commit. Because uncommitted changes can be lost, --hard should be used only when the developer is certain those changes are not needed.

Code Example

# Keep changes staged
git reset --soft HEAD~1

# Keep changes but unstage them
git reset HEAD~1

# Discard changes as well
git reset --hard HEAD~1