CurriculumAdvanced Gitgit reset & git revert
Lesson 4 of 667%
Advanced10 min+100 XP

git reset & git revert

Both commands undo changes but in opposite ways. reset rewrites history by moving the branch pointer backward — local only, destructive. revert creates a new commit that undoes a previous one — safe for shared branches because it adds to history rather than rewriting it.

01.The Three Modes of git reset

--soft: Move HEAD back but keep all changes staged. Use this to "undo" a commit and recommit differently. --mixed (the default): Move HEAD back and unstage changes but keep the file modifications. Use this to redo commits with different groupings. --hard: Move HEAD back AND discard all changes in files. Use only when you are absolutely certain you want to throw changes away.

Git's Three Areas
📁
Working Dir
Your files
→ git add
📋
Staging Area
Prepared
→ git commit
🗄️
Repository
.git/ history

02.git revert: The Collaborative Undo

revert is safe to use on shared branches because it only adds a new commit — it never rewrites existing history. If commit X added a function, reverting X creates a new commit that removes it. The original commit X is permanently preserved. Your colleagues who have pulled the original commit are unaffected. This is the only way to "undo" on main or shared branches.

Commit Timeline
a1b2c3d
init
b2c3d4e
feat
c3d4e5f
fix
d4e5f6a
HEAD

Commands

commands — click to reveal output
$
git reset --soft HEAD~1
Undo last commit, keep all changes staged
$
git reset --mixed HEAD~1
Undo last commit, unstage changes (keep modified files)
▼ show output
$
git reset --hard HEAD~1
Undo last commit AND discard all changes ⚠️ irreversible
$
git reset HEAD~3
Undo last 3 commits in mixed mode
$
git reset HEAD <file>
Unstage one specific file without touching other staged files
$
git revert a3f8c21
Create a new "undo commit" that reverses commit a3f8c21
▼ show output
$
git revert HEAD~3..HEAD
Revert the last 3 commits with individual revert commits
$
git revert --no-commit HEAD~3..HEAD
Stage all revert changes to commit as one single revert commit
KNOWLEDGE CHECK

Why is git revert safer than git reset on a shared branch?

Rate this lesson:
PreviousNext Lesson