Tour of Git
History & Undoing Changes / Lesson 3.5

git reset — Undoing Changes

The git reset command moves HEAD (and optionally the staging area and working directory) to a different commit. It's the primary tool for undoing commits.

Three Modes

git reset --soft HEAD~1    # Undo commit, keep changes staged
git reset --mixed HEAD~1   # Undo commit, unstage changes (default)
git reset --hard HEAD~1    # Undo commit, discard all changes

Think of them as levels of "undo":

ModeCommitIndexWorking Dir
--softUndoKeepKeep
--mixedUndoUndoKeep
--hardUndoUndoUndo

HEAD~1 Syntax

  • HEAD~1 — One commit before HEAD
  • HEAD~2 — Two commits before HEAD
  • Or use a specific SHA: git reset --soft abc1234

Try it!

Practice the reset modes:

  1. Create two commits:
    • echo "first" > file.txt && git add file.txt && git commit -m "First commit"
    • echo "second" > file.txt && git add file.txt && git commit -m "Second commit"
  2. Soft reset: git reset --soft HEAD~1
  3. Check status: git status — your change is still staged!
  4. Re-commit: git commit -m "Second commit (redo)"
  5. Now hard reset: git reset --hard HEAD~1
  6. Check: cat file.txt — shows "first", the second commit is gone

Warning

git reset --hard is destructive — it permanently discards uncommitted changes. Always make sure you don't need those changes before using it.

--soft and --mixed are safe — they preserve your work, just move it between states.

🎯

Goal

Create 2 commits, then use git reset --soft to undo the last one (keeping changes staged)

Terminal
$

No commits yet. Run `git init` and create your first commit.