Git reset = Go back in time by undoing commits.
Concept:
Analogy: It is like erasing pages from a notebook.
| Mode | Commits | Index/Staging | Working Directory |
|---|---|---|---|
| --soft | Undoes | Keeps | Keeps |
| --mixed | Undoes | Clears | Keeps |
| --hard | Undoes | Clears | Clears |
Simple memo:
Usage: Redo the last commit with more changes.
mkdir demo-reset && cd demo-reset
git init
echo "print('v1')" > app.py && git add . && git commit -m "Version 1"
echo "print('v2')" > app.py && git add . && git commit -m "Version 2"
# Soft reset: undo commit, keep everything in staging
git reset --soft HEAD~1
git status # Staged changes ready
git log --oneline # One commit less
cat app.py # v2 content still there
# Recommit with an improved message
git commit -m "Version 2 - With new features"Usage: Undo commits AND unstage (default mode).
echo "print('v3')" > app.py && git add . && git commit -m "Version 3"
# Mixed reset (or just git reset)
git reset HEAD~1
git status # UNSTAGED changes
git log --oneline # Commits undone
cat app.py # v3 content still there, but not staged
# Must re-add before commit
git add . && git commit -m "Version 3 - Redone cleanly"DANGER: Deletes EVERYTHING, even your uncommitted modifications!
echo "print('v4')" > app.py && git add . && git commit -m "Version 4"
# Changes in progress
echo "print('work in progress')" >> app.py
# Hard reset: EVERYTHING DISAPPEARS
git reset --hard HEAD~1
git status # Working directory clean
cat app.py # Back to v2, work in progress LOST!Usage: When you REALLY want to throw everything away and go back.
Reset is DESTRUCTIVE with shared commits!
NEVER do this:
git push origin main # Commits shared with the team
git reset --hard HEAD~3 # Deletes 3 shared commits
git push --force # Forces the deletion for everyone
# = CHAOS in the team!Safety rule:
git revert (safer)| Command | Action |
|---|---|
git reset HEAD~1 | Undo 1 commit (mixed) |
git reset --soft HEAD~2 | Undo 2 commits, keep staging |
git reset --hard HEAD~1 | Undo 1 commit, delete EVERYTHING |
git reset <hash> | Go back to a specific commit |
git reset --hard origin/main | Reset to the remote version |
Reset = Powerful but dangerous. Use with caution!