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