Bird
Raised Fist0
Gitdevops~30 mins

Difference between reset and revert in Git - Hands-On 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
Difference between reset and revert in Git
📖 Scenario: You are working on a project using Git for version control. Sometimes you make changes that you want to undo. Git offers two main ways to undo changes: reset and revert. Understanding the difference helps you choose the right command to fix mistakes safely.
🎯 Goal: Learn how to use git reset and git revert commands to undo changes in different ways. You will create a simple Git repository, make commits, then undo changes using both commands to see how they behave differently.
📋 What You'll Learn
Create a Git repository and make commits
Use git reset to undo commits locally
Use git revert to undo commits safely by creating new commits
Observe the commit history after each command
💡 Why This Matters
🌍 Real World
Developers often need to undo mistakes in code history. Knowing when to use reset or revert helps keep the project history clean and safe.
💼 Career
Understanding these commands is essential for software developers, DevOps engineers, and anyone working with Git in team environments.
Progress0 / 4 steps
1
Initialize Git repository and create commits
Run the following commands to initialize a Git repository, create a file called file.txt, add some text, and make two commits with messages First commit and Second commit:
git init
echo "Line 1" > file.txt
git add file.txt
git commit -m "First commit"
echo "Line 2" >> file.txt
git add file.txt
git commit -m "Second commit"
Git
Hint

Use git init to start a repo, then create and commit changes step by step.

2
Use git reset to undo the last commit locally
Use the command git reset --soft HEAD~1 to undo the last commit but keep the changes staged. This moves the HEAD pointer back by one commit without deleting your changes.
Git
Hint

git reset --soft HEAD~1 moves HEAD back one commit but keeps changes staged.

3
Use git revert to undo the last commit safely
Use the command git revert HEAD@{1} to create a new commit that undoes the changes made in the commit you just reset (the previous HEAD position, referenced as HEAD@{1}). This keeps the history intact and is safe for shared repositories.
Git
Hint

git revert HEAD@{1} creates a new commit that reverses the changes of the previously reset commit.

4
Show the commit history to compare reset and revert effects
Run git log --oneline to display the commit history. Observe how git reset removed the last commit from history, while git revert added a new commit that undoes the last commit.
Git
Hint

Use git log --oneline to see the short commit history.

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