Viewing History

9.1 Basic Commit History

The simplest command to view history:
1git log

This shows:
• Commit hash
• Author
• Date
• Commit message

Example output:
1commit a1c3f9d 2Author: Sarah Lee <sarah@example.com> 3Date: Tue Nov 6 16:22:10 2025 4 5 Add user authentication logic

Use this to understand the chronological sequence of project changes.

9.2 Compact, Readable Logs

One-line version:
1git log --oneline

Gives something like:
1a1c3f9d Add user authentication logic 265ab29c Update README 348cfe19 Fix typo in login page
This is great for quick scanning.

Show only last N commits:
1git log -5 --oneline

9.3 Visual Graph of Branches

To understand how branches diverged and merged:
1git log --oneline --graph --decorate --all

This displays a tree-like view:
1* a1c3f9d (HEAD -> main) Add user authentication logic 2|| * 48cfe19 Feature: add user avatar upload 3* | 65ab29c Update README 4|/ 5* 39fa114 Initial commit

Helpful for navigating complex project histories.

9.4 Filtering History (Search & Narrowing)

Find commits by author:
1git log --author="Sarah"

Search for text in commit messages:
1git log --grep="authentication"

Show commits affecting a specific file:
1git log --oneline -- <path/to/file>

Limit by date:
1git log --since="2 weeks ago" 2git log --until="2023-01-01"

Filtering helps you quickly locate relevant changes.

9.5 Inspecting Changes in Commits

Show what changed in each commit:
1git log -p

Show diffs for only one commit:
1git show <commit-hash>

Example:
1git show a1c3f9d

This reveals:
• Added lines
• Removed lines
• Updated code segments

This is essential for debugging or code review.

9.6 Browsing History with Blame

git blame lets you see who last changed each line of a file:
1git blame file.js

You get output like:
1a1c3f9d (Sarah Lee 2025-11-06) function login() { 248cfe19 (Dev Patel 2025-11-04) validateUser(input);

Use this to trace:
• When a bug was introduced
• Who last edited a specific line
• The context around a change

9.7 Viewing History Without Clutter

Exclude merge commits:
1git log --no-merges --oneline

Customize the output format:
1git log --pretty=format:"%h - %an : %s"

Custom formats help you generate clean reports or summaries.

9.8 Key Takeaways

• git log is the main tool for exploring commit history.
• Use --online, --graph, and --decorate for simplified and visual history.
• Filtering options like --author, --grep, --since, and file-specific logs help pinpoint changes.
• git show and git log -p reveal actual code modifications.
• git blame identifies line-by-line authorship for deeper investigation.
• Git's history tools make it easy to understand, audit, and navigate your project's evolution.