Search code examples
gitgit-bisectgit-plumbing

Plumbing command to get the result of git-bisect with --no-checkout


I'm writing a script which automatically finds a specific commit.

I've reached the part when I'm calling git bisect start --no-checkout and git bisect run ./my_check_commit_script.sh. It finds and displays the correct commit. However, I don't know how can I get the hash of this commit in my script.

The reference BISECT_HEAD seems to still point at the previous old revision. Other than that, using git show-ref, I've found a ref refs/bisect/new which seems to point to the correct commit but I don't know if I can rely on it existing. There is a bunch of refs/bisect/old* and I suspect it may be an accident that there is only a single refs/bisect/new.

How can I get the hash of the commit found by git bisect in my script?


Here is a script that crates a repository and successfully calls git bisect run on it:

git init repo
cd repo

echo a >foo
git add foo
git commit -m "Initial commit"

echo b >foo
git commit -a -m "Some commit"

echo c >foo
git commit -a -m "First commit which matches criteria"

echo d >foo
git commit -a -m "Some commit"

echo e >foo
git commit -a -m "Last commit"

git bisect start --no-checkout
git bisect old HEAD~4
git bisect new HEAD
git bisect run git merge-base --is-ancestor BISECT_HEAD HEAD~3

Solution

  • The reference refs/bisect/new that you've found is the correct one to use. The command rev-parse bisect/new guarantees to always return the hash of the first "new" commit.

    The Git manual tells that in the section "Basic bisect commands: start, bad, good" (emphasis mine):

    Eventually there will be no more revisions left to inspect, and the command will print out a description of the first bad commit. The reference refs/bisect/bad will be left pointing at that commit.

    The manual calls it refs/bisect/bad but because you're using old & new instead of good & bad, the reference name also changes. (Using options --term-good and --term-bad you can change this name to anything you want.)