Undoing Changes
The git reset command moves the branch pointer and optionally updates the staging area and working directory.
Reset Modes
| Mode | HEAD | Staging | Working Dir |
|---|---|---|---|
--soft | ✓ moves | unchanged | unchanged |
--mixed | ✓ moves | ✓ reset | unchanged |
--hard | ✓ moves | ✓ reset | ✓ reset |
Using Reset
Soft Reset
Moves HEAD but keeps changes staged:
git reset --soft HEAD~1
Use case: Redo the last commit with different message.
Mixed Reset (Default)
Moves HEAD and unstages changes:
git reset HEAD~1
# or
git reset --mixed HEAD~1
Use case: Undo commit but keep changes for editing.
Hard Reset
Moves HEAD and discards all changes:
git reset --hard HEAD~1
Warning: This permanently deletes uncommitted changes!
Common Reset Targets
| Target | Description |
|---|---|
HEAD | Current commit |
HEAD~1 | Previous commit |
HEAD~3 | Three commits ago |
abc1234 | Specific commit |
origin/main | Remote branch |
Unstaging Files
Remove files from staging area:
git reset HEAD filename.txt
# or (newer)
git restore --staged filename.txt
Recovering from Reset
If you accidentally reset:
# Find lost commit
git reflog
# Reset back to it
git reset --hard abc1234
Reset vs Revert
| Command | Effect |
|---|---|
reset | Rewrites history (use locally) |
revert | Creates new commit (safe for shared) |
Exercise: Reset Staging Area
Reset the staging area while keeping working directory changes:

