การแก้ไขข้อบกพร่องด้วย Git Bisect
ใช้ `git bisect` เพื่อระบุคอมมิตที่ทำให้เกิดข้อบกพร่องได้อย่างแม่นยำและมีประสิทธิภาพ ช่วยประหยัดเวลาในการแก้ไขข้อบกพร่อง
การแก้ไขข้อบกพร่องด้วย Git Bisect เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแก้ไขข้อบกพร่องด้วย Git Bisect”
ใช้ `git bisect` เพื่อระบุคอมมิตที่ทำให้เกิดข้อบกพร่องได้อย่างแม่นยำและมีประสิทธิภาพ ช่วยประหยัดเวลาในการแก้ไขข้อบกพร่อง คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การแก้ไขข้อบกพร่องด้วย Git Bisect” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม
ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกู้คืนคอมมิตและแขนงที่สูญหาย
- การแก้ไขข้อบกพร่องด้วย Git Bisect
- การเพิ่มประสิทธิภาพคลัง Git
- กู้คืองานด้วย Reflog และ Stash