Difference between reset and revert in Git - Performance Comparison
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?