What is git reset?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Git & GitHub development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
git reset moves the HEAD (and optionally the branch pointer) to a specified commit, with different effects on the staging area and working directory. Three modes: (1) --soft: git reset --soft HEAD~1 — moves HEAD back one commit but keeps all changes staged. The commit is undone, changes are in staging ready to re-commit. Safest for "undo last commit, keep changes."; (2) --mixed (default): git reset HEAD~1 — moves HEAD back, unstages changes (returns them to working directory, not staged). Useful for "undo commit and unstage."; (3) --hard: git reset --hard HEAD~1 — moves HEAD back AND discards all changes in both staging and working directory. The changes are gone. Dangerous — cannot be recovered unless you have the SHA. Use cases: git reset HEAD file.txt — unstage a file (keeps working directory); git reset --hard origin/main — discard all local commits and changes, match remote. Important: Never reset commits that have been pushed and shared with others — it rewrites history and will break teammates' repos. Use git revert instead for shared history.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Git & GitHub answers easy to follow.