git bisect 是一個(gè)強(qiáng)大的調(diào)試工具,能快速定位引入 bug 的提交。1. 啟動(dòng) bisect 會(huì)話:運(yùn)行 git bisect start;2. 標(biāo)記當(dāng)前為壞提交:git bisect bad;3. 標(biāo)記一個(gè)已知好提交:git bisect good <commit-hash>;4. Git 檢出中間提交,測(cè)試后根據(jù)結(jié)果執(zhí)行 git bisect good 或 git bisect bad;5. 重復(fù)步驟直至 Git 找到首個(gè)壞提交;6. 使用 git bisect run ./test-bug.sh 可自動(dòng)化該過程;7. 定位完成后,用 git show 查看問題提交;8. 最后運(yùn)行 git bisect reset 退出 bisect 模式。通過二分查找,即使在上千次提交中也能在極少步驟內(nèi)定位問題,極大提升調(diào)試效率,且必須在使用后重置會(huì)話以恢復(fù)正常工作狀態(tài)。
When you’re dealing with a bug that appeared somewhere in your project’s history, and you’re not sure exactly when or why it started, git bisect
is one of the most powerful tools in your debugging arsenal. Instead of manually checking every commit, Git bisect uses a binary search algorithm to quickly pinpoint the exact commit that introduced the issue.

How Git Bisect Works
At its core, git bisect
automates the process of narrowing down bad commits. You start by telling Git that you’re in bisect mode and identifying one known "bad" commit (where the bug exists) and one known "good" commit (where the bug didn’t exist). Git then checks out a commit roughly halfway between them. You test the code at that point and tell Git whether it’s good or bad. Git uses that information to halve the remaining range and continues until it identifies the first bad commit.
This binary search approach means you can sift through hundreds or even thousands of commits in logarithmic time — for example, finding the culprit in 10 steps or fewer among 1,000 commits.

Starting a Bisect Session
To begin, run:
git bisect start
Then mark the current (or a known-bad) commit as bad:

git bisect bad
Next, identify a commit where you’re sure the bug didn’t exist and mark it as good:
git bisect good <commit-hash>
After this, Git will automatically check out a commit in the middle of the range.
Now you test the application — run the relevant test case, manually reproduce the bug, or use any method to determine whether the behavior is broken at this point.
- If the bug is present:
git bisect bad
- If the bug is not present:
git bisect good
Repeat the process. Git will keep narrowing down the range.
Automating the Process with Test Scripts
If you have a reliable test that reproduces the bug, you can automate the entire bisect process.
For example, suppose you have a script test-bug.sh
that exits with code 0 if the behavior is correct ("good") and non-zero if the bug is present ("bad"). You can run:
git bisect run ./test-bug.sh
Git will automatically run the script at each step, mark the commit accordingly, and continue until it finds the first bad commit.
This is especially useful for regression testing or when the bug is caught by a unit or integration test.
Tips for Effective Bisecting
- Use a reliable test signal: The success of
git bisect
depends on your ability to accurately classify a commit as good or bad. Make sure your test is repeatable and not affected by external factors. - Avoid merge commits initially: If possible, run bisect on a linear history or use
git bisect skip
if you hit a merge commit that’s hard to evaluate. - Clean environment: Make sure your build and test environment is consistent across commits. Use tools like Docker or virtual environments if needed.
- Know when to stop: Once Git identifies the first bad commit, it will show you something like:
bisect run success <commit-hash> is the first bad commit
You can then inspect the changes in that commit:
git show <commit-hash>
Finally, always remember to end the bisect session:
git bisect reset
This returns your HEAD to where you started and exits bisect mode.
Bottom Line
git bisect
turns a potentially hours-long debugging session into a structured, efficient process. Whether you're working alone or on a large team with a fast-moving codebase, learning to use git bisect
— especially with automated tests — can save you significant time and frustration when tracking down regressions.
It’s not magic, but with a clear reproduction of the bug, it’s the closest thing Git has to a time machine for debugging.
The above is the detailed content of Finding the Root Cause of Bugs with Git Bisect. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Packfile is an efficient mechanism used by Git to package, compress and transfer repository objects. When you execute gitpush, gitfetch or gitclone, what Git actually transmits is the packfile; 1. It is initially generated by loose objects through gitgc or gitrepack commands and stored in the .git/objects/pack/ directory; 2. The packfile not only contains object data, but also records the delta relationship between objects, and achieves rapid search with index file (.idx). 3. This design reduces the transmission volume and improves synchronization efficiency; 4. A large number of small packfiles may affect performance, and can be used through gitgc or git

To view Git commit history, use the gitlog command. 1. The basic usage is gitlog, which can display the submission hash, author, date and submission information; 2. Use gitlog--oneline to obtain a concise view; 3. Filter by author or submission information through --author and --grep; 4. Add -p to view code changes, --stat to view change statistics; 5. Use --graph and --all to view branch history, or use visualization tools such as GitKraken and VSCode.

To delete a Git branch, first make sure it has been merged or no retention is required. Use gitbranch-d to delete the local merged branch. If you need to force delete unmerged branches, use the -D parameter. Remote branch deletion uses the gitpushorigin-deletebranch-name command, and can synchronize other people's local repositories through gitfetch-prune. 1. To delete the local branch, you need to confirm whether it has been merged; 2. To delete the remote branch, you need to use the --delete parameter; 3. After deletion, you should verify whether the branch is successfully removed; 4. Communicate with the team to avoid accidentally deleting shared branches; 5. Clean useless branches regularly to keep the warehouse clean.

ToswitchGitbranches,firstupdatethelocalrepowithgitfetch,checkexistingbrancheswithgitbranchcommands,thenusegitcheckoutorgitswitchtochangebranches,handlinguncommittedchangesbycommitting,stashing,ordiscardingthem.WhenswitchingGitbranches,ensureyourlocal

To discard the modifications in the Git working directory and return to the state of the last commit, 1. For the modifications of the tracked files, use gitcheckout-- or gitcheckout--. Discard all modifications; 2. For new files that are not tracked, use gitclean-f to delete the files. If the directory is included, use gitclean-fd. Before execution, use gitclean-fd to preview the delete content; 3. If you need to reset all changes (including the temporary storage area and the working directory), use gitreset-hard. This command will reset the working directory and the temporary storage area. Be sure to operate with caution. These methods can be used individually or in combination to achieve the purpose of cleaning up the working directory.

To add a subtree to a Git repository, first add the remote repository and get its history, then merge it into a subdirectory using the gitmerge and gitread-tree commands. The steps are as follows: 1. Use the gitremoteadd-f command to add a remote repository; 2. Run gitmerge-srecursive-no-commit to get branch content; 3. Use gitread-tree--prefix= to specify the directory to merge the project as a subtree; 4. Submit changes to complete the addition; 5. When updating, gitfetch first and repeat the merging and steps to submit the update. This method keeps the external project history complete and easy to maintain.

Git hooks are used to automatically run scripts before and after commits, pushes and other operations to execute tasks. Specific uses include: 1. Run code checks or tests before submission; 2. Forced submission information format; 3. Send notifications after push. They help unify team specifications and reduce manual steps, such as preventing submissions when tests fail. Git hooks are located in the .git/hooks/ directory in the repository and are not shared by default. They need to be copied manually or used tools such as Husky for team collaboration. Writing a basic hook requires creating an executable file and naming the corresponding event, such as pre-commit, and writing logical judgments there to block or allow operations.

Soundstageafafileiititwittingchatcase, USEGITIZEADTORDOREMEVOME FROMARNINGAREAILACT.TOUNDACT Rungit Reset.ForPartialStialing, Usgit rests-PtointelavEevstehuncificisshunissehunissue
