git bisect dùng binary search để tìm commit đầu tiên gây ra bug — thay vì manual check từng commit.
Workflow:
git bisect start
git bisect bad # current HEAD là bad
git bisect good v1.3.0 # version này còn good
# Git checkout commit ở giữa good và bad
# Test: chạy test hoặc reproduce bug thủ công
git bisect good # commit này không có bug
# Git checkout commit tiếp theo
git bisect bad # commit này có bug
# ... tiếp tục ~7-8 lần cho 100 commits
# Git output: "abc123 is the first bad commit"
git bisect reset # về HEAD ban đầuTự động hóa với script:
git bisect start HEAD v1.3.0
git bisect run npm test -- --testNamePattern="payment calculation"
# Git tự chạy test và tự đánh good/badVí dụ thực tế: release mới bị báo cáo "số tiền hiển thị sai". git log --oneline thấy 87 commits từ lần release cuối. Bisect tự động với test case → tìm ra commit trong 7 bước thay vì check 87 commits.
Lưu ý: script phải return exit code 0 (good) hoặc non-zero (bad). Dùng git bisect skip khi commit không thể test (build broken).
git bisect uses binary search to find the first commit that introduced a bug — instead of manually checking each commit.
Workflow:
git bisect start
git bisect bad # current HEAD is bad
git bisect good v1.3.0 # this version was still good
# Git checks out the midpoint commit
# Test: run tests or manually reproduce the bug
git bisect good # no bug in this commit
# Git checks out the next midpoint
git bisect bad # bug present in this commit
# ... ~7-8 rounds for 100 commits
# Git output: "abc123 is the first bad commit"
git bisect reset # return to original HEADAutomate with a script:
git bisect start HEAD v1.3.0
git bisect run npm test -- --testNamePattern="payment calculation"
# Git runs the test automatically and marks good/badReal-world example: new release is reported to show wrong amounts. git log --oneline shows 87 commits since the last release. Automated bisect with a test case → finds the culprit in 7 steps instead of checking 87 commits.
Note: the script must return exit code 0 (good) or non-zero (bad). Use git bisect skip when a commit cannot be tested (broken build).