Recovering lost commits with reflog in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using git reflog to recover lost commits, it's important to understand how the time to find a commit grows as the reflog grows.
We want to know how the number of steps changes when the reflog has more entries.
Analyze the time complexity of the following git commands.
git reflog
# shows list of recent HEAD changes
git checkout <commit-hash>
# moves to the lost commit
# or
git reset --hard <commit-hash>
# restores the branch to that commit
This code shows how to list recent commits and recover a lost commit by checking it out or resetting.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the reflog entries to find the target commit.
- How many times: The reflog is scanned entry by entry until the commit is found.
As the number of reflog entries grows, the time to find a commit grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 entries scanned |
| 100 | About 100 entries scanned |
| 1000 | About 1000 entries scanned |
Pattern observation: The time grows linearly as the reflog size increases.
Time Complexity: O(n)
This means the time to find a lost commit grows in direct proportion to the number of reflog entries.
[X] Wrong: "git reflog instantly finds any commit no matter how many entries there are."
[OK] Correct: The reflog is a list scanned from newest to oldest, so more entries mean more scanning time.
Understanding how git commands scale with data size shows you think about efficiency and system behavior, a valuable skill in real projects.
"What if git stored reflog entries in a database with indexes? How would the time complexity change?"
Practice
git reflog in Git?Solution
Step 1: Understand what reflog tracks
Git reflog records changes to HEAD and branch tips, showing recent commit movements.Step 2: Identify reflog's main use
It helps find lost commits by listing recent HEAD positions, not deleting or merging.Final Answer:
To show a log of where HEAD and branch references have been recently -> Option AQuick Check:
Reflog = recent HEAD changes [OK]
- Confusing reflog with git log
- Thinking reflog deletes commits
- Assuming reflog pushes commits
abc1234 by creating a new branch named recovered?Solution
Step 1: Understand how to create a branch from a commit hash
The commandgit checkout -b <branch> <commit>creates and switches to a new branch at that commit.Step 2: Check each option's correctness
git checkout -b recovered abc1234 uses correct syntax. git branch recovered && git checkout abc1234 creates branch but checks out commit separately (detached HEAD). git reflog checkout abc1234 recovered is invalid syntax. git reset --hard recovered abc1234 misuses reset.Final Answer:
git checkout -b recovered abc1234 -> Option DQuick Check:
Create branch from commit: checkout -b [OK]
- Using git reflog checkout (invalid command)
- Creating branch and checkout separately causing detached HEAD
- Misusing git reset syntax
abc1234 HEAD@{0}: commit: Fix typo
def5678 HEAD@{1}: commit: Add feature
789abcd HEAD@{2}: commit: Initial commitWhat command will restore the commit with message 'Add feature'?
Solution
Step 1: Identify the commit hash for 'Add feature'
From reflog, 'Add feature' commit hash is def5678 at HEAD@{1}.Step 2: Use git checkout with the correct hash
Checking out def5678 restores that commit state.Final Answer:
git checkout def5678 -> Option CQuick Check:
Checkout commit by hash = def5678 [OK]
- Choosing wrong commit hash
- Using HEAD@{2} which is 'Initial commit'
- Confusing latest commit with target commit
git reflog and found a lost commit hash abc1234. You tried git checkout abc1234 but got a detached HEAD warning. How do you fix this to recover the commit safely?Solution
Step 1: Understand detached HEAD state
Checking out a commit hash puts you in detached HEAD, which is risky for new work.Step 2: Create a branch to save the commit safely
Usegit checkout -b recovered abc1234to create a branch and avoid losing commits.Final Answer:
Create a new branch at that commit using git checkout -b recovered abc1234 -> Option AQuick Check:
Fix detached HEAD by creating branch [OK]
- Ignoring detached HEAD and continuing work
- Deleting reflog entries mistakenly
- Trying to push without branch
git reflog and see:abc1234 HEAD@{0}: reset: moving to abc1234
def5678 HEAD@{1}: commit: Add new feature
789abcd HEAD@{2}: commit: Fix bugHow do you restore your branch to include the lost 'Add new feature' commit?
Solution
Step 1: Identify lost commit hash from reflog
The lost commit 'Add new feature' is at def5678 (HEAD@{1}).Step 2: Use git reset to move branch pointer back
Runninggit reset --hard def5678restores branch to that commit, recovering lost work.Final Answer:
Run git reset --hard def5678 to move branch back to lost commit -> Option BQuick Check:
Reset branch to lost commit hash [OK]
- Checking out reset commit instead of resetting branch
- Deleting reflog entries unnecessarily
- Trying to merge without branch pointer
