Bird
Raised Fist0
Gitdevops~3 mins

Difference between reset and revert in Git - When to Use Which

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
The Big Idea

What if you could undo mistakes in your project as easily as hitting 'undo' in a text editor?

The Scenario

Imagine you made a mistake in your project and want to undo it. You try to fix it by manually deleting files or copying old versions from backups. This is like trying to fix a messy room by throwing things around without a plan.

The Problem

Manually undoing changes is slow and risky. You might miss some files or accidentally delete important work. It's hard to keep track of what was changed and when, leading to confusion and more mistakes.

The Solution

Git's reset and revert commands help you undo changes safely and clearly. Reset moves your project back to a previous state, like rewinding a video. Revert creates a new change that undoes a past change, like writing a correction note.

Before vs After
Before
rm -rf changed_files
cp backup/old_version/* ./
After
git reset --hard HEAD~1  # For local repos, rewinds history
git revert HEAD  # For shared repos, creates new undoing commit
What It Enables

These commands let you fix mistakes quickly without losing track of your work or confusing your team.

Real Life Example

You pushed a bad update to your shared project. Using git revert, you create a new commit that undoes the bad update without rewriting history, keeping everyone's work safe.

Key Takeaways

Reset moves your project back to an earlier state, changing history.

Revert adds a new change that undoes a previous one, preserving history.

Both help undo mistakes safely and clearly, avoiding manual errors.

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