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
Step 1: Understand git reset behavior
git reset moves the branch pointer and can remove commits from history locally.
Step 2: Understand git revert behavior
git revert creates a new commit that undoes the changes of a previous commit without changing history.
Final Answer:
git reset changes commit history, git revert creates a new commit to undo changes. -> Option B
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
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.
Step 2: Check status after reset
Since reset was hard, working directory matches "Add feature A" commit, so no changes to commit.
Final Answer:
Working directory clean, last commit is "Add feature A" -> Option A
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
Step 1: Understand git reset --soft effect
This moves HEAD back but keeps changes staged (in index).
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.
Final Answer:
You ran git reset --soft but expected it to keep changes staged; it only moves HEAD. -> Option D
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
Step 1: Understand the problem with shared commits
Reset rewrites history and can cause problems if commits are already pushed and shared.
Step 2: Choose safe undo method
git revert HEAD creates a new commit that undoes the changes without rewriting history, safe for shared repos.
Final Answer:
git revert HEAD -> Option C
Quick Check:
Revert safely undoes shared commits without history rewrite [OK]
Hint: Use revert to undo shared commits safely [OK]