Loading lessons...
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
| Mode | Staging area | Working directory |
|---|---|---|
--soft | kept | kept |
--mixed (default) | reset | kept |
--hard | reset | reset (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 resetmoves the branch pointer backward.--softkeeps changes,--mixedunstages them,--harddeletes them.--hardpermanently discards changes.- Use
--soft HEAD~1to undo the last commit but keep the work.