1GIT-BISECT(1) Git Manual GIT-BISECT(1)
2
3
4
6 git-bisect - Use binary search to find the commit that introduced a bug
7
9 git bisect <subcommand> <options>
10
11
13 The command takes various subcommands, and different options depending
14 on the subcommand:
15
16 git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
17 [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]
18 git bisect (bad|new|<term-new>) [<rev>]
19 git bisect (good|old|<term-old>) [<rev>...]
20 git bisect terms [--term-good | --term-bad]
21 git bisect skip [(<rev>|<range>)...]
22 git bisect reset [<commit>]
23 git bisect (visualize|view)
24 git bisect replay <logfile>
25 git bisect log
26 git bisect run <cmd>...
27 git bisect help
28
29 This command uses a binary search algorithm to find which commit in
30 your project’s history introduced a bug. You use it by first telling it
31 a "bad" commit that is known to contain the bug, and a "good" commit
32 that is known to be before the bug was introduced. Then git bisect
33 picks a commit between those two endpoints and asks you whether the
34 selected commit is "good" or "bad". It continues narrowing down the
35 range until it finds the exact commit that introduced the change.
36
37 In fact, git bisect can be used to find the commit that changed any
38 property of your project; e.g., the commit that fixed a bug, or the
39 commit that caused a benchmark’s performance to improve. To support
40 this more general usage, the terms "old" and "new" can be used in place
41 of "good" and "bad", or you can choose your own terms. See section
42 "Alternate terms" below for more information.
43
44 Basic bisect commands: start, bad, good
45 As an example, suppose you are trying to find the commit that broke a
46 feature that was known to work in version v2.6.13-rc2 of your project.
47 You start a bisect session as follows:
48
49 $ git bisect start
50 $ git bisect bad # Current version is bad
51 $ git bisect good v2.6.13-rc2 # v2.6.13-rc2 is known to be good
52
53
54 Once you have specified at least one bad and one good commit, git
55 bisect selects a commit in the middle of that range of history, checks
56 it out, and outputs something similar to the following:
57
58 Bisecting: 675 revisions left to test after this (roughly 10 steps)
59
60
61 You should now compile the checked-out version and test it. If that
62 version works correctly, type
63
64 $ git bisect good
65
66
67 If that version is broken, type
68
69 $ git bisect bad
70
71
72 Then git bisect will respond with something like
73
74 Bisecting: 337 revisions left to test after this (roughly 9 steps)
75
76
77 Keep repeating the process: compile the tree, test it, and depending on
78 whether it is good or bad run git bisect good or git bisect bad to ask
79 for the next commit that needs testing.
80
81 Eventually there will be no more revisions left to inspect, and the
82 command will print out a description of the first bad commit. The
83 reference refs/bisect/bad will be left pointing at that commit.
84
85 Bisect reset
86 After a bisect session, to clean up the bisection state and return to
87 the original HEAD, issue the following command:
88
89 $ git bisect reset
90
91
92 By default, this will return your tree to the commit that was checked
93 out before git bisect start. (A new git bisect start will also do that,
94 as it cleans up the old bisection state.)
95
96 With an optional argument, you can return to a different commit
97 instead:
98
99 $ git bisect reset <commit>
100
101
102 For example, git bisect reset bisect/bad will check out the first bad
103 revision, while git bisect reset HEAD will leave you on the current
104 bisection commit and avoid switching commits at all.
105
106 Alternate terms
107 Sometimes you are not looking for the commit that introduced a
108 breakage, but rather for a commit that caused a change between some
109 other "old" state and "new" state. For example, you might be looking
110 for the commit that introduced a particular fix. Or you might be
111 looking for the first commit in which the source-code filenames were
112 finally all converted to your company’s naming standard. Or whatever.
113
114 In such cases it can be very confusing to use the terms "good" and
115 "bad" to refer to "the state before the change" and "the state after
116 the change". So instead, you can use the terms "old" and "new",
117 respectively, in place of "good" and "bad". (But note that you cannot
118 mix "good" and "bad" with "old" and "new" in a single session.)
119
120 In this more general usage, you provide git bisect with a "new" commit
121 that has some property and an "old" commit that doesn’t have that
122 property. Each time git bisect checks out a commit, you test if that
123 commit has the property. If it does, mark the commit as "new";
124 otherwise, mark it as "old". When the bisection is done, git bisect
125 will report which commit introduced the property.
126
127 To use "old" and "new" instead of "good" and bad, you must run git
128 bisect start without commits as argument and then run the following
129 commands to add the commits:
130
131 git bisect old [<rev>]
132
133
134 to indicate that a commit was before the sought change, or
135
136 git bisect new [<rev>...]
137
138
139 to indicate that it was after.
140
141 To get a reminder of the currently used terms, use
142
143 git bisect terms
144
145
146 You can get just the old (respectively new) term with git bisect terms
147 --term-old or git bisect terms --term-good.
148
149 If you would like to use your own terms instead of "bad"/"good" or
150 "new"/"old", you can choose any names you like (except existing bisect
151 subcommands like reset, start, ...) by starting the bisection using
152
153 git bisect start --term-old <term-old> --term-new <term-new>
154
155
156 For example, if you are looking for a commit that introduced a
157 performance regression, you might use
158
159 git bisect start --term-old fast --term-new slow
160
161
162 Or if you are looking for the commit that fixed a bug, you might use
163
164 git bisect start --term-new fixed --term-old broken
165
166
167 Then, use git bisect <term-old> and git bisect <term-new> instead of
168 git bisect good and git bisect bad to mark commits.
169
170 Bisect visualize/view
171 To see the currently remaining suspects in gitk, issue the following
172 command during the bisection process (the subcommand view can be used
173 as an alternative to visualize):
174
175 $ git bisect visualize
176
177
178 If the DISPLAY environment variable is not set, git log is used
179 instead. You can also give command-line options such as -p and --stat.
180
181 $ git bisect visualize --stat
182
183
184 Bisect log and bisect replay
185 After having marked revisions as good or bad, issue the following
186 command to show what has been done so far:
187
188 $ git bisect log
189
190
191 If you discover that you made a mistake in specifying the status of a
192 revision, you can save the output of this command to a file, edit it to
193 remove the incorrect entries, and then issue the following commands to
194 return to a corrected state:
195
196 $ git bisect reset
197 $ git bisect replay that-file
198
199
200 Avoiding testing a commit
201 If, in the middle of a bisect session, you know that the suggested
202 revision is not a good one to test (e.g. it fails to build and you know
203 that the failure does not have anything to do with the bug you are
204 chasing), you can manually select a nearby commit and test that one
205 instead.
206
207 For example:
208
209 $ git bisect good/bad # previous round was good or bad.
210 Bisecting: 337 revisions left to test after this (roughly 9 steps)
211 $ git bisect visualize # oops, that is uninteresting.
212 $ git reset --hard HEAD~3 # try 3 revisions before what
213 # was suggested
214
215
216 Then compile and test the chosen revision, and afterwards mark the
217 revision as good or bad in the usual manner.
218
219 Bisect skip
220 Instead of choosing a nearby commit by yourself, you can ask Git to do
221 it for you by issuing the command:
222
223 $ git bisect skip # Current version cannot be tested
224
225
226 However, if you skip a commit adjacent to the one you are looking for,
227 Git will be unable to tell exactly which of those commits was the first
228 bad one.
229
230 You can also skip a range of commits, instead of just one commit, using
231 range notation. For example:
232
233 $ git bisect skip v2.5..v2.6
234
235
236 This tells the bisect process that no commit after v2.5, up to and
237 including v2.6, should be tested.
238
239 Note that if you also want to skip the first commit of the range you
240 would issue the command:
241
242 $ git bisect skip v2.5 v2.5..v2.6
243
244
245 This tells the bisect process that the commits between v2.5 and v2.6
246 (inclusive) should be skipped.
247
248 Cutting down bisection by giving more parameters to bisect start
249 You can further cut down the number of trials, if you know what part of
250 the tree is involved in the problem you are tracking down, by
251 specifying path parameters when issuing the bisect start command:
252
253 $ git bisect start -- arch/i386 include/asm-i386
254
255
256 If you know beforehand more than one good commit, you can narrow the
257 bisect space down by specifying all of the good commits immediately
258 after the bad commit when issuing the bisect start command:
259
260 $ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --
261 # v2.6.20-rc6 is bad
262 # v2.6.20-rc4 and v2.6.20-rc1 are good
263
264
265 Bisect run
266 If you have a script that can tell if the current source code is good
267 or bad, you can bisect by issuing the command:
268
269 $ git bisect run my_script arguments
270
271
272 Note that the script (my_script in the above example) should exit with
273 code 0 if the current source code is good/old, and exit with a code
274 between 1 and 127 (inclusive), except 125, if the current source code
275 is bad/new.
276
277 Any other exit code will abort the bisect process. It should be noted
278 that a program that terminates via exit(-1) leaves $? = 255, (see the
279 exit(3) manual page), as the value is chopped with & 0377.
280
281 The special exit code 125 should be used when the current source code
282 cannot be tested. If the script exits with this code, the current
283 revision will be skipped (see git bisect skip above). 125 was chosen as
284 the highest sensible value to use for this purpose, because 126 and 127
285 are used by POSIX shells to signal specific error status (127 is for
286 command not found, 126 is for command found but not executable—these
287 details do not matter, as they are normal errors in the script, as far
288 as bisect run is concerned).
289
290 You may often find that during a bisect session you want to have
291 temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a
292 header file, or "revision that does not have this commit needs this
293 patch applied to work around another problem this bisection is not
294 interested in") applied to the revision being tested.
295
296 To cope with such a situation, after the inner git bisect finds the
297 next revision to test, the script can apply the patch before compiling,
298 run the real test, and afterwards decide if the revision (possibly with
299 the needed patch) passed the test and then rewind the tree to the
300 pristine state. Finally the script should exit with the status of the
301 real test to let the git bisect run command loop determine the eventual
302 outcome of the bisect session.
303
305 --no-checkout
306 Do not checkout the new working tree at each iteration of the
307 bisection process. Instead just update a special reference named
308 BISECT_HEAD to make it point to the commit that should be tested.
309
310 This option may be useful when the test you would perform in each
311 step does not require a checked out tree.
312
313 If the repository is bare, --no-checkout is assumed.
314
316 · Automatically bisect a broken build between v1.2 and HEAD:
317
318 $ git bisect start HEAD v1.2 -- # HEAD is bad, v1.2 is good
319 $ git bisect run make # "make" builds the app
320 $ git bisect reset # quit the bisect session
321
322
323 · Automatically bisect a test failure between origin and HEAD:
324
325 $ git bisect start HEAD origin -- # HEAD is bad, origin is good
326 $ git bisect run make test # "make test" builds and tests
327 $ git bisect reset # quit the bisect session
328
329
330 · Automatically bisect a broken test case:
331
332 $ cat ~/test.sh
333 #!/bin/sh
334 make || exit 125 # this skips broken builds
335 ~/check_test_case.sh # does the test case pass?
336 $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
337 $ git bisect run ~/test.sh
338 $ git bisect reset # quit the bisect session
339
340 Here we use a test.sh custom script. In this script, if make fails,
341 we skip the current commit. check_test_case.sh should exit 0 if
342 the test case passes, and exit 1 otherwise.
343
344 It is safer if both test.sh and check_test_case.sh are outside the
345 repository to prevent interactions between the bisect, make and
346 test processes and the scripts.
347
348 · Automatically bisect with temporary modifications (hot-fix):
349
350 $ cat ~/test.sh
351 #!/bin/sh
352
353 # tweak the working tree by merging the hot-fix branch
354 # and then attempt a build
355 if git merge --no-commit hot-fix &&
356 make
357 then
358 # run project specific test and report its status
359 ~/check_test_case.sh
360 status=$?
361 else
362 # tell the caller this is untestable
363 status=125
364 fi
365
366 # undo the tweak to allow clean flipping to the next commit
367 git reset --hard
368
369 # return control
370 exit $status
371
372 This applies modifications from a hot-fix branch before each test
373 run, e.g. in case your build or test environment changed so that
374 older revisions may need a fix which newer ones have already. (Make
375 sure the hot-fix branch is based off a commit which is contained in
376 all revisions which you are bisecting, so that the merge does not
377 pull in too much, or use git cherry-pick instead of git merge.)
378
379 · Automatically bisect a broken test case:
380
381 $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
382 $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
383 $ git bisect reset # quit the bisect session
384
385 This shows that you can do without a run script if you write the
386 test on a single line.
387
388 · Locate a good region of the object graph in a damaged repository
389
390 $ git bisect start HEAD <known-good-commit> [ <boundary-commit> ... ] --no-checkout
391 $ git bisect run sh -c '
392 GOOD=$(git for-each-ref "--format=%(objectname)" refs/bisect/good-*) &&
393 git rev-list --objects BISECT_HEAD --not $GOOD >tmp.$$ &&
394 git pack-objects --stdout >/dev/null <tmp.$$
395 rc=$?
396 rm -f tmp.$$
397 test $rc = 0'
398
399 $ git bisect reset # quit the bisect session
400
401 In this case, when git bisect run finishes, bisect/bad will refer
402 to a commit that has at least one parent whose reachable graph is
403 fully traversable in the sense required by git pack objects.
404
405 · Look for a fix instead of a regression in the code
406
407 $ git bisect start
408 $ git bisect new HEAD # current commit is marked as new
409 $ git bisect old HEAD~10 # the tenth commit from now is marked as old
410
411 or:
412
413 $ git bisect start --term-old broken --term-new fixed
414 $ git bisect fixed
415 $ git bisect broken HEAD~10
416
417
418 Getting help
419 Use git bisect to get a short usage description, and git bisect help or
420 git bisect -h to get a long usage description.
421
423 Fighting regressions with git bisect[1], git-blame(1).
424
426 Part of the git(1) suite
427
429 1. Fighting regressions with git bisect
430 file:///usr/share/doc/git/git-bisect-lk2009.html
431
432
433
434Git 2.20.1 12/15/2018 GIT-BISECT(1)