Git is way more powerful than adding checkpoints to prevent lost work. One useful trick is to identify which commit introduced bugs into your codebase. Enter git bisect
.
Just three commands
I’m assuming you’re on a Git project and checked in at the version where the problems are happening. Now, you need to:
- Start the investigation:
git bisect start
- Mark the current version as bad:
git bisect bad
- Mark the last known good commit:
git bisect good <commit-hash>
good, bad, repeat
After the initial setup, Git creates a list of commits based on the good and bad marks you set. Then it checks out your code to the first commit in that list.
Now all you have to do is test whether the problem occurs in that commit. If it does, type git bisect bad
; if it doesn’t, type git bisect good
.
Once you’ve run through all commits, Git will check out the commit where the problem was introduced. This is the evidence you need to fix the problem.
To return to the commit you were on before starting the bisect, run git bisect reset
.
And that’s it! Now go impress your peers with your new debugging skills.