Git Undo Last Commit: Safe Ways to Keep Changes
In this article
To undo the last commit while keeping your changes, use git reset --soft HEAD~1. This moves the branch pointer back one commit, leaving your files and changes staged, ready to recommit or modify.
What This Command Does
Git stores history as a chain of commits. Each commit has a parent, and HEAD points to the latest commit in your current branch. When you run git reset --soft HEAD~1, Git moves HEAD (and your branch pointer) back to the previous commit, effectively removing the last commit from history. However, the changes that were part of that commit remain in your working directory and staging area. This is the safest way to undo a commit because no data is lost—your work is still there, just not committed yet.
Why Use --soft Instead of --mixed or --hard
Git reset has three main modes, each affecting your files differently:
- --soft: Moves the branch pointer, keeps changes staged. Your files are unchanged, and the changes from the undone commit are in the index (staged).
- --mixed (default): Moves the branch pointer, unstages changes. Your files are unchanged, but the changes are now in your working directory, unstaged.
- --hard: Moves the branch pointer, discards changes completely. Your files are reverted to the state of the previous commit, and the changes are lost.
For keeping changes safely, --soft is ideal because it preserves the staging exactly as it was when you committed. If you want to unstage them as well, use --mixed. Avoid --hard unless you are certain you want to delete the changes forever.
Step-by-Step: Undo and Keep Changes
- Check your current status:
git statusto confirm you are on the right branch. - Run
git reset --soft HEAD~1(orgit reset --soft HEAD^on Windows). - Verify with
git status—you should see your changes staged, ready to be committed again. - If you need to edit files, do so now. The changes are still in your working directory.
- Commit again with
git commit -m "your message".
If you committed by mistake and want to undo multiple commits, replace 1 with the number of commits to go back, e.g., git reset --soft HEAD~3.
What If You Already Pushed the Commit?
If the commit you want to undo has already been pushed to a remote repository, resetting locally creates a divergence. You will need to force-push to update the remote, but this rewrites public history and can disrupt collaborators. A safer alternative is to use git revert, which creates a new commit that undoes the changes of the target commit. This keeps history intact and is safe for shared branches. Use git revert HEAD to undo the last commit while preserving history.
Common Mistakes and How to Avoid Them
- Using --hard accidentally: Always double-check your command before running.
git reset --hardwill permanently delete changes, and they cannot be recovered unless you had a stash or another branch. - Resetting the wrong commit: If you use
HEAD~1but meant to undo a different commit, you might lose track. Usegit log --onelineto see your history before resetting. - Forgetting to commit again: After a soft reset, your changes are staged but not committed. If you switch branches, those staged changes may carry over, causing confusion.
- Confusing reset with revert: Reset moves the branch pointer; revert creates a new commit. For shared branches, revert is safer.
Alternatives to git reset
Besides reset, you can use:
- git revert: Creates a new commit that undoes the target commit. Safe for shared history.
- git commit --amend: If you want to change the last commit's message or add more changes, this modifies the commit in place. It does not remove the commit but replaces it.
- git stash: If you want to temporarily save changes and clean your working directory, stash them. This is not for undoing commits but for switching context.
How to Recover If You Accidentally Used --hard
If you ran git reset --hard and lost changes, there is still hope. Git keeps a reflog of all HEAD movements. Run git reflog to see a list of recent actions. Find the commit hash before the reset, then run git reset --hard <hash> to restore. This works as long as the commit is still in the reflog (usually 90 days). If you had uncommitted changes that were not staged, they cannot be recovered this way.
Frequently Asked Questions
Does git reset --soft delete my files?
No, it only moves the branch pointer. Your files remain exactly as they were, and the changes are staged. Nothing is deleted.
What's the difference between git reset and git revert?
Reset moves the branch pointer backward, removing commits from history. Revert creates a new commit that undoes the changes, preserving history. Use revert for shared branches, reset for local, unpublished commits.
Can I undo a commit after pushing?
Yes, but it's risky. You can reset and force-push, but this rewrites history and can break collaborators' repos. Better to use revert, which is safe for public history.
How do I undo the last commit but keep changes unstaged?
Use git reset --mixed HEAD~1 (or simply git reset HEAD~1). This unstages the changes, leaving them in your working directory.
What if I want to undo a commit but keep some changes and discard others?
After a soft or mixed reset, you can selectively stage or discard changes using git add and git checkout -- <file> (or git restore). This gives you full control over what to keep.
Is there a way to undo a commit without using command line?
Most Git GUI tools (like GitKraken, SourceTree, or the Git integration in VS Code) offer an "Undo Commit" or "Reset" option. They typically use the same underlying commands, so the behavior is the same.