当前位置: 首页 > news >正文

git bisect 使用二分法查找引入错误的提交

git bisect 使用二分法查找引入错误的提交

Git bisect 命令官方文档

git bisect 这个命令使用二分搜索算法来查找项目历史中哪个提交引入了一个错误
使用该命令时,首先告诉它一个已知包含错误的 “坏” 提交
以及一个已知在错误出现之前的 “好” 提交
然后 git bisect 在这两个端点之间挑选一个提交
并询问你所选的提交是 “好” 还是 “坏”
它继续缩小范围,直到找到引入该修改的确切提交

举例:
文件A.txt正确内容应该是 number = 28

某一时刻发现文件A.txt 中的内容被错误的修改为 number = 30

如果中间提交了许多次,很难定位具体是哪一次提交改错了

首先执行命令 git reflog 查看提交日志

$ git reflog
2fe8002 (HEAD -> master_2, origin/master_2) HEAD@{0}: commit: xiugai
f3f9398 HEAD@{1}: commit: xiugai
31e63a6 HEAD@{2}: commit: xiugai
3f93ea1 HEAD@{3}: commit: xiugai
be6f911 HEAD@{4}: commit: xiugai
8fdfa9c HEAD@{5}: commit: xiugai
b20faae HEAD@{6}: commit: xiugai
05b7272 HEAD@{7}: commit: xiugai
49e2e88 HEAD@{8}: commit: xiugia
b680d3e HEAD@{9}: commit: xiugai
4ba8bd4 HEAD@{10}: commit: xiugai
d4aa8ab HEAD@{11}: commit: xiugai
93383c8 HEAD@{12}: commit: xiugai
147a3a2 HEAD@{13}: commit: xiugai
40737a3 HEAD@{14}: commit: xiugai
aac5d98 HEAD@{15}: commit: xiugai

上面每一行开头的 前7位 就是一个 commit Id

首先定位一个 已经错误的 commit 如 2fe8002
只要找到一个错误的commit就行了,具体从哪开始错的并不关心

然后定位一个 正确的 commit 如 aac5d98
随便找一个 commit 只要是它是正确的

执行命令 git bisect start badCommit goodCommit

$ git bisect start 2fe8002 aac5d98
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[49e2e8877b14ad581f940dbd98bebf56a4a57d3b] xiugia

输入命令后指向了 commit 49e2e88

执行命令 git branch

$ git branch
* (no branch, bisect started on master_2)
  main
  master
  master_1
  master_2
  production/1.1
  production/2.1

当前不在任何分支上,并且提示 bisect started on master_2

执行命令 git status

$ git status
HEAD detached at 49e2e88
You are currently bisecting, started from branch 'master_2'.
  (use "git bisect reset" to get back to the original branch)

nothing to commit, working tree clean

可以看到当前在一个游离的提交 49e2e88 位置
也就是帮我们切换到了 49e2e88 提交的状态,此时直接打开项目中文件,查看是否正确
如果正确 执行命令 git bisect good
如果错误 执行命令 git bisect bad

经过比对,发现此时还是错误的,那么错误一定是在下面这一部分提交中

49e2e88 HEAD@{8}: commit: xiugia
b680d3e HEAD@{9}: commit: xiugai
4ba8bd4 HEAD@{10}: commit: xiugai
d4aa8ab HEAD@{11}: commit: xiugai
93383c8 HEAD@{12}: commit: xiugai
147a3a2 HEAD@{13}: commit: xiugai
40737a3 HEAD@{14}: commit: xiugai
aac5d98 HEAD@{15}: commit: xiugai

执行命令

$ git bisect bad
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[93383c8f85ac09d021dba1c45c55813339cba713] xiugai

现在指向了 93383c8f85ac09d021dba1c45c55813339cba713
执行命令 gitstatus

$ git status
HEAD detached at 93383c8
You are currently bisecting, started from branch 'master_2'.
  (use "git bisect reset" to get back to the original branch)

nothing to commit, working tree clean

当前已经切换到了 93383c8

经过比对,发现此时是正确的,那错误一定是下面这一部分提交中

49e2e88 HEAD@{8}: commit: xiugia
b680d3e HEAD@{9}: commit: xiugai
4ba8bd4 HEAD@{10}: commit: xiugai
d4aa8ab HEAD@{11}: commit: xiugai
93383c8 HEAD@{12}: commit: xiugai

执行命令 git bisect good

$ git bisect good
Bisecting: 1 revision left to test after this (roughly 1 step)
[4ba8bd490288126ac2319dc52d4e86e1c0ccecbb] xiugai

到 4ba8bd4 经过比对,发现此时还是正确的,那错误一定是下面这部分提交中
执行命令 git bisect good

$ git bisect good
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[b680d3e5aa93692ad5b847be066753b579eb437d] xiugai

到 b680d3e 经过比对,发现此时是错误的,那错误一定是在下面部分提交中

b680d3e HEAD@{9}: commit: xiugai
4ba8bd4 HEAD@{10}: commit: xiugai

执行命令 git bisect bad

$ git bisect bad
b680d3e5aa93692ad5b847be066753b579eb437d is the first bad commit
commit b680d3e5aa93692ad5b847be066753b579eb437d (HEAD)
Author: liqiang <liqiangeastsun@163.com>
Date:   Fri Feb 14 16:48:33 2025 +0800

    xiugai

 A.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

日志显示 b680d3e5aa93692ad5b847be066753b579eb437d is the first bad commit 这个第一个导致错误的提交
那么详细查看 b680d3e 这个提交具体修改了什么

退出执行命令

$ git bisect reset
Previous HEAD position was b680d3e xiugai
Switched to branch 'master_2'
Your branch is up to date with 'origin/master_2'.

执行命令 git branch

$ git branch
  main
  master
  master_1
* master_2
  production/1.1
  production/2.1

帮我们自动切换到 master_2 分支

相关文章:

  • 软件工程完整大型课设--《颐养中心系统》 三级项目报告
  • 开关电源实战(一)宽范围DC降压模块MP4560
  • 【ARM】JTAG接口介绍
  • 【编程实践】vscode+pyside6环境部署
  • 无人机不等同轴旋翼架构设计应用探究
  • 对指针的深入运用-通讯录的初步实现
  • 【鸿蒙开发】第三十五章 一次开发多端部署
  • 海康摄像头IPV6模式,手动,自动,路由公告
  • 设计模式-命令模式
  • 【含开题报告+文档+源码】基于Web的房地产销售网站的设计与实现
  • DeepSeek自然语言处理(NLP)基础与实践
  • 【3min 简单示例】Unity 通过 C# 脚本移动游戏物体
  • 6. Docker 本地镜像发布到私有库
  • RK3588 Linux平台部署DeepSeek模型教程
  • 20250212:sigmastar系列2-获取UUID进行授权
  • 自动驾驶---如何打造一款属于自己的自动驾驶系统
  • 【开源免费】基于Vue和SpringBoot的旅游管理系统(附论文)
  • 亚冬会绽放“云端”,联通云如何点亮冰城“科技之光”?
  • XML 命名空间
  • Python 面向对象的三大特征
  • 扬州市中医院“药膳面包”走红,内含党参、黄芪等中药材
  • 中央网信办部署开展“清朗·整治AI技术滥用”专项行动
  • 结婚这件事,年轻人到底怎么想的?
  • 打造沪派水乡的“湿意”,上海正在保护营造一批湿地空间
  • 泽连斯基承认乌情报部门刺杀俄军高官
  • 观察|英国航母再次部署印太,“高桅行动”也是“高危行动”