Viewing Commit History
Git keeps a complete record of every change ever made to your project. Learning to navigate and explore this history is essential for understanding your codebase and tracking down issues.
The git log Command
The primary tool for viewing history is git log:
git log
Default output:
commit a1b2c3d4e5f6789012345678901234567890abcd (HEAD -> main)
Author: Jane Doe <jane@example.com>
Date: Mon Jan 15 10:30:00 2025 -0500
Add user authentication
commit 9876543210fedcba9876543210fedcba98765432
Author: John Smith <john@example.com>
Date: Sun Jan 14 15:45:00 2025 -0500
Initial commit
Log Formatting Options
One-Line Format
git log --oneline
Output:
a1b2c3d Add user authentication
9876543 Initial commit
Graph View
git log --oneline --graph
Output:
* a1b2c3d (HEAD -> main) Merge feature branch
|\
| * b2c3d4e Add new feature
| * c3d4e5f Start feature work
|/
* 9876543 Initial commit
Include All Branches
git log --oneline --graph --all
Custom Format
git log --pretty=format:"%h %an %ar - %s"
Output:
a1b2c3d Jane Doe 2 hours ago - Add user authentication
9876543 John Smith 1 day ago - Initial commit
Format Placeholders
| Placeholder | Description |
|---|---|
%H | Full commit hash |
%h | Abbreviated hash |
%an | Author name |
%ae | Author email |
%ar | Author date, relative |
%ad | Author date |
%s | Subject (first line) |
%b | Body |
Limiting Log Output
By Number
git log -5 # Last 5 commits
git log -n 10 # Last 10 commits
By Date
git log --since="2025-01-01"
git log --after="2 weeks ago"
git log --until="2025-01-15"
git log --before="yesterday"
By Author
git log --author="Jane"
git log --author="jane@example.com"
By Message Content
git log --grep="bug"
git log --grep="fix" --grep="auth" --all-match # Both terms
By File Changes
git log -- path/to/file.js
git log -- src/
By Content Changes
# Find commits that changed a specific string
git log -S "functionName"
# Find commits matching a regex
git log -G "function.*validate"
Viewing Commit Details
git show
View the full details of a specific commit:
# Show latest commit
git show
# Show specific commit
git show a1b2c3d
# Show just the message
git show -s a1b2c3d
# Show stats only
git show --stat a1b2c3d
# Show specific file from a commit
git show a1b2c3d:path/to/file.js
git diff Between Commits
# Compare two commits
git diff a1b2c3d b2c3d4e
# Compare commit with current state
git diff a1b2c3d
# Show only file names
git diff --name-only a1b2c3d b2c3d4e
# Show stats
git diff --stat a1b2c3d b2c3d4e
Navigating History
HEAD and References
HEAD - Current commit
HEAD~1 - One commit before HEAD
HEAD~2 - Two commits before HEAD
HEAD^ - Parent of HEAD (same as HEAD~1)
HEAD^^ - Grandparent of HEAD
main - Latest commit on main branch
main~3 - Three commits before main
Examples
# Show the previous commit
git show HEAD~1
# Diff between 3 commits ago and now
git diff HEAD~3 HEAD
# Log from specific point
git log HEAD~5..HEAD
The git shortlog Command
Summarize commits by author:
git shortlog
Output:
Jane Doe (5):
Add user authentication
Fix login bug
Add password reset
Update auth tests
Refactor auth module
John Smith (3):
Initial commit
Add README
Configure CI
Count by Author
git shortlog -sn
Output:
5 Jane Doe
3 John Smith
Blame: Finding Who Changed What
The git blame command shows who last modified each line:
git blame path/to/file.js
Output:
a1b2c3d4 (Jane Doe 2025-01-15 10:30:00 -0500 1) function login(user) {
9876543a (John Smith 2025-01-14 15:45:00 -0500 2) validateInput(user);
a1b2c3d4 (Jane Doe 2025-01-15 10:30:00 -0500 3) return authenticate(user);
9876543a (John Smith 2025-01-14 15:45:00 -0500 4) }
Blame Options
# Specific line range
git blame -L 10,20 file.js
# Show email instead of name
git blame -e file.js
# Ignore whitespace changes
git blame -w file.js
Finding Bugs with git bisect
Binary search through history to find when a bug was introduced:
# Start bisecting
git bisect start
# Mark current version as bad
git bisect bad
# Mark known good version
git bisect good v1.0.0
# Git checks out a middle commit
# Test and mark as good or bad
git bisect good # or: git bisect bad
# Repeat until found
# Git tells you: "abc123 is the first bad commit"
# End bisecting
git bisect reset
Exercise: Explore History
Practice viewing and navigating commit history:
Creating Useful Aliases
Set up aliases for common log formats:
# Pretty log with graph
git config --global alias.lg "log --oneline --graph --decorate"
# Detailed log
git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Creset %s %C(cyan)(%ar)%Creset %C(blue)<%an>%Creset'"
# Log with files changed
git config --global alias.lf "log --oneline --stat"
Now use them:
git lg
git ll
git lf
Visual Tools
Many GUI tools make history exploration easier:
- gitk: Built-in visual history browser
- git log --graph: ASCII art in terminal
- GitHub/GitLab: Web interfaces
- VS Code GitLens: Extension with inline blame
- Fork, GitKraken, Sourcetree: Desktop GUI clients
Summary
- Use
git logwith various options to explore history --oneline,--graph, and--allare commonly combined- Filter by date, author, message content, or file changes
- Use
git showto view specific commits - Use
git blameto see who changed each line - Use
git bisectto binary search for bugs - Create aliases for your favorite log formats
In the next lesson, we'll learn how to undo changes and fix mistakes.

