Undo & History

How to Undo the Last Commit in Git

Undo your most recent Git commit while keeping all your edited files staged or unstaged in your working directory.

Instant 1-Line Solution
Safe CommandBeginner
git reset --soft HEAD~1
When to use this scenario:You just committed changes, but realized you forgot to include a file, made a typo in code, or need to tweak the commit message before pushing.

Step-by-Step Execution Guide

1Keep changes staged (Soft Reset)
git reset --soft HEAD~1

Removes the commit from history but keeps all your modified files in the staging area ready to re-commit.

2Keep changes unstaged (Mixed Reset - Default)
git reset HEAD~1

Undoes the commit and unstages changes, keeping files modified in your working tree.

3Permanently discard commit and all changes (Hard Reset)
git reset --hard HEAD~1

WARNING: Permanently deletes the commit and all file modifications made in it.

Critical Pitfalls & Precautions

  • β€’Never use "git reset --hard" if you have uncommitted work you want to save.
  • β€’If you have already pushed to GitHub/GitLab, rewriting history with "git reset" requires force pushing (git push --force-with-lease), which can disrupt teammates.

Alternative Approaches

If already pushed to remote
git revert HEAD

Creates a new commit that inverts the previous commit, preserving shared branch history safely.

Frequently Asked Questions

Undo Last Commit - Frequently Asked Questions

Common questions about safe rollback, remote history implications, and troubleshooting.

git reset --soft HEAD~1 only moves the HEAD pointer back, leaving your working files and staging area intact. git reset --hard destroys both the commit and any uncommitted modifications in your working directory.