6 git-bisect - Find the change that introduced a bug by binary search
11 'git bisect' <subcommand> <options>
15 The command takes various subcommands, and different options depending
19 git bisect start [<bad> [<good>...]] [--] [<paths>...]
20 git bisect bad [<rev>]
21 git bisect good [<rev>...]
22 git bisect skip [<rev>...]
23 git bisect reset [<branch>]
25 git bisect replay <logfile>
27 git bisect run <cmd>...
29 This command uses 'git-rev-list --bisect' option to help drive the
30 binary search process to find which change introduced a bug, given an
31 old "good" commit object name and a later "bad" commit object name.
36 Use "git bisect" to get a short usage description, and "git bisect
37 help" or "git bisect -h" to get a long usage description.
39 Basic bisect commands: start, bad, good
40 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42 The way you use it is:
44 ------------------------------------------------
46 $ git bisect bad # Current version is bad
47 $ git bisect good v2.6.13-rc2 # v2.6.13-rc2 was the last version
48 # tested that was good
49 ------------------------------------------------
51 When you give at least one bad and one good versions, it will bisect
52 the revision tree and say something like:
54 ------------------------------------------------
55 Bisecting: 675 revisions left to test after this
56 ------------------------------------------------
58 and check out the state in the middle. Now, compile that kernel, and
59 boot it. Now, let's say that this booted kernel works fine, then just
62 ------------------------------------------------
63 $ git bisect good # this one is good
64 ------------------------------------------------
68 ------------------------------------------------
69 Bisecting: 337 revisions left to test after this
70 ------------------------------------------------
72 and you continue along, compiling that one, testing it, and depending
73 on whether it is good or bad, you say "git bisect good" or "git bisect
74 bad", and ask for the next bisection.
76 Until you have no more left, and you'll have been left with the first
77 bad kernel rev in "refs/bisect/bad".
82 Oh, and then after you want to reset to the original head, do a
84 ------------------------------------------------
86 ------------------------------------------------
88 to get back to the original branch, instead of being in one of the
89 bisection branches ("git bisect start" will do that for you too,
90 actually: it will reset the bisection state, and before it does that
91 it checks that you're not using some old bisection branch).
96 During the bisection process, you can say
99 $ git bisect visualize
102 to see the currently remaining suspects in `gitk`. `visualize` is a bit
103 too long to type and `view` is provided as a synonym.
105 If `DISPLAY` environment variable is not set, `git log` is used
106 instead. You can even give command line options such as `-p` and
110 $ git bisect view --stat
113 Bisect log and bisect replay
114 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116 The good/bad input is logged, and
122 shows what you have done so far. You can truncate its output somewhere
123 and save it in a file, and run
126 $ git bisect replay that-file
129 if you find later you made a mistake telling good/bad about a
132 Avoiding to test a commit
133 ~~~~~~~~~~~~~~~~~~~~~~~~~
135 If in a middle of bisect session, you know what the bisect suggested
136 to try next is not a good one to test (e.g. the change the commit
137 introduces is known not to work in your environment and you know it
138 does not have anything to do with the bug you are chasing), you may
139 want to find a near-by commit and try that instead.
141 It goes something like this:
144 $ git bisect good/bad # previous round was good/bad.
145 Bisecting: 337 revisions left to test after this
146 $ git bisect visualize # oops, that is uninteresting.
147 $ git reset --hard HEAD~3 # try 3 revs before what
151 Then compile and test the one you chose to try. After that, tell
152 bisect what the result was as usual.
157 Instead of choosing by yourself a nearby commit, you may just want git
158 to do it for you using:
161 $ git bisect skip # Current version cannot be tested
164 But computing the commit to test may be slower afterwards and git may
165 eventually not be able to tell the first bad among a bad and one or
166 more "skip"ped commits.
168 Cutting down bisection by giving more parameters to bisect start
169 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171 You can further cut down the number of trials if you know what part of
172 the tree is involved in the problem you are tracking down, by giving
173 paths parameters when you say `bisect start`, like this:
176 $ git bisect start -- arch/i386 include/asm-i386
179 If you know beforehand more than one good commits, you can narrow the
180 bisect space down without doing the whole tree checkout every time you
181 give good commits. You give the bad revision immediately after `start`
182 and then you give all the good revisions you have:
185 $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
187 # v2.6.20-rc4 and v2.6.20-rc1 are good
193 If you have a script that can tell if the current source code is good
194 or bad, you can automatically bisect using:
197 $ git bisect run my_script
200 Note that the "run" script (`my_script` in the above example) should
201 exit with code 0 in case the current source code is good. Exit with a
202 code between 1 and 127 (inclusive), except 125, if the current
205 Any other exit code will abort the automatic bisect process. (A
206 program that does "exit(-1)" leaves $? = 255, see exit(3) manual page,
207 the value is chopped with "& 0377".)
209 The special exit code 125 should be used when the current source code
210 cannot be tested. If the "run" script exits with this code, the current
211 revision will be skipped, see `git bisect skip` above.
213 You may often find that during bisect you want to have near-constant
214 tweaks (e.g., s/#define DEBUG 0/#define DEBUG 1/ in a header file, or
215 "revision that does not have this commit needs this patch applied to
216 work around other problem this bisection is not interested in")
217 applied to the revision being tested.
219 To cope with such a situation, after the inner git-bisect finds the
220 next revision to test, with the "run" script, you can apply that tweak
221 before compiling, run the real test, and after the test decides if the
222 revision (possibly with the needed tweaks) passed the test, rewind the
223 tree to the pristine state. Finally the "run" script can exit with
224 the status of the real test to let "git bisect run" command loop to
230 * Automatically bisect a broken build between v1.2 and HEAD:
233 $ git bisect start HEAD v1.2 -- # HEAD is bad, v1.2 is good
234 $ git bisect run make # "make" builds the app
237 * Automatically bisect a broken test suite:
242 make || exit 125 # this "skip"s broken builds
243 make test # "make test" runs the test suite
244 $ git bisect start v1.3 v1.1 -- # v1.3 is bad, v1.1 is good
245 $ git bisect run ~/test.sh
248 Here we use a "test.sh" custom script. In this script, if "make"
249 fails, we "skip" the current commit.
251 It's safer to use a custom script outside the repo to prevent
252 interactions between the bisect, make and test processes and the
255 And "make test" should "exit 0", if the test suite passes, and
256 "exit 1" (for example) otherwise.
258 * Automatically bisect a broken test case:
263 make || exit 125 # this "skip"s broken builds
264 ~/check_test_case.sh # does the test case passes ?
265 $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
266 $ git bisect run ~/test.sh
269 Here "check_test_case.sh" should "exit 0", if the test case passes,
270 and "exit 1" (for example) otherwise.
272 It's safer if both "test.sh" and "check_test_case.sh" scripts are
273 outside the repo to prevent interactions between the bisect, make and
274 test processes and the scripts.
278 Written by Linus Torvalds <torvalds@osdl.org>
282 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
286 Part of the linkgit:git[7] suite