git bisect_如何使用git bisect发现错误

git bisect

Sometimes we work on projects for a long, long time. We might craft the perfect 1.0 release, then we release it to the public and we start working on bug fixes, then we get feature requests and we work on improving the app.

有时我们从事项目的时间很长。 我们可能会制作完美的1.0版本,然后将其发布给公众,然后开始进行错误修复,然后收到功能要求,并致力于改进应用程序。

At some point during this process you get a regression bug: an unexpected problem that is caused by an unrelated change in the code.

在此过程中的某个时候,您会得到一个回归错误 :由代码中不相关的更改引起的意外问题。

Something is not working as expected, although you didn’t really change that part of code recently.

尽管您最近并没有真正更改那部分代码,但是某些事情并没有按预期工作。

Or maybe you touched a file or function so many times that you can’t remember what change might have caused the problem that was reported to you.

或者,也许您触摸了文件或功能太多次,以致忘记了什么更改可能导致了报告给您的问题。

In any case, the first thing to do is to determine where the bug is.

无论如何,第一件事就是确定错误在哪里

If you work using a versioning control system like Git, things are easier. You can go back in time and figure out the exact commit that introduced the change. When working in teams this is important because some other person might have worked on that, and if they caused the error you might not know what to do - except asking them to fix it, because Git gives you this information.

如果您使用像Git这样的版本控制系统进行工作,那么事情会更容易。 您可以回到过去,找出引入更改的确切提交。 在团队中工作时,这一点很重要,因为其他人可能已经做过这件事,并且如果他们引起了错误,则您可能不知道该怎么做-除了要求他们进行修复之外,因为Git会为您提供此信息。

One way to do this is to manually checkout some specific commit. For example you can start by checking if yesterday’s version of the codebase worked fine.

一种方法是手动检出某些特定的提交。 例如,您可以从检查昨天的代码库版本是否正常开始。

I use GitHub Desktop, the awesome Git client by GitHub. It’s great for day to day use, but it does not let me check out a single commit. Some other clients do, but you can use the command line.

我使用GitHub Desktop ,这是GitHub上很棒的Git客户端。 它非常适合日常使用,但不允许我检查一次提交。 其他一些客户端也可以,但是您可以使用命令行

Run

git checkout <COMMIT_SHA>

where is the commit you want to roll back to. It will look something like 5a06d3ca5e7adb6e67.

哪里 是您要回滚的提交。 看起来像 5a06d3ca5e7adb6e67

If you still got the problem, you try doing the same with another commit from the day before, and so on, until you find a commit where the code works.

如果问题仍然存在,请尝试从前一天开始对另一个提交执行相同的操作,依此类推,直到找到代码可以正常工作的提交为止。

Now you need to apply the divide and conquer principle, and pick a commit in the middle of the last one that you tried (and didn’t work) and the one that works.

现在,您需要应用分而治之的原则,并在您尝试(但不起作用)的最后一个和可行的中间选择一个提交。

Repeat the process and cut in half every time the interval of commits, until you identify the commit that introduced the problem.

重复该过程,每次提交间隔减少一半,直到您确定引起问题的提交。

This is such a common and useful thing that Git introduced the bisect command to automate this process.

这是一件普通而有用的事情,Git引入了bisect命令来自动化该过程。

Start from your latest commit. Use git checkout your-branch-name, for example git checkout master if you already checked out an older commit.

从您的最新提交开始。 使用git checkout your-branch-name ,例如git checkout master如果您已经签出了较早的提交。

Then run:

然后运行:

git bisect start

Nothing happened yet. You need to tell Git a bad commit reference, where the code does not work:

没事 您需要告诉Git一个错误的提交引用,其中的代码无效:

git bisect bad HEAD

and a good commit, where the code worked:

良好的提交,代码在其中工作:

git bisect good 7f4d976e7540e28c6b0

Git starts the process:

Git启动该过程:

Bisecting: 3 revisions left to test after this (roughly 2 steps)
[d18ebf1c7db9a9b44e8facc5ddb3551e641a64e2] fixes #25

See, you have 6 commits between HEAD and the commit I mentioned as good. Git tells me I have 2 more steps until we find the problematic commit.

看,您在HEAD和我提到的很好的提交之间有6次提交。 Git告诉我,还有2个步骤,直到找到有问题的提交为止。

I go and test the code, then I tell Git the result: git bisect bad or git bisect good depending on the success.

我去测试代码,然后告诉Git结果: git bisect badgit bisect good取决于成功。

Repeat until you find the bad commit, and then run git bisect reset to get back to the HEAD commit.

重复直到找到错误的提交,然后运行git bisect reset以返回到HEAD提交。

Git will guide you in this operation of bisecting. You enter the SHA of the commit

Git将指导您进行平分操作。 您输入提交的SHA

翻译自: https://flaviocopes.com/git-bisect/

git bisect

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值