Git - History & Log

Beginner

Git's history is one of its most powerful features. The git log command lets you explore your project's evolution, find specific changes, and understand how your codebase developed over time.

Basic git log Usage

Default Log Output

git log
Expected Output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 (HEAD -> main, origin/main)
Author: John Doe 
Date:   Wed Oct 25 14:30:22 2023 -0700

    Add user authentication feature
    
    Implemented login and logout functionality with session management.
    Added password hashing and validation.

commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1
Author: Jane Smith 
Date:   Tue Oct 24 09:15:33 2023 -0700

    Update project documentation
    
    Added installation instructions and API reference.

Each commit shows:

  • Commit hash: Unique identifier (SHA-1)
  • Author: Who made the commit
  • Date: When the commit was made
  • Message: Description of changes
  • Refs: Branch and tag information (in parentheses)

One-Line Format

For a concise view of history:

git log --oneline
Expected Output:
a1b2c3d (HEAD -> main, origin/main) Add user authentication feature
b2c3d4e Update project documentation
c3d4e5f Fix bug in login validation
d4e5f6g Add responsive CSS styles
e5f6g7h Initial project setup

This shows just the short hash and first line of each commit message.

Limiting Log Output

Number of Commits

# Show last 5 commits
git log -5

# Show last 10 commits with oneline format
git log --oneline -10

# Same as above
git log -10 --oneline

Date Ranges

# Commits since specific date
git log --since="2023-10-01"
git log --after="2023-10-01"

# Commits until specific date
git log --until="2023-10-31"
git log --before="2023-10-31"

# Commits in date range
git log --since="2023-10-01" --until="2023-10-31"

# Relative dates
git log --since="2 weeks ago"
git log --since="yesterday"
git log --until="1 hour ago"

Filtering by Author

# Commits by specific author
git log --author="John Doe"

# Partial author name (case insensitive)
git log --author="john"

# Multiple authors (regex)
git log --author="John\|Jane"

# Exclude specific author
git log --author="^(?!.*John Doe).*$"

Searching Commit Messages

# Search commit messages
git log --grep="bug fix"

# Case insensitive search
git log --grep="BUG FIX" -i

# Multiple patterns (OR)
git log --grep="bug" --grep="fix"

# Multiple patterns (AND)
git log --grep="bug" --grep="fix" --all-match

Searching Code Changes

# Find commits that added or removed specific text
git log -S"function login"

# Find commits that changed number of occurrences
git log -G"function.*login"

# Show the actual changes
git log -S"function login" -p

File and Path Filtering

# Commits affecting specific file
git log README.md

# Commits affecting multiple files
git log README.md src/main.js

# Commits affecting directory
git log src/

# Follow file through renames
git log --follow README.md

# Show only merge commits
git log --merges

# Exclude merge commits
git log --no-merges

Advanced Formatting

Custom Format Strings

# Custom format
git log --format="%h - %an, %ar : %s"
Example Output:
a1b2c3d - John Doe, 2 hours ago : Add user authentication feature
b2c3d4e - Jane Smith, 1 day ago : Update project documentation
c3d4e5f - John Doe, 3 days ago : Fix bug in login validation

Common Format Placeholders

PlaceholderDescriptionExample
%HFull commit hasha1b2c3d4e5f6...
%hShort commit hasha1b2c3d
%anAuthor nameJohn Doe
%aeAuthor email[email protected]
%adAuthor dateWed Oct 25 14:30:22 2023
%arAuthor date, relative2 hours ago
%sSubject (first line)Add user authentication
%bBody (rest of message)Full commit body

Pretty Formats

# Built-in pretty formats
git log --pretty=short
git log --pretty=full
git log --pretty=fuller
git log --pretty=format:"%h %s (%an)"

Visual History with Graphs

Branch Visualization

# Show branch graph
git log --graph --oneline

# More detailed graph
git log --graph --pretty=format:"%h - %an, %ar : %s"

# Include all branches
git log --graph --oneline --all
Graph Output Example:
* a1b2c3d (HEAD -> main) Add user authentication feature
*   b2c3d4e Merge branch 'feature-login'
|\  
| * c3d4e5f Add login form validation
| * d4e5f6g Create login form component
|/  
* e5f6g7h Update project documentation
* f6g7h8i Initial project setup

Showing File Changes

Statistics and Patches

# Show files changed in each commit
git log --name-only

# Show files with status (added, modified, deleted)
git log --name-status

# Show statistics (lines added/deleted)
git log --stat

# Show actual changes (full diff)
git log -p
git log --patch
Statistics Example:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe 
Date:   Wed Oct 25 14:30:22 2023 -0700

    Add user authentication feature

 src/auth.js    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 src/login.html | 23 +++++++++++++++++++++++
 src/styles.css | 12 ++++++++++++
 3 files changed, 80 insertions(+)

Comparing Branches and Commits

# Commits in feature branch not in main
git log main..feature-branch

# Commits in main not in feature branch
git log feature-branch..main

# Commits in either branch but not both
git log main...feature-branch

# Show commits that will be merged
git log --oneline main..HEAD

Advanced Log Techniques

Finding Lost Commits

# Show all commits including unreachable ones
git log --all --full-history --branches --remotes --tags

# Show reflog (local history)
git reflog

# Find commits with specific content that may be lost
git log --all -S"lost content"

Performance Analysis

# Show commits by activity
git log --all --graph --decorate --oneline --simplify-by-decoration

# Show merge commits only
git log --merges --oneline

# Show first parent only (follow main line of development)
git log --first-parent

Practical Log Examples

Daily Development Workflow

Morning standup - what happened yesterday:
# Your commits from yesterday
git log --author="$(git config user.name)" --since="yesterday" --oneline

# Team activity from last week
git log --since="1 week ago" --pretty=format:"%h - %an: %s" --no-merges

Release Planning

Changes since last release:
# Commits since tag v1.0.0
git log v1.0.0..HEAD --oneline

# Statistics for release notes
git log v1.0.0..HEAD --stat --no-merges

# Contributors for credits
git log v1.0.0..HEAD --pretty=format:"%an" | sort | uniq -c | sort -rn

Bug Investigation

Finding when a bug was introduced:
# Find commits that modified specific function
git log -S"buggyFunction" --oneline

# Show changes to specific file
git log -p -- src/problematic-file.js

# Find commits with "fix" in message
git log --grep="fix" --oneline

Log Configuration

Default Log Format

# Set default log format
git config --global format.pretty "format:%h - %an, %ar : %s"

# Set default to show graph
git config --global log.graph true

# Show dates relative by default
git config --global log.date relative

Useful Aliases

# Create helpful log aliases
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.lgs "log --oneline --graph --decorate --stat"
git config --global alias.lga "log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all"
git config --global alias.lgp "log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit -p"

Now you can use:

git lg      # Beautiful graph view
git lgs     # Graph with statistics
git lga     # Detailed graph all branches
git lgp     # Graph with patches

Understanding Git History

Linear vs Non-Linear History

Linear history (rebased):
* a1b2c3d Add feature C
* b2c3d4e Add feature B  
* c3d4e5f Add feature A
* d4e5f6g Initial commit
Non-linear history (merged):
*   a1b2c3d Merge branch 'feature-c'
|\  
| * b2c3d4e Add feature C
|/  
*   c3d4e5f Merge branch 'feature-b'
|\  
| * d4e5f6g Add feature B
|/  
* e5f6g7h Add feature A
* f6g7h8i Initial commit

History Traversal

Git history is a Directed Acyclic Graph (DAG):

  • Each commit points to its parent(s)
  • Merge commits have multiple parents
  • You can traverse in any direction
  • History can be filtered and formatted in many ways

Troubleshooting History Issues

Performance with Large Repositories

# Limit history depth
git log --max-count=100

# Skip expensive operations
git log --no-merges --simplify-by-decoration

# Focus on specific paths
git log -- src/ docs/

Finding Specific Changes

# When was file last modified?
git log -1 --format="%ad" -- filename.txt

# Who last modified this line?
git blame filename.txt

# What commit introduced this bug?
git bisect start
git bisect bad HEAD
git bisect good v1.0.0

Summary

You now understand Git history and log:

  • git log shows commit history in various formats
  • ✅ Filtering by date, author, message, and content
  • ✅ Visual graphs help understand branch relationships
  • ✅ Custom formatting creates readable output
  • ✅ File-specific history tracks changes over time
  • ✅ Advanced techniques help with debugging and analysis
  • ✅ Aliases make complex commands more accessible

Next Steps

Now that you can explore history, let's learn about branches and how to manage parallel development:

Git - Branching Basics

Practice Tip: Explore the history of open source projects you use. Try different log formats and filters to understand how large projects evolve over time.