What is the difference between git reset, git revert, and git checkout?
Understand how git reset, git revert, and git checkout differ, when each is safe to use, and how to undo changes without wrecking shared history.
Expected Interview Answer
git reset moves the current branch pointer (and optionally the index and working tree) to a different commit, git revert creates a new commit that undoes a previous commit's changes, and git checkout switches branches or restores files without rewriting history.
reset rewrites history by relocating the branch tip — dangerous on shared branches — and its --soft, --mixed, and --hard modes control whether the index and working directory are also updated. revert is the safe way to undo published commits because it adds a new inverse commit rather than deleting anything. checkout (now often split into git switch and git restore) navigates between branches or pulls file contents from a commit, changing your working state without altering commit history.
- reset lets you unstage changes or rewind a local branch precisely
- revert safely undoes commits on shared branches without rewriting history
- checkout moves between branches and restores files from any commit
- Understanding all three prevents accidental history loss
- Choosing the right tool keeps team history clean and recoverable
AI Mentor Explanation
reset is the umpire recalling deliveries and re-scoring the over as if some balls never happened — powerful but confusing if the crowd already saw them. revert is bowling an extra corrective delivery that officially cancels a no-ball's runs while keeping the record honest. checkout is simply walking to a different pitch or picking up a fresh bat without touching the scorebook at all.
Step-by-Step Explanation
Step 1
Identify the goal
Decide whether you want to move a branch pointer, undo a commit safely, or just switch context.
Step 2
Use reset to rewind locally
git reset --soft/--mixed/--hard moves the branch tip and optionally the index and working tree.
Step 3
Use revert to undo publicly
git revert <commit> creates a new inverse commit, preserving history for shared branches.
Step 4
Use checkout/switch to navigate
git switch <branch> or git checkout <branch> changes your active branch without altering history.
Step 5
Use restore for files
git restore [--staged] <file> or git checkout <commit> -- <file> recovers file contents from a commit.
What Interviewer Expects
- Knowing reset rewrites history while revert preserves it
- Explaining reset's --soft, --mixed, and --hard modes
- Understanding why revert is safe on shared branches
- Awareness that checkout was split into switch and restore
- Choosing the right command for a public vs local scenario
Common Mistakes
- Using git reset --hard on a shared branch and losing others' work
- Thinking git revert deletes the original commit
- Confusing git reset with git revert in interviews
- Not knowing --soft keeps changes staged while --hard discards them
- Believing git checkout of a branch changes commit history
Best Answer (HR Friendly)
“reset rewinds your branch to an earlier point, revert safely undoes a change by adding a new correcting change, and checkout simply moves you between branches or restores files. In short: reset and revert undo work, while checkout is mostly for navigating.”
Code Example
# reset: move branch tip back one commit, keep changes staged
git reset --soft HEAD~1
# reset: rewind and discard all local changes (destructive)
git reset --hard HEAD~1
# revert: safely undo a published commit with a new inverse commit
git revert a1b2c3d
# checkout / switch: move to another branch (no history change)
git switch feature-login
# older equivalent:
git checkout feature-login
# restore a single file's contents from a past commit
git checkout a1b2c3d -- src/config.jsFollow-up Questions
- What do the --soft, --mixed, and --hard flags of git reset do?
- Why is git revert preferred over git reset on shared branches?
- How were git checkout's responsibilities split into switch and restore?
- How can you recover a commit after an accidental git reset --hard?
- Does git revert ever cause merge conflicts?
MCQ Practice
1. Which command undoes a commit by creating a new inverse commit, preserving history?
git revert adds a new commit that reverses the target's changes, keeping the original commit in history — safe for shared branches.
2. What does git reset --soft HEAD~1 do?
--soft moves the branch pointer back but leaves the index and working tree intact, so the changes remain staged.
3. Which pair of commands modern Git introduced to split git checkout's jobs?
git switch handles branch navigation and git restore handles restoring file contents, clarifying git checkout's overloaded roles.
Flash Cards
Does git reset rewrite history? — Yes — it moves the branch pointer, so avoid it on shared branches. Use git revert there instead.
What does git revert do? — Creates a new commit that undoes a previous commit's changes without deleting history.
reset --soft vs --mixed vs --hard? — --soft keeps changes staged, --mixed unstages them (default), --hard discards working tree changes.
What replaced git checkout's two roles? — git switch (change branches) and git restore (restore file contents).
Safe way to undo a pushed commit? — git revert — it adds an inverse commit rather than rewriting shared history.
Continue Learning
Related Interview Questions
How does a Git branch work internally and why is branching cheap in Git?
medium
What is Git and how does distributed version control differ from centralized version control?
easy
What is the difference between git merge and git rebase?
medium
What is the Git staging area (index) and why does it exist?
easy