There are several ways to undo the most recent commits in Git depending on whether you want to keep the changes or delete them entirely.
# 1. Undo the last commit but keep the changes in your working directory
git reset --soft HEAD~1
The last commit is undone.
All changes from that commit stay staged (in index).
If you want them unstaged (just in working dir), use:
git reset HEAD~1
# 2. Undo the last commit and discard the changes completely
git reset --hard HEAD~1
This will delete the last commit and its changes from your working directory.
# 3. Undo multiple commits (e.g., last 3 commits)
git reset --hard HEAD~3
Or keep the changes:
git reset --soft HEAD~3
# 4. If the commits are already pushed, use git revert instead
To undo a pushed commit safely (without rewriting history):
git revert HEAD
This will create a new commit that "reverses" the changes made by the last one.