← Back to Mission Control

Advanced History Analysis

10 min read

Git Log Mastery & Forensics

Repository Detective Training

You are now entering advanced forensic investigation training. These skills will help you solve complex mysteries hidden in repository history and track down the source of critical issues.

Mission Briefing

Commander, every repository tells a story. Within the commit history lies crucial intelligence: who changed what, when, and why. Sometimes missions fail not because of current problems, but because of decisions made months or years ago. The ability to investigate repository history like a detective is what separates elite commanders from those who merely manage code.

You'll master advanced Git log techniques that transform you into a repository archaeologist, capable of extracting precise information from massive codebases, tracking down elusive bugs, and understanding complex development patterns that span years of mission history.

Investigation Objectives

  • Master advanced Git log filtering and search techniques
  • Implement forensic analysis for bug tracking and blame investigation
  • Execute sophisticated commit analysis and pattern recognition
  • Deploy advanced formatting and visualization techniques
  • Establish systematic approaches for repository archaeology

Advanced Log Filtering

Advanced filtering transforms git log into a precision investigation tool.

Essential Filters

# Time-based filtering
git log --since="2 weeks ago" --until="yesterday"
git log --since="2024-01-01"

# Author filtering
git log --author="Sarah"
git log --author="sarah@"  # Email match

# Content searches
git log --grep="security"  # Search commit messages
git log -S "function_name"  # Pickaxe: when code was added/removed
git log -G "pattern"  # Regex search in changes

# File-specific
git log --follow -- filename
git log -- "*.js"

# Branch comparisons
git log main..feature  # In feature, not in main
git log --graph --oneline --all

Forensic Analysis

Git Blame

Determine who wrote each line and when:

# Basic blame
git blame filename

# With details
git blame -e --date=short filename

# Specific line range
git blame -L 100,120 filename

# From specific commit
git blame abc123f -- filename

Git Bisect - Binary Bug Hunt

Find the exact commit that introduced a bug:

# Start investigation
git bisect start
git bisect bad  # Current is bad
git bisect good v1.0  # Known good version

# Test and mark
git bisect good  # or bad
# Repeat until Git finds the commit

# Automated
git bisect run npm test

# Finish
git bisect reset

Repository Statistics

# Top contributors
git log --format='%aN' | sort | uniq -c | sort -nr

# Most modified files
git log --name-only --format='' | grep -v '^$' | sort | uniq -c | sort -nr

# Commits by month
git log --format='%ad' --date=format:'%Y-%m' | sort | uniq -c

Custom Formatting

# Compact one-liner with graph
git log --oneline --graph --all

# Custom format with colors
git log --pretty=format:"%C(yellow)%h %C(red)%ad %C(blue)%an%C(reset): %s" --date=short

# Detailed format
git log --pretty=format:"Hash: %H%nAuthor: %an <%ae>%nDate: %ad%nSubject: %s%n" --date=iso

# With file statistics
git log --stat --oneline

# Export to CSV
git log --format='"%h","%an","%ae","%ad","%s"' --date=iso > commits.csv

Hands-On: Find a Bug with Bisect

Practice using git bisect to hunt down a performance regression:

# Create test repository
mkdir bug-hunt && cd bug-hunt && git init

# Create fast version
echo "// Fast implementation" > app.js
git add app.js && git commit -m "Fast version" && git tag v1.0

# Add commits
echo "// Feature A" >> app.js && git add app.js && git commit -m "Add feature A"
echo "// Feature B" >> app.js && git add app.js && git commit -m "Add feature B"

# Introduce bug
echo "// SLOW CODE HERE" >> app.js && git add app.js && git commit -m "Refactor"

# More commits
echo "// Feature C" >> app.js && git add app.js && git commit -m "Add feature C"

# Find the bug
git bisect start
git bisect bad  # Current is slow
git bisect good v1.0  # v1.0 was fast

# Test each commit Git checks out
# Mark as good or bad until Git finds the culprit
git bisect reset  # When done

Quick Reference

Search Messages

git log --grep="keyword"

Find Code

git log -S "code"

Who Changed Line

git blame file

Hunt Bug

git bisect start

Top Contributors

git log --format='%aN' | sort | uniq -c | sort -nr

File History

git log --follow -- file

Useful Aliases

# Add to ~/.gitconfig
[alias]
    investigate = log --oneline --graph --all
    hunt = log --grep
    authors = log --format='%aN' | sort | uniq -c | sort -nr
    hotfiles = log --name-only --format='' | grep -v '^$' | sort | uniq -c | sort -nr

Mission Complete!

You've mastered advanced Git history analysis and forensic techniques.

Skills Acquired

Continue to Refs & Reflog Mastery to master Git's internal reference system.