Reflog for finding lost commits in Git - Time & Space Complexity
When using git reflog to find lost commits, we want to know how long it takes to search through the history of changes.
We ask: How does the time to find a commit grow as the number of reflog entries increases?
Analyze the time complexity of this git reflog command sequence.
git reflog
# Lists all recent HEAD changes
git show HEAD@{5}
# Shows the commit at position 5 in reflog
# Searching reflog for a commit by message or hash
git reflog | grep 'fix bug'
This code lists reflog entries and searches for a specific commit by scanning the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each reflog entry one by one.
- How many times: Once for each reflog entry, from the newest to the oldest.
As the number of reflog entries grows, the time to scan them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 scans |
| 100 | 100 scans |
| 1000 | 1000 scans |
Pattern observation: The time grows directly with the number of reflog entries.
Time Complexity: O(n)
This means the time to find a commit grows linearly with the number of reflog entries.
[X] Wrong: "Searching reflog is instant no matter how many entries there are."
[OK] Correct: Each reflog entry must be checked one by one, so more entries mean more time.
Understanding how git commands scale helps you explain your problem-solving skills clearly and confidently.
"What if git stored reflog entries in a database with indexes? How would the time complexity change?"
Practice
git reflog?Solution
Step 1: Understand what reflog tracks
Reflog records the history of where HEAD and branch references have pointed, including commits that might be lost after resets or rebases.Step 2: Compare options with reflog purpose
Only To record where HEAD and branches have pointed recently correctly describes reflog's role. Options B, C, and D describe unrelated git functions.Final Answer:
To record where HEAD and branches have pointed recently -> Option CQuick Check:
Reflog tracks HEAD history = A [OK]
- Confusing reflog with git log
- Thinking reflog deletes commits
- Assuming reflog lists remote branches
Solution
Step 1: Recall the basic reflog command
The simplest and correct command to show reflog entries isgit reflog.Step 2: Evaluate other options
git reflog --showis invalid;git reflog listandgit reflog -aare invalid commands.Final Answer:
git reflog -> Option AQuick Check:
Basic reflog command = git reflog [OK]
- Adding unnecessary subcommands like 'list'
- Using invalid flags like '-a'
- Confusing reflog with git log
git reset --hard HEAD~1, you realize you lost a commit. Which command helps you find the lost commit's hash?Solution
Step 1: Understand what happens after reset
git reset --hard HEAD~1moves HEAD back one commit, making the latest commit unreachable but still in reflog.Step 2: Identify command to find lost commit
git reflogshows recent HEAD positions including the lost commit's hash. Other commands won't show unreachable commits.Final Answer:
git reflog -> Option AQuick Check:
Reflog shows lost commits after reset = C [OK]
- Using git status which shows working tree only
- Using git branch which lists branches, not commits
- Using git log which hides unreachable commits
git reset --hard HEAD~2 but want to restore the latest commit. Which command correctly resets to the lost commit using reflog?Solution
Step 1: Understand reflog indexing
After resetting HEAD back two commits, reflog entries show HEAD@{0} as current, HEAD@{1} as previous position (the lost commit), and HEAD@{2} as older.Step 2: Choose correct reflog entry to restore
To restore the latest lost commit, reset to HEAD@{1} which points to it. HEAD@{2} is older, HEAD~1 is relative to current HEAD, and origin/main may differ.Final Answer:
git reset --hard HEAD@{1} -> Option DQuick Check:
Use HEAD@{1} to restore last HEAD position [OK]
- Using HEAD@{2} which is older than needed
- Using HEAD~1 which is relative to current HEAD
- Resetting to origin/main which may differ
Solution
Step 1: Locate lost commits in reflog
Deleted branch commits remain in reflog as recent HEAD positions or branch references before deletion.Step 2: Create a new branch at the lost commit hash
After finding the commit hash in reflog, usegit branch new-branch-name <commit-hash>to restore the branch with lost commits.Final Answer:
Find the commit hash in git reflog and create a new branch at that hash -> Option BQuick Check:
Reflog + new branch recovers deleted commits [OK]
- Trying to delete branch again to restore it
- Resetting to origin which may not have deleted commits
- Re-cloning loses local lost commits
Ready to prove it?
Pass the test and this pattern moves unknown
