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