Find the commit that broke your code with git bisect
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.
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 stashotherwise) - 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
-
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.
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 -
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 formgit bisect start <bad> <good>does all three in one command. On a history with many merged branches,--first-parentrestricts the search to commits on the main branch.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 -
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. Answergit bisect goodif the bug is absent,git bisect badif 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_modulesorvendorgives false results. -
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.git bisect skip git bisect skip a1b2c3d e4f5a6b -
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 +xon macOS and Linux; on Windows, use Git 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 -
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 loglists all your answers andgit bisect replayreplays them if you made a mistake at one step, after fixing the wrong line in the file.git show abc1234 git bisect log > ../bisect.log # corriger une réponse dans bisect.log, puis : git bisect reset git bisect replay ../bisect.log -
Finish with git bisect reset
Bisection leaves the repository in detached HEAD on an old commit.
git bisect resettakes you back to the branch you started from;git bisect reset HEADstays 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-oldand--term-new.git bisect reset git bisect start --term-old=rapide --term-new=lent git bisect lent HEAD git bisect rapide v3.0.0 -
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.
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
- Starting from a fake good commit: if the bug already existed, the result points to an innocent commit. Verify the starting point.
- Not reinstalling dependencies: a
node_modules,vendoror build cache from another commit skews the test. - A flaky test: a test that fails one time in three sends the bisection the wrong way. Make it reliable first.
- Keeping the test script in the tracked repository: it disappears or changes depending on the commit being tested.
- 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
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug Node.js with breakpoints (VS Code, Chrome)
- Debug PHP with Xdebug 3 (PhpStorm, VS Code)
- Read Docker container logs and debug a crashing container
- Debug an Arduino with the Serial Monitor
- How to create a minimal reproducible example