How do I undo the most recent local commits in Git?
I accidentally committed the wrong files to Git but haven’t pushed the commit to the server yet. How do I undo those commits from the local repository?
You can safely undo recent local commits in Git using the git reset command with different flags depending on what you want to achieve. The safest method for undoing local commits that haven’t been pushed is git reset --soft HEAD~1, which removes the commit but keeps your changes staged and ready to be committed again.
Contents
- Understanding Git Reset Modes
- Safe Methods for Undoing Local Commits
- Step-by-Step Guide with Examples
- Safety Precautions and Best Practices
- What to Do After Undoing Commits
Understanding Git Reset Modes
Git provides three main modes for the reset command, each handling your repository state differently:
git reset --soft: Only moves the HEAD pointer to the previous commit. Your changes remain in the staging area and working directory, ready to be committed again.git reset --mixed(default): Moves HEAD pointer and resets the staging area to match the previous commit, but leaves your working directory unchanged. Changes become unstaged but still exist.git reset --hard: Moves HEAD pointer, resets the staging area, AND updates your working directory to match the previous commit. This is the most dangerous option as it permanently discards changes.
⚠️ Important Warning: According to GeeksforGeeks, using
--hardcan permanently delete data, so you should always check your current state withgit statusbefore performing a hard reset.
Safe Methods for Undoing Local Commits
Method 1: Undo Last Commit (Keep Changes Staged)
For when you want to undo the last commit but keep all changes ready to be re-commited:
git reset --soft HEAD~1
This is the safest method for local commits as it doesn’t lose any work. As Medium contributor Sivaraaj explains, this “keeps the changes but undo the last commit.”
Method 2: Undo Last Commit (Keep Changes Unstaged)
For when you want to undo the last commit but keep the changes in your working directory to be edited:
git reset --mixed HEAD~1
# or simply (since --mixed is the default):
git reset HEAD~1
As Stack Overflow explains, this “resets the HEAD and updates the staging area, but leaves the working directory unchanged.”
Method 3: Undo Last Commit (Discard All Changes)
For when you want to completely undo the last commit and discard all changes:
git reset --hard HEAD~1
⚠️ Extreme Caution: As noted by TheServerSide, this “blows out everything” and can cause permanent data loss. Only use this when you’re certain you don’t need any of the changes.
Method 4: Undo Multiple Commits
To undo multiple recent commits:
# Undo last 3 commits, keeping changes staged
git reset --soft HEAD~3
# Undo last 3 commits, keeping changes unstaged
git reset HEAD~3
# Undo last 3 commits, discarding all changes
git reset --hard HEAD~3
Step-by-Step Guide with Examples
Scenario: You accidentally committed wrong files
Let’s say you committed some temporary files by mistake, and now you want to undo that commit:
-
Check your current state first:
bashgit log --oneline -3 # Should show your accidental commits -
Undo the last commit safely (method 1):
bashgit reset --soft HEAD~1
-
Verify the result:
bashgit status # Should show your files as staged changes git log --oneline -3 # Should now show one less commit -
Re-commit correctly (if desired):
bashgit commit -m "Correct commit message"
Alternative Approach: Using git reset HEAD
If you haven’t committed yet but just want to unstage files:
# Unstage all files
git reset HEAD
# Or unstage specific files
git reset HEAD path/to/file.txt
As Noble Desktop explains, “the commits will be removed, but the changes will appear as uncommitted, giving you access to the code.”
Safety Precautions and Best Practices
Always Check Before Resetting
Before performing any reset operation:
# Check what you're about to do
git status
git log --oneline -5
git diff --cached # See staged changes
git diff # See unstaged changes
Use git reflog for Safety
The GitKraken documentation suggests using git reflog to see your recent actions:
git reflog
# Shows a chronological list of your actions
# Including previous commit states you can return to
Never Use --hard on Shared Branches
As Warp.dev warns, “if other developers are using the commits you have undone, then this will cause conflicts.” Use --soft or --mixed instead when working with shared branches.
Create a Backup Before Major Changes
For important operations:
# Create a backup branch before hard reset
git branch backup-before-reset
git reset --hard HEAD~1
What to Do After Undoing Commits
If You Need to Preserve Changes
After using git reset --soft or git reset --mixed:
- Review your changes carefully
- Make any necessary corrections
- Commit again with the proper message
# Review what changed
git diff --cached # staged changes
git diff # unstaged changes
# Make corrections if needed
# Then commit
git commit -m "Proper commit message"
If You Need to Discard Changes
After using git reset --hard:
- Double-check that you don’t need any of the changes
- Consider creating a backup branch first (as shown above)
- Accept that the changes are permanently lost
When Working with Others
If you’ve accidentally committed to a shared branch:
- Avoid using
--hardon shared branches - Consider using
git revertinstead (though this creates a new commit rather than removing the old one) - Communicate with your team about what you’re doing
💡 Pro Tip: According to Aviator Blog, “the safest approach is to create a new commit that reverses the changes” when working with shared repositories.
Conclusion
Undoing recent local commits in Git is straightforward when you choose the right method for your situation. The key takeaways are:
-
Use
git reset --soft HEAD~1when you want to undo the last commit but keep all changes staged and ready to be re-commited - this is the safest method for local commits. -
Use
git reset --mixed HEAD~1(or justgit reset HEAD~1) when you want to undo the last commit but keep the changes in your working directory as unstaged modifications. -
Avoid
git reset --hardunless you’re absolutely certain you want to permanently discard all changes, as it can permanently delete work. -
Always check your current state with
git statusandgit logbefore performing any reset operation. -
Consider using
git reflogas a safety net to see your recent actions and potentially recover from mistakes.
By following these safe methods and precautions, you can confidently undo commits without losing important work or disrupting your workflow.
Sources
- Stack Overflow - How do I undo the most recent local commits in Git?
- GeeksforGeeks - How to Undo the Most Recent Local Commits in Git
- Medium - How to Undo the Most Recent Local Commits in Git
- Stack Overflow - What’s the difference between git reset --mixed, --soft, and --hard?
- GeeksforGeeks - What’s The Difference Between git reset --mixed, --soft, and --hard?
- TheServerSide - Git reset hard vs. soft: What’s the difference?
- Noble Desktop - Undo Changes in Git
- Warp.dev - How To Undo Your Last Git Commit(s)
- Aviator Blog - How to Git Undo Commit: Best Practices
- GitKraken - Git Reset | Hard, Soft & Mixed | Learn Git