History & Inspection
Comprehensive guide to examining repository history, searching through commits, and analyzing changes over time.
Git Log
Basic Log Commands
# Basic log
git log
# Compact one-line format
git log --oneline
# Show last n commits
git log -n 5
git log -5 # Short form
# Show graph structure
git log --graph
git log --graph --oneline
git log --graph --oneline --all
# Decorations (show refs)
git log --decorate
git log --oneline --decorate
Advanced Log Formatting
# Custom format
git log --pretty=format:"%h %an %ar %s"
git log --pretty=format:"%C(yellow)%h%C(reset) %C(blue)%an%C(reset) %C(green)%ar%C(reset) %s"
# Built-in formats
git log --pretty=oneline
git log --pretty=short
git log --pretty=medium
git log --pretty=full
git log --pretty=fuller
# Show file statistics
git log --stat
git log --shortstat
git log --numstat
# Show patches
git log -p
git log --patch
git log -p --word-diff
Log Filtering
# Filter by date
git log --since="2023-01-01"
git log --until="2023-12-31"
git log --since="2 weeks ago"
git log --after="2023-06-01" --before="2023-06-30"
# Filter by author
git log --author="John Doe"
git log --author="john" # Partial match
git log --committer="jane@example.com"
# Filter by commit message
git log --grep="bug fix"
git log --grep="feature" --grep="fix" --all-match # Both patterns
git log --grep="pattern" -i # Case insensitive
# Filter by file
git log -- filename.txt
git log --follow -- filename.txt # Follow renames
git log -- "*.js" # Pattern matching
# Filter by content changes
git log -S "function_name" # Pickaxe search
git log -G "regex_pattern" # Regex search
git log -S "text" --source --all
Log Range and Branch Filtering
# Commit ranges
git log main..feature-branch # Commits in feature not in main
git log feature-branch..main # Commits in main not in feature
git log main...feature-branch # Commits in either but not both
# Multiple branches
git log branch1 branch2 ^main # In branch1 or branch2 but not main
git log --branches --not --remotes # Local commits not pushed
# All branches
git log --all
git log --branches
git log --remotes
git log --tags
Specialized Log Views
# Merge commits only
git log --merges
git log --no-merges
# First parent only (clean history)
git log --first-parent
# Reverse chronological order
git log --reverse
# Show commits that touched specific lines
git log -L 10,20:filename.txt
git log -L :function_name:filename.js
# Show ancestry graph
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Git Show
Examining Commits
# Show latest commit
git show
# Show specific commit
git show abc123
git show HEAD~1
git show main~2
# Show commit with statistics
git show --stat
git show --name-only
git show --name-status
# Show specific file from commit
git show abc123:path/to/file.txt
git show HEAD~1:filename.js
Show Object Types
# Show tag information
git show v1.0.0
git show --pretty=fuller v1.0.0
# Show tree objects
git show HEAD^{tree}
git show abc123^{tree}
# Show blob objects
git show abc123:filename.txt
# Show object type and size
git show --format=fuller abc123
Git Diff
Working Directory Comparisons
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
git diff --cached # Same as --staged
# Show all changes (staged + unstaged)
git diff HEAD
# Compare specific files
git diff filename.txt
git diff --staged filename.txt
git diff HEAD filename.txt
Commit Comparisons
# Compare commits
git diff abc123 def456
git diff HEAD~1 HEAD
git diff main feature-branch
# Compare with ancestor
git diff HEAD~3..HEAD
git diff main...feature-branch # Since common ancestor
# Compare branches
git diff main..feature-branch
git diff feature-branch..main
# Compare tags
git diff v1.0.0..v1.1.0
Advanced Diff Options
# Word-level diff
git diff --word-diff
git diff --word-diff=color
git diff --word-diff-regex="[^[:space:]]"
# Character-level diff
git diff --color-words
# Ignore whitespace
git diff --ignore-space-change
git diff --ignore-all-space
git diff --ignore-blank-lines
# Context lines
git diff -U10 # Show 10 context lines
git diff --unified=5
# Show function context
git diff -p
git diff --show-function-line="^class\|^def"
Diff Output Formats
# Different output formats
git diff --name-only # File names only
git diff --name-status # File names with status
git diff --stat # Statistics summary
git diff --shortstat # Short statistics
git diff --numstat # Numeric statistics
# Raw format
git diff --raw
git diff --no-color
# Patch format
git diff --patch
git format-patch -1 HEAD # Create patch file
Git Blame
Line-by-Line History
# Basic blame
git blame filename.txt
# Blame with line numbers
git blame -L 10,20 filename.txt
git blame -L 10,+5 filename.txt # 5 lines starting from 10
# Blame specific commit
git blame abc123 -- filename.txt
git blame HEAD~3 -- filename.txt
# Follow file history through renames
git blame --follow filename.txt
Blame Options
# Show original line numbers
git blame -n filename.txt
# Ignore whitespace changes
git blame -w filename.txt
# Show commit email
git blame -e filename.txt
# Show commit hash only
git blame -s filename.txt
# Show relative dates
git blame --date=relative filename.txt
git blame --date=short filename.txt
# Blame with more context
git blame -C filename.txt # Detect copies
git blame -M filename.txt # Detect moves
Searching and Grep
Git Grep
# Search in files
git grep "pattern"
git grep "function_name"
# Search with line numbers
git grep -n "pattern"
# Search with context
git grep -A 3 -B 3 "pattern" # 3 lines after and before
git grep -C 2 "pattern" # 2 lines context
# Case insensitive search
git grep -i "pattern"
# Word boundaries
git grep -w "word"
# Extended regex
git grep -E "pattern1|pattern2"
git grep -P "perl_regex" # Perl-compatible regex
Advanced Grep
# Search in specific file types
git grep "pattern" -- "*.js"
git grep "pattern" -- "*.py" "*.js"
# Search in specific directories
git grep "pattern" -- src/
git grep "pattern" -- src/ tests/
# Search in specific commits
git grep "pattern" abc123
git grep "pattern" HEAD~5
# Search with file names only
git grep -l "pattern"
# Search and show function context
git grep -p "pattern" -- "*.c"
# Count matches
git grep -c "pattern"
# Invert match (files not containing pattern)
git grep --files-without-match "pattern"
Log Analysis
Statistics and Summaries
# Contribution statistics
git shortlog
git shortlog -s # Summary only
git shortlog -n # Sort by number of commits
git shortlog -e # Include email addresses
# Activity by author
git log --author="John" --oneline | wc -l
git log --format='%aN' | sort | uniq -c | sort -rn
# Files changed over time
git log --name-only --pretty=format: | sort | uniq -c | sort -rn
# Lines of code by author
git log --author="John" --pretty=tformat: --numstat | awk '{add+=$1; subs+=$2; loc+=$1-$2} END {printf "Added lines: %s Removed lines: %s Total lines: %s\n", add, subs, loc}'
Time-based Analysis
# Commits by date
git log --since="2023-01-01" --until="2023-12-31" --oneline | wc -l
# Activity by day of week
git log --date=format:'%A' --pretty=format:'%ad' | sort | uniq -c
# Activity by month
git log --date=format:'%Y-%m' --pretty=format:'%ad' | sort | uniq -c
# Busiest files
git log --name-only --pretty=format: | sort | uniq -c | sort -rn | head -10
Advanced History Inspection
Finding Specific Changes
# Find when a line was added/removed
git log -S "specific_text" --source --all
git log -G "regex_pattern" --patch
# Find when a file was added
git log --follow --find-renames --diff-filter=A -- filename.txt
# Find when a file was deleted
git log --follow --find-renames --diff-filter=D -- filename.txt
# Find commits that modified a function
git log -L :function_name:filename.js
git log -p -S "function_name"
Binary Search (Bisect) Preparation
# Find commit range for bisecting
git log --oneline --since="2023-01-01" --until="2023-06-01"
# Identify potential problematic commits
git log --grep="performance" --since="1 month ago"
git log --author="developer" --since="2 weeks ago"
# Check commits that touched specific files
git log --oneline -- critical-file.js
Repository Analysis
# Largest files in history
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sed -n 's/^blob //p' | sort --numeric-sort --key=2 | tail -10
# Most active files
git log --name-only --pretty=format: | sort | uniq -c | sort -rn | head -20
# Repository size analysis
git count-objects -vH
# Check for large files
git rev-list --all --objects | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
Visual History Tools
Built-in Visualization
# Text-based graph
git log --graph --oneline --all
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
# Show merge tree
git show-branch
git show-branch --all
git show-branch main feature-1 feature-2
External Tools
# Launch gitk (if available)
gitk
gitk --all
gitk filename.txt # History of specific file
# Other GUI tools
git gui
git instaweb # Web interface
Best Practices
Efficient History Navigation
# Use aliases for common patterns
git config --global alias.hist 'log --oneline --graph --decorate --all'
git config --global alias.last 'log -1 HEAD --stat'
git config --global alias.unstage 'reset HEAD --'
# Search efficiently
git grep -n "TODO" # Find all TODOs
git log --grep="fix" --since="1 week ago" # Recent fixes
# Use ranges for focused analysis
git log main..feature-branch --oneline # What's new in feature
git diff main...feature-branch --stat # Summary of changes
Performance Tips
# Limit output for large repositories
git log --oneline -100 # Last 100 commits only
git log --since="1 month ago" # Recent history only
# Use specific paths to narrow search
git log -- src/ # Only commits affecting src/
git grep "pattern" -- "*.js" # Only search JS files
# Shallow clone for faster inspection
git clone --depth=50 https://github.com/user/repo.git
Master history inspection before diving into Undoing Changes and Advanced Git.