HomebaseWorks
Lesson 2 of 4

Undoing changes in Git: reset, revert, restore and stash

Pick the right undo command for each situation, and know which ones are safe after you have pushed.

EWritten by the HomebaseWorks editorial team · reviewed 26 Oct 2026

Screenings often ask how to undo something, because choosing the wrong command can delete work or break a shared branch. The right choice depends on two questions: where is the change now, and has it been pushed?

Advertisement

The three places a change can be

  • Working directory: edited files not yet staged.
  • Staging area: changes added with git add, waiting for the next commit.
  • Commits: saved history, which may also be on the remote after a push.

git status tells you which of these your change is in. Read it before running any undo command.

Undoing uncommitted work

  • git restore --staged file.py unstages a file but keeps your edits.
  • git restore file.py throws away unstaged edits to that file. This cannot be undone.
  • git stash puts all uncommitted changes aside so you can switch branches. git stash list shows what is saved and git stash pop brings the latest back.

Undoing commits: reset versus revert

git reset moves your branch back to an earlier commit. The mode decides what happens to the changes:

git reset --soft HEAD~1   # undo commit, keep changes staged
git reset --mixed HEAD~1  # undo commit, keep changes unstaged (default)
git reset --hard HEAD~1   # undo commit and discard the changes

git revert <hash> works differently. It adds a new commit that reverses an old one, so history only grows. That makes revert the right tool for anything already pushed to a shared branch.

A simple rule works in most cases:

  1. Not pushed yet: reset is fine, choosing soft or mixed if you want to keep the work.
  2. Already pushed and others may have pulled: use revert.
  3. Never force-push over a shared branch to hide a mistake unless the team has agreed to it.

When something seems lost

Commits removed by a reset are usually still recoverable for a while. git reflog lists where HEAD has been, and you can return with git reset --hard to the hash shown there. Uncommitted changes discarded with --hard or restore are not recorded, so they are truly gone.

Checklist before undoing: run git status, decide whether the change is pushed, prefer revert for shared history, and stash anything you are not sure about before using --hard.

Advertisement
Continue with Git4 lessons · 1 interview set · 1 test · 1 certificate
Not tested

More in Cloud and DevOps