Undo the Last Commit in Git
How to Undo Your Most Recent Git Commit: A Step by Step Guide
Published by Carlo van Wyk on June 15, 2025 in Git

The method you use to undo the most recent Git commit depends on whether you've pushed your changes to a remote repository or not. Here's how to handle both scenarios.
Undoing the Last Local Commit (Not Yet Pushed to Remote Respository)
When you haven't pushed your most recent commit to a remote repository yet, you can safely use
git reset
to undo it. You have two main options:Option 1 to Undo Last Local Commit: git reset --soft
git reset --soft HEAD~1
What it does:
- Removes the last commit from the Git history
- Keeps all the changes from that commit in your staging area (ready to be committed again)
- Your working directory remains unchanged
When to use it: When you want to modify the commit message or add more changes before recommitting.
Option 2 to Undo Last Local Commit: git reset --hard
git reset --hard HEAD~1
What it does:
- Removes the last commit from the Git history
- Completely discards all changes from that commit
- Resets both your staging area and working directory to match the previous commit
When to use it: When you want to completely eliminate the last commit and all its changes.
This option will permanently delete your most recent changes.
Undoing a Remote Commit (Already Pushed)
When you've already pushed your most recent commit to a remote repository, you should never use
git reset
because it rewrites history and will cause problems for other collaborators, as their local histories will no longer align with the rewritten remote history, leading to merge conflicts for your entire team.The recommended approach is to use
git revert
.git revert HEAD
What it does:
- Creates a new commit that undoes the changes from the last commit
- Preserves the original commit in the Git history
- Safe to use on shared repositories
Why use git revert instead of reset:
- Doesn't rewrite Git history
- Safe for collaboration
- Maintains a clear record of what was changed and then undone
After running git revert, you'll need to push the new revert commit:
git push origin head
Key Differences between Git Reset and Git Revert
- Use
git reset
when you haven't pushed the commit - Use
git revert
when you've already pushed the commit to a remote repository