Use git bisect to find first bad commit
Today I learned that there is a way to find first bad commit with git. Before learning about git bisect
, whenever I see a commit is buggy after introducing a new feature, I usually go back to a commit that I think introduces the bug and manually test it, this manual tracing back method is okay is there are only a few commits and you are sort of sure where it went wrong, but if there are many commits in between the first bad commit to current commit, this manual way won’t work.
While learning about binary search, I found that there is a git command git bisect
which utilizes binary search under the hood, to find the first bad commit in less time. I won’t be listing how binary search works here but I’ll make a note of how to use git bisect
.
Say you find that the latest commit is broken, and you know that 30 commits before the code works fine, and you want to know which commit introduces the bug:
This is very handy for finding the first bad commit, and using binary search under the hood takes less time.