updated git doc
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / git-bisect.xml
bloba423fa7c91852bced6bbefb2e7c758076e2d3720
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-bisect(1)">\r
5 <articleinfo>\r
6     <title>git-bisect(1)</title>\r
7 <indexterm>\r
8 <primary>git-bisect(1)</primary>\r
9 </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-bisect - Find by binary search the change that introduced a bug</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <blockquote>\r
18 <literallayout><emphasis>git bisect</emphasis> &lt;subcommand&gt; &lt;options&gt;</literallayout>\r
19 </blockquote>\r
20 </simplesect>\r
21 <simplesect id="_description">\r
22 <title>DESCRIPTION</title>\r
23 <simpara>The command takes various subcommands, and different options depending\r
24 on the subcommand:</simpara>\r
25 <literallayout class="monospaced">git bisect help\r
26 git bisect start [--no-checkout] [&lt;bad&gt; [&lt;good&gt;...]] [--] [&lt;paths&gt;...]\r
27 git bisect bad [&lt;rev&gt;]\r
28 git bisect good [&lt;rev&gt;...]\r
29 git bisect skip [(&lt;rev&gt;|&lt;range&gt;)...]\r
30 git bisect reset [&lt;commit&gt;]\r
31 git bisect visualize\r
32 git bisect replay &lt;logfile&gt;\r
33 git bisect log\r
34 git bisect run &lt;cmd&gt;...</literallayout>\r
35 <simpara>This command uses <emphasis>git rev-list --bisect</emphasis> to help drive the\r
36 binary search process to find which change introduced a bug, given an\r
37 old "good" commit object name and a later "bad" commit object name.</simpara>\r
38 <section id="_getting_help">\r
39 <title>Getting help</title>\r
40 <simpara>Use "git bisect" to get a short usage description, and "git bisect\r
41 help" or "git bisect -h" to get a long usage description.</simpara>\r
42 </section>\r
43 <section id="_basic_bisect_commands_start_bad_good">\r
44 <title>Basic bisect commands: start, bad, good</title>\r
45 <simpara>Using the Linux kernel tree as an example, basic use of the bisect\r
46 command is as follows:</simpara>\r
47 <screen>$ git bisect start\r
48 $ git bisect bad                 # Current version is bad\r
49 $ git bisect good v2.6.13-rc2    # v2.6.13-rc2 was the last version\r
50                                  # tested that was good</screen>\r
51 <simpara>When you have specified at least one bad and one good version, the\r
52 command bisects the revision tree and outputs something similar to\r
53 the following:</simpara>\r
54 <screen>Bisecting: 675 revisions left to test after this</screen>\r
55 <simpara>The state in the middle of the set of revisions is then checked out.\r
56 You would now compile that kernel and boot it. If the booted kernel\r
57 works correctly, you would then issue the following command:</simpara>\r
58 <screen>$ git bisect good                       # this one is good</screen>\r
59 <simpara>The output of this command would be something similar to the following:</simpara>\r
60 <screen>Bisecting: 337 revisions left to test after this</screen>\r
61 <simpara>You keep repeating this process, compiling the tree, testing it, and\r
62 depending on whether it is good or bad issuing the command "git bisect good"\r
63 or "git bisect bad" to ask for the next bisection.</simpara>\r
64 <simpara>Eventually there will be no more revisions left to bisect, and you\r
65 will have been left with the first bad kernel revision in "refs/bisect/bad".</simpara>\r
66 </section>\r
67 <section id="_bisect_reset">\r
68 <title>Bisect reset</title>\r
69 <simpara>After a bisect session, to clean up the bisection state and return to\r
70 the original HEAD, issue the following command:</simpara>\r
71 <screen>$ git bisect reset</screen>\r
72 <simpara>By default, this will return your tree to the commit that was checked\r
73 out before <emphasis>git bisect start</emphasis>.  (A new <emphasis>git bisect start</emphasis> will also do\r
74 that, as it cleans up the old bisection state.)</simpara>\r
75 <simpara>With an optional argument, you can return to a different commit\r
76 instead:</simpara>\r
77 <screen>$ git bisect reset &lt;commit&gt;</screen>\r
78 <simpara>For example, <emphasis>git bisect reset HEAD</emphasis> will leave you on the current\r
79 bisection commit and avoid switching commits at all, while <emphasis>git bisect\r
80 reset bisect/bad</emphasis> will check out the first bad revision.</simpara>\r
81 </section>\r
82 <section id="_bisect_visualize">\r
83 <title>Bisect visualize</title>\r
84 <simpara>To see the currently remaining suspects in <emphasis>gitk</emphasis>, issue the following\r
85 command during the bisection process:</simpara>\r
86 <screen>$ git bisect visualize</screen>\r
87 <simpara><emphasis>view</emphasis> may also be used as a synonym for <emphasis>visualize</emphasis>.</simpara>\r
88 <simpara>If the <emphasis>DISPLAY</emphasis> environment variable is not set, <emphasis>git log</emphasis> is used\r
89 instead.  You can also give command line options such as <emphasis>-p</emphasis> and\r
90 <emphasis>--stat</emphasis>.</simpara>\r
91 <screen>$ git bisect view --stat</screen>\r
92 </section>\r
93 <section id="_bisect_log_and_bisect_replay">\r
94 <title>Bisect log and bisect replay</title>\r
95 <simpara>After having marked revisions as good or bad, issue the following\r
96 command to show what has been done so far:</simpara>\r
97 <screen>$ git bisect log</screen>\r
98 <simpara>If you discover that you made a mistake in specifying the status of a\r
99 revision, you can save the output of this command to a file, edit it to\r
100 remove the incorrect entries, and then issue the following commands to\r
101 return to a corrected state:</simpara>\r
102 <screen>$ git bisect reset\r
103 $ git bisect replay that-file</screen>\r
104 </section>\r
105 <section id="_avoiding_testing_a_commit">\r
106 <title>Avoiding testing a commit</title>\r
107 <simpara>If, in the middle of a bisect session, you know that the next suggested\r
108 revision is not a good one to test (e.g. the change the commit\r
109 introduces is known not to work in your environment and you know it\r
110 does not have anything to do with the bug you are chasing), you may\r
111 want to find a nearby commit and try that instead.</simpara>\r
112 <simpara>For example:</simpara>\r
113 <screen>$ git bisect good/bad                   # previous round was good or bad.\r
114 Bisecting: 337 revisions left to test after this\r
115 $ git bisect visualize                  # oops, that is uninteresting.\r
116 $ git reset --hard HEAD~3               # try 3 revisions before what\r
117                                         # was suggested</screen>\r
118 <simpara>Then compile and test the chosen revision, and afterwards mark\r
119 the revision as good or bad in the usual manner.</simpara>\r
120 </section>\r
121 <section id="_bisect_skip">\r
122 <title>Bisect skip</title>\r
123 <simpara>Instead of choosing by yourself a nearby commit, you can ask git\r
124 to do it for you by issuing the command:</simpara>\r
125 <screen>$ git bisect skip                 # Current version cannot be tested</screen>\r
126 <simpara>But git may eventually be unable to tell the first bad commit among\r
127 a bad commit and one or more skipped commits.</simpara>\r
128 <simpara>You can even skip a range of commits, instead of just one commit,\r
129 using the "<emphasis>&lt;commit1&gt;</emphasis>..<emphasis>&lt;commit2&gt;</emphasis>" notation. For example:</simpara>\r
130 <screen>$ git bisect skip v2.5..v2.6</screen>\r
131 <simpara>This tells the bisect process that no commit after <emphasis>v2.5</emphasis>, up to and\r
132 including <emphasis>v2.6</emphasis>, should be tested.</simpara>\r
133 <simpara>Note that if you also want to skip the first commit of the range you\r
134 would issue the command:</simpara>\r
135 <screen>$ git bisect skip v2.5 v2.5..v2.6</screen>\r
136 <simpara>This tells the bisect process that the commits between <emphasis>v2.5</emphasis> included\r
137 and <emphasis>v2.6</emphasis> included should be skipped.</simpara>\r
138 </section>\r
139 <section id="_cutting_down_bisection_by_giving_more_parameters_to_bisect_start">\r
140 <title>Cutting down bisection by giving more parameters to bisect start</title>\r
141 <simpara>You can further cut down the number of trials, if you know what part of\r
142 the tree is involved in the problem you are tracking down, by specifying\r
143 path parameters when issuing the <emphasis>bisect start</emphasis> command:</simpara>\r
144 <screen>$ git bisect start -- arch/i386 include/asm-i386</screen>\r
145 <simpara>If you know beforehand more than one good commit, you can narrow the\r
146 bisect space down by specifying all of the good commits immediately after\r
147 the bad commit when issuing the <emphasis>bisect start</emphasis> command:</simpara>\r
148 <screen>$ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 --\r
149                    # v2.6.20-rc6 is bad\r
150                    # v2.6.20-rc4 and v2.6.20-rc1 are good</screen>\r
151 </section>\r
152 <section id="_bisect_run">\r
153 <title>Bisect run</title>\r
154 <simpara>If you have a script that can tell if the current source code is good\r
155 or bad, you can bisect by issuing the command:</simpara>\r
156 <screen>$ git bisect run my_script arguments</screen>\r
157 <simpara>Note that the script (<emphasis>my_script</emphasis> in the above example) should\r
158 exit with code 0 if the current source code is good, and exit with a\r
159 code between 1 and 127 (inclusive), except 125, if the current\r
160 source code is bad.</simpara>\r
161 <simpara>Any other exit code will abort the bisect process. It should be noted\r
162 that a program that terminates via "exit(-1)" leaves $? = 255, (see the\r
163 exit(3) manual page), as the value is chopped with "&amp; 0377".</simpara>\r
164 <simpara>The special exit code 125 should be used when the current source code\r
165 cannot be tested. If the script exits with this code, the current\r
166 revision will be skipped (see <emphasis>git bisect skip</emphasis> above). 125 was chosen\r
167 as the highest sensible value to use for this purpose, because 126 and 127\r
168 are used by POSIX shells to signal specific error status (127 is for\r
169 command not found, 126 is for command found but not executable---these\r
170 details do not matter, as they are normal errors in the script, as far as\r
171 "bisect run" is concerned).</simpara>\r
172 <simpara>You may often find that during a bisect session you want to have\r
173 temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a\r
174 header file, or "revision that does not have this commit needs this\r
175 patch applied to work around another problem this bisection is not\r
176 interested in") applied to the revision being tested.</simpara>\r
177 <simpara>To cope with such a situation, after the inner <emphasis>git bisect</emphasis> finds the\r
178 next revision to test, the script can apply the patch\r
179 before compiling, run the real test, and afterwards decide if the\r
180 revision (possibly with the needed patch) passed the test and then\r
181 rewind the tree to the pristine state.  Finally the script should exit\r
182 with the status of the real test to let the "git bisect run" command loop\r
183 determine the eventual outcome of the bisect session.</simpara>\r
184 </section>\r
185 </simplesect>\r
186 <simplesect id="_options">\r
187 <title>OPTIONS</title>\r
188 <variablelist>\r
189 <varlistentry>\r
190 <term>\r
191 --no-checkout\r
192 </term>\r
193 <listitem>\r
194 <simpara>Do not checkout the new working tree at each iteration of the bisection\r
195 process. Instead just update a special reference named <emphasis>BISECT_HEAD</emphasis> to make\r
196 it point to the commit that should be tested.</simpara>\r
197 <simpara>This option may be useful when the test you would perform in each step\r
198 does not require a checked out tree.</simpara>\r
199 <simpara>If the repository is bare, <emphasis>--no-checkout</emphasis> is assumed.</simpara>\r
200 </listitem>\r
201 </varlistentry>\r
202 </variablelist>\r
203 </simplesect>\r
204 <simplesect id="_examples">\r
205 <title>EXAMPLES</title>\r
206 <itemizedlist>\r
207 <listitem>\r
208 <simpara>\r
209 Automatically bisect a broken build between v1.2 and HEAD:\r
210 </simpara>\r
211 <screen>$ git bisect start HEAD v1.2 --      # HEAD is bad, v1.2 is good\r
212 $ git bisect run make                # "make" builds the app</screen>\r
213 </listitem>\r
214 <listitem>\r
215 <simpara>\r
216 Automatically bisect a test failure between origin and HEAD:\r
217 </simpara>\r
218 <screen>$ git bisect start HEAD origin --    # HEAD is bad, origin is good\r
219 $ git bisect run make test           # "make test" builds and tests</screen>\r
220 </listitem>\r
221 <listitem>\r
222 <simpara>\r
223 Automatically bisect a broken test case:\r
224 </simpara>\r
225 <screen>$ cat ~/test.sh\r
226 #!/bin/sh\r
227 make || exit 125                     # this skips broken builds\r
228 ~/check_test_case.sh                 # does the test case pass?\r
229 $ git bisect start HEAD HEAD~10 --   # culprit is among the last 10\r
230 $ git bisect run ~/test.sh</screen>\r
231 <simpara>Here we use a "test.sh" custom script. In this script, if "make"\r
232 fails, we skip the current commit.\r
233 "check_test_case.sh" should "exit 0" if the test case passes,\r
234 and "exit 1" otherwise.</simpara>\r
235 <simpara>It is safer if both "test.sh" and "check_test_case.sh" are\r
236 outside the repository to prevent interactions between the bisect,\r
237 make and test processes and the scripts.</simpara>\r
238 </listitem>\r
239 <listitem>\r
240 <simpara>\r
241 Automatically bisect with temporary modifications (hot-fix):\r
242 </simpara>\r
243 <screen>$ cat ~/test.sh\r
244 #!/bin/sh\r
246 # tweak the working tree by merging the hot-fix branch\r
247 # and then attempt a build\r
248 if      git merge --no-commit hot-fix &amp;&amp;\r
249         make\r
250 then\r
251         # run project specific test and report its status\r
252         ~/check_test_case.sh\r
253         status=$?\r
254 else\r
255         # tell the caller this is untestable\r
256         status=125\r
257 fi\r
259 # undo the tweak to allow clean flipping to the next commit\r
260 git reset --hard\r
262 # return control\r
263 exit $status</screen>\r
264 <simpara>This applies modifications from a hot-fix branch before each test run,\r
265 e.g. in case your build or test environment changed so that older\r
266 revisions may need a fix which newer ones have already. (Make sure the\r
267 hot-fix branch is based off a commit which is contained in all revisions\r
268 which you are bisecting, so that the merge does not pull in too much, or\r
269 use <emphasis>git cherry-pick</emphasis> instead of <emphasis>git merge</emphasis>.)</simpara>\r
270 </listitem>\r
271 <listitem>\r
272 <simpara>\r
273 Automatically bisect a broken test case:\r
274 </simpara>\r
275 <screen>$ git bisect start HEAD HEAD~10 --   # culprit is among the last 10\r
276 $ git bisect run sh -c "make || exit 125; ~/check_test_case.sh"</screen>\r
277 <simpara>This shows that you can do without a run script if you write the test\r
278 on a single line.</simpara>\r
279 </listitem>\r
280 <listitem>\r
281 <simpara>\r
282 Locate a good region of the object graph in a damaged repository\r
283 </simpara>\r
284 <screen>$ git bisect start HEAD &lt;known-good-commit&gt; [ &lt;boundary-commit&gt; ... ] --no-checkout\r
285 $ git bisect run sh -c '\r
286         GOOD=$(git for-each-ref "--format=%(objectname)" refs/bisect/good-*) &amp;&amp;\r
287         git rev-list --objects BISECT_HEAD --not $GOOD &gt;tmp.$$ &amp;&amp;\r
288         git pack-objects --stdout &gt;/dev/null &lt;tmp.$$\r
289         rc=$?\r
290         rm -f tmp.$$\r
291         test $rc = 0'</screen>\r
292 <simpara>In this case, when <emphasis>git bisect run</emphasis> finishes, bisect/bad will refer to a commit that\r
293 has at least one parent whose reachable graph is fully traversable in the sense\r
294 required by <emphasis>git pack objects</emphasis>.</simpara>\r
295 </listitem>\r
296 </itemizedlist>\r
297 </simplesect>\r
298 <simplesect id="_see_also">\r
299 <title>SEE ALSO</title>\r
300 <simpara>link:git-bisect-lk2009.html[Fighting regressions with git bisect],\r
301 <xref linkend="git-blame(1)" />.</simpara>\r
302 </simplesect>\r
303 <simplesect id="_git">\r
304 <title>GIT</title>\r
305 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>\r
306 </simplesect>\r
307 </article>\r