Git bisect saved me from a week of debugging. Should've used it sooner.
Source: Dev.to
Two weeks ago, my coworker merged a feature branch on Friday afternoon. By Monday morning, half our test suite was red. Nobody knew which commit broke things. The branch had 47 commits.
I opened git log, stared at it, and closed it. Forty‑seven commits—no way I was reading all of them. I tried the usual manual approach, blaming everyone in the commit history until I found someone to be mad at. Just kidding. Mostly.
The manual approach
git blame told me which lines changed but not why they broke. git log showed me filenames but not the actual damage. I spent two days narrowing it down manually: checking out commits one by one, running the test suite, and watching them fail.
My approach was scientific. My time investment was not.
# What I was doing (spoiler: wrong)
git checkout commit-xyz
npm test # 47 timesAt roughly three minutes per test run, that was over two hours of pure waiting, plus checkout time and context switching.
Enter git bisect
A senior dev walked by, saw my screen, and asked why I wasn’t using git bisect. I didn’t have a good answer.
git bisect performs a binary search on your commit history. You tell it a known good commit and a known bad commit; it checks out the midpoint, you test, then mark the result as good or bad. It repeats until it isolates the offending commit.
# Start the hunt
git bisect start
# Mark current HEAD as bad
git bisect bad
# Mark the last known good commit
git bisect good v2.4.0
# Test. Then mark:
git bisect good # tests pass
git bisect bad # tests failThat’s the whole workflow. Instead of 47 manual checkouts, I did six tests and found the culprit: a configuration change made at 11 PM that added a required field to user validation without updating the test fixtures.
Total time: ~20 minutes.
The binary‑search math explains the speed: N commits require at most log₂(N) tests. Forty‑seven commits needed only six tests instead of forty‑seven—a difference between an hour of waiting and five minutes.
git bisect may sound intimidating because “binary search” sounds technical, but it’s just two commands to start and one command after each test. It comes with Git—no plugins needed.
# Cleanup when done
git bisect reset