The Hook (The "Byte-Sized" Intro)
git log turns your project into a Netflix episode guide — every commit is an episode with a title, a date, and a synopsis. The raw output is noisy, but with the right flags, you can slice, filter, and format history like a search engine. The developers who debug fastest aren't the smartest — they're the ones who know git log.
📖 What is git log?
git log displays the commit history of your repository. By default, it shows every commit in reverse chronological order with full details. But its real power comes from flags — you can filter by author, date, file, message, and more.
Conceptual Clarity
git logreads from the current branch's HEAD backward through the parent chain- Each entry shows: hash, author, date, message
- The log is local — it shows the history in your repository (use
git fetchfirst to update remote history) - Flags let you customize the output format and filter results
- Combine flags to build powerful queries
Real-Life Analogy
git log is like the "Recently Edited" history in Google Docs — but with superpowers. Imagine being able to filter Google Docs history to show only edits by Alice, in January, to Chapter 3, that mention "refactor."
Visual Architecture
Why It Matters
- Debugging: Find exactly which commit introduced a bug by searching messages or diffs.
- Code review: Understand how a feature evolved over time.
- Accountability: See who changed what and when.
- Releases: Identify what's changed since the last release tag.
Code
# ─── Default: full details (verbose) ───
git log
# Output: hash, author, date, full message for each commit
# ─── Compact: one line per commit ───
git log --oneline
# Output:
# a1b2c3d (HEAD -> main) Add user auth
# e4f5g6h Fix navbar overflow
# i7j8k9l Initial commit
# ─── Limit to last N commits ───
git log --oneline -5
# ─── Visual branch graph ───
git log --oneline --graph --all
# Output:
# * a1b2c3d (HEAD -> main) Merge feature/login
# |\
# | * b2c3d4e Add login form
# | * c3d4e5f Add auth API
# |/
# * d4e5f6g Update README
# ─── Filter by author ───
git log --author="Nikhil"
# ─── Filter by date range ───
git log --after="2026-01-01" --before="2026-02-01"
# ─── Filter by commit message keyword ───
git log --grep="fix" --oneline
# ─── Show only commits that touched a specific file ───
git log --oneline -- src/app.js
# ─── Show the diff in each commit ───
git log -p -1
# Shows the full diff of the most recent commit
# ─── Show stats (files changed, insertions, deletions) ───
git log --stat -3
# ─── Custom format ───
git log --pretty=format:"%h | %an | %ar | %s" -5
# Output:
# a1b2c3d | Nikhil | 2 hours ago | Add user auth
# e4f5g6h | Nikhil | 1 day ago | Fix navbarEssential git log Flags
| Flag | Purpose | Example |
|---|---|---|
--oneline | Compact format | git log --oneline |
-n N | Limit to N commits | git log -5 |
--graph | ASCII branch visualization | git log --graph --oneline |
--all | Show all branches | git log --all --oneline |
--author | Filter by author | git log --author="Alice" |
--grep | Search commit messages | git log --grep="fix" |
--after/--before | Filter by date | git log --after="2026-01-01" |
-p | Show diffs | git log -p -1 |
--stat | Show file change stats | git log --stat |
-- <file> | Filter by file path | git log -- README.md |
--pretty=format: | Custom output format | See example above |
Key Takeaways
git logis your window into commit history — use it constantly.--onelineand--graphare the two flags you'll use most.- Combine
--author,--grep,--after, and-- <file>to create precise queries. git log -pshows the actual code changes alongside each commit.
Interview Prep
-
Q: How do you find all commits by a specific author? A: Use
git log --author="Name". This filters the log to show only commits where the author field matches the given string. -
Q: How can you see which commits changed a specific file? A: Use
git log -- <filepath>. For example,git log --oneline -- src/app.jsshows all commits that modified that file. -
Q: What is the difference between
git logandgit log --all? A:git logshows the history reachable from the current HEAD (current branch only).git log --allshows commits from all branches and tags, which is useful for seeing the full repository graph.