Quick answer
To undo the last Git commit without losing your work, use git reset –soft HEAD~1 when you want the changes to stay staged. Use git reset HEAD~1 if you want the same changes left in your working directory, just unstaged.
git reset --soft HEAD~1Use reset only while the commit is still local and hasn’t been pushed. If it’s already been pushed to a shared branch, use git revert HEAD instead. That creates a new commit that safely reverses the previous one.
git revert HEADSafe rule use reset for local commits, and use revert for commits other people may already have pulled.
What you’ll learn
- How to undo the last Git commit while keeping your code changes
- How –soft, –mixed, and –hard differ
- What to do if the commit was already pushed to GitHub, GitLab, or Bitbucket
- How to check that your changes are still safe after undoing the commit
Before you start
- Git installed on your computer
- A terminal or command prompt opened inside your repository
- Basic familiarity with
git statusandgit log
Before you run any undo command, check your current branch and working tree. It takes a few seconds, and it can save you from fixing the wrong thing.
git status
git branch --show-currentWhich command should you use?
| Situation | Command | What happens to your changes? |
|---|---|---|
| Undo last local commit and keep changes staged | git reset --soft HEAD~1 | Changes stay staged and ready to recommit |
| Undo last local commit and keep changes unstaged | git reset HEAD~1 | Changes stay in your files but are unstaged |
| Undo a commit that was already pushed | git revert HEAD | A new commit reverses the previous commit safely |
| Delete the commit and discard its changes | git reset --hard HEAD~1 | Changes are removed from your working tree |
Step-by-step instructions
1. Check the last commit
Start by confirming the commit you’re about to undo. That’s the simple guardrail against resetting the wrong branch or the wrong commit.
git log --oneline -5You’ll see recent commits in a short format. The top entry is the current HEAD, meaning the latest commit on your branch.
2. Undo the last commit but keep changes staged
If the mistake is only the commit message, a missing file, or the way you grouped the changes, use a soft reset.
git reset --soft HEAD~1This removes the last commit from the branch history. Your changes, though, stay in the staging area. Run this next:
git statusYour files should appear under changes to be committed. From there, you can amend the files, add anything missing, and commit again.
git add .
git commit -m 'Updated commit message'3. Undo the last commit and keep changes unstaged
Maybe you want to inspect the changes before staging them again. In that case, use the default mixed reset:
git reset HEAD~1This removes the last commit, but it leaves the modified files in your working directory. Nothing is staged, so you can go file by file and review what changed.
git diff
git statusWhen you’re ready, stage the file you want and commit again:
git add path/to/file
git commit -m 'Better commit message'4. Undo a commit that has already been pushed
If the commit has been pushed to a shared remote branch, don’t rewrite history unless you’re absolutely sure nobody else is using that branch. The safer option is git revert.
git revert HEADGit will create a new commit that reverses the changes introduced by the last commit. After that, push normally:
git pushThis is the best approach for shared branches such as main, master, develop, or team feature branches.
5. Undo the last commit and force push, if you really need to
Only do this on a branch you own, such as a personal feature branch. A force push rewrites remote history, and it can disrupt other developers.
git reset --soft HEAD~1
git push --force-with-leasePrefer --force-with-lease over --force. It refuses to overwrite remote work if someone else pushed changes after your last fetch.
Examples
Example 1. Fix the last commit message
If the only problem is the commit message, you may not need to reset at all. Use amend instead:
git commit --amend -m 'Correct commit message'If the commit was already pushed, you’ll need to push with lease:
git push --force-with-leaseExample 2. Add a forgotten file to the last commit
git reset --soft HEAD~1
git add forgotten-file.txt
git commit -m 'Add complete feature changes'This recreates the commit with the missing file included.
Example 3. Undo the last commit but keep editing
git reset HEAD~1Now your files are back as local modifications. You can edit, test, split the changes into multiple commits, or discard individual files if needed.
Common mistakes
- Using –hard by accident
git reset --hard HEAD~1removes the commit and discards the file changes from your working tree. - Resetting a shared branch If the commit is already pushed and others may have pulled it, use
git revert. - Not checking the branch first Always run
git branch --show-currentbefore changing history. - Force pushing with –force Use
--force-with-leasewhen you must rewrite remote history.
Good habits
- Create a backup branch before risky history edits:
git branch backup-before-reset - Use
git statusbefore and after running reset or revert commands - Use
git reverton shared branches to keep team history clean - Use small commits so mistakes are easier to undo
- Read the output Git prints after every command; it usually tells you exactly what changed
Check your work
After undoing the commit, check both your history and your files.
git log --oneline -5
git statusIf you used git reset --soft HEAD~1, your files should appear as staged. If you used git reset HEAD~1, they should show up as modified but unstaged. And if you used git revert HEAD, you should see a new revert commit at the top of the log.
Recovery tip if you reset the wrong commit
If you made a mistake, Git often lets you recover using the reflog. It records where HEAD recently pointed.
git reflogFind the commit hash from before the reset, then restore it:
git reset --hard commit_hashUse this carefully. If you have uncommitted changes you want to keep, save them first with git stash or copy them somewhere else.