How do I undo the most recent local commits in Git?
Sometimes you need to roll back more than just the last commit. Maybe a series of changes introduced a bug, or you want to squash several work-in-progress commits before pushing. Git's reset command handles this by moving HEAD back by however many commits you specify.
Soft reset (keep changes staged)
A soft reset undoes the commits but leaves all the changes staged and ready to recommit:
$ git reset --soft HEAD~3
This is useful when you want to collapse several commits into one. After running this, all the work from those three commits is still in your staging area. Just run git commit to create a single, cleaner commit.
Mixed reset (keep changes unstaged)
The default reset mode moves HEAD back and unstages everything, but your files stay as they are in the working directory:
$ git reset HEAD~3
This gives you the chance to review your changes, re-stage selectively, and commit in a different arrangement. It is the safest local option since nothing is deleted.
Hard reset (discard everything)
WARNING: This permanently deletes all changes from the undone commits. You cannot recover them.
$ git reset --hard HEAD~3
HEAD is now at d240853 Initial commit
Only use this when you genuinely want to throw away the work. Once it is gone, it is gone (unless you act quickly with git reflog, but do not count on that as a safety net).
Using a specific commit hash
Instead of counting commits back from HEAD, you can reset directly to a known commit:
$ git log --oneline
f4a1b2c Fix typo in README
e3d2c1b Add caching layer
d240853 Initial commit
$ git reset --soft d240853
This undoes everything after d240853, keeping the changes staged. Replace --soft with --mixed or --hard depending on what you want to keep.
If the commits were already pushed
Rewriting shared history with git reset breaks things for anyone else working on the branch. Use git revert to create new commits that undo the changes without altering history:
$ git revert HEAD~3..HEAD
This creates three new revert commits, one for each of the commits being undone. The original history stays intact, and your teammates can pull without conflicts.
For more detail on the difference, see git reset vs git revert. If you only need to undo a single commit, see how to undo your last commit. You can also explore the full git reset reference or read about reverting to a previous commit.
DeployHQ deploys directly from your Git repository, so you can push a clean history and have it live in seconds. Try it free.