Interview Q&A / Git & GitHub

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do you undo the last commit but keep your changes?

SHORT ANSWER

Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them unstaged. The commit disappears from the branch while your work stays in the files.

Explain it simply

You committed too early, perhaps with the wrong message or half the files. You want to rewind one step without losing a line of code.

HEAD~1 means the commit before the current one. Resetting the branch there removes the last commit from history, but unlike --hard, soft and mixed leave your files alone. Use soft if you plan to commit again right away, mixed if you want to re-pick what to stage. Only do this before pushing; otherwise prefer revert.

Example

bash
git log --oneline -2
# b2c3d4e Add cart and wishlist
# a1b2c3d Add header
git reset HEAD~1
git status      # cart and wishlist files appear as modified

What to say in the interview

"I run git reset --soft HEAD~1 or the default mixed reset. The commit is removed from the branch but my edits remain, staged with soft or unstaged with mixed. I do this only for unpushed commits."

#git#reset#commit

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.