用 git bisect 找引入 bug 的那次提交
线上出了个 bug,知道上个版本是好的,但中间有 200 多次提交,逐个看太慢。
git bisect start
git bisect bad HEAD
git bisect good v1.4.0
git 会检出中间的提交,你测一下,git bisect good 或 bad,重复大约 log2(200)≈8 次。找到后 git bisect reset 回到原分支。
能自动判定的话更快:
git bisect run ./test.sh
脚本返回 0 表示 good,非 0 表示 bad。适合有稳定复现步骤的场景。
注意几点:依赖变更(lock 文件、数据库迁移)会让中间提交跑不起来,这时候要 git bisect skip;合并提交多的时候用 --first-parent 只看主线,能少走弯路。