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.
Explore related tools
More from the GitKron Blog
All articles- TutorialsGetting Started with GitKron: Visual Git Setup in 5 MinutesSet up GitKron on macOS, Windows, or Linux, connect GitHub or GitLab via OAuth, configure SSH commit signing, and navigate your first interactive commit graph.
- Engineering PracticesMastering Team Git Workflows: Trunk-Based vs Gitflow in 2026Compare trunk-based development and structured release branching for modern engineering teams—and learn how to cut PR cycle times and branch drift.
- GuidesHow to Resolve Git Merge Conflicts Safely Without Breaking BuildsLearn why 2-way conflict markers cause regressions and how a true 3-way merge view (base, yours, theirs) makes complex Git conflicts predictable.
- AI & ProductivityWriting Better Git Commit Messages with Explainable AIReplace "fix stuff" and "wip" history with structured Conventional Commits generated from staged diffs—while keeping human review and privacy intact.
- Git FundamentalsGit Rebase vs Merge: When to Use Each in Team RepositoriesLearn the exact difference between git rebase and git merge, when linear history helps code reviews, why force-pushing shared branches breaks teammates, and how to use --force-with-lease safely.
- GuidesGit Interactive Rebase Tutorial: Squash, Reword & Reorder CommitsTurn messy "wip", "fix lint", and "address review" commits into clean, atomic pull request commits using interactive rebase—from the terminal or visually via drag and drop.
- GuidesHow Git Cherry-Pick Works: Backport Hotfixes Without Branch DriftApply specific bug-fix commits across release branches cleanly using git cherry-pick -x, handle cherry-pick conflicts in 3-way view, and avoid duplicate history.