Recovering deleted branches in Git - Time & Space Complexity
When recovering deleted branches in git, we want to know how the time to find and restore a branch changes as the repository grows.
How does git search through its history to recover a branch?
Analyze the time complexity of this git command sequence.
# Find the commit hash of the deleted branch
git reflog
# Create a new branch at that commit
git branch recovered-branch <commit-hash>
This code finds the commit where the deleted branch pointed and recreates the branch there.
Look for repeated steps that take time as input grows.
- Primary operation: Scanning the reflog entries to find the commit hash.
- How many times: Once per reflog entry, which grows with the number of recent git actions.
As the number of reflog entries grows, git scans more entries to find the commit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reflog entries scanned |
| 100 | About 100 reflog entries scanned |
| 1000 | About 1000 reflog entries scanned |
Pattern observation: The time grows roughly in direct proportion to the number of reflog entries.
Time Complexity: O(n)
This means the time to recover a deleted branch grows linearly with the number of reflog entries git must check.
[X] Wrong: "Recovering a deleted branch is instant no matter how big the repo is."
[OK] Correct: Git must scan reflog entries one by one, so more history means more time to find the right commit.
Understanding how git searches history helps you reason about performance in real projects and shows you can think about tool behavior beyond just commands.
What if git stored reflog entries in a database with indexes? How would that change the time complexity?
Practice
Solution
Step 1: Understand the purpose of
git refloggit reflogrecords all changes to the HEAD, including deleted branches.Step 2: Identify the command that shows commit history including deleted refs
git refloglets you find the commit hash of the deleted branch.Final Answer:
git reflog-> Option AQuick Check:
Find deleted branch commit =git reflog[OK]
git reflog to find lost commits fast [OK]- Confusing
git branch -das recovery command - Using
git reset --hardwhich discards changes - Thinking
git mergerecovers deleted branches
feature1 from a commit hash abc1234 and switches to it?Solution
Step 1: Understand the syntax to create a branch at a commit
The correct syntax isgit checkout -b [branch] [commit]to create and switch.Step 2: Identify the correct command
git checkout -b feature1 abc1234creates branchfeature1at commitabc1234.Final Answer:
git checkout -b feature1 abc1234-> Option AQuick Check:
Create branch at commit =git checkout -b branch commit[OK]
git checkout -b branch commit to restore branch [OK]- Using
git checkout commit branchwithout-b - Using
git resetwhich does not create branches - Using
git branchwithout switching
bugfix, you run git reflog and see this entry:abc1234 HEAD@{2}: checkout: moving from bugfix to mainWhich command will correctly restore the
bugfix branch at that commit?Solution
Step 1: Identify commit hash from reflog
The commit hashabc1234is wherebugfixwas last checked out.Step 2: Use command to recreate branch at that commit
git checkout -b bugfix abc1234creates and switches tobugfixat that commit.Final Answer:
git checkout -b bugfix abc1234-> Option DQuick Check:
Restore branch from commit =git checkout -b branch commit[OK]
git checkout -b [OK]- Using
git branch -dwhich deletes branches - Using
git reset --hardwhich resets HEAD but not branches - Trying
git checkout bugfixwithout recreating branch
git checkout -b feature abc123 but got an error: fatal: A branch named 'feature' already exists.What is the best way to fix this?
Solution
Step 1: Understand the error
The error means branchfeaturealready exists locally.Step 2: Delete existing branch to recreate
Usegit branch -D featureto force delete the existing branch, then recreate it.Final Answer:
Delete existing branch withgit branch -D featurethen retry -> Option BQuick Check:
Delete conflicting branch before recreate [OK]
-D before recreating [OK]- Trying
git branch -bwhich is invalid syntax - Ignoring the existing branch and forcing checkout
- Renaming commit hash which is not possible
release and want to recover it. You run git reflog and find multiple commits related to release. Which sequence of commands correctly recovers the branch at the latest commit and deletes the old branch pointer?Solution
Step 1: Delete any existing branch pointer named
Usereleasegit branch -D releaseto remove old or conflicting branch references.Step 2: Create a new branch
Usereleaseat the desired commitgit checkout -b release <commit-hash>to recreate and switch to the branch at the correct commit.Final Answer:
git branch -D release; git checkout -b release <commit-hash>-> Option CQuick Check:
Delete old branch then recreate at commit [OK]
- Using
git checkout releasewhen branch is deleted - Using
git reset --hardwithout recreating branch - Using
git branch releasewithout switching
Ready to prove it?
Pass the test and this pattern moves unknown
