How to undo git commit --amend
Accidentally amending the wrong commit or amending with incorrect changes requires undoing the amend operation to restore the previous state. With over 25 years of software development experience and as the creator of CoreUI, I’ve had to undo mistaken amends countless times. Git’s reflog tracks all HEAD movements including amends, allowing you to reset to the commit state before the amend. This approach safely restores your repository to its pre-amend state without losing work.
Use git reset with the reflog to undo a commit amend and restore the previous commit state.
# View reflog to find commit before amend
git reflog
# You'll see something like:
# abc1234 HEAD@{0}: commit (amend): Updated message
# def5678 HEAD@{1}: commit: Original message
# Reset to commit before amend
git reset --soft HEAD@{1}
# Or use the commit hash
git reset --soft def5678
Different reset modes:
# Keep changes staged (recommended)
git reset --soft HEAD@{1}
# Keep changes unstaged
git reset --mixed HEAD@{1}
# Discard all changes (DANGER - loses work)
git reset --hard HEAD@{1}
If you already pushed the amended commit:
# Reset locally
git reset --soft HEAD@{1}
# Force push (use with caution)
git push --force-with-lease
# Or create new commit reverting the amend
git revert HEAD
Check what will be reset:
# View commit before amend
git show HEAD@{1}
# Compare current commit with previous
git diff HEAD@{1} HEAD
Restore specific files from before amend:
# Restore single file
git checkout HEAD@{1} -- path/to/file.js
# Restore multiple files
git checkout HEAD@{1} -- file1.js file2.js
Best Practice Note
Always use --soft reset to undo amend—it keeps your changes staged so you can re-commit correctly. The reflog keeps track of all HEAD movements for 90 days by default. Use git reflog to find the exact commit before the amend. If you’ve pushed the amended commit, coordinate with your team before force pushing. For shared branches, create a new commit instead of force pushing. Use --force-with-lease instead of --force for safer force pushes. This is how we handle amend mistakes in CoreUI development—quickly reverting to pre-amend state using reflog without losing any work.



