Cheat Sheets · Git Cheat Sheet · Git Undo Last Commit — 3 Safe Ways

Git Undo Last Commit — 3 Safe Ways

git reset --soft HEAD~1

Kept, still staged

git reset HEAD~1

Kept, unstaged

git reset --hard HEAD~1

Deleted

The three resets

CommandChanges in the commitUse when
git reset --soft HEAD~1Kept, still stagedYou want to re-commit with a different message or grouping
git reset HEAD~1Kept, unstagedYou want to re-stage selectively
git reset --hard HEAD~1DeletedThe commit was garbage and so is its content

Related moves

CommandDoes
git commit --amend -m "better message"Rewrite the last commit instead of undoing it
git reflogTime machine — find any commit HEAD has ever pointed to
git revert <sha>Undo a commit that was already pushed (safe on shared branches)

Undoing the last commit has three flavors — the difference is what happens to your changes. Pick by how much you want to keep.

Related Git recipes

FAQ

I ran --hard by mistake — is my work gone?
Probably not. git reflog lists every position HEAD has held; find the commit before the reset and git reset --hard HEAD@{n} back to it. Reflog entries live for ~90 days.

Can I undo a pushed commit with reset?
Do not. Rewriting shared history forces everyone to fix their clones. Use git revert — it creates a new commit that undoes the old one cleanly.

reset vs revert in one line?
reset rewrites history (local work only); revert adds new history (safe to share).

Stop memorizing — build it interactively

Generate the exact command in the Git Builder

Full reference: Git Cheat Sheet