长春制作门户网站的公司,php购物网站设计代码,深圳公明网站制作,网站后台怎么上传文章git bisect 使用二分法查找引入错误的提交
Git bisect 命令官方文档
git bisect 这个命令使用二分搜索算法来查找项目历史中哪个提交引入了一个错误 使用该命令时#xff0c;首先告诉它一个已知包含错误的 “坏” 提交 以及一个已知在错误出现之前的 “好” 提交 然后 git b…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)mainmastermaster_1master_2production/1.1production/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 liqiangeastsun163.com
Date: Fri Feb 14 16:48:33 2025 0800xiugaiA.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 branchmainmastermaster_1
* master_2production/1.1production/2.1帮我们自动切换到 master_2 分支