跳到正文
DEV Community· kush_exe·· 54 分鐘前AI 評分53

Git Bisect 解説:用約 10 次檢查找出有問題的提交

Git Bisect Explained: Find the Bad Commit in About 10 Checks

AI 導讀

Git bisect 透過二分搜尋提交歷史,根據一個已知有問題的提交和一個較早的正常提交,約用 10 次檢查找出有問題的提交。git bisect run 可執行測試腳本並按退出碼判定:0 為正常、1 至 127(125 除外)為有問題、125 跳過,128 至 255 則停止搜尋。文章亦説明錯誤判定或不可靠的測試會影響結果,並介紹如何修正錯誤記錄後重播搜尋。

正文

Skip to content

Image 1: DEV Community

Powered by Algolia

Log inCreate account

DEV Community

Image 20 Add reaction

Image 30 Like Image 40 Unicorn Image 50 Exploding Head Image 60 Raised Hands Image 70 Fire

0 Jump to Comments 0 Save Boost

Pick as gem

Copy link

Copied to Clipboard

Share to XShare to LinkedInShare to FacebookShare to Mastodon

Share Post via...Report Abuse

Image 8: Cover image for Git Bisect Explained: Find the Bad Commit in About 10 Checks

Image 9: kush_exe

kush_exe Posted on Nov 23•Originally published at kuzscotech.github.io

Git Bisect Explained: Find the Bad Commit in About 10 Checks

#programming#productivity#tooling

Git bisect explained: mark one bad commit and an older good one, and Git's binary search finds which of the last thousand commits broke your code in about ten checks, not one by one. Each answer throws out about half the suspects. In the manual's own example, 675 revisions left becomes 337, and that example goes back to Linus Torvalds's original git bisect commit from July 2005.

Then the surprise: you don't have to answer yourself. git bisect run takes your test script and reads its exit code: 0 means good, 1 to 127 means bad, 125 means skip this commit, and anything above 127 stops the search. We also cover custom terms like fast and slow for finding the commit that made things slow, and the catches: one wrong answer sends the search off course, your test has to be reliable, skipping a commit next to the culprit leaves you a list of suspects, and bisect needs one commit that still worked. It ends with the five commands to run and why small commits make bisect more effective.

Video 1

Cheat sheet

What git bisect does

git bisect is a binary search over your history. You give it one bad commit and one older good commit, it checks out a commit between them and asks good or bad, and each answer throws out about half the suspects. Source: the git bisect manual.

The manual loop

The manual's own example:

git bisect start
git bisect bad                 # Current version is bad
git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good

Git checks out a commit in the middle of the range and prints something like Bisecting: 675 revisions left to test after this (roughly 10 steps). Test that commit, answer git bisect good or git bisect bad, and repeat until Git names the commit.

git bisect reset

git bisect reset returns you to the commit that was checked out before git bisect start.

Let a script answer

git bisect start
git bisect bad
git bisect good <commit that worked>
git bisect run <your test>
git bisect reset

git bisect run runs your command at every step and reads its exit code:

Exit code What Git does
0 Marks the commit good
1 to 127, except 125 Marks the commit bad
125 Skips the commit: it cannot be tested
128 to 255 Stops the bisect

The manual's example script, which skips commits that do not build:

#!/bin/sh
make || exit 125                     # this skips broken builds
~/check_test_case.sh                 # does the test case pass?
git bisect start HEAD HEAD~10 --   # culprit is among the last 10
git bisect run ~/test.sh
git bisect reset                   # quit the bisect session

The same thing on one line, with no script file:

git bisect run sh -c "make || exit 125; ~/check_test_case.sh"

The manual says it is safer to keep the test scripts outside the repository.

Not only for bugs

Pick your own terms to find the commit that changed any property:

git bisect start --term-old fast --term-new slow
git bisect start --term-new fixed --term-old broken

You cannot mix good and bad with old and new in one session.

Fix a wrong answer

Save the output of git bisect log to a file, remove the wrong entries, then:

git bisect reset
git bisect replay that-file

Fewer checks

  • Limit the search to the paths you suspect: git bisect start -- arch/i386 include/asm-i386
  • Give several known-good commits at the start.
  • Skip a whole range: git bisect skip v2.5..v2.6

How fast it narrows

Git prints Estimate Where
675 revisions left roughly 10 steps The manual's example
337 revisions left roughly 9 steps The manual's example, one answer later
10928 revisions left roughly 14 steps Linux kernel v2.6.25 to v2.6.27, in Git's bisect paper

The count is what is left after the commit just checked out.

Catches

  • One wrong answer. The Linux kernel's guide warns that getting it wrong just once sends the rest of the bisection totally off course.
  • An unreliable test. The result is only as accurate as the test. Chasing a kernel that booted only sometimes, Nathan Chancellor passed a commit only when it booted ten times out of ten.
  • A crashing test. An exit code from 128 to 255, such as 255 or a test killed by a signal, stops the bisect instead of marking the commit bad.
  • Skipping next to the culprit. Git cannot tell which commit was first bad. It lists the candidates and prints We cannot bisect more!.
  • Where it changed, not where the mistake is. The commit bisect names is where behavior changed. LWN noted in 2008 that the patch that makes a bug show up may not be the one that introduced it.
  • No good commit, no bisect. Bisect only works if there was a point where the problem did not happen.
  • Merges.--first-parent (Git 2.29 and later) follows only the main line, so a bug that came in through a merged branch is pinned on the merge commit.

Keep commits small

Git's documentation advises one small logical change per commit and no commits that knowingly break things: the smaller the changes in a commit, the more effective git bisect is.

Which commit broke it? About 10 checks

You asked for this one. Which of the last thousand commits broke your code? You'd assume you test them one by one. Git needs about ten checks. The command is git bisect. And yes, it's binary search: the manual says so itself.

How git bisect works: good, bad, halve

You mark one commit bad and an older one good. Git checks out one in the middle and asks: good, or bad? Each answer throws out about half the suspects. In the manual's example, 675 revisions left becomes 337.

Linus Torvalds's 2005 example, and 10,928 revisions

That example goes back to Linus Torvalds's original git bisect commit, in July 2005. And it scales. In Git's docs, a kernel search starts with nearly eleven thousand revisions left: roughly 14 steps.

git bisect run: your script answers with an exit code

Here's the surprise: you don't have to answer yourself. Git bisect run takes your test script, runs it at every step, and reads the exit code. Exit zero means good. Anything from 1 to 127 means bad, with one exception.

Exit 125: skip commits you can't test

Exit 125 means: I can't test this commit, skip it. Anything above 127 stops the search. So the manual's example script starts with make, or exit 125. A commit that won't build gets skipped, not blamed.

Best case: a kernel bisect in 20-30 minutes; not only bugs

As kernel developer Ingo Molnar put it, in a 2009 paper in Git's docs: "With Git bisect it's a breeze." His best case: a roughly 15-step kernel bisection in 20 to 30 minutes, automated. It isn't only for bugs. Pick your own terms, like fast and slow, and it finds the commit that made things slow.

Catches: one wrong answer, unreliable tests, replay

Now the catches. The Linux kernel's guide warns that one wrong answer sends the rest of the search "totally off course." Your test has to be reliable. Chasing a kernel that booted only sometimes, developer Nathan Chancellor passed a commit only when it booted ten times out of ten. Answered one wrong anyway? You don't have to start over: save git bisect log, remove the wrong entry, and replay it.

Skipped commits, and bugs that were always there

Skipping has a price too. Skip a commit right next to the culprit, and Git can't name one: you get a list of suspects. And the commit it names is where behavior changed, not always where the mistake was made. If the bug was always there, bisect can't help.

What to do: start, bad, good, run, reset

So here's what to do. Git bisect start, mark the bad commit and one that still worked, then git bisect run your test. When Git names the commit, git bisect reset puts you back where you started.

Keep commits small, and the verdict

And keep commits small: Git's docs say the smaller the change, the more effective bisect gets. So yes, it's binary search, but you don't do the searching. Give git bisect one good commit and a test you trust, and let it run.

Sources

Read the full transcript, with every source, on the page for this video. More explainers like this one are on the Kush_EXE YouTube channel.

This article is built from the video's script and published sources. The video uses AI illustrations.

Image 10: Google Cloud Google CloudPromoted

Image 11: Fully Managed MySQL, PostgreSQL, and SQL Server

Fully Managed MySQL, PostgreSQL, and SQL Server

Fully managed, cost-effective relational database service for PostgreSQL, MySQL, and SQL Server. Try Enterprise Plus edition for a 99.99% availability SLA and category-leading performance.

Learn More →

Image 12: profile The DEV TeamPromoted


Image 13: Strands harness

Strands harness is a state-of-the-art, fully assembled agent harness.

Strands harness is a complete, production-ready agent the moment you install it. Shell, file, and web tools, plus context management, memory, sessions, and subagents, are tuned and ready. Run it as a library or from the strands CLI, on any model and any cloud. Every default is yours to change through the open-source Harness SDK.

Get Started with Strands

Read More

Top comments (0)

Subscribe

Image 14: pic

Submit PreviewDismiss

Code of Conduct•Report abuse

Image 15: profile The DEV TeamPromoted


Image 16: Strands harness

Strands harness is a state-of-the-art, fully assembled agent harness.

Strands harness is a complete, production-ready agent the moment you install it. Shell, file, and web tools, plus context management, memory, sessions, and subagents, are tuned and ready. Run it as a library or from the strands CLI, on any model and any cloud. Every default is yours to change through the open-source Harness SDK.

Get Started with Strands

Image 17: Google Cloud Google CloudPromoted

Image 18: Custom VMs From 1 to 96 vCPUs With 99.95% Uptime

Custom VMs From 1 to 96 vCPUs With 99.95% Uptime

Run VMs on high-performance and reliable cloud infrastructure. Choose from preset or custom machine types for web servers, databases, and applications that fuel your agents.

Learn More →

Image 19 kush_exe

Follow

  • Joined Nov 11, 2026

More from kush_exe

Git Worktree Explained: Run Coding Agents in Parallel (and the Catch) #programming#productivity#toolingCan you trust ChatGPT to tell you if a photo is AI? #ai#news#programming

Image 20: profile The DEV TeamPromoted


Image 21: Strands harness

Strands harness is a state-of-the-art, fully assembled agent harness.

Strands harness is a complete, production-ready agent the moment you install it. Shell, file, and web tools, plus context management, memory, sessions, and subagents, are tuned and ready. Run it as a library or from the strands CLI, on any model and any cloud. Every default is yours to change through the open-source Harness SDK.

Get Started with Strands

Image 22: Google Cloud Google CloudPromoted

Image 23: $300 Free Credits for Your Google Cloud Projects

$300 Free Credits for Your Google Cloud Projects

Easily integrate with Google Cloud's AI, AppDev, and data services. Avoid vendor lock-in and run your apps on open source solutions. Start your proof of concept with Google Cloud’s easy-to-use platform, tools, and APIs.

Learn More →

👋 Kindness is contagious


Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay

DEV Community — A space to discuss and keep up software development and manage your software career

Built on Forem — the open source software that powers DEV and other inclusive communities.

Made with love and Ruby on Rails. DEV Community © 2016 - 2026.

Image 24: DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log inCreate account

Image 25Image 26Image 27Image 28Image 29

來源:DEV Community · dev.to