To find which commit introduced a bug using binary search in Git, you can use the git bisect command. This command helps you identify the commit that introduced a specific issue by performing a binary search through your commit history.
Here’s a basic outline of how you would use git bisect:
1. Start the bisect process:
git bisect start
2. Mark the current commit (the one with the bug) as bad:
git bisect bad
3. Mark a known good commit (a commit that does not contain the bug):
git bisect good <good_commit_hash>
Replace <good_commit_hash> with the hash of a commit you know is stable.
4. Git will then check out a commit in the middle of the range. You need to test this commit to see if the bug is present.
5. After testing, you mark the commit as either bad (if the bug is present) or good (if the bug is not present):
git bisect bad
or
git bisect good
6. Repeat steps 4 and 5 until Git narrows down to the specific commit that introduced the bug.
7. Once you find the offending commit, you can use:
git bisect reset
to end the bisect session and return to your original branch.
By using git bisect, you efficiently zero in on the commit that introduced the bug through a series of tests and checks.