LearnThatStack Ace your next interview
Git & Version Control · question
Question 4 of 54

How do you undo your most recent commit without losing the work?

beginner
← All Git & Version Control questions
Re-explain

git reset --soft HEAD~1 undoes the last commit and keeps the changes staged, ready to commit again. Reset moves the branch pointer back one commit. Your files on disk never change.

git reset --soft HEAD~1   # commit undone, changes stay staged
git reset HEAD~1          # same, but changes end up unstaged

The default mode, --mixed, keeps the work unstaged instead. The --hard mode throws away your edits, so avoid it here.

Reset only moves your local branch, so use git revert HEAD for a commit you already pushed. Revert adds a new commit that undoes the old one, and nobody's history is rewritten.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Git & Version Control One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Say you had already pushed that commit. What would you do instead, and why? A strong answer uses git revert HEAD, says it adds a new commit that undoes the old one, and explains that reset would rewrite history teammates already pulled.
  • What exactly is the difference between --soft, --mixed, and --hard? A strong answer says what each mode does to the staging area and to the working tree, and points out that only --hard deletes your edits.
  • You ran reset --hard by mistake. How do you get the commit back? A strong answer uses git reflog to find where HEAD was and resets back to that entry. Uncommitted edits that --hard wiped are not in the reflog.
  • What if you only need to fix the commit message or add a file you forgot? A strong answer names git commit --amend, says the amended commit gets a new hash, and warns against amending a commit you already pushed.
  • How would you undo your last three commits and still keep all the work? A strong answer uses HEAD~3 with --soft or --mixed and says the changes from all three commits come back together as one set of edits.

What you can say

  1. I'd run git reset --soft HEAD~1. It undoes the last commit and leaves the changes staged, so I can commit them again.
  2. Reset moves the branch pointer back one commit, and the files on disk don't change at all.
  3. If I want the changes unstaged, plain git reset HEAD~1 does that, because --mixed is the default mode.
  4. I'd stay away from --hard here, because it throws the edits away.
  5. Reset only moves my local branch. If the commit is already pushed, I'd use git revert HEAD, which adds a new commit that undoes it and rewrites nobody's history.

Weak answers to avoid

  • Reaches for reset --hard That mode deletes the edits, which is the opposite of what was asked. Say that --soft or --mixed keeps the work and that --hard is the one to avoid.
  • Ignores whether the commit was pushed Resetting a shared commit rewrites history that others have pulled. Say reset is for local commits and git revert HEAD is the safe undo after a push.
  • Says reset wipes out the commit's changes With --soft or --mixed, reset moves only the branch pointer and your files stay as they are. Explain that the pointer moves and the work stays.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Git & Version Control cheatsheet.

← Back to all Git & Version Control questions
Pro · $10/mo

47 of 54 Git & Version Control answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime