Back to Git & Dev Tools
2026-04-068 min read

Fighting regressions with git bisect (Git & Dev Tools)

Learn Fighting regressions with git bisect (Git & Dev Tools) step by step with clear examples and exercises.

Title: Fighting Regressions with Git Bisect (Git & Dev Tools)

Why This Matters

In software development, regressions - when a previously working feature stops functioning due to code changes - can be a significant source of frustration and wasted time. Git bisect is a powerful tool that helps developers quickly identify the commit responsible for introducing a regression, allowing them to fix the issue more efficiently. This lesson will explain how git bisect works, demonstrate its usage through a worked example, discuss common mistakes, provide practice questions, answer frequently asked questions, and delve deeper into various aspects of Git bisect.

Prerequisites

To understand and use git bisect effectively, you should have a basic understanding of Git, including:

  1. Familiarity with the Git command line
  2. Understanding of Git branches and merging
  3. Knowledge of common Git commands like add, commit, pull, and push
  4. Basic understanding of how to debug code issues
  5. Familiarity with using Git on a project with multiple contributors
  6. Comfort working with the command line and text editors
  7. Understanding of version control concepts, such as branches, merges, and conflicts

Core Concept

Git bisect is a command that helps developers find the commit responsible for introducing a regression by performing a binary search through the commit history. It works by first marking the last known good commit (commit A) and the latest bad commit (commit B). Git bisect then checks out an intermediate commit (commit C), compiles, tests, and determines whether it is good or bad. Based on the result, git bisect will either move towards the good commits or the bad commits until it finds the offending commit.

Git Bisect Workflow

  1. Initialize the bisect: git bisect start
  2. Mark the last known good commit: git bisect good
  3. Mark the latest bad commit: git bisect bad
  4. Let git bisect perform the binary search: git bisect run (optional - this allows you to specify a script for testing your code)
  5. Once git bisect has found the offending commit, it will print the commit hash and prompt you to complete the bisect:
Bisecting: 2 revisions left to test after this (roughly 4 steps)
[a637e89] Add feature X
Your branch is up to date with 'origin/master'.
  1. At this point, you can use git bisect good or git bisect bad to confirm the correct commit and complete the bisecting process.

Git Bisect Internals

Git bisect uses a binary search algorithm to find the offending commit. It works by calculating a hash of the object database for each commit, comparing it with the hashes of the good and bad commits, and using the results to determine which half of the commit history contains the offending commit. This process continues recursively until the offending commit is found.

Git Bisect and Merge Conflicts

When working on a project with multiple contributors, it's possible that merge conflicts may arise during the bisecting process. In such cases, you will need to resolve the conflict manually before proceeding with the bisect. To do this, follow these steps:

  1. Checkout the commit in question: git checkout
  2. Resolve any merge conflicts that may have occurred
  3. Add and commit the resolved changes: git add . && git commit -m "Resolved merge conflict"
  4. Continue with the bisect process as usual

Git Bisect and Rebasing

If you are working on a feature branch and need to rebase it onto the latest master, you can still use git bisect to find the offending commit. First, perform the rebase:

git checkout <feature-branch>
git rebase origin/master

Then, initialize the bisect and mark the last known good commit (which should be the tip of your feature branch) and the latest bad commit on the master branch:

git checkout master
git bisect start
git bisect good <commit-on-master>
git checkout <feature-branch>
git bisect bad HEAD
git bisect run <script>

Follow the prompts to complete the bisect process.

Worked Example

Let's walk through a simple example where we introduce a regression in our codebase:

  1. Initialize a new Git repository and make an initial commit:
git init
echo "Hello, World!" > main.c
git add .
git commit -m "Initial commit"
  1. Create a file with a bug:
echo "printf(\"Hello, World!\\n\");" > main_buggy.c
  1. Commit the buggy file:
git add main_buggy.c
git commit -m "Introduce bug in main.c"
  1. Initialize bisect, mark the last known good commit, and the latest bad commit:
git checkout master
git bisect start
git bisect good HEAD~1
git bisect bad HEAD
  1. Let git bisect perform the binary search:
git bisect run ./test_main.sh

(Assuming you have a script test_main.sh that tests your code.)

  1. Follow the prompts to confirm good or bad commits until git bisect finds the offending commit.

Common Mistakes

  1. Forgetting to mark the last known good commit: Make sure you have a clean, working version of your code before starting the bisect process.
  2. Marking the wrong commit as good or bad: Double-check that you are marking the correct commits to ensure an accurate search.
  3. Not providing a testing script: If you don't provide a testing script, git bisect will use the default behavior of running make if available or using the last commit's editor configuration.
  4. Failing to complete the bisect process: Once git bisect has found the offending commit, make sure to complete the bisect by marking it as good or bad.
  5. Ignoring merge conflicts during the bisect process: When working on a project with multiple contributors, you may encounter merge conflicts that need to be resolved before proceeding with the bisect.
  6. Using Git Bisect inappropriately: Git Bisect is best suited for finding regressions introduced by specific commits. It's not intended for general debugging or finding performance issues.
  7. Not understanding the commit history: Before starting a bisect, it's important to have a clear understanding of the commit history and any relevant context that may impact the search.
  8. Bisecting on unstable branches: If your branch is frequently being updated with breaking changes, Git Bisect may not be the best tool for finding regressions. In such cases, consider using other tools or strategies to isolate the issue.

Practice Questions

  1. You have a Git repository with multiple branches. How would you use git bisect to find the commit that introduced a regression in the develop branch?
  • First, checkout the develop branch: git checkout develop.
  • Initialize the bisect and mark the last known good commit on the develop branch as good, and the latest bad commit on the master branch as bad.
  • Run git bisect run to perform the binary search.
  1. Suppose you are working on a large codebase, and you're not sure which part of the code is causing a regression. How can you narrow down your search using git bisect?
  • You can start by isolating the feature or functionality that appears to be affected by the regression. Then, create a new branch containing only the relevant changes.
  • Initialize the bisect on the new branch and mark the last known good version as good, and the latest bad commit as bad. Git Bisect will help you find the offending commit within the isolated codebase.
  1. You have a Git repository with many commits, and the regression was introduced several commits ago. What strategies can you employ to make the git bisect process more efficient?
  • To speed up the bisect process, you can use git log --graph to visualize your commit history and identify potential candidates for the offending commit. This will help you narrow down the search area before starting the bisect.
  • If the regression was introduced a long time ago, consider creating a new branch from an older commit and performing the bisect on that branch.

FAQ

Q: Can I use git bisect with non-C projects?

A: Yes, git bisect works with any project managed by Git as long as it has a way to compile and test the code.

Q: What happens if multiple commits introduce the same regression?

A: In this case, git bisect will find the earliest commit that introduced the regression among the offending commits.

Q: Can I use git bisect to find a commit that fixed a bug instead of finding the commit that introduced it?

A: Yes, you can do this by marking the latest good commit as bad and then following the same process to find the commit that fixed the bug.

Q: What should I do if git bisect encounters an error or gets stuck?

A: If git bisect encounters an error or gets stuck, you can manually reset your branch to a known good state using git reset --hard and then restart the bisect process.

Q: Can I use Git Bisect to find performance regressions?

A: While Git Bisect is designed for finding regressions that affect functionality, it can also be used to identify performance regressions if you have a way to measure and compare performance between commits. However, other tools like perf or valgrind may be more suitable for this purpose.

Q: Can I use Git Bisect with GitHub?

A: Yes, you can use Git Bisect with GitHub by following the same process as described in this lesson. You'll need to have a local clone of your repository and perform the bisect locally before pushing the changes back to GitHub.

Q: Can I use Git Bisect with pull requests?

A: Yes, you can use Git Bisect with pull requests by following these steps:

  • Create a new branch based on the pull request's source branch.
  • Initialize the bisect and mark the last known good commit as good, and the latest bad commit on the target branch (usually master or main) as bad.
  • Run git bisect run to perform the binary search.

This will help you find the commit that introduced the regression in the pull request.

Fighting regressions with git bisect (Git & Dev Tools) | Git & Dev Tools | XQA Learn