Git Undo Last Commit: Soft, Mixed, Hard, and Revert Commands (2026)
Need a fast pre-reset safety check? Compare staged and unstaged changes in Git Diff Viewer before choosing soft, mixed, or hard reset.
Need a clean recommit message after rollback? Draft it quickly in Git Commit Message Generator before you rerun git commit.
Need to pick reset mode without guesswork? Compare staged and unstaged hunks in Git Diff Viewer before choosing soft, mixed, or hard reset.
Need the full Git recovery toolkit first? Open Git troubleshooting tools to compare diffs, verify output, and reduce undo mistakes before changing history.
Need command syntax during incident response? Browse developer cheat sheets and keep the Git Commands Cheat Sheet open while you run reset and revert steps.
Start with a fast safety check: Review what will actually change in Git Diff Viewer before you reset or force-push.
Need a quick side-by-side patch review? Compare current and recovered output in Diff Checker before finalizing your rewritten commit.
Hit a push rejection instead? Follow the step-by-step fix in Git Push Rejected (Non-Fast-Forward): Fix Without Losing Work.
Seeing git index.lock exists after reset or rebase interruption? Use the index.lock + worktree collision recovery decision path before retrying push commands.
Already pushed to a shared branch? Use Git Undo Pushed Commit Guide when you need the safest recovery path first.
If you just made a bad commit, the fix depends on one question: do you want to keep your file changes? In most cases you do, and the right command is git reset --soft HEAD~1 or git reset HEAD~1.
If your bad commit was a config edit that started failing CI, use this yaml expected key fix decision path before retrying pushes.
If you are balancing release pressure and personal workload, route your next sprint in FocusKit Time Block Calculator before rewriting history under time stress.
This guide gives exact copy/paste commands for all common cases: keep changes staged, keep changes unstaged, discard everything, and safely undo a commit that is already pushed.
Table of contents
1. Copy/paste commands
Undo last commit and keep changes staged (most common)
git reset --soft HEAD~1
Undo last commit and keep changes unstaged
git reset HEAD~1
# same as: git reset --mixed HEAD~1
Undo last commit and discard all tracked changes (destructive)
git reset --hard HEAD~1
Undo last pushed commit safely on shared branch
git revert HEAD
git push origin <branch>
Undo last 3 commits and keep everything staged
git reset --soft HEAD~3
git status before and after undo commands. It confirms whether changes are staged, unstaged, or discarded.
Pre-undo safety checkpoint (staged vs unstaged)
Most "undo went wrong" incidents are not command mistakes, they are visibility mistakes. People run a reset while unclear on which changes are staged, which files were amended, and whether a force-push would overwrite teammate work.
Before you undo, capture a quick baseline:
- Run
git statusand note staged vs unstaged files. - Run
git log --oneline -n 5and copy the last known-good commit hash. - Inspect the exact per-file patch in Git Diff Viewer so you can confirm you are undoing only the intended changes.
This 60-second preflight makes the right command choice obvious: --soft when you want to keep staged edits, --mixed when you want to re-stage selectively, and revert when branch history is already shared.
2. Which command should you use?
| Your goal | Command | Result |
|---|---|---|
| Remove commit, keep all changes staged | git reset --soft HEAD~1 |
Commit removed, files still staged |
| Remove commit, keep changes but unstage | git reset HEAD~1 |
Commit removed, files unstaged |
| Remove commit and throw away tracked edits | git reset --hard HEAD~1 |
Commit and tracked changes deleted |
| Undo pushed commit without rewriting history | git revert HEAD |
New inverse commit is created |
git reset --hard is destructive. If you are not 100% sure, use --soft or --mixed first, or stash changes before hard reset.
3. Local commit workflows
Case A: Commit message is wrong
Use soft reset, then recommit with a corrected message:
git reset --soft HEAD~1
git commit -m "Correct message"
Case B: You committed too early and want to edit files
Use mixed reset to unstage and keep files editable:
git reset HEAD~1
# edit files
# then stage only what you want
git add -p
git commit -m "Better scoped commit"
When this split gets complex, run a quick side-by-side in Text Compare to keep each replacement commit focused on a single intent.
Case C: Split one bad commit into two clean commits
git reset HEAD~1
git add -p
git commit -m "Part 1"
git add -p
git commit -m "Part 2"
If you frequently do this, the deeper command behavior is covered in Git Reset: Soft, Mixed, Hard & Keep.
4. Already pushed commit workflows
Recovery options: safe revert path vs force-push caveat
If the bad commit is already on a shared branch, default to git revert. Revert adds a new commit that undoes the change, keeps history linear for teammates, and avoids rewriting remote history.
Force-push is only reasonable when you fully control the branch and intentionally rewrote history. Even then, use --force-with-lease so Git aborts if remote moved unexpectedly.
| Situation | Recommended path | Why |
|---|---|---|
Shared branch (main, release, hotfix) |
git revert <commit> then normal push |
Preserves audit trail and protects collaborators |
| Private feature branch (only you use it) | git reset ... then git push --force-with-lease |
Allows clean history with guardrails against overwrite |
| Unsure who already pulled the branch | Treat as shared and use revert | Minimizes risk of rewriting someone else's base |
git push --force can overwrite teammate commits silently. If you truly need rewrite, prefer git push --force-with-lease and coordinate first.
# safe shared-branch recovery
git revert HEAD
git push origin main
# history rewrite only on private branch you control
git reset --soft HEAD~1
git push --force-with-lease origin your-branch
5. Recovery if you used the wrong command
If you accidentally ran git reset --hard and lost a commit, check reflog immediately:
git reflog
# find the commit before the reset, then:
git reset --hard <that-hash>
Reflog usually recovers committed history. It cannot recover uncommitted edits that were never recorded.
6. FAQ
Can I undo the last commit without changing files at all?
Yes. git reset --soft HEAD~1 removes the commit while preserving your exact file state and staging state.
What is the difference between HEAD^ and HEAD~1?
For normal commits they are equivalent. For merge commits, HEAD^ refers to the first parent and HEAD~1 follows the first-parent chain by one step.
Can I undo only the latest pushed merge commit?
Yes, but use git revert -m 1 <merge-hash>. See Git Revert a Merge Commit for the exact parent-selection workflow.
Where do I learn full reset vs revert vs restore decisions?
Use Git Undo: Reset, Revert & Restore for the full decision map.
How do I undo git add after resetting?
Run git restore --staged <file> or git restore --staged .. Detailed examples are in Undo git add.