Undo or revise your latest local Git commit
Keep changes staged, move them back to the working tree, discard them deliberately, or amend the commit. Use a corrective commit for shared history.
Choose the outcome before undoing a commit. If the latest commit is local and has a parent, reset can remove it from the current branch while keeping or discarding its file changes. Amend replaces the latest commit with a revised one.
| Desired outcome | Command |
|---|---|
| Keep the current index and files for recommitting | git reset --soft HEAD^ |
| Reset the index but keep working-tree contents | git reset HEAD^ |
| Restore the parent's tracked contents | git reset --hard HEAD^ |
| Revise the latest commit | git commit --amend |
These examples assume the branch and target are the ones you intend. Save unrelated edits first; reset modes operate on the current index and working tree as a whole, not only on a neatly isolated “last commit patch.”
Fix the latest commit
git add -- forgotten-file.txt
git commit --amend
Review all staged contents before amending because they become part of the replacement commit. To keep the existing message, use git commit --amend --no-edit. To change only its message, ensure no unintended content changes are staged.
Split a commit into smaller changes
Use a mixed reset so the changes are available for selective staging:
git reset HEAD^
git add -p
git commit -m "First part of the change"
Repeat for the remaining changes. New files may require explicit staging. A soft reset would leave the whole previous index staged, so simply running git add -p afterward would not by itself split it.
Discard the work only when intended
git reset --hard HEAD^
This discards current staged and unstaged tracked changes and can delete untracked obstructions. A backup branch preserves commits; it does not save uncommitted files. Reflog recovery may recover the old commit while it remains available, but it is not a backup for arbitrary edits.
Keep shared history with revert
For an ordinary commit already used by others:
git revert HEAD
Review and publish the new reversing commit through the normal workflow. Reverting a merge needs a mainline choice and has additional ancestry consequences. Rewriting published history is possible only as a separately coordinated operation.
FAQ
Are HEAD^ and HEAD~1 different for a merge?
No. Both select the first parent, even for a merge. HEAD^2 selects its second parent; HEAD~2 follows the first-parent chain twice. A root commit has no parent, so neither HEAD^ nor HEAD~1 resolves there.
Is amend identical to reset --soft and recommit?
Not in every case. Amend normally preserves the original author and parent list, including a merge commit's parents. A normal recommit after resetting can use different authorship and ancestry. They can produce similar file contents without being equivalent history operations.
Can I undo several local commits?
Select and inspect an older target such as HEAD~3, then choose the reset mode deliberately. Consider a backup branch before a larger rewrite.
See git-reset, git-commit, and Git revision syntax.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan