Viewing History
Understanding your project's history is crucial for debugging, code review, and collaboration. In this lesson, you'll master the powerful commands Git provides for exploring commits, comparing changes, and navigating through your project's evolution.
Git Log Basics
The git log command is your window into the project's history:
# Basic log (shows full commit details)
git log
# One line per commit (easier to scan)
git log --oneline
# Show last 5 commits
git log -5
# Show commits with file changes
git log --stat
# Show commits with actual code changes
git log -p
Key Fact: By default, git log shows commits in reverse chronological order (newest first). Press q to exit the log viewer, Space to scroll down, and b to scroll up.
Formatting Log Output
Customize how commit information is displayed:
# Graphical representation of branches
git log --graph --oneline --all
# Show with dates
git log --pretty=format:"%h - %an, %ar : %s"
# Detailed format with colors
git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit
# Show references (branches, tags)
git log --oneline --decorate
# Show all branches
git log --oneline --all
Filtering Log Output
Find specific commits based on various criteria:
# By author
git log --author="John Smith"
# By date range
git log --since="2024-01-01"
git log --after="2 weeks ago"
git log --before="2024-12-31"
git log --until="yesterday"
# By commit message
git log --grep="bug fix"
git log --grep="feature" --grep="update" --all-match
# Commits that modified a specific file
git log -- filename.txt
git log --follow -- renamed-file.txt # Track through renames
# Commits in date range
git log --since="2024-01-01" --until="2024-12-31"
Tip: Combine filters for powerful searches! For example: git log --author="Jane" --since="1 month ago" --grep="bug" shows all bug-related commits by Jane in the last month.
Git Show Command
Examine specific commits in detail:
# Show last commit (HEAD)
git show
# Show specific commit
git show 2f8a7e9
# Show specific commit for a file
git show 2f8a7e9:path/to/file.txt
# Show commit message only
git show -s 2f8a7e9
# Show changes for a specific file in a commit
git show 2f8a7e9 -- filename.txt
# Show tag information
git show v1.0.0
Git Diff Command
Compare different states of your repository:
# Unstaged changes (working directory vs staging)
git diff
# Staged changes (staging vs last commit)
git diff --staged
git diff --cached # Same as --staged
# All changes (working directory vs last commit)
git diff HEAD
# Compare two commits
git diff abc123..def456
# Compare branches
git diff main..feature-branch
# Show only file names that changed
git diff --name-only
# Show statistics
git diff --stat
Understanding Diff Output: Lines starting with - (red) are removed, lines with + (green) are added. The @@ markers show line numbers where changes occurred.
Comparing Branches and Commits
Understand differences between branches:
# Commits in feature-branch not in main
git log main..feature-branch
# Commits in main not in feature-branch
git log feature-branch..main
# All commits that differ between branches
git log main...feature-branch
# Show difference between branches with files
git diff main..feature-branch --name-only
# Compare specific files between branches
git diff main..feature-branch -- path/to/file.txt
# Show commits unique to current branch
git log --no-merges main..HEAD
Advanced Log Options
Powerful options for specific use cases:
# Show merge commits only
git log --merges
# Exclude merge commits
git log --no-merges
# Show commits that add or remove a specific string
git log -S "function_name"
# Show commits where a function was added or modified
git log -G "function.*calculate"
# Show commits that modified lines in a range
git log -L 10,20:filename.txt
# Show first parent only (linear history)
git log --first-parent
# Reverse order (oldest first)
git log --reverse
Performance Tip: When working with large repositories, use --since or limit the number of commits with -n to speed up log queries.
Visualizing History
Create visual representations of your commit history:
# ASCII graph of branches
git log --graph --oneline --all
# Detailed graph with all information
git log --graph --all --decorate --stat
# Custom format with graph
git log --graph --pretty=format:"%C(yellow)%h%Creset %C(cyan)%ar%Creset %C(green)%an%Creset %s %C(bold red)%d%Creset"
# Create an alias for a beautiful log
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Now use it with:
git lg
Finding Bugs with Log
Use log to track down when bugs were introduced:
# Find when a file was deleted
git log --diff-filter=D -- path/to/file.txt
# Find when a file was added
git log --diff-filter=A -- path/to/file.txt
# Show who changed a specific line (blame)
git blame filename.txt
# Blame with line range
git blame -L 10,20 filename.txt
# Find commits that touched specific code
git log -S "buggy_function" -p
# Show history of a function
git log -L :function_name:filename.js
Pro Tip: Create custom aliases for your most-used log formats. Example: git config --global alias.recent 'log --oneline -10' creates a shortcut to see the last 10 commits with git recent.
Practical Examples
Real-world scenarios for viewing history:
# Scenario 1: Find all commits by a team member this week
git log --author="Alice" --since="1 week ago" --oneline
# Scenario 2: See what changed in the last release
git log v1.0..v1.1 --oneline
# Scenario 3: Find when a bug was introduced
git log --grep="fix" --grep="bug" --since="1 month ago"
# Scenario 4: Review changes before pushing
git log origin/main..HEAD --oneline
# Scenario 5: Find all changes to configuration files
git log --oneline -- "*.config"
# Scenario 6: See commit history for a renamed file
git log --follow -- current-filename.js
Using Git Shortlog
Summarize contributions by author:
# Summary of commits per author
git shortlog
# Just count commits per author
git shortlog -s -n
# Show email addresses
git shortlog -s -e
# For a specific time range
git shortlog --since="1 month ago"
# Exclude merge commits
git shortlog --no-merges
Practice Exercise:
Challenge: Using git log commands, answer these questions about your repository:
- How many commits have been made in the last month?
- Who is the most active contributor (most commits)?
- Find all commits that mention "security" in the message
- What files were changed in the last 3 commits?
- Show a visual graph of the last 10 commits
Commands to use:
# 1. Count recent commits
git log --since="1 month ago" --oneline | wc -l
# 2. Most active contributor
git shortlog -s -n | head -1
# 3. Find security commits
git log --grep="security" --oneline
# 4. Files changed in last 3 commits
git log -3 --name-only --pretty=format:""
# 5. Visual graph
git log --graph --oneline -10
Git Log Best Practices
✓ DO:
- Use --oneline for quick scans
- Combine filters to narrow results
- Use --stat to see files changed
- Create aliases for common log formats
- Use --grep to find specific commits
- Follow renamed files with --follow
✗ DON'T:
- Run git log without limits on huge repos
- Forget to use -q to exit the pager
- Ignore --since/--until for large histories
- Mix up .. and ... in range comparisons
- Forget that log shows newest commits first
Summary
In this lesson, you learned:
- Using
git log with various formatting options
- Filtering commits by author, date, and message
- Using
git show to examine specific commits
- Comparing changes with
git diff
- Finding differences between branches and commits
- Creating visual representations of history
- Using logs for debugging and tracking changes
- Creating custom aliases for efficiency
Next Up: In the next lesson, we'll learn how to undo changes safely using various Git commands for different scenarios!