git revert to undo a commit safely - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using git revert, it's important to know how the time to undo a commit changes as your project grows.
We want to understand how the work done by git revert scales with the size of the commit and repository.
Analyze the time complexity of the following git command.
git revert <commit-hash>
This command creates a new commit that undoes the changes introduced by the specified commit.
What happens internally when git revert runs?
- Primary operation: Git reads the changes (diff) introduced by the commit to be reverted.
- How many times: It processes each changed file and each change within those files once.
The time to revert grows with the number of changes in the commit.
| Input Size (number of changed lines) | Approx. Operations |
|---|---|
| 10 | About 10 operations to reverse changes |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The work grows roughly in direct proportion to the number of changes in the commit.
Time Complexity: O(n)
This means the time to revert grows linearly with the number of changes in the commit.
[X] Wrong: "Reverting a commit always takes the same time regardless of its size."
[OK] Correct: The time depends on how many changes the commit has; bigger commits take longer to revert.
Understanding how git commands scale helps you explain your approach to managing code history clearly and confidently.
What if we reverted a merge commit instead of a regular commit? How would the time complexity change?
Practice
git revert command do in a Git repository?Solution
Step 1: Understand the purpose of
git revertgit revertcreates a new commit that reverses the changes made by a specified previous commit.Step 2: Compare with other commands
Unlikegit reset, it does not remove commits from history but safely adds a new commit to undo changes.Final Answer:
It creates a new commit that undoes the changes of a previous commit -> Option DQuick Check:
git revert = new commit undoing changes [OK]
- Confusing revert with reset which removes commits
- Thinking revert deletes commits permanently
- Assuming revert merges branches
Solution
Step 1: Identify the correct command to revert the latest commit
The latest commit is referenced byHEAD, and the correct command isgit revert HEAD.Step 2: Check invalid options
Options like--latest,-mwithout context, or--undoare not valid revert flags.Final Answer:
git revert HEAD -> Option AQuick Check:
Revert latest commit = git revert HEAD [OK]
- Using invalid flags like --latest or --undo
- Confusing revert options with reset options
- Trying to revert without specifying a commit
git commit -m "Add feature A" git commit -m "Fix bug B" git revert HEAD
What will be the result of the last command?
Solution
Step 1: Identify the commit pointed by HEAD
After two commits, HEAD points to "Fix bug B" commit.Step 2: Understand what
It creates a new commit that reverses the changes introduced by the latest commit "Fix bug B".git revert HEADdoesFinal Answer:
A new commit is created that undoes the changes from "Fix bug B" -> Option CQuick Check:
git revert HEAD = new commit undoing latest commit [OK]
- Thinking revert deletes commits from history
- Confusing revert with reset which moves HEAD
- Assuming revert causes errors on HEAD
git revert abc123 but got a merge conflict error. What should you do to fix this?Solution
Step 1: Understand merge conflicts during revert
Revert can cause conflicts if changes overlap. You must resolve conflicts manually.Step 2: Continue revert after resolving conflicts
After fixing conflicts, rungit revert --continueto complete the revert commit.Final Answer:
Manually resolve the conflicts, then rungit revert --continue-> Option BQuick Check:
Resolve conflicts + git revert --continue = fix revert conflict [OK]
- Trying to delete commits to fix revert conflicts
- Using git reset which discards changes unsafely
- Aborting revert without resolving conflicts
Solution
Step 1: Consider shared repository safety
When commits are shared, rewriting history (reset + force push) can cause problems for others.Step 2: Use revert to safely undo changes
git revertadds a new commit that reverses changes without rewriting history, keeping the project safe and intact.Final Answer:
Usegit revert <commit-hash>to create a new commit that undoes the changes -> Option AQuick Check:
Revert = safe undo for shared commits [OK]
- Using reset and force push on shared branches
- Deleting commits manually from remote
- Checking out old commits without reverting
