Git Log Mastery — History, Diff, Blame
Intermediategit log, git diff, and git blame are your detective tools. Knowing how to filter, format, and search commit history is essential for debugging production issues, reviewing code, and understanding a codebase.
Overview
When a production bug appears, the first question is "what changed recently?". Git's history tools answer this in seconds — if you know the right commands. git log has over 30 formatting options and powerful filters. git diff can compare any two commits, branches, files, or line ranges. git blame reveals who wrote every line and in which commit. Combine these with git bisect (binary search for the commit that introduced a bug) and you have a complete debugging toolkit.
Advanced git log Filtering
The default git log is too verbose for daily use. Learn the filters and you will find anything in seconds.
# Visual graph (bookmark this)
git log --oneline --graph --all --decorate
# Filter by author
git log --author="Akshay"
git log --author="akshay|john" # multiple authors (regex)
# Filter by date
git log --since="2024-01-01"
git log --until="yesterday"
git log --since="2 weeks ago" --until="1 week ago"
# Filter by commit message
git log --grep="payment" # case-sensitive
git log --grep="payment" -i # case-insensitive
git log --grep="fix" --grep="auth" --all-match # both terms
# Find commits that added/removed a specific string (pickaxe)
git log -S "validateEmail" # string added or removed
git log -G "validate.*Email" # regex in diff content
# Filter by file
git log -- src/auth.js # commits touching this file
git log -- "*.test.js" # all test files
# Combine filters
git log --author="Akshay" --since="1 month ago" -- src/payment/Custom Log Formatting
Use --format or --pretty to tailor output for scripts, reports, or readability.
# Built-in formats
git log --oneline # short hash + subject
git log --short # slightly more detail
git log --full # full hash
git log --fuller # author + committer info
# Custom format placeholders
# %H = full hash, %h = short hash
# %s = subject, %b = body
# %an = author name, %ae = author email
# %ar = relative date, %ad = absolute date
# %Cred / %Cgreen / %Creset = colours
git log --pretty=format:"%h | %an | %ar | %s"
# a3f8c12 | Akshay | 2 days ago | Add payment gateway
git log --pretty=format:"%Cgreen%h%Creset %s (%an, %ar)"
# Use in shell scripts / CI
git log --format="%H" -20 # last 20 commit hashes
git log --format="%s" HEAD~5..HEAD # last 5 commit subjectsgit diff — Compare Anything
git diff can compare working directory vs staging, staging vs last commit, any two commits, or any two branches.
# Working directory vs staging area
git diff # what's not yet staged
# Staging area vs last commit
git diff --staged # what will go into the next commit
# Two commits
git diff HEAD~3 HEAD # last 3 commits' net changes
git diff a3f8c12 9d2e441 # specific commits
git diff a3f8c12..9d2e441 # same (range syntax)
# Two branches
git diff main feature/cart
git diff main..feature/cart
# Only show file names (not the diffs)
git diff --name-only main feature/cart
git diff --stat HEAD~5 HEAD # files + line counts
# Diff a specific file only
git diff HEAD~1 -- src/auth.js
# Ignore whitespace changes
git diff -w
git diff --ignore-all-spacegit blame and git bisect
git blame annotates every line with the commit and author. git bisect does a binary search through history to find the commit that introduced a bug — cutting hundreds of commits down to ~10 checks.
# Who wrote each line and when?
git blame src/auth.js
# a3f8c12 (Akshay 2024-01-10) function validateEmail(email) {
# 9d2e441 (John 2024-01-08) return /S+@S+.S+/.test(email)
# Show only certain lines
git blame -L 10,25 src/auth.js
# Show the last commit that touched a range of lines
git log -L 10,25:src/auth.js
# ── git bisect: binary search for a bug ──────────────────
git bisect start
# Mark current commit as bad (has the bug)
git bisect bad
# Mark a known good commit (before the bug existed)
git bisect good v2.0.0
# Git checks out the midpoint — you test and tell it:
git bisect good # if this commit is fine
git bisect bad # if this commit has the bug
# Git narrows down in ~logâ‚‚(n) steps
# "bisect found: 7b2e441 is the first bad commit"
git bisect reset # return to original HEAD when doneKey Points to Remember
- 1git log -S "string" (pickaxe) finds commits that added or removed a specific string — great for debugging
- 2git log --graph --oneline --all gives the best quick visualisation of all branches
- 3git diff --staged shows exactly what will be in the next commit — review before committing
- 4git blame -L 10,25 file shows who wrote specific lines and in which commit
- 5git bisect does O(log n) binary search to find the exact commit that introduced a bug
Interview Questions
Sign in to ask AriaHow would you find which commit introduced a specific bug using Git?
What does git log -S do? Give a use case.
How do you compare two branches using git diff?
Ask Aria about Git Log Mastery — History, Diff, Blame
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.