This document explains the three main types of git reset
and when to use each.
- Removes the last two commits.
- Keeps all changes in the staging area (index).
- Nothing is lost; changes are ready to be committed again.
- You want to rewrite or squash commits.
- You want to change the commit message or combine multiple commits.
git reset --soft HEAD~2
git commit -m "new single commit message"
git push --force
- Removes the last two commits.
- Keeps changes in the working directory, but unstages them.
- You'll need to
git add
again before committing.
- You want to reselect or re-stage the changes.
- You want to edit or clean up before committing again.
git reset --mixed HEAD~2
# make changes or stage selected files
git add .
git commit -m "cleaned up commit"
git push --force
- Removes the last two commits.
- Deletes all changes in those commits and in the working directory.
- Irreversible unless backed up.
- You are sure you don’t want to keep the changes.
- You want a clean state of the third-to-last commit.
git reset --hard HEAD~2
git push --force
Command | Commits removed | Staging area | Working directory |
---|---|---|---|
--soft |
✅ | ✅ | ✅ |
--mixed (default) |
✅ | ❌ | ✅ |
--hard |
✅ | ❌ | ❌ |
Use --force
only when you're sure that no one else is working on the branch, as it will overwrite the remote history.