Sub topic 4
Narrowing log by count, date, author, message, and path
This chapter assumes you can run git log and read its default output, and that you know commits, branches, and the working tree. Here you will cut a long history down to the commits you care about.
Why filter
A real repository can hold tens of thousands of commits. Filters are options that tell git log which commits to print. They combine, and a commit must pass every filter to appear.
Limit by count
Use -n <number> (or -<number>) to stop after that many commits. Git walks from the newest commit backward, so you get the most recent ones.
git log -n 5 --oneline
git log -5 --onelineBoth commands print the same five commits.
Limit by date
--since and --until (also spelled --after and --before) compare against the commit date. They accept absolute dates and relative phrases.
git log --since="2 weeks ago" --oneline
git log --since="2026-11-01" --until="2026-11-30" --onelineLimit by author and message
--author matches the author name or email. --committer does the same for the committer. --grep matches the commit message. All three take a regular expression, not a plain string.
git log --author="Alice" --oneline
git log --grep="fix" --oneline
git log --grep="fix" -i --onelineThe -i option makes the match case-insensitive. Without it, --grep="fix" will miss "Fix". If you pass --grep more than once, Git shows commits that match any of the patterns. Add --all-match to require every pattern.
Limit by path
Put a -- separator after the options, then list paths. Git shows only commits that changed those paths.
git log --oneline -- src/
git log --oneline -- src/config.c docs/The -- tells Git that what follows is a path, not a branch name. Without it, a name like docs could be read as either, and Git may stop with an "ambiguous argument" error.
Combine filters
git log --since="2 weeks ago" --author="Alice" --oneline -- src/This shows commits from the last two weeks, by authors matching "Alice", that touched files under src/.
The diagram shows the idea, not Git's exact internal order. What matters is the result: the count limit applies to commits that already passed the other filters. -n 5 with --author="Alice" gives Alice's five latest commits, not the last five commits filtered by Alice.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ambiguous argument 'src'` | Path not separated from revisions | Add `--` before paths |
| `--grep` finds nothing | Case mismatch | Add `-i` |
| `--author="alice"` misses commits | Name is stored differently, such as an email-only match | Match a unique part of the name or email |
| Path filter hides a known change | File was renamed or lived elsewhere | See the later chapter on following renames |
Exercise
Write one command that prints, one line per commit, the three most recent commits whose message contains "timeout" (any letter case) and that changed files under net/.
Show answer
git log -n 3 --oneline -i --grep="timeout" -- net/