How to undo the last commit in Git

Undoing the last commit in Git allows you to correct mistakes, remove unwanted changes, or reorganize your commit history before sharing with the team. As the creator of CoreUI, a widely used open-source UI library, I’ve used git reset countless times across development workflows to fix commit mistakes and maintain clean project history. From my expertise, the most effective approach is using git reset with appropriate flags to control whether changes are preserved or discarded. This method provides flexible commit undoing with control over working directory and staging area states.

Use git reset with appropriate flags to undo commits while controlling the state of your changes.

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes unstaged
git reset HEAD~1
# or
git reset --mixed HEAD~1

# Undo last commit, discard all changes
git reset --hard HEAD~1

# Undo multiple commits (last 3 commits)
git reset --soft HEAD~3

# Check what will be undone before resetting
git log --oneline -5

# Alternative: create a new commit that undoes the last commit
git revert HEAD

The git reset command moves the current branch pointer backward. Use –soft to keep changes staged, –mixed (default) to keep changes unstaged, or –hard to discard changes completely. HEAD~1 refers to the commit before the current one. Always verify your changes with git log before resetting.

Best Practice Note:

This is the same git reset workflow we use in CoreUI development to maintain clean commit history and fix mistakes before pushing 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

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Use Bootstrap Tooltip in Vue 3 – The Right Way with CoreUI
How to Use Bootstrap Tooltip in Vue 3 – The Right Way with CoreUI

How to get element ID in JavaScript
How to get element ID in JavaScript

How to Add a Tab in HTML
How to Add a Tab in HTML

What is the difference between typeof and instanceof in JavaScript
What is the difference between typeof and instanceof in JavaScript

Answers by CoreUI Core Team