Exploring History with git log
Reviewed & published by Brayan K
Your repo is a time machine, and git log is the dial. By the end of this lesson you'll read history compactly, draw your branches as a graph, filter commits by author, message, date, and file, and inspect any single commit in full with git show.
Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
What You'll Learn
- Read history with git log and the compact --oneline
- Visualise branches with --graph --all
- Limit output with -n / -5
- Filter by author with --author and message with --grep
- Filter by date and by a single file path
- Inspect one commit's full diff with git show
1️⃣ git log Basics & --oneline
git log walks backwards through the current branch, newest first, showing each commit's hash, author, date, and message. The full output is verbose, so --oneline collapses each commit to a single line. Limit the count with -n 3 (or just -3), and press q to leave the pager.
# git log walks backwards through your history, newest commit first.
# Each entry shows the full hash, author, date, and message.
git log
# Too verbose? --oneline collapses each commit to a single line:
# <short-hash> <subject>
git log --oneline
# Show just the last 3 commits (-n 3 is the long form of -3)
git log --oneline -3
# Press q to quit the pager when you're done scrolling.2️⃣ Visualising with --graph --all
Text history hides the shape of your branches. git log --oneline --graph --all draws the whole repository as an ASCII graph: --graph adds the branch/merge lines, and --all includes every branch rather than just the one you're on. Learn this command early — it's the fastest way to picture what your branches are doing.
# The killer combo for understanding branches: draw the whole repo.
git log --oneline --graph --all
# --oneline one compact line per commit
# --graph ASCII lines showing branches & merges
# --all every branch, not just the current one
# Add --decorate to label refs (modern Git shows these automatically)
git log --oneline --graph --all --decorate3️⃣ Filtering the History
Big projects need filters. --author keeps commits by one person, --grep searches commit messages, --since/--until bound a time window, and -- <path> limits to commits that touched one file. These all combine, so you can ask very specific questions of your history.
# Real projects have thousands of commits — filter to find what matters.
# Commits by a particular author
git log --author="Alice" --oneline
# Commits whose MESSAGE contains a word
git log --grep="bugfix" --oneline
# Commits in a time window
git log --since="2 weeks ago" --until="yesterday" --oneline
# Commits that touched ONE file (note the -- separator)
git log --oneline -- src/app.js
# Combine freely — author AND message match
git log --author="Alice" --grep="login" --oneline4️⃣ Inspecting One Commit with git show
Once git log helps you find the commit, git show zooms in. It prints the commit's metadata and the exact diff it introduced. Give it HEAD, a short hash, or any commit reference; add --stat for just the changed-files summary.
# git log lists commits; git show zooms into ONE of them — metadata
# plus the exact diff it introduced.
# Inspect the latest commit in full (message + patch)
git show HEAD
# Inspect any commit by its (short) hash
git show 1005316
# Just the file list and stats, no full patch
git show --stat HEADYour turn. Read history compactly, graph it, then search messages. Fill in the three blanks.
# 🎯 YOUR TURN — replace each ___ then run it in your terminal.
# Goal: navigate a project's history.
# 1) See history as a compact one-line-per-commit list
git log ___ # 👉 the flag that gives a single line per commit
# 2) Draw the branches and merges as a graph (all branches)
git log --oneline ___ --all # 👉 the flag that draws the ASCII graph
# 3) Find commits whose message mentions "login"
git log ___="login" --oneline # 👉 the flag that searches commit messages
# ✅ Expected: step 1 lists short hashes + subjects, step 2 adds the ASCII
# branch lines, step 3 lists only commits mentioning "login".Common Errors (and the fix)
- git log seems stuck: you're in the pager. Press q to quit, or run with --no-pager.
- "unknown revision or path not in the working tree": Git thinks your file name is a branch. Add the -- separator: git log -- file.js.
- --grep finds nothing: it searches commit messages, not file contents. To search code use git log -S"text" (the pickaxe).
- A commit is missing from the log: it's probably on another branch. Add --all to include every branch.
Pro Tips
- 💡 Alias the graph: set git lg to log --oneline --graph --all --decorate.
- 💡 Search code, not messages: git log -S"functionName" finds when a string was added or removed.
- 💡 Blame a line: git blame file.js shows which commit last touched each line.
- 💡 Stat over patch: add --stat when you only want the list of changed files.
📋 Quick Reference
| Command | Purpose |
|---|---|
| git log --oneline | Compact one-line-per-commit history |
| git log --graph --all | Draw all branches as a graph |
| git log -5 | Show the last 5 commits |
| git log --author=Name | Filter by author |
| git log --grep=word | Search commit messages |
| git log -- file | History of one file |
| git show <commit> | Inspect a commit's full diff |
Mini-Challenge: Investigate a Repo
No commands given this time — just the questions. Answer each with a single git log or git show command.
# 🎯 MINI-CHALLENGE: investigate a repo's history.
# No commands given — just the questions. Answer each with one git log command.
#
# 1. What were the last 5 commits? (compact form)
# 2. Which commits did "Alice" author?
# 3. Which commits mention "fix" in their message?
# 4. What is the full diff of the most recent commit?
# 5. Draw all branches as a graph and identify the merge commits
#
# ✅ Expected: you can answer every question with git log (+ flags) and
# git show, without opening any file in an editor.
# your commands here🎉 Lesson Complete!
- ✅ git log walks history newest-first; --oneline compacts it
- ✅ --graph --all draws every branch as an ASCII map
- ✅ -5 limits count; --author and --grep filter
- ✅ git log -- file shows one file's history
- ✅ git show <commit> reveals a single commit's full diff
Practice quiz
What does `git log` show?
- Your branches
- The staging area
- The commit history of the current branch
- Remote URLs
Answer: The commit history of the current branch. git log lists commits, newest first, on the current branch.
What does the `--oneline` flag do?
- Condenses each commit to a single line (short hash + message)
- Shows only one commit
- Shows commits from one author
- Limits to one branch
Answer: Condenses each commit to a single line (short hash + message). --oneline prints a compact one-line summary per commit.
What does `--graph` add to git log?
- A bar chart of file sizes
- Author photos
- A pie chart
- An ASCII drawing of branch and merge structure
Answer: An ASCII drawing of branch and merge structure. --graph draws the branching/merging topology as ASCII lines on the left.
What does `--all` do in `git log --all`?
- Shows every file
- Includes commits from ALL branches, not just the current one
- Shows all authors only
- Disables paging
Answer: Includes commits from ALL branches, not just the current one. --all includes the history of every branch and ref, not just HEAD's branch.
How do you limit git log to the last 5 commits?
- git log -5 (or -n 5)
- git log --last 5
- git log --limit=5
- git log HEAD~5
Answer: git log -5 (or -n 5). git log -5 (equivalently -n 5) shows the five most recent commits.
Which flag filters commits by who made them?
- --committer-only
- --by=Name
- --author=Name
- --user=Name
Answer: --author=Name. --author=Name keeps only commits whose author matches the pattern.
Which flag searches commit MESSAGES for a word?
- --message
- --grep=word
- --search=word
- --find=word
Answer: --grep=word. --grep=word filters to commits whose message matches the pattern.
How do you see the history of just one file?
- git log onlyfile
- git log --file path
- git history file
- git log -- path/to/file
Answer: git log -- path/to/file. git log -- <path> limits the log to commits that touched that path.
What does `git show <commit>` display?
- Only the message
- The commit's metadata AND its full diff
- Just the author
- The whole repo
Answer: The commit's metadata AND its full diff. git show prints the commit details together with the patch it introduced.
In `git log --oneline`, the short string before each message is...
- The branch name
- The author's initials
- An abbreviated commit hash
- A line number
Answer: An abbreviated commit hash. It's the abbreviated SHA-1 hash uniquely identifying that commit.
Continue this course
- Previous: Ignoring Files with .gitignore
- Next: Amending & Fixing Commits — Fix the last commit's message or contents with git commit --amend
- Quick reference: Git cheat sheet › Inspecting
Frequently asked questions
How do I make git log less overwhelming?
Reach for --oneline, which collapses each commit to a single line of short-hash plus subject. Combine it with -n (e.g. -10) to limit how many commits you see, and --graph --all to visualise branches. Many people alias the whole thing — for example git config --global alias.lg "log --oneline --graph --all --decorate" — so a quick git lg shows a readable map.
What's the difference between git log and git show?
git log lists many commits (the history), each summarised by message and metadata. git show zooms into a single commit and prints its full details plus the exact diff it introduced. Use log to find the commit you care about, then show <hash> to inspect precisely what it changed.
How do I see the history of just one file?
Use git log -- path/to/file. The -- separates paths from other arguments so Git knows you mean a file, not a branch. This shows only the commits that touched that file. Add -p to also see the diff each of those commits made to the file, which is great for tracking down when a line changed.
Can I search commits by author or message?
Yes. --author=Name keeps only commits by a matching author, and --grep=text keeps only commits whose message matches. Both accept patterns and can be combined, and you can add date filters like --since and --until. For example, git log --author=Alice --grep=login --since="1 month ago" narrows to Alice's recent login-related commits.
Why are commit hashes so long, and can I use the short form?
Git identifies each commit by a 40-character SHA-1 hash, which guarantees uniqueness across the whole repo. In practice you rarely type the whole thing — --oneline shows an abbreviated hash (usually 7 characters), and Git accepts that short prefix anywhere a commit is expected, as long as it's unambiguous.