Bird
Raised Fist0
Gitdevops~7 mins

Difference between reset and revert in Git - CLI 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
Introduction
Sometimes you want to undo changes in your project. Git offers two ways: reset and revert. They undo changes differently and affect your project history in unique ways.
When you want to completely remove recent commits from your project history before sharing with others.
When you want to undo a commit but keep the history intact for others to see what happened.
When you accidentally committed something and want to erase it as if it never happened.
When you want to fix a mistake by adding a new commit that cancels out the bad one.
When you want to clean your working directory and staging area to a previous state.
Commands
Shows the last 3 commits in short form to identify which commit to reset or revert.
Terminal
git log --oneline -3
Expected OutputExpected
a1b2c3d Fix typo in README 9f8e7d6 Add user login feature 4d5e6f7 Initial commit
--oneline - Shows each commit in one line for easy reading
-3 - Limits output to last 3 commits
Moves the current branch pointer back to commit 9f8e7d6 and discards all changes after it, including in working directory and staging area.
Terminal
git reset --hard 9f8e7d6
Expected OutputExpected
No output (command runs silently)
--hard - Resets working directory and staging area to match the commit
Verifies that the branch history now ends at commit 9f8e7d6 after reset.
Terminal
git log --oneline -3
Expected OutputExpected
9f8e7d6 Add user login feature 4d5e6f7 Initial commit
--oneline - Shows each commit in one line
-3 - Limits output to last 3 commits
Creates a new commit that undoes the changes introduced by commit a1b2c3d, preserving history.
Terminal
git revert a1b2c3d
Expected OutputExpected
[main 7g8h9i0] Revert "Fix typo in README" 1 file changed, 1 deletion(-)
Shows the last 3 commits including the new revert commit.
Terminal
git log --oneline -3
Expected OutputExpected
7g8h9i0 Revert "Fix typo in README" a1b2c3d Fix typo in README 9f8e7d6 Add user login feature
--oneline - Shows commits in one line
-3 - Limits output to last 3 commits
Key Concept

If you remember nothing else, remember: reset changes history by moving branch pointer and can erase commits, while revert adds a new commit to undo changes without changing history.

Common Mistakes
Using git reset --hard on a shared branch
It rewrites history and can cause problems for others who already pulled those commits.
Use git revert to safely undo changes on shared branches.
Expecting git revert to remove commits from history
Revert does not remove commits; it adds a new commit that undoes changes.
Understand revert preserves history and is safe for public branches.
Not specifying --hard with git reset when wanting to discard changes
Without --hard, reset only moves the branch pointer but leaves working files unchanged.
Use git reset --hard to reset both history and working directory.
Summary
git reset moves the branch pointer and can erase commits and changes.
git revert creates a new commit that undoes a previous commit without changing history.
Use reset for local undo before sharing; use revert for safe undo on shared branches.

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