Git bisect

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the git category.

Last Updated: 2024-11-21

Git bisect allows you to automatically go through (via binary search) many commits and find the one that causes some condition to change from true to false. This condition is contained within a program you pass to git bisect and which will exit 0 for good commits and exist non-zero for bad ones

For example:

# git bisect start bad-commit good-commit
git bisect start 1dd16e0d2fc7608515914f71abb13910c087d251 5ec997fbc
git bisect run ./test

Then test might contain the following code to see if a build file was over 3000kb

#! /usr/bin/env bash

# We don't want to pollute the shell with all this output
yarn 1>/dev/null 2>/dev/null
yarn build 1>/dev/null 2>/dev/null

# We want to see the growth of the size of this going forwards per commit
echo "commit" $(git rev-parse HEAD) $(du -k mysite/static/react-app/build/bundle.js)

# The "cut" part is necessary because `du` also returns the file name, which is not a number.
if [[ $(du -k mysite/static/react-app/build/bundle.js | cut -f1) -gt 3000 ]]; then
  echo "File is too big"
  echo
  exit 2
else
  echo "File is small enough"
  exit 0
fi