interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-bisect.adoc

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