Difference between reset and revert in Git - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When using Git, commands like reset and revert change commit history. Understanding their time complexity helps us know how their work grows as the number of commits increases.
We want to see how the cost of these commands changes when the commit history grows longer.
Analyze the time complexity of these Git commands:
# Reset to a previous commit (hard reset)
git reset --hard <commit-hash>
# Revert a specific commit by creating a new commit
git revert <commit-hash>
The reset command moves the current branch pointer to a previous commit, changing the working directory and index. The revert command creates a new commit that undoes changes from a past commit.
Look at what each command does repeatedly:
- Reset: Mainly updates pointers and files once to the target commit.
- Revert: Applies the changes from one commit in reverse to create a new commit.
- Primary operation: Both commands operate on a single commit at a time, no loops over many commits.
Since both commands focus on one commit, their work does not increase with the total number of commits in the repository.
| Input Size (n commits) | Approx. Operations |
|---|---|
| 10 | Constant work on 1 commit |
| 100 | Constant work on 1 commit |
| 1000 | Constant work on 1 commit |
Pattern observation: The time to run reset or revert stays about the same no matter how many commits exist.
Time Complexity: O(1)
This means the commands take roughly the same time regardless of how many commits are in the project.
[X] Wrong: "Reset or revert will take longer if the project has many commits."
[OK] Correct: Both commands target a single commit and do not loop through all commits, so their work does not grow with commit count.
Knowing that reset and revert run in constant time helps you explain how Git handles history changes efficiently. This shows you understand how Git works under the hood, a useful skill in real projects.
What if we tried to revert a range of multiple commits at once? How would the time complexity change?
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
