Skip to main content

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

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

Format Placeholders

PlaceholderMeaning
%H / %hFull / short commit hash
%anAuthor name
%aeAuthor email
%adAuthor date
%sSubject (first line of message)
%dRef names (branches, tags)
%bBody 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 WantCommand
Simple historygit log --oneline
Visual branch graphgit log --oneline --graph --all
Changes in each commitgit log --stat
Full diff for each commitgit log -p
Filter by authorgit log --author="name"
Filter by dategit log --since="2 weeks ago"
Filter by messagegit log --grep="keyword"
History of one filegit log -- <file>
Who wrote each linegit blame <file>
Inspect one commitgit show <hash>
Recovery (HEAD movements)git reflog

Troubleshooting

ProblemCauseFix
Log shows too many commitsNo filter appliedUse -n 10 or --since
Can't find a commitWrong branchAdd --all to search all branches
Author search returns nothingName mismatchUse partial match: --author="don"
git blame shows merge commitsMerge introduced the linesUse git blame -w -M to ignore whitespace/moves

Best Practices

  • Use --oneline --graph for quick visual overview
  • Set up a Git alias: git config --global alias.lg "log --oneline --graph --all --decorate"
  • Use git blame to understand context before modifying unfamiliar code
  • Know git reflog exists — it can save you from disasters

What's Next

  1. stash & clean — Save work temporarily or clean up untracked files
  2. Branching Fundamentals — Work in parallel with branches