1. Undo the commit, keep changes staged
git reset --soft HEAD~1
The commit disappears, but your files stay exactly as they were and remain staged. This is the safest option when you just committed too early.
2. Undo and unstage the changes
git reset --mixed HEAD~1
Same as soft, but your changes are no longer staged. --mixed is the default, so plain git reset HEAD~1 does the same thing.
3. Undo and discard everything
git reset --hard HEAD~1
This deletes the commit and the changes in it. Only use it when you are sure you do not want the work back.
4. Undo a pushed commit safely
If you already pushed, do not rewrite history. Make a new commit that reverses it.
git revert HEAD
git revert creates a fresh commit undoing the last one, which is safe for shared branches.
Which command should you use?
- --soft — you committed too soon and want to keep editing.
- --mixed — you want to keep the changes but restage them yourself.
- --hard — you want the commit and its changes gone.
- revert — the commit is already pushed and shared.
Frequently asked questions
Will I lose my work with git reset?
Only with --hard. The --soft and --mixed options keep your edits in the working directory; just be careful with --hard.
What does HEAD~1 mean?
It means one commit before the current one. HEAD~2 would mean two commits back, and so on.
New to version control? See our complete guide to learning to code.