Skip to content
  1. LuzineDebug
  2. Guides
  3. Find the commit that broke your code with git bi…
Guide Intermediate ≈ 12 min

Find the commit that broke your code with git bisect

In short

git bisect runs a binary search through history: you mark a commit with the bug (bad) and one without it (good), Git checks out the middle and you say whether it is good or bad. With 1,000 commits, about ten tests are enough. git bisect run automates everything with a script.

Be the first to approve this guide

Why this method

A feature worked three weeks ago, it is broken today, and hundreds of commits slipped in between. Instead of reading every diff, git bisect halves the range at each step and isolates the exact commit that introduced the regression. Once that commit is found, the diff is often short and the fix obvious. The method works for a bug, a performance drop or any behaviour you know how to test.

What you need

  • Git 2.x and a repository with no pending changes (git stash otherwise)
  • A commit, tag or release where the bug did not exist
  • A reliable way to reproduce the bug, ideally an automated test

Step by step

  1. Find a good commit

    You need a starting point where the bug did not exist: a release tag, a deploy commit, or a commit picked from a month ago. Actually verify it: check it out as a detached HEAD, install dependencies and test. If the bug is already there, go further back. A wrong starting point makes the whole result wrong.

    bash
    git log --oneline --since="4 weeks ago" | tail -5
    git tag --sort=-creatordate | head
    git switch --detach v3.2.0
    # tester, puis revenir
    git switch main
  2. Start the bisection

    Run git bisect start, mark the current commit as bad and the good commit as good. Git immediately checks out the middle commit and tells you how many steps remain. The short form git bisect start <bad> <good> does all three in one command. On a history with many merged branches, --first-parent restricts the search to commits on the main branch.

    bash
    git bisect start
    git bisect bad              # HEAD contient le bug
    git bisect good v3.2.0
    # Bisecting: 120 revisions left to test after this (roughly 7 steps)
    
    # équivalent en une ligne
    git bisect start HEAD v3.2.0
  3. Test and mark each step

    At each step, rebuild what is needed (npm ci, composer install, migrations on a test database) then test the behaviour. Answer git bisect good if the bug is absent, git bisect bad if it is present. Git moves to the middle of the remaining range. The number of steps is about log2 of the number of commits: 7 for 120 commits, 10 for 1,000.

    Dependencies change between commits: a stale node_modules or vendor gives false results.

  4. Skip commits you cannot test

    Some commits do not build or break something else, so you cannot judge the bug. Do not mark them good or bad at random: use git bisect skip. Git picks a nearby commit. If too many commits around the culprit are skipped, Git ends by listing several candidates instead of one, which you settle by reading their diffs.

    bash
    git bisect skip
    git bisect skip a1b2c3d e4f5a6b
  5. Automate with git bisect run

    If a script can tell whether the bug is present, Git does the rest. The exit code decides: 0 means good, 1 to 127 (except 125) means bad, 125 means "skip", above 127 the bisection aborts. Keep the script outside the repository or in an untracked file: otherwise it changes from one commit to the next. Do not forget chmod +x on macOS and Linux; on Windows, use Git Bash.

    bash
    #!/bin/sh
    # ../bisect-test.sh (outside the repository)
    npm ci --silent || exit 125     # cannot install: skip
    npx vitest run tests/cart-total.test.ts
  6. Read the result and keep a record

    At the end, Git prints "abc1234 is the first bad commit" with author, date and message. Read its diff with git show. Before leaving, save the log: git bisect log lists all your answers and git bisect replay replays them if you made a mistake at one step, after fixing the wrong line in the file.

    bash
    git show abc1234
    git bisect log > ../bisect.log
    # corriger une réponse dans bisect.log, puis :
    git bisect reset
    git bisect replay ../bisect.log
  7. Finish with git bisect reset

    Bisection leaves the repository in detached HEAD on an old commit. git bisect reset takes you back to the branch you started from; git bisect reset HEAD stays on the commit you found. To search for something other than a bug, for example the commit that fixed an issue or made a page slow, rename the terms with --term-old and --term-new.

    bash
    git bisect reset
    
    git bisect start --term-old=rapide --term-new=lent
    git bisect lent HEAD
    git bisect rapide v3.0.0
  8. Real case: a wrong cart total

    Since version 3.4, the cart total is off by a few cents. Version 3.2.0 was correct and 120 commits lie in between. We write a test that checks the total of a three-item cart, keep it outside the repository, then start an automatic bisection. Seven steps later, Git points to a commit titled "refactor: prices as float": switching from integer cents to floating-point numbers introduced the rounding error.

    bash
    git stash
    git bisect start HEAD v3.2.0
    git bisect run ../bisect-test.sh
    # 9f3e2d1 is the first bad commit
    git show 9f3e2d1 --stat
    git bisect reset
    git stash pop

Mistakes to avoid

  1. Starting from a fake good commit: if the bug already existed, the result points to an innocent commit. Verify the starting point.
  2. Not reinstalling dependencies: a node_modules, vendor or build cache from another commit skews the test.
  3. A flaky test: a test that fails one time in three sends the bisection the wrong way. Make it reliable first.
  4. Keeping the test script in the tracked repository: it disappears or changes depending on the commit being tested.
  5. Forgetting git bisect reset: you keep working in detached HEAD and new commits belong to no branch.

Checklist

Did this guide help you?

Approve it in one click: it will be highlighted for other developers.

Frequently asked questions

How does git bisect work?

It runs a binary search between a good and a bad commit: each answer halves the range, until the first bad commit is isolated.

What if a commit does not build during git bisect?

Type git bisect skip: Git picks a nearby commit. In a git bisect run script, exit with code 125.

How do I cancel a git bisect?

git bisect reset stops the bisection and takes you back to the original branch, without changing history.

Can git bisect be automated?

Yes: git bisect run ./script.sh or directly git bisect run npm test. Exit code 0 means good, 1 to 127 bad and 125 skip.

Tools to apply this guide

More guides