Debugging and Searching

Back

Loading concept...

🔍 Git Detective: Finding Bugs & Searching Code

Imagine you’re a detective. Your code suddenly broke, and you need to find the culprit commit. Or maybe you’re searching for a specific word hidden somewhere in thousands of files. Git has two super-powered tools for exactly this!


🎯 The Big Picture

Think of your Git history like a long line of dominoes. Each domino is a commit. Somewhere in that line, one domino was placed wrong and now things are falling apart.

Git Bisect = A smart way to find which domino caused the problem Git Grep = A flashlight to search inside all your files instantly


🔬 Part 1: Debugging with Git Bisect

What is Git Bisect?

Imagine you have 100 commits. Your app worked at commit #1 but is broken at commit #100. Somewhere in between, a bug was introduced.

The slow way: Check commit #2, then #3, then #4… (could take forever!)

The smart way: Git Bisect uses binary search - it cuts the list in half each time!

100 commits → 50 → 25 → 12 → 6 → 3 → Found it!

Instead of checking 100 commits, you only check about 7. Magic! ✨

The Detective Story 🕵️

Picture this: You’re building a game. Last week it worked perfectly. Today, the score counter is broken. You made 64 commits this week. Let’s catch the bug!

Step 1: Start the Investigation

git bisect start

This tells Git: “Hey, I’m starting my bug hunt!”

Step 2: Mark the Crime Scene

git bisect bad

This marks your current commit as “broken.” This is where you saw the bug.

Step 3: Find a Working Version

Go back to a commit where things worked:

git bisect good abc1234

Replace abc1234 with a commit hash where your code was working fine.

Step 4: The Magic Begins

Git now jumps to the middle commit between good and bad. Test your app!

  • If it’s broken: Type git bisect bad
  • If it works: Type git bisect good

Git keeps cutting the range in half until it finds THE ONE commit that broke things.

Step 5: Case Closed!

When Git finds the culprit, you’ll see:

abc5678 is the first bad commit
Author: Developer Name
Date: Tuesday

    Added new feature X

Now you know exactly which commit introduced the bug!

Step 6: End the Investigation

git bisect reset

This brings you back to where you started.


🎯 Visual: How Bisect Works

graph TD A["64 Commits Total"] --> B["Check Middle: #32"] B -->|Bad| C["Check #16"] B -->|Good| D["Check #48"] C -->|Good| E["Check #24"] C -->|Bad| F["Check #8"] E -->|Bad| G["Found: #20!"]

Each step cuts your search in half!


Pro Tip: Automatic Bisect 🤖

Have a test script? Let Git do ALL the work:

git bisect start
git bisect bad HEAD
git bisect good v1.0
git bisect run npm test

Git will automatically run your tests and find the broken commit without you pressing any buttons!


🔦 Part 2: Searching with Git Grep

What is Git Grep?

Git Grep is like having a super-powered flashlight that can search through ALL your code files instantly.

Think of it like this:

  • Regular search: Looking through one drawer at a time
  • Git Grep: X-ray vision that sees through everything at once!

Why Not Just Use Regular Grep?

Git Grep is faster because:

  1. It only searches tracked files (ignores junk files)
  2. It knows your project structure
  3. It can search old versions of your code!

Basic Search: Find a Word

Looking for where “password” appears in your code?

git grep "password"

Output:

config.js:  password: "secret123"
auth.js:    checkPassword(user)

Boom! Found in 2 files instantly.


Search with Line Numbers

Want to know exactly which line?

git grep -n "TODO"

Output:

app.js:42:  // TODO: fix this later
utils.js:15: // TODO: add validation

The -n flag shows line numbers. Super helpful!


Case-Insensitive Search

Find “Error”, “ERROR”, “error” - all at once:

git grep -i "error"

The -i flag ignores upper/lowercase.


Search in Specific Files

Only want to search JavaScript files?

git grep "function" -- "*.js"

The -- "*.js" part limits search to .js files only.


Count Matches

How many times does “console.log” appear?

git grep -c "console.log"

Output:

app.js:12
debug.js:45
utils.js:3

Shows count per file. Great for cleanup missions!


Search Old Commits

Here’s the magic - search in ANY version of your code:

git grep "oldFunction" v1.0

This searches the code as it was in version 1.0. Time travel! 🚀


Show Context Around Matches

See 2 lines before and after each match:

git grep -C 2 "important"

This helps you understand what’s around your search result.


🎯 Visual: Git Grep Power

graph TD A["Git Grep"] --> B["Search Current Code"] A --> C["Search Old Versions"] A --> D["Search Specific Files"] B --> E["Fast - Only Tracked Files"] C --> F["Time Travel Search"] D --> G["Filter by Extension"]

🌟 Real-World Scenarios

Scenario 1: Bug Hunt with Bisect

“My login page broke sometime last week!”

git bisect start
git bisect bad          # Now is broken
git bisect good HEAD~50 # 50 commits ago worked
# Test... mark good/bad... repeat
git bisect reset        # Done!

Scenario 2: Finding All API Calls

“I need to update every API endpoint”

git grep -n "api.example.com"

Now you see every file and line that needs changing.

Scenario 3: Security Audit

“Did anyone hardcode passwords?”

git grep -i "password" -- "*.js" "*.ts"

Quickly audit all JavaScript/TypeScript files.


📝 Quick Commands Summary

Task Command
Start bug search git bisect start
Mark current as bad git bisect bad
Mark commit as good git bisect good <hash>
Auto-test bisect git bisect run <test>
End bisect git bisect reset
Basic search git grep "word"
Search with line # git grep -n "word"
Case insensitive git grep -i "word"
Search file type git grep "x" -- "*.js"
Count matches git grep -c "word"
Search old version git grep "x" v1.0

🎉 You Did It!

You now have two powerful detective tools:

  1. Git Bisect - Binary search to find bug-introducing commits
  2. Git Grep - Lightning-fast code search across your project

These tools will save you hours of debugging time. Instead of guessing where bugs came from, you can find them scientifically. Instead of manually searching files, you can scan everything in seconds.

Happy debugging, detective! 🕵️‍♂️

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.