使用 git bisect 定位你的 BUG

Git 是开发者的好帮手,今天跟你们分享的是用 git bisect 来找到你代码中的 bad commit 。
<!-- more -->html

背景

你可能遇到过这种状况, 昨天下班前把模块开发完了, 单元测试验证经过, git commmit 盖上电脑 开开心心下班啦 😄
次日啥上午来了,继续开发,提交了几个 commit ,下午部署了一个版本,发现昨天测试经过的代码出现了 BUG 😂
这个时间你会怎么作, 可能的翻出现 BUG 代码文件的 git log 一翻发现 有20个 commit 🤦‍
此时你的心情多是崩溃的 🔥 git

告别人肉排查 bad commit 借助 git bisect 找 BUG ✌️segmentfault

具体方法

先经过 git log 命令 找到你肯定代码是 OK 的 git hash 再找到你当前出现 BUG 的 git hash

而后 使用 git bisect start 开始我们的奇妙 debug 之旅 🚀

开始对 commit 作标记

告诉 git 这个 commit(刚才找出来肯定代码是OK的 commit hash) 是 OK 的 git bisect good 5d5dba7

告诉 git 当前最后一个 commit 代码是有 BUG 的 git bisect bad 692ac39

git 会进行二分查找

而后开始开始 test 验证当前 hash 的 commit 的代码是否是 OK 的

若是当前代码 test 不经过 就标记为 bad git bisect bad

反之若是当前代码 test 经过 就标记为 good git bisect good

反复的进行 test 和标记 直到找出那个 bad commit 为止

而后退出 git bisect 模式 git bisect reset

附上操做流程图 git bisectapi

附上官方文档 Git Docs

附上二分查找可视化动画 binary search visualization

附上个人命令行操做记录

wujunze@Mac: ~/monkey/code/monkey-api develop
 $ git bisect start                                                                                                                                              [20:31:46]
 
wujunze@Mac: ~/monkey/code/monkey-api develop
 $ git logg                                                                                                                                                      [20:31:50]
 
wujunze@Mac: ~/monkey/code/monkey-api develop
 $ git bisect good 16e91a8                                                                                                                                       [20:31:54]
 
wujunze@Mac: ~/monkey/code/monkey-api develop
 $ git logg                                                                                                                                                      [20:31:59]
 
wujunze@Mac: ~/monkey/code/monkey-api develop
 $ git bisect bad 692ac39                                                                                                                                        [20:32:04]
Bisecting: 9 revisions left to test after this (roughly 3 steps)
[cd1a0814fe21aa3e06020efb5aa4118ead17acce] not filter
 
wujunze@Mac: ~/monkey/code/monkey-api cd1a081
 $ git bisect bad                                                                                                                                                [20:32:07]
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[63bf3176854a4fe112d612cee3f6bacce9e77e7d] fix merge
 
wujunze@Mac: ~/monkey/code/monkey-api 63bf317
 $ git bisect good                                                                                                                                               [20:32:11]
Bisecting: 2 revisions left to test after this (roughly 1 step)
[798239a0397c52127c721b8b84bb430b5fd0e83b] debug
 
wujunze@Mac: ~/monkey/code/monkey-api 798239a
 $ git bisect bad                                                                                                                                                [20:32:14]
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[5d5dba7c3fc947768cc609493de9808f3d9cf635] fix assert logic
 
wujunze@Mac: ~/monkey/code/monkey-api 5d5dba7
 $ git bisect bad                                                                                                                                                [20:32:23]
5d5dba7c3fc947768cc609493de9808f3d9cf635 is the first bad commit
commit 5d5dba7c3fc947768cc609493de9808f3d9cf635
Author: wujunze <itwujunze@163.com>
Date:   Tue Oct 29 18:20:36 2019 +0800

    fix assert logic

:040000 040000 b5d77b6ac82d8427d1bc3a9db2213f6c10ea0d63 3f49c18b6569282f7fa2a2c935b9ba73d6d0fbc0 M      app
 
wujunze@Mac: ~/monkey/code/monkey-api 5d5dba7
 $ git bisect reset                                                                                                                                              [20:32:27]
Previous HEAD position was 5d5dba7 fix assert logic
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
 
wujunze@Mac: ~/monkey/code/monkey-api develop
 $                                                                                                                                                               [20:36:38]

## 百闻不如一 Run 你们能够本身试一下 有什么问题能够在文章下面评论bash

笔者才疏学浅,仓促成文,若有不当之处,还请你们斧正.
本文由 吴钧泽博客 发布!
相关文章
相关标签/搜索