I just came across a neat tool that I did not know existed in git.
I was trying to track down an issue in an app. The issue existed in our integration branch but not in production. I pulled up a diff between the two branches and found that there were over 200 commits. Clearly this would take a long time if I had to go one by one, build, and check if the issue existed on that commit.
It sounded like I could use a binary search in order to more quickly find the issue, so I posted in the team slack channel that I would attempt to do that the following day.
The next morning I check slack and a co-worker had mentioned that git bisect could help.
git-bisect - Use binary search to find the commit that introduced a bug
What!? You mean git has something built in to do exactly what I was going to try and do manually? AWESOME!
This saved me a lot of overhead of manually determining how to search the commits, and it was super simple. To start things off:
git bisect start HEAD v0.4
HEAD contains the issue and release
v0.4 did not. After running that git conveniently lets you know how many revisions are left to test, roughly how many steps, and which commit you're currently on:
Bisecting: 117 revisions left to test after this (roughly 7 steps) [commit] commit message
Now you just build your project and check if the issue occurs. If the issue is still present you use
git bisect bad and if the issue is gone
git bisect good.
Git will checkout the appropriate commit:
Bisecting: 56 revisions left to test after this (roughly 6 steps)
and then you just repeat the steps above until you've found your culprit:
f1c7e3c5c907fa3a34a96ae524c2fcc64decad2c is the first bad commit commit f1c7e3c5c907fa3a34a96ae524c2fcc64decad2c Author: Some Guy <firstname.lastname@example.org> Date: Fri Mar 17 22:02:01 2017 -0700 COMMIT MESSAGE
Just use your super developer debugging powers to then comb through the changes and find the bug. Once you're done you'll still be in bisect mode so just run
git bisect reset.
git-bisect is a super handy tool for situations like these and a big time saver. For the advanced user there are more subcommands and options that are available in the docs.