使用 Git Bisect 调试
利用 `git bisect` 高效定位引入错误的确切提交,节省宝贵的调试时间
使用 Git Bisect 调试 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Git Bisect?
Imagine a bug appears in your project, but you're not sure which recent change caused it. Manually checking each commit can be a huge time sink!
git bisect is a powerful Git command that uses a binary search algorithm to quickly find the exact commit that introduced a bug.
The Binary Search Advantage
How does git bisect work so fast? It's like a game of 'hot or cold' with your commit history.
- You tell Git a commit you know is bad (has the bug).
- You tell Git a commit you know is good (before the bug existed).
- Git picks a commit roughly in the middle and asks you to test it.
- Based on your feedback, it halves the search space, repeating until the culprit is found.
Starting Your Bisect Hunt
To begin, first you need to tell Git you're starting a bisect session. Then, you mark the known 'bad' (buggy) commit and a known 'good' (working) commit.
You can use commit hashes, branch names, or tags.
git bisect start
git bisect bad HEAD
git bisect good <known_good_commit_hash>The Iterative Process: Test & Mark
After you define your good and bad commits, Git will automatically check out a commit in the middle of that range. Your job is to test the code at this commit.
- If the bug is present, mark it as bad.
- If the bug is NOT present, mark it as good.
Git will then move to the next commit, halving the remaining search space.
Marking a Commit
Once you've tested the currently checked-out commit, use these commands to tell Git whether it's good or bad:
- To mark the current commit as bad:
git bisect bad - To mark the current commit as good:
git bisect good
Git will then automatically check out the next commit to test.
git bisect bad
# OR
git bisect goodNarrowing Down the Problem
You'll repeat the 'test and mark' process. With each step, git bisect gets closer to the single commit that introduced the bug. It's much faster than checking commits one by one!
For a range of 100 commits, it takes at most 7 tests (log2(100) ≈ 6.64).
Found the Culprit!
Eventually, git bisect will narrow the range down to a single commit. It will then report the first 'bad' commit found, which is your bug introducer!
After finding the bug, it's crucial to exit the bisect session to return your repository to its original state (usually the branch you were on when you started).
git bisect resetHandling Untestable Commits
Sometimes, git bisect might land on a commit that can't be tested (e.g., it doesn't compile, or dependencies are broken). In such cases, you can tell Git to skip it:
git bisect skip
Git will then pick another commit to test, making sure to exclude the skipped one from the potential range of the bug.
git bisect skipAutomating with Git Bisect Run
For even faster debugging, you can automate the testing process using git bisect run. This command takes a script that performs the test and exits with specific codes:
0: The commit is good.(except1-127125): The commit is bad.125: The commit should be skipped (e.g., cannot build).
Git will then run the script automatically for each commit until the bug is found.
#!/bin/bash
# Example test script for git bisect run
# Replace 'make test' with your actual test command
if make test;
then
exit 0 # Tests passed, commit is good
else
exit 1 # Tests failed, commit is bad
fiBisect Workflow Check
Which of the following commands are essential when performing a manual git bisect to find a bug?
Debugging Power-Up!
You've mastered git bisect! This powerful tool transforms bug hunting from a tedious chore into an efficient, systematic process using binary search.
- Start with
git bisect start, defining good and bad commits. - Test and mark commits as
goodorbad. - Use
git bisect skipfor untestable commits. - Automate with
git bisect runfor fast, hands-free debugging. - Always finish with
git bisect reset.
Happy bug hunting!
常见问题解答
「使用 Git Bisect 调试」课时是免费的吗?
是的 — 「使用 Git Bisect 调试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
「使用 Git Bisect 调试」这节课中我会学到什么?
利用 `git bisect` 高效定位引入错误的确切提交,节省宝贵的调试时间 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Git Bisect 调试」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 恢复丢失的提交与分支
- 使用 Git Bisect 调试
- 优化 Git 代码仓库性能
- 使用引用日志和暂存区挽救工作