Git Bisect for Debugging
Utilize `git bisect` to efficiently find the specific commit that introduced a bug, saving valuable debugging time.
Git Bisect for Debugging is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Finding Bugs Faster with Git
Debugging can be tough. Sometimes a bug appears, and you don't know which change caused it.
git bisect is a powerful Git command that helps you pinpoint the exact commit that introduced a bug. It uses a binary search algorithm to quickly narrow down thousands of commits.
The Power of Binary Search
Imagine your project has 100 commits, and a bug appeared somewhere between the first and last. Checking each one manually is slow!
git bisect works like a "guess the number" game:
- You tell Git a "good" commit (where the bug wasn't present).
- You tell Git a "bad" commit (where the bug is present).
- Git picks a commit roughly in the middle and asks you to test it.
- You mark it "good" or "bad", and Git halves the search space.
Let's Start the Search!
To begin, you need to tell Git you're starting a bisect session. This command initializes the process:
git bisect start
After starting, you'll specify a known "bad" commit (where the bug exists) and a known "good" commit (where the bug definitely did not exist).
Often, the "bad" commit is your current HEAD, and a "good" commit might be an older release tag or a commit from a few days ago.
Marking Your Commit Boundaries
Once you've started git bisect, you define your search range:
- Bad Commit: This is a commit where you know the bug exists. Use
git bisect bad <commit-hash>or simplygit bisect badif it's your currentHEAD. - Good Commit: This is a commit where you know the bug did NOT exist. Use
git bisect good <commit-hash>.
Git will then automatically checkout a commit in the middle of this range.
Testing & Narrowing Down
After you've marked your good and bad commits, Git will automatically check out a commit in the middle of the range. Your job is to test the code at this checked-out commit.
- If the bug IS present, type
git bisect bad. - If the bug IS NOT present, type
git bisect good.
Git will then pick another middle commit from the remaining range. You repeat this until Git finds the first "bad" commit.
Simulating a Bug's Introduction
Let's imagine a simple "bug" in a script. We'll simulate a project history where a small error was introduced.
Here's our initial "good" script:
main.sh
#!/bin/bash
echo "Hello from script!"
sum=0
for i in $(seq 1 3); do
sum=$((sum + i))
done
echo "Sum is: $sum"Manual Bisect Walkthrough
Now, let's say a later commit accidentally changed the loop to seq 1 4, making the sum incorrect (10 instead of 6).
We'd start:
git bisect start
Then mark our current HEAD (where the bug exists) as bad:
git bisect bad HEAD
And an older, known working commit as good:
git bisect good <old_good_commit_hash>
Git will then guide you, checking out commits. You'd run ./main.sh, see if the sum is 6 or 10, and then type git bisect good or git bisect bad accordingly.
Automating the Debugging Process
Testing each commit manually can still be time-consuming if the test is complex. git bisect run lets you automate this!
You provide a script that exits with:
0(zero) if the commit is good.125if the commit should be skipped.- Any other non-zero value (e.g.,
1) if the commit is bad.
Git will execute this script on each tested commit until the bug is found.
#!/bin/bash
./main.sh | grep "Sum is: 6"
if [ $? -eq 0 ]; then
exit 0 # Good commit
else
exit 1 # Bad commit
fiFinishing Up Your Search
Once git bisect successfully identifies the first bad commit, it will tell you the commit hash and its message.
After you've found the culprit and understand the issue, it's crucial to reset your repository to its original state before the bisect started:
git bisect reset
This command takes you back to the commit you were on when you initiated the bisect, cleaning up the temporary bisect state.
Bisect Knowledge Check
You've just run git bisect start, marked your current HEAD as bad, and an older commit as good. Git checks out a commit in the middle.
You run your tests, and the bug IS PRESENT at this middle commit.
`git bisect` Recap
Congratulations! You've learned how to use git bisect:
- It's a powerful tool for finding the exact commit that introduced a bug.
- It uses a binary search for efficiency.
- You define "good" (bug-free) and "bad" (bug-present) commits.
- You can automate the testing process with
git bisect run <script>. - Always remember to run
git bisect resetto clean up after your search!
Frequently asked questions
Is the “Git Bisect for Debugging” lesson free?
Yes — the full text of “Git Bisect for Debugging” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Git Bisect for Debugging”?
Utilize `git bisect` to efficiently find the specific commit that introduced a bug, saving valuable debugging time. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Git Bisect for Debugging” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.