git log & History Navigation
Quick Summary
git log is your time machine — it shows the complete history of commits in your repository. From simple chronological lists to complex filtered searches, mastering the log is essential for understanding what happened, when, and why.
Basic Log
git log
commit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 (HEAD -> main, origin/main)
Author: donnyaw <donnyaw@gmail.com>
Date: Mon Feb 17 08:00:00 2026 +0800
feat: add user authentication module
commit b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1
Author: donnyaw <donnyaw@gmail.com>
Date: Sun Feb 16 15:30:00 2026 +0800
fix: correct validation regex for email input
Useful Log Formats
- One line per commit
- Graph view
- Detailed with stats
- Custom format
git log --oneline
a1b2c3d feat: add user authentication module
b2c3d4e fix: correct validation regex for email input
c3d4e5f docs: update API documentation
d4e5f6a refactor: extract database helpers
git log --oneline --graph --all
* a1b2c3d (HEAD -> main) Merge branch 'feature/auth'
|\
| * f6a7b8c feat: add login endpoint
| * e5f6a7b feat: add user model
|/
* d4e5f6a fix: update dependencies
* c3d4e5f Initial commit
git log --stat
commit a1b2c3d
Author: donnyaw <donnyaw@gmail.com>
Date: Mon Feb 17 08:00:00 2026 +0800
feat: add user authentication module
src/auth.js | 45 +++++++++++++++++++++
src/models.js | 12 ++++++
test/auth.test.js | 30 ++++++++++++++
3 files changed, 87 insertions(+)
git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
a1b2c3d 2026-02-17 | feat: add auth module (HEAD -> main) [donnyaw]
b2c3d4e 2026-02-16 | fix: validation regex [donnyaw]
c3d4e5f 2026-02-15 | docs: update API docs [donnyaw]
Format Placeholders
| Placeholder | Meaning |
|---|---|
%H / %h | Full / short commit hash |
%an | Author name |
%ae | Author email |
%ad | Author date |
%s | Subject (first line of message) |
%d | Ref names (branches, tags) |
%b | Body of commit message |
Filtering History
By Author
git log --author="donnyaw"
git log --author="donnyaw\|alice" # Multiple authors
By Date
git log --since="2026-01-01"
git log --after="2 weeks ago"
git log --until="2026-02-01"
git log --since="2026-01-01" --until="2026-02-01"
By Message Content
git log --grep="fix" # Commits mentioning "fix"
git log --grep="auth" --grep="login" --all-match # Both terms
By File
git log -- src/auth.js # Commits that touched this file
git log --follow -- old-name.js # Follow renames
git log -p -- src/auth.js # Show patches for this file
By Number
git log -5 # Last 5 commits
git log -1 # Last commit only
Advanced History Commands
git show — Inspect a Single Commit
git show a1b2c3d
Shows the commit metadata + full diff of changes.
git blame — Who Changed Each Line?
git blame src/auth.js
a1b2c3d4 (donnyaw 2026-02-17 08:00:00 +0800 1) const jwt = require('jsonwebtoken');
b2c3d4e5 (donnyaw 2026-02-16 15:30:00 +0800 2) const bcrypt = require('bcrypt');
c3d4e5f6 (alice 2026-02-10 10:00:00 +0800 3)
a1b2c3d4 (donnyaw 2026-02-17 08:00:00 +0800 4) function login(user, password) {
git shortlog — Summary by Author
git shortlog -sn
42 donnyaw
15 alice
3 bot
git reflog — Recovery Safety Net
git reflog
a1b2c3d HEAD@{0}: commit: feat: add auth module
b2c3d4e HEAD@{1}: checkout: moving from feature to main
f6a7b8c HEAD@{2}: commit: feat: add login endpoint
Reflog is your safety net
Even if you accidentally delete a branch or reset to the wrong commit, reflog shows every position HEAD has been in. You can recover almost anything with it.
Quick Reference
| What You Want | Command |
|---|---|
| Simple history | git log --oneline |
| Visual branch graph | git log --oneline --graph --all |
| Changes in each commit | git log --stat |
| Full diff for each commit | git log -p |
| Filter by author | git log --author="name" |
| Filter by date | git log --since="2 weeks ago" |
| Filter by message | git log --grep="keyword" |
| History of one file | git log -- <file> |
| Who wrote each line | git blame <file> |
| Inspect one commit | git show <hash> |
| Recovery (HEAD movements) | git reflog |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Log shows too many commits | No filter applied | Use -n 10 or --since |
| Can't find a commit | Wrong branch | Add --all to search all branches |
| Author search returns nothing | Name mismatch | Use partial match: --author="don" |
git blame shows merge commits | Merge introduced the lines | Use git blame -w -M to ignore whitespace/moves |
Best Practices
- Use
--oneline --graphfor quick visual overview - Set up a Git alias:
git config --global alias.lg "log --oneline --graph --all --decorate" - Use
git blameto understand context before modifying unfamiliar code - Know
git reflogexists — it can save you from disasters
What's Next
- stash & clean — Save work temporarily or clean up untracked files
- Branching Fundamentals — Work in parallel with branches