Sub topic 11
Reviewing history with git log
This chapter assumes you have a repository with at least two commits and a configured author identity. You should also know how git add and git commit work.
What git log shows
git log prints the commits reachable from your current commit, newest first. Each entry shows the commit's hash, author, date, and message.
git logcommit 9f2c1ab7d3e4f5a60718293a4b5c6d7e8f901234
Author: Ada Lovelace <[email protected]>
Date: Fri Dec 4 14:40:12 2026 +0000
Add usage notes to README
Explain how to run the project locally.
commit 3b7e0c4d5a6f718293a4b5c6d7e8f9012345abcd
Author: Ada Lovelace <[email protected]>
Date: Fri Dec 4 14:31:50 2026 +0000
Add READMEThe hash is a 40-character identifier that names the commit. The first entry is the most recent commit. Git reads the history by following parent links backward from it.
Compact output
The default format is long. The --oneline option shows one commit per line, with an abbreviated hash and the subject line.
git log --oneline9f2c1ab Add usage notes to README
3b7e0c4 Add READMEShort hashes work anywhere Git expects a commit, as long as they are unambiguous.
Limiting and filtering
These options cover most day-to-day questions.
| Option | Effect |
|---|---|
| `-n 5` or `-5` | Show only the latest 5 commits |
| `--author="Ada"` | Show commits whose author matches the text |
| `--since="2 days ago"` | Show commits newer than the given date |
| `--grep="README"` | Show commits whose message matches the text |
| `-- README.md` | Show only commits that changed that path |
You can combine them:
git log --oneline -5 --author="Ada" -- README.mdThe -- separates options from paths. It prevents Git from mistaking a file name for a branch or option name.
Seeing the changes
Add --stat to list the files each commit touched, or -p to see the full diff.
git log --stat -1
git log -p -1To inspect a single commit, use git show:
git show 9f2c1abGraph view
Once you have branches, the following command draws them as text:
git log --oneline --graph --decorate --all--decorate labels commits with branch names and HEAD. --all includes every branch, not just the current one. In a linear history, the graph is a single column.
Common errors
- "fatal: your current branch 'main' does not have any commits yet": the repository has no commits. Make one first.
- Empty output with a path filter: the path is wrong, or no commit ever touched it. Check the spelling and the directory you are in.
- A stuck terminal: the pager is waiting for input. Press
q.
Exercise
You want to see, one line per commit, the last three commits that changed README.md. Which command does this?
Show answer
git log --oneline -3 -- README.md
Exercise
What does git log --stat show that git log --oneline does not?
Show answer
It shows which files each commit changed and how many lines were added or removed in each file. The --oneline format shows only the short hash and the subject line.