Bird
Raised Fist0
Gitdevops~5 mins

Difference between reset and revert in Git - Quick Revision & Key Differences

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
Recall & Review
beginner
What does the git reset command do?

git reset moves the current branch pointer to a specified commit, changing the commit history. It can also modify the staging area and working directory depending on options used.

Click to reveal answer
beginner
What is the purpose of git revert?

git revert creates a new commit that undoes the changes introduced by a previous commit, preserving the commit history.

Click to reveal answer
intermediate
How does git reset affect commit history compared to git revert?

git reset rewrites commit history by moving the branch pointer backward, potentially removing commits. git revert keeps history intact by adding a new commit that reverses changes.

Click to reveal answer
beginner
Which command is safer to use on a shared public branch: git reset or git revert?

git revert is safer on shared branches because it preserves history and avoids rewriting commits others may have based work on.

Click to reveal answer
intermediate
Can git reset modify the working directory and staging area?

Yes, depending on the mode (--soft, --mixed, --hard), git reset can change the staging area and working directory along with moving the branch pointer.

Click to reveal answer
What does git revert do?
ACreates a new commit that undoes a previous commit
BMoves the branch pointer to an earlier commit
CDeletes commits permanently
DResets the staging area only
Which command rewrites commit history by moving the branch pointer?
Agit commit
Bgit revert
Cgit reset
Dgit merge
Which is safer to use on a shared branch?
Agit revert
Bgit reset --soft
Cgit reset --hard
Dgit clean
What does git reset --hard do?
ADeletes untracked files
BMoves branch pointer and resets staging area and working directory
CCreates a new commit
DOnly moves branch pointer
Does git revert remove commits from history?
ANo, it only resets staging area
BYes, it deletes commits
CYes, it moves branch pointer
DNo, it adds a new commit that undoes changes
Explain the difference between git reset and git revert in simple terms.
Think about how each command affects commit history and collaboration.
You got /4 concepts.
    When would you choose to use git revert instead of git reset?
    Consider teamwork and the impact on others' work.
    You got /4 concepts.

      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