How to View Git Commit History
Understanding your project's commit history is essential for tracking changes, debugging issues, and collaborating effectively with your team. Git provides rich tools for exploring and analyzing commit history, from simple chronological views to complex filtering and searching capabilities.
In this guide, you'll learn how to effectively view and analyze Git commit history using various git log
options and filtering techniques.
Prerequisites
You need Git installed on your system and access to a Git repository with some commit history. Basic familiarity with Git commits and branches will help you understand the different viewing options available.
Basic Commit History Viewing
Standard Git Log
The simplest way to view commit history is using the basic git log
command:
# View complete commit history
git log
# View commits with abbreviated information
git log --oneline
# View last 5 commits
git log -5
# View commits with statistics
git log --stat
The standard git log
output includes:
- Commit hash (SHA-1)
- Author name and email
- Date and time
- Commit message
One-Line Format
For a concise overview of your commit history:
# Compact one-line per commit
git log --oneline
# One-line with decorations (branch/tag info)
git log --oneline --decorate
# One-line for specific number of commits
git log --oneline -10
The --oneline
format shows abbreviated commit hashes and the first line of each commit message, making it easy to scan through many commits quickly.
Visual History Representation
Graph View
To see branch relationships and merge patterns:
# ASCII graph of commit history
git log --graph
# Graph with one-line format
git log --graph --oneline
# Graph showing all branches
git log --graph --oneline --all
# Detailed graph with decorations
git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit
The graph view helps you understand how branches were created, merged, and how commits relate to each other in the project timeline.
Pretty Formatting
Customize the log output format:
# Custom format showing hash, author, and message
git log --pretty=format:"%h - %an, %ar : %s"
# Show commit hash, date, author, and subject
git log --pretty=format:"%h %ad %an %s" --date=short
# Full formatting with colors
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(bold blue)%an%C(reset), %C(green)%cr%C(reset) : %s"
Common format placeholders:
%h
: Abbreviated commit hash%H
: Full commit hash%an
: Author name%ae
: Author email%ad
: Author date%ar
: Author date, relative%s
: Subject (commit message)
Filtering Commit History
By Date Range
View commits within specific time periods:
# Commits from last week
git log --since="1 week ago"
# Commits from specific date
git log --since="2024-01-01"
# Commits between dates
git log --since="2024-01-01" --until="2024-01-31"
# Commits from last 2 weeks
git log --since="2 weeks ago" --oneline
# Commits before specific date
git log --until="2024-12-01"
By Author
Filter commits by specific authors:
# Commits by specific author
git log --author="John Doe"
# Multiple authors (regex)
git log --author="John\|Jane"
# Case-insensitive author search
git log --author="john" -i
# Commits by email
git log --author="[email protected]"
By Commit Message
Search commit messages for specific content:
# Commits containing specific text
git log --grep="bug fix"
# Case-insensitive search
git log --grep="feature" -i
# Multiple search terms
git log --grep="fix" --grep="bug" --all-match
# Commits containing text in message or files
git log -S "function_name"
By File or Directory
View history for specific files or directories:
# History for a specific file
git log -- filename.js
# History for multiple files
git log -- file1.js file2.css
# History for a directory
git log -- src/components/
# Follow file renames
git log --follow -- filename.js
# Show patches for file changes
git log -p -- filename.js
Advanced History Analysis
Diff and Patch Information
See what changed in each commit:
# Show patches (diffs) for each commit
git log -p
# Show patches for specific file
git log -p -- filename.js
# Show stat information (files changed, insertions, deletions)
git log --stat
# Show short stat
git log --shortstat
# Show name status only
git log --name-status
Merge Commit Handling
Control how merge commits are displayed:
# Show merge commits
git log --merges
# Hide merge commits
git log --no-merges
# Show first parent only (cleaner history)
git log --first-parent
# Show merge commits with their parents
git log --graph --merges
Branch-Specific History
View history for specific branches:
# Commits on current branch only
git log HEAD
# Commits on specific branch
git log branch-name
# Commits on branch but not on main
git log main..feature-branch
# Commits on main but not on feature branch
git log feature-branch..main
# Commits that exist on either branch but not both
git log --left-right main...feature-branch
Searching Code Changes
Content-Based Searching
Find commits that introduced or removed specific code:
# Find commits that added or removed specific text
git log -S "function_name"
# Find commits with regex pattern changes
git log -G "regex_pattern"
# Find when a line was added, modified, or removed
git log -L 10,20:filename.js
# Follow function changes
git log -L :function_name:filename.js
File Movement and Renames
Track files across renames and moves:
# Follow file history across renames
git log --follow -- current-filename.js
# Show what happened to a file
git log --name-status --follow -- filename.js
# Find when files were moved or renamed
git log --diff-filter=R --summary
Useful Log Combinations
Recent Activity Summary
Get an overview of recent project activity:
# Recent commits with authors and dates
git log --oneline --since="1 week ago" --author-date-order
# Activity by team members
git shortlog --since="1 week ago" --numbered --summary
# Changes to specific areas
git log --oneline --since="1 month ago" -- src/
Release History
Track releases and major changes:
# Commits between releases (assuming tags)
git log v1.0.0..v1.1.0 --oneline
# All release tags
git tag -l
# Commits since last tag
git log $(git describe --tags --abbrev=0)..HEAD --oneline
Debugging History
Find commits that might have introduced issues:
# Recent commits to a problematic file
git log -5 --oneline -- problematic-file.js
# Changes around a specific date when bug appeared
git log --since="2024-11-15" --until="2024-11-20" --oneline
# Who last modified specific lines
git blame filename.js
# See changes in context
git log -p --follow -- filename.js | head -50
Creating Aliases for Common Patterns
Set up shortcuts for frequently used log commands:
# Create useful aliases
git config --global alias.lg "log --graph --oneline --decorate --all"
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
git config --global alias.recent "log --oneline -10"
# Use your aliases
git lg
git hist
git recent
Exporting and Sharing History
Generating Reports
Create reports of commit history:
# Generate changelog between versions
git log v1.0.0..HEAD --oneline --no-merges > CHANGELOG.txt
# Author statistics
git shortlog -sn --since="1 month ago"
# Weekly summary
git log --since="1 week ago" --pretty=format:"%h %s (%an)" --no-merges
Comparing Branches
Analyze differences between branches:
# Commits unique to feature branch
git log main..feature-branch --oneline
# Visual comparison of two branches
git log --graph --oneline main feature-branch
# Detailed comparison
git log --left-right --graph --cherry-pick --oneline main...feature-branch
Now you have comprehensive knowledge of viewing and analyzing Git commit history. These techniques will help you understand your project's evolution, track down issues, and collaborate more effectively with your team by providing insights into how your codebase has changed over time.
Found an issue?