World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Find the bug with bisect

You will learn: how to find the commit that broke something, in a few quick tests.

The problem

The shop page shows the price as RM0! Last week it said RM10. Ten commits since then. Which one broke it?

Pick one answer.

Watch

git bisect start with HEAD as bad and HEAD~9 as good. Git says 4 revisions are left and checks out Add a footer. Terminal output: $ git bisect start HEAD HEAD~9 Bisecting: 4 revisions left to test after this (roughly 2 steps) [82a74e38e18f28bb624a2849ee62343753d9790d] Add a footer

bisect: Finding the commit that broke something by testing the middle, again and again. plays “higher or lower” with your history. Give it one bad and one good commit. Git checks out the middle one for you to test.

Try it

Be the detective. Run the test, then mark the commit good or bad.

The oldest commit works. The newest one is broken. Find the first bad commit.

The test: the price in index.html must not be RM0.

  1. 87509ecAdd shop pagegood
  2. a2fb616Add a photomaybe
  3. 70587efAdd opening hoursmaybe
  4. 0844232Add a menu linkmaybe
  5. 82a74e3Add a footermaybe
  6. c35bad4Tidy up spacingmaybe
  7. 30a497fAdd phone numbermaybe
  8. 037d9a6Add map linkmaybe
  9. 823e211Add FAQ linkmaybe
  10. accccefUpdate footer yearbad

Tests used: 0. For 9 commits, bisect needs at most 4.

Predict

Now there are 1,000 commits between good and bad. About how many tests does bisect need?

Pick one answer.

Watch

git bisect run sh test.sh tests three commits by itself and names Tidy up spacing as the first bad commit. Terminal output: $ git bisect start HEAD HEAD~9 Bisecting: 4 revisions left to test after this (roughly 2 steps) [82a74e38e18f28bb624a2849ee62343753d9790d] Add a footer $ git bisect run sh test.sh running 'sh' 'test.sh' Bisecting: 2 revisions left to test after this (roughly 1 step) [30a497f8e9e2c392b1a4a259fd5d43e8cde21bfb] Add phone number running 'sh' 'test.sh' Bisecting: 0 revisions left to test after this (roughly 0 steps) [c35bad4140074d2904a4cff307aafecb7d75173e] Tidy up spacing running 'sh' 'test.sh' c35bad4140074d2904a4cff307aafecb7d75173e is the first 'bad' commit commit c35bad4140074d2904a4cff307aafecb7d75173e (HEAD) Author: You <you@example.com> Date: Fri Oct 30 09:06:00 2026 +0800 Tidy up spacing index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) bisect found first 'bad' commit

Got a test script? git bisect run sh test.sh does every step for you. Exit code 0 means good. 1 means bad.

Try it

Your turn. Start bisect, let test.sh judge, then go back home.

87509eca2fb61670587ef084423282a74e3c35bad430a497f037d9a6823e211accccef

Check

Put a bisect hunt in order.

First step at the top. Put these in order. Use the arrows, or drag.

  • Test the commit Git checks out
  • Run git bisect reset
  • Run git bisect start
  • Mark it good or bad, until Git names the culprit
  • Mark one bad and one good commit

You did it!

You can now find the commit that broke something, fast.

Go deeper (optional)

Can’t test one commit, say it doesn’t build? git bisect skip picks a neighbour. git bisect log shows your steps. Hunting a fix, not a bug? Use git bisect old and git bisect new instead of good and bad. In bisect run, exit code 125 means skip.

After git bisect skip, Git checks out a different commit to test. Terminal output: $ git bisect start HEAD HEAD~9 Bisecting: 4 revisions left to test after this (roughly 2 steps) [82a74e38e18f28bb624a2849ee62343753d9790d] Add a footer $ git bisect skip Bisecting: 4 revisions left to test after this (roughly 2 steps) [c35bad4140074d2904a4cff307aafecb7d75173e] Tidy up spacing