log Git Command Guide
The git log command displays the commit logs for a repository, showing commit information in reverse chronological order. It provides comprehensive history browsing with formatting options, filtering capabilities, and detailed commit information display.
git log Syntax:
Section titled “git log Syntax:”git log [<options>] [<revision-range>] [[--] <path>...]Display Options:
Section titled “Display Options:”| Option | Description |
|---|---|
--oneline | Condensed format: abbreviated hash and title |
--short | Shorter format than default |
--full | Full format (default) |
--fuller | Even more information |
--raw | Show raw commit information |
--graph | Display ASCII graph of branch structure |
| `—decorate[=short | full |
--stat | Show diff statistics |
--numstat | Show numeric diff statistics |
--shortstat | Show summary of changes |
--name-only | Show only names of changed files |
--name-status | Show names and status of changed files |
Limiting Options:
Section titled “Limiting Options:”| Option | Description |
|---|---|
-<n> | Show only last n commits |
--since=<date> | Show commits more recent than date |
--until=<date> | Show commits older than date |
--author=<pattern> | Show commits by authors matching pattern |
--committer=<pattern> | Show commits by committers matching pattern |
--grep=<pattern> | Show commits with messages matching pattern |
--all-match | Show only commits matching all —grep |
--invert-grep | Show commits not matching —grep |
-S<string> | Show commits changing the number of occurrences |
-G<regex> | Show commits whose diff matches regex |
--follow | Follow file renames |
--no-merges | Don’t show merge commits |
--merges | Show only merge commits |
--first-parent | Follow only first parent of merges |
--not | Reverse the meaning of ^ prefix |
--all | Show all refs |
Formatting Options:
Section titled “Formatting Options:”| Option | Description |
|---|---|
--pretty=<format> | Pretty-print with format |
--format=<format> | Alias for —pretty=format |
--abbrev-commit | Show abbreviated commit hash |
--no-abbrev-commit | Don’t abbreviate hash |
--oneline | Shortcut for —pretty=oneline —abbrev-commit |
--encoding=<encoding> | Re-encode commits to encoding |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<revision-range> | Range of commits to show (e.g., master..feature) |
<path>... | Show only commits affecting these paths |
Basic Usage Examples:
Section titled “Basic Usage Examples:”View Recent Commits:
Section titled “View Recent Commits:”# Show recent commits in default formatgit log
# Show last 5 commitsgit log -5
# Show commits in compact formatgit log --onelineGraph Visualization:
Section titled “Graph Visualization:”# Show branch structure with graphgit log --oneline --graph --all
# View merge historygit log --graph --oneline --first-parentTime-Based Filtering:
Section titled “Time-Based Filtering:”# Commits from last weekgit log --since="1 week ago"
# Commits from specific date rangegit log --since="2023-01-01" --until="2023-12-31"
# Commits by date with authorgit log --since="2 months ago" --author="John"Content Filtering:
Section titled “Content Filtering:”# Commits mentioning specific textgit log --grep="fix"
# Case-insensitive searchgit log --grep="FIX" -i
# Commits by author patterngit log --author=".*@company.com"Path-Specific History:
Section titled “Path-Specific History:”# Commits affecting specific filegit log --follow -- src/main.c
# Commits in directorygit log -- src/
# Multiple filesgit log -- README.md CHANGELOG.mdAdvanced Formatting:
Section titled “Advanced Formatting:”Custom Pretty Formats:
Section titled “Custom Pretty Formats:”# Custom formatgit log --pretty=format:"%h %s %d"
# Full custom formatgit log --pretty=format:'%C(yellow)%h%Creset %Cgreen%ad%Creset %s %Cblue%an%Creset %Cred%d%Creset' --date=short
# Include trailersgit log --pretty=format:"%h %s%n%(trailers:unfold,key=Signed-off-by,valueonly)"Differential Statistics:
Section titled “Differential Statistics:”# Show file changes summarygit log --stat
# Show numerical statistics onlygit log --numstat
# Overall statisticsgit log --shortstatBranch and Tag Analysis:
Section titled “Branch and Tag Analysis:”# Show all referencesgit log --oneline --all --graph
# Compare branchesgit log --oneline master..feature-branch
# Find branching pointgit log --oneline --merges masterSpecialized Queries:
Section titled “Specialized Queries:”Author and Committer Analysis:
Section titled “Author and Committer Analysis:”# Detailed author informationgit log --format='%aN <%aE>' | sort | uniq -c | sort -rn
# Committer vs author differencesgit log --format='%an vs %cn' --all-match --grep="Co-authored-by"
# Recent contributorsgit log --since="6 months ago" --format='%aN' | sort | uniqMessage Pattern Analysis:
Section titled “Message Pattern Analysis:”# Commits starting with certain patternsgit log --grep="^feat\|fix\|docs\|style\|refactor\|test\|chore" --oneline
# Breaking changesgit log --grep="BREAKING\|breaking" --oneline
# Issue tracker linksgit log --grep="#[0-9]\+" --onelineChange Type Analysis:
Section titled “Change Type Analysis:”# Only merge commitsgit log --merges --oneline
# Exclude mergesgit log --no-merges --oneline
# First parent path (main branch development)git log --first-parent --onelinePractical Workflow Examples:
Section titled “Practical Workflow Examples:”Code Review Preparation:
Section titled “Code Review Preparation:”# See changes for reviewgit log --oneline --stat origin/main..HEAD
# Detailed diff for reviewergit log -p --reverse origin/main..HEAD
# Summary for release notesgit log --oneline --grep="feat\|fix\|docs" origin/v1.0..HEADBug Investigation:
Section titled “Bug Investigation:”# Find commits affecting file around dategit log --since="2023-01-01" --until="2023-01-31" -- src/file.c
# Find commits changing specific stringgit log -S"bug" --oneline
# Find commit introducing regex patterngit log -G"TODO|FIXME" --onelineRelease Management:
Section titled “Release Management:”# Generate changeloggit log --oneline --format="* %s (%h)" v1.0..HEAD
# Version bump commits onlygit log --grep="bump\|version\|release" --oneline
# Feature additions since releasegit log --grep="^feat" --oneline tag/v1.0.0..HEADRepository Analysis:
Section titled “Repository Analysis:”# Most active contributorsgit log --format='%aN' | sort | uniq -c | sort -nr | head -10
# Commits by weekgit log --date=short --format='%ad' | cut -d- -f1-2 | sort | uniq -c
# Productivity metrics (commits per day)git log --date=short --format='%ad' | sort | uniq -c | awk '{print $2 ": " $1 " commits"}'Performance Optimization:
Section titled “Performance Optimization:”Large Repository Handling:
Section titled “Large Repository Handling:”# Limit depth for speedgit log --oneline -100
# Specific time range for analysisgit log --since="3 months ago" --oneline
# Path-limited searchgit log -- src/lib/ --onelinePipeline Optimization:
Section titled “Pipeline Optimization:”# Efficient author searchinggit log --format='%H %aE %aN' --all | awk '/@company.com/ {print $1}' | xargs git show --stat
# Batch processing commitsgit log --format='%H' --oneline | head -50 | xargs -I {} sh -c 'git show --name-only {} | head -20'Integration Examples:
Section titled “Integration Examples:”With External Tools:
Section titled “With External Tools:”# Export for project managementgit log --format='%H|%an|%ae|%s|%(trailers:key=Fixes,valueonly)' > commits.csv
# Feed to ticket systemgit log --grep="#[0-9]\+" --format='%H %s' | while read hash msg; do ticket=$(echo "$msg" | grep -o '#[0-9]\+') echo "Commit $hash affects ticket $ticket"doneCI/CD Integration:
Section titled “CI/CD Integration:”#!/bin/bash# Generate deployment notesgit log --oneline --format="* %s (%aN)" origin/main..HEAD > deploy-notes.txt
# Validate commit standardsif ! git log --format='%s' -1 | grep -q "^(feat|fix|docs|style|refactor|test|chore):"; then echo "Commit message doesn't follow conventional commits" exit 1fiFormatting and Display Customization:
Section titled “Formatting and Display Customization:”Color Customization:
Section titled “Color Customization:”# Colored outputgit log --color --oneline
# Configure colorsgit config color.log.date yellowgit config color.log.author blueTerminal Pagination:
Section titled “Terminal Pagination:”# Configure pagergit config core.pager "less -FRSX"git config pager.log "true"
# Disable pagination for scriptsgit --no-pager log --oneline | head -10Troubleshooting:
Section titled “Troubleshooting:”Empty Log Output:
Section titled “Empty Log Output:”# Check if in repositorygit status
# Check current branch commitsgit log --oneline HEAD
# See all branchesgit log --all --oneline | head -10Performance Issues:
Section titled “Performance Issues:”# Time-consuming operationstime git log --all --graph
# Optimize with cachinggit config core.logAllRefUpdates trueEncoding Problems:
Section titled “Encoding Problems:”# Fix character encodinggit log --encoding=UTF-8
# Configure repository encodinggit config i18n.logOutputEncoding UTF-8Path Resolution Issues:
Section titled “Path Resolution Issues:”# Verify file exists in historygit log --follow -- missing-file.txt 2>/dev/null || echo "File not in history"
# Check case sensitivitygit log --follow -- 'path/file.txt'How does git log —graph work?
Section titled “How does git log —graph work?”Creates ASCII visualization of commit ancestry, showing branches, merges, and commit relationships through text characters connecting related commits.
What’s the difference between —stat and —shortstat?
Section titled “What’s the difference between —stat and —shortstat?”—stat shows per-file statistics with +/- counts; —shortstat shows only repository-level summary (files changed, insertions, deletions).
Why use —first-parent?
Section titled “Why use —first-parent?”Follows only first parent of merges, showing mainline development while hiding merged feature branches - useful for understanding deployment timeline.
How do trailers work with git log?
Section titled “How do trailers work with git log?”Use %(trailers) format specifier to display or process commit message trailers separately from main commit message.
What’s the performance cost of —follow?
Section titled “What’s the performance cost of —follow?”Follows file renames across entire repository history - expensive for large repos, suitable for single file analysis only.
Can git log show binary file changes?
Section titled “Can git log show binary file changes?”Shows file was modified but doesn’t display binary diff. Use —name-only to see which binaries changed without content display.
How does —reverse work?
Section titled “How does —reverse work?”Reverses normal chronological order, showing oldest commits first - useful for recreating development timeline or chronological analysis.
What’s the difference between —author and —committer?
Section titled “What’s the difference between —author and —committer?”Author is who wrote the code, committer is who created the commit. Different in patch workflows where someone applies another’s changes.
Can git log filter by file type?
Section titled “Can git log filter by file type?”Yes, combine with path patterns: git log — ‘.c’ ‘.h’ for only C source files, or use —name-only with grep for post-filtering.
How do I see only commits changing specific lines?
Section titled “How do I see only commits changing specific lines?”Use git log -L
What’s the —ancestry-path option?
Section titled “What’s the —ancestry-path option?”Shows commits reachable from
How do reflog vs log differ?
Section titled “How do reflog vs log differ?”git log shows committed history; git reflog shows local reference changes including resets, rebases, amendments - broader than permanent history.
Applications of the git log command
Section titled “Applications of the git log command”- Development Monitoring: Track code evolution and contributor activity over time
- Bug Investigation: Trace when bugs were introduced and through which commit paths
- Code Review Facilitation: Prepare comprehensive change summaries for review teams
- Release Preparation: Generate changelogs and validate release candidate contents
- Team Collaboration: Share repository history and coordinate development activities
- Compliance Auditing: Verify change history for regulatory and quality requirements