Lesson 23 +20 XP

Git Reset

Git Reset

git reset moves a branch pointer backward, which removes commits from your current branch's history.

The reset command

git reset --hard abc123

This moves your branch back to commit abc123 and discards everything after it. The commits are gone from this branch.

Three modes

ModeStaging areaWorking directory
--softkeptkept
--mixed (default)resetkept
--hardresetreset (files rewritten)
  • --soft: moves the pointer, keeps all changes staged.
  • --mixed: moves the pointer, keeps changes but unstages them.
  • --hard: moves the pointer and throws away changes.

Danger!

--hard discards your changes permanently in your working directory. Only use it when you are sure. Once files are overwritten, recovery is hard.

Reset the last commit (keep changes)

git reset --soft HEAD~1

This removes the last commit but keeps its changes ready in the staging area, so you can redo the commit.

TL;DR

  • git reset moves the branch pointer backward.
  • --soft keeps changes, --mixed unstages them, --hard deletes them.
  • --hard permanently discards changes.
  • Use --soft HEAD~1 to undo the last commit but keep the work.