488

I have read some articles saying that git bisect is awesome. However, I'm not a native speaker and I can't understand why it's awesome.

Could someone please demonstrate with some code sample:

  1. How to use it?
  2. Is it just like svn blame?
Amit Tripathi
  • 5,967
  • 3
  • 25
  • 50
IAdapter
  • 55,820
  • 69
  • 166
  • 236
  • @01: As the git book says: *perform a brute-force search through the project's history*. – eckes Jan 17 '11 at 12:52
  • 12
    Not so ///brute :-), it uses binary search. – cojocar Jan 17 '11 at 13:30
  • "git blame" is similar to "svn blame". "git bisect" is a completely different thing – William Pursell Jan 17 '11 at 15:09
  • For what it's worth, there's a good [description of bisect in Pro Git](http://progit.org/book/ch6-5.html#binary_search) too. Sylvain's answer is another good shot at it. If after looking at all of that you still don't get it, I'd suggest you ask a more specific question. General questions beget general answers. – Cascabel Jan 17 '11 at 16:27
  • ProGit has better explanation, but i think Sylvain explain it a lot better. From what I understand its not only search. – IAdapter Jan 17 '11 at 18:57
  • 4
    updated book link: http://git-scm.com/book/en/Git-Tools-Debugging-with-Git#Binary-Search – gdelfino Dec 20 '13 at 15:09
  • Super useful demo: http://webchick.net/node/99 – Gabriel Staples Jun 21 '18 at 19:56

6 Answers6

706

The idea behind git bisect is to perform a binary search in the history to find a particular regression. Imagine that you have the following development history:

... --- 0 --- 1 --- 2 --- 3 --- 4* --- 5 --- current

You know that your program is not working properly at the current revision, and that it was working at the revision 0. So the regression was likely introduced in one of the commits 1, 2, 3, 4, 5, current.

You could try to check out each commit, build it, check if the regression is present or not. If there is a large number of commits, this can take a long time. This is a linear search. We can do better by doing a binary search. This is what the git bisect command does. At each step it tries to reduce the number of revisions that are potentially bad by half.

You'll use the command like this:

$ git stash save
$ git bisect start
$ git bisect bad
$ git bisect good 0
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[< ... sha ... >] 3

After this command, git will checkout a commit. In our case, it'll be commit 3. You need to build your program, and check whether or not the regression is present. You'll also need to tell git the status of this revision with either git bisect bad if the regression is present, or git bisect good if it is not.

Let's suppose that the regression was introduced in commit 4. Then the regression is not present in this revision, and we tell it to git.

$ make
$ make test
... ... ...
$ git bisect good
Bisecting: 0 revisions left to test after this (roughly 1 step)
[< ... sha ... >] 5

It will then checkout another commit. Either 4 or 5 (as there are only two commits). Let's suppose it picked 5. After a build we test the program and see that the regression is present. We then tell it to git:

$ make
$ make test
... ... ...
$ git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[< ... sha ... >] 4

We test the last revision, 4. And since it is the one that introduced the regression, we tell it to git:

$ make
$ make test
... ... ...
$ git bisect bad
< ... sha ... > is the first bad commit
< ... commit message ... >

In this simple situation, we only had to test 3 versions (3, 4, 5) instead of 4 (1, 2, 3, 4). This is a small win, but this is because our history is so small. If the search range is of N commits, we should expect to test 1 + log2 N commits with git bisect instead of roughly N / 2 commits with a linear search.

Once you've found the commit that introduced the regression, you can study it to find the issue. Once this is done, you use git bisect reset to put everything back on the original state before using git bisect command.

Sylvain Defresne
  • 38,469
  • 11
  • 70
  • 82
  • 6
    I'm going to contrare here, this is a good explanation of bisect but really doesn't help me use it. Especially since I've managed to find a good commit and I'm on that branch now. From this position this explanation is no help at all. How do I specify the bad branch without checking it out, for example – PandaWood Dec 02 '13 at 01:19
  • 5
    You can use `git bisect bad [...]` to mark specific revisions as bad (or good with `git bisect good [...]`). `rev` can be any revision identifier like a branch name, a tag, a commit hash (or unique prefix of commit hash), ... – Sylvain Defresne Dec 02 '13 at 10:02
  • 46
    ...and once you are done, you type `git bisect reset` to put everything back on the recent commit – peetonn Jan 28 '14 at 14:09
  • 19
    One of the most awesome answers on stack. Very well articulated. I've been doing this process manually for years, just kind of picking an arbitrary halfway point between a good and bad commit, then again between that one and the good/bad one depending on if it was good/bad itself. It has always been a huge pain in the ass and I had never even heard of this git subcommand until today... hahaha – Chev Feb 23 '14 at 02:17
  • How can it be useful for PHP projects? If it can? – Nemoden Jan 20 '15 at 05:17
  • 3
    @Nemoden, yes, it can, basically it can be useful for any type of project. You just need to replace "make test" step with "deploy the web site and reproduce the issue" – alex.b Jan 29 '15 at 21:41
180

git bisect run automatic bisect

If you have an automated ./test script that has exit status 0 iff the test is OK, you can automatically find the bug with bisect run:

git checkout KNOWN_BAD_COMMIT
git bisect start

# Confirm that our test script is correct, and fails on the bad commit.
./test
# Should output != 0.
echo $?
# Tell Git that the current commit is bad.
git bisect bad

# Same for a known good commit in the past.
git checkout KNOWN_GOOD_COMMIT
./test
# Should output 0.
echo $?
# After this, git automatically checks out to the commit
# in the middle of KNOWN_BAD_COMMIT and KNOWN_GOOD_COMMIT.
git bisect good

# Bisect automatically all the way to the first bad or last good rev.
git bisect run ./test

# End the bisect operation and checkout to master again.
git bisect reset

This supposes of course that if the test script ./test is git tracked, that it does not disappear on some earlier commit during bisection.

I have found that very often you can get away by just copying the in-tree script out of tree, and possibly playing with PATH-like variables, and running it from there instead.

Of course, if the test infrastructure on which test depends breaks on older commits, then there is no solution, and you will have to do things manually, deciding how to test commits one by one.

I have found however that using this automation often works, and can be a huge time saver for slower tests lying in your backlog of tasks, where you can just let it run overnight, and possibly have your bug identified by next morning, it is worth the try.

More tips

Stay on the first failing commit after bisect instead of going back to master:

git bisect reset HEAD

start + initial bad and good in one go:

git bisect start KNOWN_BAD_COMMIT KNOWN_GOOD_COMMIT~

is the same as:

git checkout KNOWN_BAD_COMMIT
git bisect start
git bisect bad
git bisect good KNOWN_GOOD_COMMIT

See what has been tested so far (by manual good and bad or run):

git bisect log

Sample output:

git bisect log
git bisect start
# bad: [00b9fcdbe7e7d2579f212b51342f4d605e53253d] 9
git bisect bad 00b9fcdbe7e7d2579f212b51342f4d605e53253d
# good: [db7ec3d602db2d994fe981c0da55b7b85ca62566] 0
git bisect good db7ec3d602db2d994fe981c0da55b7b85ca62566
# good: [2461cd8ce8d3d1367ddb036c8f715c7b896397a5] 4
git bisect good 2461cd8ce8d3d1367ddb036c8f715c7b896397a5
# good: [8fbab5a3b44fd469a2da3830dac5c4c1358a87a0] 6
git bisect good 8fbab5a3b44fd469a2da3830dac5c4c1358a87a0
# bad: [dd2c05e71c246f9bcbd2fbe81deabf826c54be23] 8
git bisect bad dd2c05e71c246f9bcbd2fbe81deabf826c54be23
# bad: [c536b1b7242d5fcf92cd87e9a534bedb1c0c9c05] 7
git bisect bad c536b1b7242d5fcf92cd87e9a534bedb1c0c9c05
# first bad commit: [c536b1b7242d5fcf92cd87e9a534bedb1c0c9c0

Show good and bad refs on git log to get a better notion of time:

git log --decorate --pretty=fuller --simplify-by-decoration master

This only shows commits with a corresponding ref, which reduces the noise grealy, but does include autogenerated refs of type:

refs/bisect/good*
refs/bisect/bad*

which tell us which commits we marked as good or bad.

Consider this test repo if you want to play around with the command.

Failure is fast, success is slow

Sometimes:

  • failure happens fast, e.g. one of the first tests breaks
  • success takes a while, e.g. the broken test passes, and all other tests we don't care about follow

For those cases, e.g. supposing the failure always happens withing 5 seconds, and if we are lazy to make the test more specific as we really should, we can use timeout as in:

#!/usr/bin/env bash
timeout 5 test-command
if [ $? -eq 1 ]; then
  exit 1
fi

This works since timeout exits 124 while the failure of test-command exits 1.

Magic exit statuses

git bisect run is a bit picky about exit statuses:

  • anything above 127 makes the bisection fail with something like:

    git bisect run failed:
    exit code 134 from '../test -aa' is < 0 or >= 128
    

    In particular, a C assert(0) leads to a SIGABRT and exits with status 134, very annoying.

  • 125 is magic and makes the run be skipped with git bisect skip.

    The intention of this is to help skip broken builds due to unrelated reasons.

See man git-bisect for the details.

So you might want to use something like:

#!/usr/bin/env bash
set -eu
./build
status=0
./actual-test-command || status=$?
if [ "$status" -eq 125 ] || [ "$status" -gt 127 ]; then
  status=1
fi
exit "$status"

Tested on git 2.16.1.

  • 7
    How does git know to keep your new test from disappearing when reverting/bisecting back to a previous/bad revision (that didn't have your newly written test)? – thebjorn Apr 21 '14 at 15:51
  • 8
    @thebjorn you have a point: as far as I know, either the test must be on an external executable in PATH, or in an untracked file in the repo. In many cases this is possible: put the test on a separate file, include necessary test boilerplate with a well crafted `test_script` + modular test suite, and run it from the separate file while bisecting. When you fix, merge the test into main test suite. – Ciro Santilli新疆棉花TRUMP BAN BAD Apr 21 '14 at 18:02
  • 1
    @CiroSantilli六四事件法轮功纳米比亚威视 Sorry, I rolled back your edit below, because I feel its more explanatory for newbies and its just adding a further point to your answer (not an exact answer as yours--so mentioning that, its to add a point) – Nicks Sep 30 '15 at 00:33
  • 2
    There are lots of ways to go wrong with 'git bisect run' - for example seeing how a good commit was reversed by a bad merge. It comes in, out, back in and out again and only the last "out" is bad. However, you can always do a manual 'git bisect'. Because it's a bisect, it only takes a few steps - e.g 1024 commits in 10 steps. – combinatorist Apr 01 '18 at 12:25
  • 1
    @combinatorist you are right that it might fail. I find `bisect run` specially useful when the test takes a long time to finish, and I'm pretty sure that the test system won't break. This way, I can just leave it running on the background, or overnight if it takes up too many resources, without losing any brain context switch time. – Ciro Santilli新疆棉花TRUMP BAN BAD Aug 11 '18 at 19:40
  • A newbie's doubt, once the bug is found, do I have to fix it before doing a reset? I mean bisect at the end it puts me in the bad commit, then I do a git stash saving my new changes and then I do a reset? – jcarlosweb Feb 13 '21 at 14:13
  • 1
    @jcarlosweb you generally want to `reset` and then fix, because your fix could have merge conflicts with master if you do it on an old revision. – Ciro Santilli新疆棉花TRUMP BAN BAD Feb 13 '21 at 14:16
137

TL;DR

Start:

$ git bisect start
$ git bisect bad
$ git bisect good <goodcommit>

Or

$ git bisect start
$ git bisect good
$ git bisect bad <badcommit>

Bisecting: X revisions left to test after this (roughly Y steps)

Repeat:

Issue still exists?

  • Yes: $ git bisect bad
  • No: $ git bisect good

Result:

<abcdef> is the first bad commit

When Done:

git bisect reset
Geoffrey Hale
  • 8,152
  • 4
  • 34
  • 42
  • 4
    Make sure you're at the root of your git repo, or you'll get a weird "You need to run this command from the toplevel of the working tree." error. – Paul Danelli Aug 24 '17 at 08:59
  • 1
    So i did git bad on my HEAD and git good on the first commit, when the error was not present. So what to do next? when bug is not present git bisect good to move to next commit? – Gobliins Feb 20 '18 at 10:40
  • @Gobliins When bug is not present, correct, `git bisect good` to move to next commit. – Geoffrey Hale Aug 16 '18 at 18:41
42

Just to add a further point:

We can specify a file name or path to git bisect start in case we know that the bug has come from particular files. For example, Suppose we knew that the changes that caused the regression were in the com/workingDir directory then we can run git bisect start com/workingDir This means that only the commits that changed the contents of this directory will be checked, and this makes things even faster.

Also,If it’s difficult to tell if a particular commit is good or bad, you can run git bisect skip, which will ignore it. Given there are enough other commits, git bisect will use another to narrow the search instead.

Nicks
  • 14,934
  • 7
  • 54
  • 62
15

$ git bisect .. bascically a Git tool for debugging. 'Git Bisect' debugs by going through the previous commits since your last (known) working commit. It uses binary search to go through all those commits, to get to the one which introduced the regression/bug.

$ git bisect start # Starting bisect

$ git bisect bad # stating that the current commit (v1.5) has the regression/ setting 'bad' point

$ git bisect good v1.0 # mentioning it the last good working commit (without regression)

This mentioning of 'bad' and 'good' points will help git bisect (binary search) pick the middle element (commit v1.3). If the regression is there at commit v1.3, you'll set it as the new 'bad' point i.e. (Good -> v1.0 and Bad -> v1.3)

$ git bisect bad

or similarly if the commit v1.3 is bug-free you'll set it as the new 'Good point' i.e. (*Good -> v1.3 and Bad -> v1.6).

$ git bisect good
Nabeel Ahmed
  • 14,549
  • 4
  • 50
  • 54
4

Note: the terms good and bad are not the only ones you can use for marking a commit with or without a certain property.

Git 2.7 (Q4 2015) introduced new git bisect options.

 git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
                  [--no-checkout] [<bad> [<good>...]] [--] [<paths>...]

With documentation adding:

Sometimes you are not looking for the commit that introduced a breakage, but rather for a commit that caused a change between some other "old" state and "new" state.

For example, you might be looking for the commit that introduced a particular fix.
Or you might be looking for the first commit in which the source-code filenames were finally all converted to your company's naming standard. Or whatever.

In such cases it can be very confusing to use the terms "good" and "bad" to refer to "the state before the change" and "the state after the change".

So instead, you can use the terms "old" and "new", respectively, in place of "good" and "bad".
(But note that you cannot mix "good" and "bad" with "old" and "new" in a single session.)

In this more general usage, you provide git bisect with a "new" commit has some property and an "old" commit that doesn't have that property.

Each time git bisect checks out a commit, you test if that commit has the property:
If it does, mark the commit as "new"; otherwise, mark it as "old".

When the bisection is done, git bisect will report which commit introduced the property.


See commit 06e6a74, commit 21b55e3, commit fe67687 (29 Jun 2015) by Matthieu Moy (moy).
See commit 21e5cfd (29 Jun 2015) by Antoine Delaite (CanardChouChinois).
(Merged by Junio C Hamano -- gitster -- in commit 22dd6eb, 05 Oct 2015)

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283