Undo your last Git commit without losing work. Learn when to use reset --soft, a mixed reset, amend or revert, and why reset --hard is the wrong tool for this job.
The short answer
#If you have not pushed the commit yet, run git reset --soft HEAD~1. It moves your branch back one commit and leaves every change from that commit staged, ready to commit again. If you have already pushed it, use git revert HEAD instead so you do not rewrite history that other people may have.
_10# Undo the last commit, keep the changes staged_10git reset --soft HEAD~1
Why it happens
#A commit is a snapshot of your files plus a pointer to its parent commit. A branch name such as main is only a label that points at the latest commit. So "undoing" a commit means one of two things:
- Moving the branch label back to the parent commit. This is what
git resetdoes. The commit is no longer part of the branch, but your files can stay exactly as they are. - Adding a new commit that does the opposite. This is what
git revertdoes. History keeps both commits.
git reset has three modes that decide what happens to your files when the label moves:
--softmoves the branch and keeps your changes staged.--mixed(the default) moves the branch and keeps your changes, but unstages them.--hardmoves the branch and overwrites your staging area and working files to match the older commit.
Only --hard touches the files on disk, which is why it is the one to avoid when you want to keep your work.
How to fix it
#Keep the changes staged with reset --soft
Use this when you want to commit the same work again, perhaps with a different message or an extra file.
_10git reset --soft HEAD~1_10git status # your changes are listed under "Changes to be committed"_10git commit -m "A better commit message"
Keep the changes unstaged with a mixed reset
Use this when you want to split the work into several smaller commits.
_10git reset HEAD~1_10# the same as: git reset --mixed HEAD~1_10_10git add -p # stage the changes piece by piece_10git commit -m "Add validation helpers"_10git add ._10git commit -m "Use validation in the signup form"
Fix the commit in place with amend
If you only need to change the message or include a file you forgot, you do not need to undo anything. Amend replaces the last commit with a new one.
_10git add forgotten-file.ts_10git commit --amend --no-edit # keep the existing message_10_10git commit --amend -m "Clearer message" # or replace the message
Amend creates a new commit with a new hash, so it counts as rewriting history in the same way a reset does.
Undo a pushed commit with revert
If the commit is already on a shared branch, revert it. Git creates a new commit that reverses the changes, and you can push it normally.
_10git revert HEAD_10git push
The original changes are still in history, so you can bring them back later with git revert on the revert commit, or with git cherry-pick and the original commit hash.
If you already ran reset --hard
Committed work is usually recoverable. git reflog lists where your branch pointed before the reset, and you can move back to it.
_10git reflog_10# a81b4de HEAD@{0}: reset: moving to HEAD~1_10# 3f2a1c9 HEAD@{1}: commit: Add signup form_10_10git reset --hard 3f2a1c9
Changes that were never committed are not recorded in the reflog, so --hard can lose them for good.
Common pitfalls
#- Running
git reset --hard HEAD~1to undo a commit. It also throws away the changes you meant to keep. - Resetting or amending a commit you have already pushed. Your next push is rejected, and forcing it rewrites the branch for everyone else. Use revert on shared branches.
- Counting wrongly.
HEAD~3moves back three commits. Check withgit log --onelinebefore you reset. - Trying
HEAD~1on the very first commit in a repository. There is no parent, so the command fails.git update-ref -d HEADremoves that first commit and leaves your files staged. - Using
HEAD^in Windows Command Prompt, where the caret is an escape character.HEAD~1avoids the problem.
Related reading
#Was this resource helpful?