This guide explains how to remove the last two commits from your branch and restore the state to the third commit from the end (keeping your working tree clean and history intact).
- You're on a feature branch (e.g.,
feature/add-auth
) based onmain
- You've made 4 commits
- You want to remove the last 2 commits, keeping the state from the third last commit
git checkout feature/add-auth
git reset --hard HEAD~2
This removes the last two commits and resets your working directory to the state of the third commit from the end.
git reset --soft HEAD~2
Keeps all changes in the staging area, allowing you to re-commit.
git reset --mixed HEAD~2
Useful if you want to selectively add files again before committing.
git push --force
Your branch feature/add-auth
will be set back to the state of the third last commit, and the last two commits will be removed from history.