Skip to content

Latest commit

 

History

History
260 lines (164 loc) · 3.38 KB

File metadata and controls

260 lines (164 loc) · 3.38 KB

🔍 Git Bisect (Find Bugs in History)

Quickly find the commit that introduced a bug using binary search.


📌 What Is Git Bisect?

git bisect helps you:

Identify the exact commit that introduced a bug.


🧠 Why Use Bisect?

Without bisect:

  • manually checking commits ❌
  • time-consuming ❌

With bisect:

  • binary search 🔍
  • fast debugging ⚡

🗺️ Big Picture

flowchart LR
    A[Old Good Commit] --> B[Mid Commit]
    B --> C[New Bad Commit]
Loading

🧬 Binary Search Concept

Total commits: 100

Without bisect → check 100 commits ❌
With bisect → check ~7 commits ✅

🧱 Basic Workflow

flowchart TD
    A[Start Bisect] --> B[Test Commit]
    B --> C{Good or Bad?}
    C -->|Good| D[Move Forward]
    C -->|Bad| E[Move Backward]
    D --> B
    E --> B
    B --> F[Bug Found]
Loading

🧱 Step-by-Step


Step 1 — Start bisect

git bisect start

Step 2 — Mark bad commit

git bisect bad

(Current commit has bug)


Step 3 — Mark good commit

git bisect good <commit-hash>

(Older commit without bug)


Step 4 — Test commits

Git automatically checks out middle commit.


Step 5 — Mark result

git bisect good

OR

git bisect bad

Step 6 — Repeat

Git continues narrowing down.


Step 7 — Result

Commit XYZ introduced the bug

Step 8 — Exit

git bisect reset

🧪 Real-World Scenario

1. App worked last week
2. Now broken
3. Unknown commit caused issue
4. Use git bisect
5. Find exact commit quickly

🧠 Internal Behavior

Git performs binary search
Each step halves search space

🔄 Visual Flow

[Good] ---- mid ---- [Bad]
          ↓
      test mid

⚙️ Automating Bisect (Advanced)

git bisect run test-script.sh

Git will:

  • run script automatically
  • decide good/bad
  • find bug faster

🚨 Common Mistakes

  • marking wrong commit
  • not testing properly
  • forgetting to reset

✅ Best Practices

  • identify correct good commit
  • test carefully
  • use automation when possible

🎤 Interview Questions

What is git bisect?

Tool to find bug-causing commit using binary search.


Why is it efficient?

Reduces search from linear to logarithmic.


When to use?

When bug origin is unknown.


🧪 Practice Lab

git bisect start
git bisect bad
git bisect good <old-commit>

# test each step
git bisect good
git bisect bad

git bisect reset

🎯 Final Takeaway

Git bisect is a debugging superpower.

Use it when:

  • bug source unknown
  • large history
  • need fast isolation

👉 Next Step

➡️ 05-git-tag.md