Recovering from hard reset in Git - Time & Space Complexity
When recovering from a hard reset in git, it's important to understand how the time to recover grows as the number of commits or changes increases.
We want to know how the effort to find and restore lost commits scales with the size of the commit history.
Analyze the time complexity of the following git commands used to recover lost commits after a hard reset.
# List all reflog entries
$ git reflog
# Find the commit hash before reset
$ git checkout <commit-hash>
# Create a new branch to save the commit
$ git branch recovery-branch
# Switch back to main branch
$ git checkout main
# Reset main branch to recovered commit
$ git reset --hard recovery-branch
This sequence helps find and restore commits lost after a hard reset by using the reflog and resetting branches.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the reflog entries to find the commit before the reset.
- How many times: The reflog is traversed once, but its size depends on the number of recent commits and actions.
The time to scan reflog entries grows as the number of recorded commits and actions increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Scan 10 reflog entries |
| 100 | Scan 100 reflog entries |
| 1000 | Scan 1000 reflog entries |
Pattern observation: The time grows linearly with the number of reflog entries scanned.
Time Complexity: O(n)
This means the time to recover grows in direct proportion to the number of reflog entries you need to check.
[X] Wrong: "Recovering lost commits after a hard reset is instant regardless of history size."
[OK] Correct: The recovery depends on scanning the reflog, which grows with your commit history and actions, so it takes longer if there are many entries.
Understanding how git operations scale helps you explain your troubleshooting approach clearly and shows you grasp practical version control challenges.
"What if we used git fsck to find dangling commits instead of reflog? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of
git refloggit reflogshows a log of all recent commits and HEAD movements, including resets.Step 2: Compare with other commands
git log --onelineshows commit history but not HEAD changes;git statusshows current state;git reset --softchanges HEAD but does not show history.Final Answer:
git reflog -> Option CQuick Check:
View recent HEAD changes = git reflog [OK]
- Confusing git log with git reflog
- Using git status to find past commits
- Thinking git reset shows history
Solution
Step 1: Identify the correct reset syntax
The correct command to reset to a previous commit and discard changes isgit reset --hard <commit>.Step 2: Eliminate incorrect options
-softis invalid flag,git checkout --hardis not a valid command, andgit revertdoes not accept --hard.Final Answer:
git reset --hard <commit> -> Option AQuick Check:
Hard reset syntax = git reset --hard [OK]
- Using -soft instead of --hard
- Confusing git revert with reset
- Trying git checkout with --hard
git reset --hard HEAD~2, which command will help you find the commit hash to recover your lost changes?Solution
Step 1: Understand what
This command moves HEAD back two commits and discards changes, potentially losing recent commits from view.git reset --hard HEAD~2doesStep 2: Identify command to find lost commit hashes
git reflogshows all recent HEAD positions including those before the reset, allowing recovery.Final Answer:
git reflog -> Option DQuick Check:
Find lost commits after reset = git reflog [OK]
- Using git status which shows current state only
- Using git branch which lists branches, not commits
- Using git diff which shows changes, not history
git reset --hard HEAD~1 and lost your latest commit. Which sequence of commands will help you recover it?Solution
Step 1: Use
git reflogto find lost commit hashgit refloglists all recent HEAD positions including the lost commit before reset.Step 2: Reset hard to the lost commit hash
Usegit reset --hard <hash>to restore the repository to that commit state.Final Answer:
git reflog then git reset --hard <hash> -> Option AQuick Check:
Recover lost commit = reflog + reset --hard [OK]
- Using git log which may not show lost commits
- Trying git revert which creates new commits
- Using reset --soft after hard reset without commit hash
Solution
Step 1: Find lost commits using
git refloggit reflogshows the commit hashes that were lost after the hard reset.Step 2: Use
This preserves current changes and reapplies lost commits without resetting the branch.git cherry-pickto apply lost commits onto current branchFinal Answer:
Use git reflog and cherry-pick lost commits -> Option BQuick Check:
Recover lost commits without losing current changes = reflog + cherry-pick [OK]
- Using reset --hard to go back loses current changes
- Using revert on reset commit does not restore lost commits
- Merging from old branch may cause conflicts or duplicate commits
Ready to prove it?
Pass the test and this pattern moves unknown
