Bird
Raised Fist0
Gitdevops~10 mins

Difference between reset and revert in Git - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Difference between reset and revert
Start: Commit History
Choose action
reset
Move HEAD
Change branch state
Discard or keep changes
This flow shows how git reset moves the branch pointer to an earlier commit, changing history, while git revert creates a new commit that undoes changes, preserving history.
Execution Sample
Git
git reset --hard HEAD~1
git revert HEAD
Reset moves the branch back one commit discarding changes; revert creates a new commit that undoes the last commit.
Process Table
StepCommandActionBranch HEADWorking DirectoryResult
1Initial stateBranch at commit C3C3Matches C3Ready to reset or revert
2git reset --hard HEAD~1Move HEAD back to C2, discard changesC2Matches C2History changed, C3 no longer on branch
3git revert HEADCreate new commit C4 that undoes C3C4Matches C4History preserved, undo applied as new commit
4ExitNo more commandsC4Matches C4Reset changed history; revert preserved history
💡 Execution stops after reset and revert commands show different effects on branch and history.
Status Tracker
VariableStartAfter resetAfter revert
Branch HEADC3C2C4
Working DirectoryMatches C3Matches C2Matches C4
Key Moments - 3 Insights
Why does 'git reset' change the commit history but 'git revert' does not?
'git reset' moves the branch pointer backward (see execution_table step 2), effectively removing commits from the branch history. 'git revert' adds a new commit that undoes changes (step 3), so history stays intact.
What happens to the working directory after 'git reset --hard'?
The working directory matches the commit HEAD points to after reset (step 2), discarding any changes from the reset commits.
Can 'git revert' be used safely on shared branches?
Yes, because it creates a new commit without rewriting history (step 3), it is safe for shared branches unlike 'git reset' which rewrites history.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the branch HEAD after 'git reset --hard HEAD~1'?
AC4
BC3
CC2
DHEAD~1
💡 Hint
Check the 'Branch HEAD' column at step 2 in the execution_table.
At which step does git create a new commit to undo changes?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where 'Create new commit' is mentioned in the Action column.
If you want to undo a commit but keep the history intact, which command should you use?
Agit reset --hard
Bgit revert
Cgit checkout
Dgit branch
💡 Hint
Refer to the concept_flow and execution_table showing which command preserves history.
Concept Snapshot
git reset moves the branch pointer to an earlier commit, changing history and optionally discarding changes.
git revert creates a new commit that undoes changes from a previous commit, preserving history.
Use reset for local undo when history rewrite is safe.
Use revert to safely undo changes on shared branches.
Reset affects HEAD and working directory; revert adds a new commit.
Full Transcript
This lesson shows the difference between git reset and git revert by tracing their effects on commit history and working directory. Git reset moves the branch pointer backward, changing history and discarding changes if --hard is used. Git revert creates a new commit that undoes changes from a previous commit, preserving history. The execution table tracks branch HEAD and working directory state after each command. Key moments clarify why reset rewrites history and revert does not, and when to use each safely. Visual quizzes test understanding of branch state and command effects.

Practice

(1/5)
1. What is the main difference between git reset and git revert?
easy
A. git reset creates a new commit, git revert deletes commits permanently.
B. git reset changes commit history, git revert creates a new commit to undo changes.
C. git reset only works on remote branches, git revert only on local branches.
D. git reset merges branches, git revert rebases branches.

Solution

  1. Step 1: Understand git reset behavior

    git reset moves the branch pointer and can remove commits from history locally.
  2. Step 2: Understand git revert behavior

    git revert creates a new commit that undoes the changes of a previous commit without changing history.
  3. Final Answer:

    git reset changes commit history, git revert creates a new commit to undo changes. -> Option B
  4. Quick Check:

    Reset changes history, revert adds undo commit [OK]
Hint: Reset rewinds history, revert adds undo commit [OK]
Common Mistakes:
  • Thinking revert deletes commits
  • Confusing reset with revert effects on remote
  • Believing reset creates new commits
2. Which of the following is the correct syntax to undo the last commit using git revert?
easy
A. git revert HEAD
B. git revert --hard HEAD
C. git revert HEAD~1
D. git revert --reset HEAD

Solution

  1. Step 1: Identify the commit to revert

    The last commit is referenced by HEAD.
  2. Step 2: Use correct revert syntax

    git revert HEAD creates a new commit that undoes the last commit.
  3. Final Answer:

    git revert HEAD -> Option A
  4. Quick Check:

    Revert last commit with git revert HEAD [OK]
Hint: Revert last commit with 'git revert HEAD' [OK]
Common Mistakes:
  • Using HEAD~1 to revert last commit
  • Adding invalid flags like --hard or --reset
  • Confusing revert syntax with reset
3. Given this sequence of commands:
git commit -m "Add feature A"
git commit -m "Fix bug B"
git reset --hard HEAD~1
git status
What will git status show after these commands?
medium
A. Working directory clean, last commit is "Add feature A"
B. Working directory clean, last commit is "Fix bug B"
C. Uncommitted changes from "Fix bug B" present
D. Error: reset failed

Solution

  1. Step 1: Understand the commits and reset

    Two commits made: "Add feature A" then "Fix bug B". git reset --hard HEAD~1 moves branch back one commit, removing "Fix bug B" commit and resets files.
  2. Step 2: Check status after reset

    Since reset was hard, working directory matches "Add feature A" commit, so no changes to commit.
  3. Final Answer:

    Working directory clean, last commit is "Add feature A" -> Option A
  4. Quick Check:

    Hard reset removes last commit and cleans changes [OK]
Hint: Hard reset moves HEAD and cleans working directory [OK]
Common Mistakes:
  • Thinking reset keeps last commit
  • Assuming changes remain after hard reset
  • Confusing reset with revert effects
4. You ran git reset --soft HEAD~1 but your changes disappeared from the staging area. What is the likely mistake?
medium
A. You forgot to commit after reset, so changes are lost.
B. You should have used git reset --hard to keep changes staged.
C. You should have used git revert instead to undo the commit.
D. You ran git reset --soft but expected it to keep changes staged; it only moves HEAD.

Solution

  1. Step 1: Understand git reset --soft effect

    This moves HEAD back but keeps changes staged (in index).
  2. Step 2: Identify why changes disappeared

    If changes disappeared from staging, likely a misunderstanding: --soft keeps changes staged, but if you see no changes staged, maybe you checked wrong or used wrong flag.
  3. Final Answer:

    You ran git reset --soft but expected it to keep changes staged; it only moves HEAD. -> Option D
  4. Quick Check:

    Soft reset moves HEAD, keeps staged changes [OK]
Hint: Soft reset moves HEAD, does not remove staged changes [OK]
Common Mistakes:
  • Confusing soft reset with hard reset
  • Expecting soft reset to undo commit and unstaged changes
  • Using revert when reset is intended
5. You pushed a commit to a shared repository but later found it causes issues. Which command should you use to undo the commit safely without rewriting history?
hard
A. git reset --hard HEAD~1
B. git checkout HEAD~1
C. git revert HEAD
D. git reset --soft HEAD~1

Solution

  1. Step 1: Understand the problem with shared commits

    Reset rewrites history and can cause problems if commits are already pushed and shared.
  2. Step 2: Choose safe undo method

    git revert HEAD creates a new commit that undoes the changes without rewriting history, safe for shared repos.
  3. Final Answer:

    git revert HEAD -> Option C
  4. Quick Check:

    Revert safely undoes shared commits without history rewrite [OK]
Hint: Use revert to undo shared commits safely [OK]
Common Mistakes:
  • Using reset on shared branches causing conflicts
  • Thinking checkout undoes commits
  • Using soft reset expecting safe undo