Bisect for finding bug-introducing commits in Git - Time & Space Complexity
When using git bisect, we want to find which commit introduced a bug efficiently.
We ask: How does the number of steps grow as the number of commits increases?
Analyze the time complexity of this git bisect process.
git bisect start
# mark bad commit
git bisect bad
# mark good commit
git bisect good <commit-hash>
# git automatically checks out middle commit
# user tests and marks commit as good or bad
# repeat until bug-introducing commit is found
This code snippet shows the basic steps of starting bisect, marking commits, and testing to find the bad commit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking out and testing the middle commit in the current range.
- How many times: About log base 2 of the number of commits between good and bad.
Each step halves the number of commits left to check.
| Input Size (n commits) | Approx. Steps (tests) |
|---|---|
| 10 | 4 |
| 100 | 7 |
| 1000 | 10 |
Pattern observation: The number of steps grows slowly, roughly doubling input size adds only one extra step.
Time Complexity: O(log n)
This means the number of tests grows slowly as the number of commits grows, making bisect efficient.
[X] Wrong: "Git bisect checks every commit one by one, so it takes as many steps as commits."
[OK] Correct: Git bisect uses a smart divide-and-conquer method, cutting the search space in half each time, so it needs far fewer steps.
Understanding how bisect works shows you can analyze efficient search methods, a useful skill in many tech tasks.
"What if the commits are not in a straight line but in multiple branches? How would that affect the bisect time complexity?"
Practice
git bisect?Solution
Step 1: Understand the role of git bisect
Git bisect helps identify which commit introduced a bug by testing commits between a known good and a bad commit.Step 2: Compare with other git commands
Merging branches, deleting commits, or creating branches are different git operations unrelated to bisect.Final Answer:
To find the first commit that introduced a bug -> Option DQuick Check:
git bisect = find bug commit [OK]
- Confusing bisect with merge or branch commands
- Thinking bisect deletes commits
- Assuming bisect creates branches
HEAD is bad and v1.0 is good?Solution
Step 1: Recall git bisect start syntax
The correct syntax isgit bisect start <bad> <good>, where the first argument is the bad commit and the second is the good commit.Step 2: Match the given commits
SinceHEADis bad andv1.0is good, the command isgit bisect start HEAD v1.0.Final Answer:
git bisect start HEAD v1.0 -> Option BQuick Check:
git bisect start bad good = start [OK]
- Reversing good and bad commits in the command
- Using incorrect command like bisect begin or init
- Omitting the start keyword
Bisecting: 3 revisions left to test after this (roughly 2 steps) Testing commit abc1234
If you run
git bisect good and then git bisect bad on the next commit, what will git do next?Solution
Step 1: Understand bisect workflow
Marking commits as good or bad tells git which side of the commit range to keep searching. After marking, git checks out the next commit to test.Step 2: Analyze the given commands
Marking one commit good, then the next bad, narrows the search range. Git continues bisecting until it finds the first bad commit.Final Answer:
Git will narrow down the range and checkout the next commit to test -> Option AQuick Check:
Mark good/bad -> git picks next commit [OK]
- Thinking bisect ends after one good and one bad mark
- Assuming git resets automatically after marking
- Believing marking good then bad consecutively causes error
Solution
Step 1: Understand bisect marking correction
Git bisect allows re-marking a commit to correct its status if marked incorrectly.Step 2: Correct the marking
Rungit bisect goodon the commit you accidentally marked as bad to update the bisect state.Final Answer:
Rungit bisect goodon the same commit to correct it -> Option CQuick Check:
Re-mark good/bad to correct [OK]
- Thinking you must reset instead of re-marking
- Editing internal git files manually
- Using skip to fix a marking error
abc123 is good and def456 is bad. You want to find the first bad commit using git bisect. Which sequence of commands correctly performs this task?Solution
Step 1: Start bisect with bad then good commit
The correct syntax isgit bisect start <bad> <good>. Sincedef456is bad andabc123is good, the command isgit bisect start def456 abc123.Step 2: Mark the known commits correctly
Mark the bad commit withgit bisect bad def456and the good commit withgit bisect good abc123. Then test intermediate commits as git suggests.Step 3: Finish bisect session
After finding the first bad commit, rungit bisect resetto return to the original branch.Final Answer:
git bisect start def456 abc123; git bisect bad def456; git bisect good abc123; test commits; git bisect reset -> Option AQuick Check:
Start with bad then good, mark correctly, reset [OK]
- Reversing good and bad commits in start command
- Marking commits with wrong status
- Skipping reset after bisect finishes
Ready to prove it?
Pass the test and this pattern moves unknown
