Reset all changes after last commit in git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Undoing changes after the last commit in Git can mean several different things: removing the last commit but keeping the work, discarding the last commit entirely, or cleaning up untracked files as well. The safest command depends on exactly what state you want to end up with.
Decide What You Want to Preserve
Before running any reset command, decide whether you want to keep:
- the commit history
- the staged changes
- the working-directory changes
- untracked files
Those are separate concerns, and Git gives you different commands for each.
Remove the Last Commit but Keep the Changes Staged
If you want to undo the commit itself but keep the changes ready to recommit, use a soft reset.
This moves HEAD back one commit and leaves the content staged. It is useful when the last commit message was wrong or when you want to squash the changes differently.
Remove the Last Commit and Unstage the Changes
If you want to keep the file changes but move them back to the working tree, use the default mixed reset.
Now the last commit is gone, and the files are modified but unstaged.
This is a good choice when you want to rework the content before committing again.
Discard the Last Commit and Its Changes Completely
If you want the repository to look exactly like it did before the last commit, use a hard reset.
This is destructive. The last commit and its tracked-file changes are discarded from the current branch state.
Use it only when you are sure you do not need the work or when you know how to recover it from the reflog if necessary.
Clean Up Untracked Files Separately
A hard reset affects tracked files. It does not remove untracked files or directories.
If you also want to remove untracked content, use git clean explicitly.
This combination:
returns the working tree to the last commit and removes untracked files as well.
Do Not Rewrite Shared History Carelessly
If the last commit has already been pushed and other people may have based work on it, resetting the branch changes history and can disrupt collaborators.
In that situation, git revert HEAD is often safer than git reset, because it creates a new commit that undoes the previous one without rewriting history.
So the right answer depends not just on your local files, but on whether the commit is already shared.
A Simple Decision Guide
Use:
- '
git reset --soft HEAD~1to keep changes staged' - '
git reset --mixed HEAD~1to keep changes but unstage them' - '
git reset --hard HEAD~1to discard the commit and tracked changes' - '
git clean -fdto remove untracked files too' - '
git revert HEADwhen the commit is already shared'
That mental model is more important than memorizing one “undo” command.
Common Pitfalls
The most common mistake is using git reset --hard when the goal was only to edit the last commit message or adjust staged content.
Another common issue is forgetting that git clean and git reset affect different kinds of files. Developers also often rewrite history on a shared branch when git revert would have been safer for the team.
Summary
- “Reset after the last commit” can mean several different final states.
- Use
--softto keep changes staged. - Use
--mixedto keep changes but unstage them. - Use
--hardonly when you truly want to discard tracked changes. - Use
git revertinstead of reset when the commit has already been shared.
Related reading
- Reset local repository branch to be just like remote repository HEAD
- Resetting remote to a certain commit
- Resetting remote to a certain commit
- Resolve conflicts using remote changes when pulling from Git remote
- Resolve Git merge conflicts in favor of their changes during a pull
- Resolving a Git conflict with binary files
- Restore file from old commit in git
- Retrieve the commit log for a specific line in a file?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.