<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-bisect(1)">
<articleinfo>
    <title>git-bisect(1)</title>
        <indexterm>
                <primary>git-bisect(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-bisect - Find the change that introduced a bug by binary search</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara><emphasis>git bisect</emphasis> &lt;subcommand&gt; &lt;options&gt;</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>The command takes various subcommands, and different options depending
on the subcommand:</simpara>
<literallayout class="monospaced">git bisect help
git bisect start [&lt;bad&gt; [&lt;good&gt;...]] [--] [&lt;paths&gt;...]
git bisect bad [&lt;rev&gt;]
git bisect good [&lt;rev&gt;...]
git bisect skip [(&lt;rev&gt;|&lt;range&gt;)...]
git bisect reset [&lt;branch&gt;]
git bisect visualize
git bisect replay &lt;logfile&gt;
git bisect log
git bisect run &lt;cmd&gt;...</literallayout>
<simpara>This command uses <emphasis>git rev-list --bisect</emphasis> to help drive the
binary search process to find which change introduced a bug, given an
old "good" commit object name and a later "bad" commit object name.</simpara>
<simplesect id="_getting_help">
<title>Getting help</title>
<simpara>Use "git bisect" to get a short usage description, and "git bisect
help" or "git bisect -h" to get a long usage description.</simpara>
</simplesect>
<simplesect id="_basic_bisect_commands_start_bad_good">
<title>Basic bisect commands: start, bad, good</title>
<simpara>The way you use it is:</simpara>
<literallayout>$ git bisect start
$ git bisect bad                 # Current version is bad
$ git bisect good v2.6.13-rc2    # v2.6.13-rc2 was the last version
                                 # tested that was good</literallayout>
<simpara>When you give at least one bad and one good versions, it will bisect
the revision tree and say something like:</simpara>
<literallayout>Bisecting: 675 revisions left to test after this</literallayout>
<simpara>and check out the state in the middle. Now, compile that kernel, and
boot it. Now, let&#8217;s say that this booted kernel works fine, then just
do</simpara>
<literallayout>$ git bisect good                       # this one is good</literallayout>
<simpara>which will now say</simpara>
<literallayout>Bisecting: 337 revisions left to test after this</literallayout>
<simpara>and you continue along, compiling that one, testing it, and depending
on whether it is good or bad, you say "git bisect good" or "git bisect
bad", and ask for the next bisection.</simpara>
<simpara>Until you have no more left, and you&#8217;ll have been left with the first
bad kernel rev in "refs/bisect/bad".</simpara>
</simplesect>
<simplesect id="_bisect_reset">
<title>Bisect reset</title>
<simpara>Oh, and then after you want to reset to the original head, do a</simpara>
<literallayout>$ git bisect reset</literallayout>
<simpara>to get back to the original branch, instead of being on the bisection
commit ("git bisect start" will do that for you too, actually: it will
reset the bisection state).</simpara>
</simplesect>
<simplesect id="_bisect_visualize">
<title>Bisect visualize</title>
<simpara>During the bisection process, you can say</simpara>
<literallayout>$ git bisect visualize</literallayout>
<simpara>to see the currently remaining suspects in <emphasis>gitk</emphasis>.  <literal>visualize</literal> is a bit
too long to type and <literal>view</literal> is provided as a synonym.</simpara>
<simpara>If <emphasis>DISPLAY</emphasis> environment variable is not set, <emphasis>git log</emphasis> is used
instead.  You can even give command line options such as <literal>-p</literal> and
<literal>--stat</literal>.</simpara>
<literallayout>$ git bisect view --stat</literallayout>
</simplesect>
<simplesect id="_bisect_log_and_bisect_replay">
<title>Bisect log and bisect replay</title>
<simpara>The good/bad input is logged, and</simpara>
<literallayout>$ git bisect log</literallayout>
<simpara>shows what you have done so far. You can truncate its output somewhere
and save it in a file, and run</simpara>
<literallayout>$ git bisect replay that-file</literallayout>
<simpara>if you find later you made a mistake telling good/bad about a
revision.</simpara>
</simplesect>
<simplesect id="_avoiding_to_test_a_commit">
<title>Avoiding to test a commit</title>
<simpara>If in a middle of bisect session, you know what the bisect suggested
to try next is not a good one to test (e.g. the change the commit
introduces is known not to work in your environment and you know it
does not have anything to do with the bug you are chasing), you may
want to find a near-by commit and try that instead.</simpara>
<simpara>It goes something like this:</simpara>
<literallayout>$ git bisect good/bad                   # previous round was good/bad.
Bisecting: 337 revisions left to test after this
$ git bisect visualize                  # oops, that is uninteresting.
$ git reset --hard HEAD~3               # try 3 revs before what
                                        # was suggested</literallayout>
<simpara>Then compile and test the one you chose to try. After that, tell
bisect what the result was as usual.</simpara>
</simplesect>
<simplesect id="_bisect_skip">
<title>Bisect skip</title>
<simpara>Instead of choosing by yourself a nearby commit, you may just want git
to do it for you using:</simpara>
<literallayout>$ git bisect skip                 # Current version cannot be tested</literallayout>
<simpara>But computing the commit to test may be slower afterwards and git may
eventually not be able to tell the first bad among a bad and one or
more "skip"ped commits.</simpara>
<simpara>You can even skip a range of commits, instead of just one commit,
using the "<emphasis>&lt;commit1&gt;</emphasis>..<emphasis>&lt;commit2&gt;</emphasis>" notation. For example:</simpara>
<literallayout>$ git bisect skip v2.5..v2.6</literallayout>
<simpara>would mean that no commit between <literal>v2.5</literal> excluded and <literal>v2.6</literal> included
can be tested.</simpara>
<simpara>Note that if you want to also skip the first commit of a range you can
use something like:</simpara>
<literallayout>$ git bisect skip v2.5 v2.5..v2.6</literallayout>
<simpara>and the commit pointed to by <literal>v2.5</literal> will be skipped too.</simpara>
</simplesect>
<simplesect id="_cutting_down_bisection_by_giving_more_parameters_to_bisect_start">
<title>Cutting down bisection by giving more parameters to bisect start</title>
<simpara>You can further cut down the number of trials if you know what part of
the tree is involved in the problem you are tracking down, by giving
paths parameters when you say <literal>bisect start</literal>, like this:</simpara>
<literallayout>$ git bisect start -- arch/i386 include/asm-i386</literallayout>
<simpara>If you know beforehand more than one good commits, you can narrow the
bisect space down without doing the whole tree checkout every time you
give good commits. You give the bad revision immediately after <literal>start</literal>
and then you give all the good revisions you have:</simpara>
<literallayout>$ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
                   # v2.6.20-rc6 is bad
                   # v2.6.20-rc4 and v2.6.20-rc1 are good</literallayout>
</simplesect>
<simplesect id="_bisect_run">
<title>Bisect run</title>
<simpara>If you have a script that can tell if the current source code is good
or bad, you can automatically bisect using:</simpara>
<literallayout>$ git bisect run my_script</literallayout>
<simpara>Note that the "run" script (<literal>my_script</literal> in the above example) should
exit with code 0 in case the current source code is good.  Exit with a
code between 1 and 127 (inclusive), except 125, if the current
source code is bad.</simpara>
<simpara>Any other exit code will abort the automatic bisect process. (A
program that does "exit(-1)" leaves $? = 255, see exit(3) manual page,
the value is chopped with "&amp; 0377".)</simpara>
<simpara>The special exit code 125 should be used when the current source code
cannot be tested. If the "run" script exits with this code, the current
revision will be skipped, see <literal>git bisect skip</literal> above.</simpara>
<simpara>You may often find that during bisect you want to have near-constant
tweaks (e.g., s/#define DEBUG 0/#define DEBUG 1/ in a header file, or
"revision that does not have this commit needs this patch applied to
work around other problem this bisection is not interested in")
applied to the revision being tested.</simpara>
<simpara>To cope with such a situation, after the inner <emphasis>git bisect</emphasis> finds the
next revision to test, with the "run" script, you can apply that tweak
before compiling, run the real test, and after the test decides if the
revision (possibly with the needed tweaks) passed the test, rewind the
tree to the pristine state.  Finally the "run" script can exit with
the status of the real test to let the "git bisect run" command loop to
determine the outcome.</simpara>
</simplesect>
</simplesect>
<simplesect id="_examples">
<title>EXAMPLES</title>
<itemizedlist>
<listitem>
<simpara>
Automatically bisect a broken build between v1.2 and HEAD:
</simpara>
<literallayout>$ git bisect start HEAD v1.2 --      # HEAD is bad, v1.2 is good
$ git bisect run make                # "make" builds the app</literallayout>
</listitem>
<listitem>
<simpara>
Automatically bisect a broken test suite:
</simpara>
<literallayout>$ cat ~/test.sh
#!/bin/sh
make || exit 125                   # this "skip"s broken builds
make test                          # "make test" runs the test suite
$ git bisect start v1.3 v1.1 --    # v1.3 is bad, v1.1 is good
$ git bisect run ~/test.sh</literallayout>
<simpara>Here we use a "test.sh" custom script. In this script, if "make"
fails, we "skip" the current commit.</simpara>
<simpara>It&#8217;s safer to use a custom script outside the repo to prevent
interactions between the bisect, make and test processes and the
script.</simpara>
<simpara>And "make test" should "exit 0", if the test suite passes, and
"exit 1" (for example) otherwise.</simpara>
</listitem>
<listitem>
<simpara>
Automatically bisect a broken test case:
</simpara>
<literallayout>$ cat ~/test.sh
#!/bin/sh
make || exit 125                     # this "skip"s broken builds
~/check_test_case.sh                 # does the test case passes ?
$ git bisect start HEAD HEAD~10 --   # culprit is among the last 10
$ git bisect run ~/test.sh</literallayout>
<simpara>Here "check_test_case.sh" should "exit 0", if the test case passes,
and "exit 1" (for example) otherwise.</simpara>
<simpara>It&#8217;s safer if both "test.sh" and "check_test_case.sh" scripts are
outside the repo to prevent interactions between the bisect, make and
test processes and the scripts.</simpara>
</listitem>
</itemizedlist>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
