git
version control
git reset
undo changes
git commands

Reset all changes after last commit in git

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Undoing changes after the last commit in Git can mean several different things: removing the last commit but keeping the work, discarding the last commit entirely, or cleaning up untracked files as well. The safest command depends on exactly what state you want to end up with.

Decide What You Want to Preserve

Before running any reset command, decide whether you want to keep:

  • the commit history
  • the staged changes
  • the working-directory changes
  • untracked files

Those are separate concerns, and Git gives you different commands for each.

Remove the Last Commit but Keep the Changes Staged

If you want to undo the commit itself but keep the changes ready to recommit, use a soft reset.

bash
git reset --soft HEAD~1

This moves HEAD back one commit and leaves the content staged. It is useful when the last commit message was wrong or when you want to squash the changes differently.

Remove the Last Commit and Unstage the Changes

If you want to keep the file changes but move them back to the working tree, use the default mixed reset.

bash
git reset --mixed HEAD~1

Now the last commit is gone, and the files are modified but unstaged.

This is a good choice when you want to rework the content before committing again.

Discard the Last Commit and Its Changes Completely

If you want the repository to look exactly like it did before the last commit, use a hard reset.

bash
git reset --hard HEAD~1

This is destructive. The last commit and its tracked-file changes are discarded from the current branch state.

Use it only when you are sure you do not need the work or when you know how to recover it from the reflog if necessary.

Clean Up Untracked Files Separately

A hard reset affects tracked files. It does not remove untracked files or directories.

If you also want to remove untracked content, use git clean explicitly.

bash
git clean -fd

This combination:

bash
git reset --hard HEAD
git clean -fd

returns the working tree to the last commit and removes untracked files as well.

Do Not Rewrite Shared History Carelessly

If the last commit has already been pushed and other people may have based work on it, resetting the branch changes history and can disrupt collaborators.

In that situation, git revert HEAD is often safer than git reset, because it creates a new commit that undoes the previous one without rewriting history.

bash
git revert HEAD

So the right answer depends not just on your local files, but on whether the commit is already shared.

A Simple Decision Guide

Use:

  • 'git reset --soft HEAD~1 to keep changes staged'
  • 'git reset --mixed HEAD~1 to keep changes but unstage them'
  • 'git reset --hard HEAD~1 to discard the commit and tracked changes'
  • 'git clean -fd to remove untracked files too'
  • 'git revert HEAD when the commit is already shared'

That mental model is more important than memorizing one “undo” command.

Common Pitfalls

The most common mistake is using git reset --hard when the goal was only to edit the last commit message or adjust staged content.

Another common issue is forgetting that git clean and git reset affect different kinds of files. Developers also often rewrite history on a shared branch when git revert would have been safer for the team.

Summary

  • “Reset after the last commit” can mean several different final states.
  • Use --soft to keep changes staged.
  • Use --mixed to keep changes but unstage them.
  • Use --hard only when you truly want to discard tracked changes.
  • Use git revert instead of reset when the commit has already been shared.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.