How do you undo the last commit in Git without losing your changes?
Learn how to undo the last Git commit while keeping your changes with git reset --soft HEAD~1, plus when to use --mixed, --hard, revert and amend.
Expected Interview Answer
Run git reset --soft HEAD~1: it removes the last commit but keeps every change staged in the index, so nothing you wrote is lost.
The --soft flag moves the branch pointer back by one commit while leaving the working tree and the staging area untouched, so your files stay exactly as they were and your changes remain staged, ready to be re-committed. Use --mixed (the default) if you want the changes kept but unstaged, and reserve --hard for when you genuinely want to discard the work. If the commit was already pushed and shared, prefer git revert instead, which records a new inverse commit rather than rewriting history.
- Keeps all your edits intact
- Lets you fix a bad commit message or grouping
- Re-stage and re-commit cleanly
- Avoids rewriting shared history when combined with revert
- Fast, single-command recovery
AI Mentor Explanation
Think of a scorer who writes the over into the official book too early. Rather than tearing the page and losing the runs, they lift the entry back onto the scratch pad where the balls are still recorded, ready to be logged again correctly. A soft reset does the same: it pulls the last commit off the branch but keeps every run — your changes — sitting staged, so you re-enter them the moment you are ready.
Step-by-Step Explanation
Step 1
Check the state
Run git log --oneline -3 and git status to confirm which commit is last and what it contains.
Step 2
Soft reset one commit
Run git reset --soft HEAD~1 to remove the last commit while keeping its changes staged.
Step 3
Adjust the changes
Edit files, split them, or fix the grouping now that everything is back in the staging area.
Step 4
Re-commit
Run git commit again with a corrected message or a cleaner change set.
Step 5
Prefer revert if pushed
If the commit was already pushed and shared, use git revert <sha> instead to avoid rewriting shared history.
What Interviewer Expects
- Knowing --soft keeps changes staged
- Difference between --soft, --mixed and --hard
- Understanding HEAD~1 notation
- When to use revert instead of reset on shared branches
- Awareness that --hard discards work
Common Mistakes
- Using git reset --hard and losing the changes
- Confusing reset with revert on pushed commits
- Thinking --soft also unstages the changes
- Force-pushing a rewritten commit to a shared branch
- Not checking git status before resetting
Best Answer (HR Friendly)
“You tell Git to remove just the last commit but keep all your edits, using a soft reset. Your work stays ready to be saved again, so you can fix the mistake and commit properly without losing anything.”
Code Example
# Inspect recent history first
git log --oneline -3
# Remove the last commit but keep its changes staged
git reset --soft HEAD~1
# Changes are now staged again
git status
# Fix things, then commit with a better message
git commit -m "Corrected commit"# Keep changes but unstage them (default reset)
git reset --mixed HEAD~1
# Just fix the message/content of the last commit in place
git commit --amend
# If the commit was already pushed, revert instead of reset
git revert HEADFollow-up Questions
- What is the difference between git reset --soft, --mixed and --hard?
- When should you use git revert instead of git reset?
- How does git commit --amend differ from resetting and re-committing?
- What does HEAD~1 mean compared to HEAD^?
- How do you recover a commit removed by a --hard reset using the reflog?
MCQ Practice
1. Which command undoes the last commit while keeping its changes staged?
The --soft flag moves the branch pointer back one commit but leaves the working tree and staging area untouched, so changes stay staged.
2. Why is git revert preferred over git reset for a commit already pushed to a shared branch?
Revert adds a new commit that undoes the changes, avoiding the history rewrite that reset causes, which would disrupt collaborators.
3. What happens to your changes if you run git reset --hard HEAD~1?
The --hard flag resets the working tree and index to the target commit, discarding the changes from the removed commit.
Flash Cards
Undo last commit, keep changes staged — git reset --soft HEAD~1
Undo last commit, keep changes unstaged — git reset --mixed HEAD~1 (the default)
Undo last commit and discard changes — git reset --hard HEAD~1 (destructive)
Undo a pushed commit safely — git revert <sha> — creates an inverse commit instead of rewriting history
Just fix the last commit's message — git commit --amend