Lesson 11: Viewing History

What You'll Learn

In this lesson, you'll learn how to explore your project's history using git log and git diff. These tools help you understand what changed, when, why, and by whom.

Why View History?

Viewing history helps you:

  • Find when bugs were introduced - Track down problematic changes
  • Understand code evolution - See why code looks the way it does
  • Learn from past decisions - Review previous solutions
  • Write better commit messages - Learn from examples
  • Review contributions - See who worked on what

Part 1: Basic git log

View All Commits

git log

Output shows:

commit abc1234567890def (HEAD -> main, origin/main)
Author: Your Name <your.email@example.com>
Date:   Mon Jan 6 10:30:00 2026 +0200

    Add user authentication feature

commit def9876543210abc
Author: Your Name <your.email@example.com>
Date:   Sun Jan 5 15:20:00 2026 +0200

    Update README with setup instructions

Understanding the Output

Part Meaning
commit hash Unique ID for this commit
HEAD -> main You're currently on this commit/branch
origin/main GitHub is at this commit
Author Who made the commit
Date When the commit was made

Part 2: Useful git log Options

Compact One-Line View

git log --oneline

Output:

abc1234 Add user authentication feature
def9876 Update README with setup instructions
ghi5432 Fix navigation bug
jkl1098 Initial commit

Show Last N Commits

git log -5  # Last 5 commits
git log -3  # Last 3 commits

Visual Branch Graph

git log --oneline --graph --all

Output:

* abc1234 (HEAD -> main) Merge branch 'feature/auth'
|\
| * def9876 Add login form
| * ghi5432 Add authentication logic
|/
* jkl1098 Update README

Show Specific Date Range

# Since specific date
git log --since="2026-01-01"

# Last week
git log --since="1 week ago"

# Between dates
git log --since="2026-01-01" --until="2026-01-07"

Show Commits by Author

git log --author="John"

Search Commit Messages

git log --grep="bug fix"

Part 3: Viewing Commit Details

Show Specific Commit

git show abc1234

This shows:

  • Commit information (author, date, message)
  • Full diff of what changed
  • Which files were modified

Show Latest Commit

git show HEAD

Show Files Changed in Commit

git show --name-only abc1234

Show Stats

git show --stat abc1234

Output:

index.html  |  15 +++++++++++++++
style.css   |   8 ++++++++
2 files changed, 23 insertions(+)

Part 4: Using git diff

What is git diff?

Shows differences between versions of files.

See Unstaged Changes

git diff

Shows changes in your working directory that haven't been staged.

See Staged Changes

git diff --staged

Shows changes that are staged for commit.

Compare Two Commits

git diff abc1234 def5678

Compare Branches

git diff main feature/new-feature

Show Changes in Specific File

git diff index.html

Understanding Diff Output

Symbol Meaning
+ Line was added
- Line was removed
@@ Line numbers

Part 5: Finding Who Changed What

git blame

Shows who last modified each line of a file:

git blame index.html

Output shows:

abc1234 (John Doe 2026-01-05) <h1>Welcome</h1>
def5678 (Jane Smith 2026-01-06) <p>Hello world</p>

Show Blame for Specific Lines

git blame -L 10,20 index.html

Why Use git blame?

  • Find who to ask about code
  • Understand when changes were made
  • Track down when bugs were introduced

Part 6: Searching Through History

Find Commits That Changed a File

git log -- index.html

Find When Text Was Added/Removed

git log -S "searchText"

Shows commits that added or removed "searchText".

Show Commits That Touch Specific Lines

git log -L 10,20:index.html

Shows history of lines 10-20 in index.html.

Part 7: Pretty Formatting

Custom Format

# Short hash, date, author, message
git log --pretty=format:"%h - %an, %ar : %s"

Output:

abc1234 - John Doe, 2 days ago : Add login feature
def5678 - Jane Smith, 1 week ago : Fix bug in navigation

Format Placeholders

Placeholder Meaning
%h Abbreviated commit hash
%an Author name
%ar Author date, relative
%s Commit message

Practice Exercise

  1. View your repository's full history:
    git log
  2. View compact one-line history:
    git log --oneline
  3. View last 5 commits:
    git log -5
  4. See details of latest commit:
    git show HEAD
  5. Make a change to a file and see the diff:
    echo "New line" >> README.md
    git diff
  6. See who last modified each line:
    git blame README.md

Summary

Essential History Commands

Command Purpose
git log View commit history
git log --oneline Compact history view
git log --graph Visual branch history
git show HASH View specific commit
git diff See unstaged changes
git blame FILE See who changed each line

What's Next?

In the next lesson, you'll learn how to undo changes - using git reset, revert, and other commands to fix mistakes and recover from errors.