How to undo the last commit in Git

Undoing the last commit is crucial when you realize you made an error or committed prematurely and need to make additional changes. As the creator of CoreUI, a widely used open-source UI library, I’ve needed to undo commits countless times during development over 25 years of software engineering. From my expertise, the safest approach is using git reset --soft HEAD~1, which undoes the commit but keeps all changes staged for easy recommitment. This provides flexibility to modify files before creating a corrected commit.

Use git reset --soft HEAD~1 to undo the last commit while keeping changes staged.

git reset --soft HEAD~1
# Changes are now staged, ready for a new commit
git commit -m "Corrected commit message"

Here git reset --soft HEAD~1 moves the HEAD pointer back one commit while keeping all changes in the staging area. This allows you to modify files, update the commit message, or split changes into multiple commits. The HEAD~1 refers to the commit before the current HEAD, effectively removing the last commit from history.

Best Practice Note:

This is the same commit correction approach we use in CoreUI development for maintaining clean project history. Use --soft to keep changes staged, --mixed (default) to unstage changes, or --hard to discard all changes completely. Never reset commits that have been pushed to shared repositories.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author