Debugging with Git

Master Git's powerful debugging and analysis tools

Learning Objectives

  • Learn to use git blame for tracking changes
  • Master git bisect for finding problematic commits
  • Understand debugging strategies and workflows
  • Analyze and optimize Git performance

Git Debugging Tools

Git provides powerful tools for debugging code and understanding history. These tools help you track down bugs, understand code evolution, and optimize performance.

Key Tools

  • git blame:Track line-by-line changes
  • git bisect:Binary search through history
  • git log:Advanced history analysis

Use Cases

  • Finding bug introductions
  • Understanding code history
  • Performance optimization

Using Git Blame

Git blame helps you track line-by-line changes in files, showing who last modified each line and when. This is invaluable for understanding code evolution and tracking down bugs.

Git Blame Commands
# Basic blame output
$ git blame filename.js
# Ignore whitespace changes
$ git blame -w filename.js
# Show specific line range
$ git blame -L 10,20 filename.js
# Show commit details
$ git blame -c filename.js
# Trace line moves
$ git blame -M filename.js

Advanced Blame Options

Detecting Moved Code

# Detect moved or copied lines
$ git blame -M -C filename.js
# Show email addresses
$ git blame -e filename.js

Historical Analysis

# Blame from specific revision
$ git blame revision^ filename.js
# Show author dates
$ git blame --date=relative filename.js

Binary Search with Git Bisect

Git bisect helps you find the commit that introduced a bug using binary search. It's especially useful when you know a problem exists but don't know when it was introduced.

Git Bisect Workflow
# Start bisect
$ git bisect start
# Mark current version as bad
$ git bisect bad
# Mark last known good version
$ git bisect good v1.0
# Test and mark each commit
$ git bisect good # or
$ git bisect bad
# End bisect
$ git bisect reset

Automated Bisect

# Create test script
$ echo '#!/bin/sh
make && ./run-tests
exit $?' > test.sh
# Run automated bisect
$ git bisect start HEAD v1.0
$ git bisect run ./test.sh

Debugging Strategies

Effective debugging in Git combines various tools and techniques to quickly identify and resolve issues.

History Analysis

# Search commits
$ git log --grep="bug"
# Show changes
$ git log -p filename
# Find author changes
$ git log --author="name"
# Show branch graph
$ git log --graph --oneline

File History

# File history
$ git log --follow filename
# Show copies
$ git log --find-copies-harder
# Check renames
$ git log --follow --name-status

Performance Analysis

Understanding and optimizing Git's performance helps maintain smooth workflow in large repositories.

Performance Monitoring

# Check repository size
$ git count-objects -vH
# Trace performance
$ GIT_TRACE=1 git status
# Profile operations
$ GIT_TRACE_PERFORMANCE=1 git status

Optimization

# Pack repository
$ git gc --aggressive
# Use sparse checkout
$ git sparse-checkout set <dirs>
# Enable file system monitor
$ git config core.fsmonitor true

What's Next?

Now that you've mastered Git's debugging tools, let's put your knowledge into practice with hands-on exercises. In the next lesson, you'll work on:

  • Basic Git operations practice
  • Step-by-step guided exercises
  • Hands-on workflow examples
  • Practice problem solutions