Skip to content
Tutorials6 min read

How to Undo the Last Git Commit Safely (reset, revert & reflog)

Committed to the wrong branch or forgot a file? Learn whether to use git reset --soft HEAD~1, git commit --amend, git revert, or git reflog without losing uncommitted work.

GitKron EngineeringUpdated

Undo a local commit you have not pushed yet

If the commit only exists on your local machine, git reset --soft HEAD~1 is the safest command: it moves HEAD back by one commit while keeping all of your changes staged in the index ready to edit or recommit on a new branch.

# Undo last local commit, keeping all changes staged:
git reset --soft HEAD~1

# Undo last local commit, keeping files modified but unstaged:
git reset HEAD~1

# Fix a typo in the last commit message without changing files:
git commit --amend -m "fix(auth): refresh session cookie before expiry"

Undo a commit that has already been pushed to main

Once a commit is pushed to a shared branch, avoid resetting history. Instead, run git revert <commit-sha> to create a new forward commit that cleanly inverses the unwanted diff while keeping branch history intact for teammates and CI pipelines.

  • git reset --soft HEAD~1: uncommits locally while keeping changes staged.
  • git revert HEAD: safely reverses a pushed commit by creating a new inverse commit.
  • git reflog: recovers seemingly "lost" commits after an accidental hard reset or bad rebase.
  • In GitKron Desktop, click Undo in the toolbar to step backward safely via automatic reflog checkpoints.

Frequently asked questions

Does git reset --soft HEAD~1 delete my code?

No. --soft only moves the branch pointer back one commit and leaves every modified file safely staged in your working directory.

Can I recover commits after git reset --hard?

Yes, provided the commits were actually committed locally before resetting. Run git reflog (or open Reflog in GitKron) to locate the previous SHA hash and check it out or reset back to it.

All articles