What if you could undo mistakes in your project as easily as hitting 'undo' in a text editor?
Difference between reset and revert in Git - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.
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.
rm -rf changed_files cp backup/old_version/* ./
git reset --hard HEAD~1 # For local repos, rewinds history git revert HEAD # For shared repos, creates new undoing commit
These commands let you fix mistakes quickly without losing track of your work or confusing your team.
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.
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
git reset and git revert?Solution
Step 1: Understand
git resetbehaviorgit resetmoves the branch pointer and can remove commits from history locally.Step 2: Understand
git revertbehaviorgit revertcreates a new commit that undoes the changes of a previous commit without changing history.Final Answer:
git resetchanges commit history,git revertcreates a new commit to undo changes. -> Option BQuick Check:
Reset changes history, revert adds undo commit [OK]
- Thinking revert deletes commits
- Confusing reset with revert effects on remote
- Believing reset creates new commits
git revert?Solution
Step 1: Identify the commit to revert
The last commit is referenced byHEAD.Step 2: Use correct revert syntax
git revert HEADcreates a new commit that undoes the last commit.Final Answer:
git revert HEAD -> Option AQuick Check:
Revert last commit withgit revert HEAD[OK]
- Using HEAD~1 to revert last commit
- Adding invalid flags like --hard or --reset
- Confusing revert syntax with reset
git commit -m "Add feature A" git commit -m "Fix bug B" git reset --hard HEAD~1 git statusWhat will
git status show after these commands?Solution
Step 1: Understand the commits and reset
Two commits made: "Add feature A" then "Fix bug B".git reset --hard HEAD~1moves 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 AQuick Check:
Hard reset removes last commit and cleans changes [OK]
- Thinking reset keeps last commit
- Assuming changes remain after hard reset
- Confusing reset with revert effects
git reset --soft HEAD~1 but your changes disappeared from the staging area. What is the likely mistake?Solution
Step 1: Understand
This moves HEAD back but keeps changes staged (in index).git reset --softeffectStep 2: Identify why changes disappeared
If changes disappeared from staging, likely a misunderstanding:--softkeeps changes staged, but if you see no changes staged, maybe you checked wrong or used wrong flag.Final Answer:
You rangit reset --softbut expected it to keep changes staged; it only moves HEAD. -> Option DQuick Check:
Soft reset moves HEAD, keeps staged changes [OK]
- Confusing soft reset with hard reset
- Expecting soft reset to undo commit and unstaged changes
- Using revert when reset is intended
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 HEADcreates a new commit that undoes the changes without rewriting history, safe for shared repos.Final Answer:
git revert HEAD -> Option CQuick Check:
Revert safely undoes shared commits without history rewrite [OK]
- Using reset on shared branches causing conflicts
- Thinking checkout undoes commits
- Using soft reset expecting safe undo
