Last Updated: February 25, 2016
·
1.05K
· quard

How to find bad commit using git

Not many developers know, that git have a great tool for finding ‘bad’ commit. Its name - bisect.

To start using bisect, we have to run three commands:

  1. git bisect - to start process
  2. git bisect good <hash of good commit> - to mark some commit as good (commit where there is no error)
  3. git bisect bad - mark current commit as bad

Git will ‘jump’ to middle commit between good and bad. We have to check, if error appears, we say “git bisect bad” to mark current commit as bad. And if there are no errors, we mark commit as good - “git bisect good”. We should do it until git says in which commit errors appear first time.

We can automate this process if we have test. Git runs command and checks returned code to mark commit as good or bad. It’s very helpful.

There you can find repository with sample source and test for finding first bad commit by git bisect.

To use source from sample repository you have to:

check out it

git clone https://github.com/Quard/test-git-bisect
cd test-git-bisect

start bisect and set good and bad commits (last commit is bad)

git bisect start
git bisect bad
git bisect good d8e32db15267e857dda146d6a131a1289e91da74

run bisect in automation mode (there is adult version, to use it - simple change “test.py” to “friday_18+/test.py”)

git bisect run python test.py

At the end Git will print commit, where error appears first time.

Source available on GitHub

for adult version you have to install OpenCV