Git Bisect command
Git Bisect command
Let’s understand the Git bisect command in this tutorial. You’re working on a project, and everything seems fine until suddenly, a bug appears. You’re sure the code was working perfectly last week, but now something is broken. The problem is, you’ve made dozens of changes since then. How do you find the exact commit that introduced the bug without checking out and testing every single one?
git bisect
, your new best friend for debugging.
What is Git Bisect?
Imagine git bisect
as a super-smart detective for your codebase. It uses a powerful algorithm called a binary search to quickly pinpoint the exact commit where a bug was introduced.
Here’s the simple idea behind it:
- You tell Git about a “good” commit where the code was working correctly.
- You tell Git about a “bad” commit where the bug is present (usually the latest commit).
- Git then starts a bisect session, jumping to a commit halfway between the good and bad ones.
- You test the code at that commit and tell Git if it’s “good” or “bad”.
- Git repeats this process, halving the number of suspects each time, until it finds the very first bad commit.
Instead of checking all 100 commits, git bisect
will find the problematic one in about 7 steps (log2(100) ≈ 7). It’s incredibly efficient!
Git bisect Syntax
Using git bisect</code
involves a series of commands. Here are the most important ones:
# Start the bisect session
$ git bisect start
# Mark the current commit (usually the latest) as "bad"
$ git bisect bad
# Mark an older commit (where things worked) as "good"
$ git bisect good <commit-hash>
# After marking a commit during the session:
$ git bisect good # If the bug is NOT present at this commit
$ git bisect bad # If the bug IS present at this commit
# If you want to abort the bisect session and return to normal
$ git bisect reset
# Once bisect finds the culprit, it will tell you. Use reset to end the session.
A Real-World Example
Let’s say a button on your website has suddenly disappeared. You know it was working 50 commits ago, but now it’s gone.
Start the Investigation
Open your terminal, navigate to your project, and start the bisect session.
$ git bisect start
Define the Crime Scene
Mark the current commit as “bad” because the button is missing.
$ git bisect bad
Establish an Alibi
Find the hash of an old commit where the button was definitely working (you can use git log --oneline
to find it). Let’s say the hash is a1b2c3d
.
$ git bisect good a1b2c3d
Git will now check out a commit halfway between the good and bad ones. Your terminal might say something like:
Bisecting: 25 revisions left to test after this (roughly 5 steps)
Test the Evidence
Now, you test your project. Run your application, check if the button is there.
- If the button is present, type:
git bisect good
- If the button is missing, type:
git bisect bad
Repeat and Conquer
Git will automatically move to a new commit. You test again and mark it as good or bad. You’ll repeat this process.
The Big Reveal
After a few steps (usually logarithmic to the number of commits), Git will proudly announce the first bad commit:
abc1234 is the first bad commit
commit abc1234 Author: Dev Devson
Date: Thu Aug 10 14:32:10 2023
Refactored the navigation component
Case Closed
You’ve found the culprit! The commit with the hash abc1234
is where the bug was introduced. Now you can study the changes in that commit to fix the bug. Finally, end the bisect session to return to the present.
$ git bisect reset
git bisect
transforms a daunting, manual process into a swift and systematic investigation. It’s an essential tool for any developer’s toolkit, saving you hours of frustration and making you look like a Git wizard. The next time a mysterious bug appears, don’t panic—just bisect!