1git-bisect(1) 2============= 3 4NAME 5---- 6git-bisect - Find by binary search the change that introduced a bug 7 8 9SYNOPSIS 10-------- 11'git bisect' <subcommand> <options> 12 13DESCRIPTION 14----------- 15The command takes various subcommands, and different options depending 16on the subcommand: 17 18 git bisect help 19 git bisect start [<bad> [<good>...]] [--] [<paths>...] 20 git bisect bad [<rev>] 21 git bisect good [<rev>...] 22 git bisect skip [(<rev>|<range>)...] 23 git bisect reset [<branch>] 24 git bisect visualize 25 git bisect replay <logfile> 26 git bisect log 27 git bisect run <cmd>... 28 29This command uses 'git rev-list --bisect' to help drive the 30binary search process to find which change introduced a bug, given an 31old "good" commit object name and a later "bad" commit object name. 32 33Getting help 34~~~~~~~~~~~~ 35 36Use "git bisect" to get a short usage description, and "git bisect 37help" or "git bisect -h" to get a long usage description. 38 39Basic bisect commands: start, bad, good 40~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 42Using the Linux kernel tree as an example, basic use of the bisect 43command is as follows: 44 45------------------------------------------------ 46$ git bisect start 47$ git bisect bad # Current version is bad 48$ git bisect good v2.6.13-rc2 # v2.6.13-rc2 was the last version 49 # tested that was good 50------------------------------------------------ 51 52When you have specified at least one bad and one good version, the 53command bisects the revision tree and outputs something similar to: 54 55------------------------------------------------ 56Bisecting: 675 revisions left to test after this 57------------------------------------------------ 58 59and then checks out the state in the middle. You would now compile 60that kernel and boot it. If the booted kernel works correctly, you 61would then issue the following command: 62 63------------------------------------------------ 64$ git bisect good # this one is good 65------------------------------------------------ 66 67which would then output something similar to: 68 69------------------------------------------------ 70Bisecting: 337 revisions left to test after this 71------------------------------------------------ 72 73and you continue along, compiling that one, testing it, and depending 74on whether it is good or bad issuing the command "git bisect good" 75or "git bisect bad" to ask for the next bisection. 76 77Eventually there will be no more revisions left to bisect, and you 78will have been left with the first bad kernel revision in "refs/bisect/bad". 79 80Bisect reset 81~~~~~~~~~~~~ 82 83To return to the original head after a bisect session, you issue the 84command: 85 86------------------------------------------------ 87$ git bisect reset 88------------------------------------------------ 89 90This resets the tree to the original branch instead of being on the 91bisection commit ("git bisect start" will also do that, as it resets 92the bisection state). 93 94Bisect visualize 95~~~~~~~~~~~~~~~~ 96 97During the bisection process, you issue the command: 98 99------------ 100$ git bisect visualize 101------------ 102 103to see the currently remaining suspects in 'gitk'. `view` may also 104be used as a synonym for `visualize`. 105 106If the 'DISPLAY' environment variable is not set, 'git log' is used 107instead. You can also give command line options such as `-p` and 108`--stat`. 109 110------------ 111$ git bisect view --stat 112------------ 113 114Bisect log and bisect replay 115~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 116 117The good/bad input is logged, and: 118 119------------ 120$ git bisect log 121------------ 122 123shows what you have done so far. You can truncate its output somewhere 124and save it in a file, and run: 125 126------------ 127$ git bisect replay that-file 128------------ 129 130if you find later that you made a mistake specifying revisions as good/bad. 131 132Avoiding testing a commit 133~~~~~~~~~~~~~~~~~~~~~~~~~ 134 135If in the middle of a bisect session, you know that the next suggested 136revision is not a good one to test (e.g. the change the commit 137introduces is known not to work in your environment and you know it 138does not have anything to do with the bug you are chasing), you may 139want to find a nearby commit and try that instead. 140 141For example: 142 143------------ 144$ git bisect good/bad # previous round was good/bad. 145Bisecting: 337 revisions left to test after this 146$ git bisect visualize # oops, that is uninteresting. 147$ git reset --hard HEAD~3 # try 3 revisions before what 148 # was suggested 149------------ 150 151Then compile and test the chosen revision. Afterwards the revision 152is marked as good/bad in the usual manner. 153 154Bisect skip 155~~~~~~~~~~~~ 156 157Instead of choosing by yourself a nearby commit, you can ask git 158to do it for you by issuing the command: 159 160------------ 161$ git bisect skip # Current version cannot be tested 162------------ 163 164But computing the commit to test may be slower afterwards and git may 165eventually not be able to tell the first bad commit among a bad commit 166and one or more skipped commits. 167 168You can even skip a range of commits, instead of just one commit, 169using the "'<commit1>'..'<commit2>'" notation. For example: 170 171------------ 172$ git bisect skip v2.5..v2.6 173------------ 174 175would mean that no commit between `v2.5` excluded and `v2.6` included 176can be tested. 177 178Note that if you also want to skip the first commit of the range you 179would issue the command: 180 181------------ 182$ git bisect skip v2.5 v2.5..v2.6 183------------ 184 185and the commit pointed to by `v2.5` would also be skipped. 186 187Cutting down bisection by giving more parameters to bisect start 188~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 189 190You can further cut down the number of trials, if you know what part of 191the tree is involved in the problem you are tracking down, by specifying 192path parameters when issuing the `bisect start` command, like this: 193 194------------ 195$ git bisect start -- arch/i386 include/asm-i386 196------------ 197 198If you know beforehand more than one good commit, you can narrow the 199bisect space down by specifying all of the good commits immediately after 200the bad commit when issuing the `bisect start` command: 201 202------------ 203$ git bisect start v2.6.20-rc6 v2.6.20-rc4 v2.6.20-rc1 -- 204 # v2.6.20-rc6 is bad 205 # v2.6.20-rc4 and v2.6.20-rc1 are good 206------------ 207 208Bisect run 209~~~~~~~~~~ 210 211If you have a script that can tell if the current source code is good 212or bad, you can bisect by issuing the command: 213 214------------ 215$ git bisect run my_script 216------------ 217 218Note that the script (`my_script` in the above example) should 219exit with code 0 if the current source code is good, and exit with a 220code between 1 and 127 (inclusive), except 125, if the current 221source code is bad. 222 223Any other exit code will abort the bisect process. It should be noted 224that a program that terminates via "exit(-1)" leaves $? = 255, (see the 225exit(3) manual page), as the value is chopped with "& 0377". 226 227The special exit code 125 should be used when the current source code 228cannot be tested. If the script exits with this code, the current 229revision will be skipped (see `git bisect skip` above). 230 231You may often find that during a bisect session you want to have 232temporary modifications (e.g. s/#define DEBUG 0/#define DEBUG 1/ in a 233header file, or "revision that does not have this commit needs this 234patch applied to work around another problem this bisection is not 235interested in") applied to the revision being tested. 236 237To cope with such a situation, after the inner 'git bisect' finds the 238next revision to test, the script can apply the patch 239before compiling, run the real test, and afterwards decide if the 240revision (possibly with the needed patch) passed the test and then 241rewind the tree to the pristine state. Finally the script should exit 242with the status of the real test to let the "git bisect run" command loop 243to determine the eventual outcome of the bisect session. 244 245EXAMPLES 246-------- 247 248* Automatically bisect a broken build between v1.2 and HEAD: 249+ 250------------ 251$ git bisect start HEAD v1.2 -- # HEAD is bad, v1.2 is good 252$ git bisect run make # "make" builds the app 253------------ 254 255* Automatically bisect a broken test suite: 256+ 257------------ 258$ cat ~/test.sh 259#!/bin/sh 260make || exit 125 # this skips broken builds 261make test # "make test" runs the test suite 262$ git bisect start v1.3 v1.1 -- # v1.3 is bad, v1.1 is good 263$ git bisect run ~/test.sh 264------------ 265+ 266Here we use a "test.sh" custom script. In this script, if "make" 267fails, we skip the current commit. 268+ 269It is safer to use a custom script outside the repository to prevent 270interactions between the bisect, make and test processes and the 271script. 272+ 273"make test" should "exit 0", if the test suite passes, and 274"exit 1" otherwise. 275 276* Automatically bisect a broken test case: 277+ 278------------ 279$ cat ~/test.sh 280#!/bin/sh 281make || exit 125 # this skips broken builds 282~/check_test_case.sh # does the test case passes ? 283$ git bisect start HEAD HEAD~10 -- # culprit is among the last 10 284$ git bisect run ~/test.sh 285------------ 286+ 287Here "check_test_case.sh" should "exit 0" if the test case passes, 288and "exit 1" otherwise. 289+ 290It is safer if both "test.sh" and "check_test_case.sh" scripts are 291outside the repository to prevent interactions between the bisect, 292make and test processes and the scripts. 293 294Author 295------ 296Written by Linus Torvalds <torvalds@osdl.org> 297 298Documentation 299------------- 300Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. 301 302GIT 303--- 304Part of the linkgit:git[1] suite