git log From Zero: The History Is There — Learn to Read It
git log from zero: a 63-line transcript on git 2.43.0 takes a three-commit demo repo through --oneline, --graph, -n/--since/--author filters, --stat/-p and --format, plus two real traps: --grep searches messages only, not code (-S searches code), and a branch sharing a file's name fails with exit 128 unless you add --; plus a --follow rename test and a cheat sheet.

Every Git user sooner or later hits the same moment: who changed this file, what did that commit last week actually do, where did the line I wrote yesterday go? The answer is always the same command: git log. It is Git's built-in history recorder — every commit, every change, every rename is remembered. But most people use git log by typing it once, glancing at the list and closing it, which is like guarding an archive and only ever reading the catalogue at the door. This guide starts from zero, using a three-commit demo repository and 63 lines of tested output, to explain the everyday uses of git log and two genuine traps: --grep searches commit messages, not code, and a branch that shares a name with a file makes git log fail outright. Both traps were hit for real during the demo — they were not invented afterwards.
The demo environment first: Git 2.43.0, demo script demo274_log.sh, working directory /tmp/demo274-log-work, wiped and rebuilt on every run. To make the output reproducible byte for byte, the author and committer dates of all three commits are fixed, so the commit hashes are the same on every run: 1b29e08, 53bbcb3 and bc90324. If you follow along on your own machine, your hashes will differ — that is not an error, only your commit times being different. The commands and the shape of the output are the same.
§0 First, build a three-commit demo repository
The demo repository has just two files and three commits, each doing one distinct thing so every section can demonstrate one idea. The first commit (20 November 2026) creates notes.txt and app.py, with the message "feat: v1 greeting". The second commit (21 November 2026) adds a line TOKEN = "needle-123" to app.py, with the message "feat: add token constant". The third commit (22 November 2026) moves notes.txt into a docs/ directory, with the message "docs: move notes into docs/". Two branches are also created, one called notes.txt and one called app.py — they exist purely to set up the trap in §5, so just remember they are there for now.
Once built, run the plainest form first, git log --oneline: three lines, one short hash plus one message line each, newest first — bc90324 docs: move notes into docs/, 53bbcb3 feat: add token constant, 1b29e08 feat: v1 greeting. That is the default view of git log: commits in reverse chronological order. Everything that follows is filtering, formatting or graphing on top of this view.
§1 One line per commit: --oneline
--oneline is the most used option of all: it squeezes each commit into a single line, a short hash plus the subject line of the message. Without it, git log prints the full record for every commit — full hash, author, date, body — pages of it. For everyday history browsing, --oneline is close to mandatory. The output of §1 in the demo is identical to the last three lines of §0, the same three lines. Keep that output in mind: every section below uses it as a reference.
A small suggestion: turn git log --oneline into an alias so you stop typing the full form. Git aliases live in the config file and take one command to set: git config --global alias.lg "log --oneline", after which git lg is enough. An alias changes no behaviour, it just saves keystrokes, but typed dozens of times a day, the saving adds up. The demo spells out every command in full so each step is visible; in real use, the alias is more comfortable.
The default order is newest first, which fits the habit of looking at the recent past first. But sometimes you want to read history forwards — for example, when you have just inherited a repository and want to see how it grew from the first commit. Add --reverse: git log --oneline --reverse puts 1b29e08 at the top and bc90324 at the bottom. One detail to watch when combining it with -n: git log --oneline -2 --reverse takes the two newest commits first and then reverses them — it does not take the two oldest. Getting the two oldest needs a different approach, a trap you can leave until the day you actually need it.
§2 Graphs and decoration: --graph --all --decorate
A bare list cannot show how branches relate. Add --graph --all --decorate and the output changes: the first line gains an asterisk and a parenthesis, * bc90324 (HEAD -> main, notes.txt) docs: move notes into docs/, and the two lines below each get an asterisk. Three pieces of information are packed into that line: the asterisks joined into a line show the parent-child relationships between commits, HEAD -> main says which branch you are on and which commit it points at, and notes.txt says another branch with the same name points at the same commit.
Each option does one job: --graph draws the lines between commits, so forks and merges become visible once branches multiply; --all lists commits from every branch, while without it you only see the current branch's history; --decorate labels commits with branch and tag names. The demo repository has a single straight line of history, so the graph is just three asterisks in a column — nothing spectacular. In a real repository with dozens of branches, the same history without these three options is an unreadable stream. When you need to know which branch a commit sits on, use all three together; missing one leaves the picture incomplete.
One more detail in graph output is worth noting: a merge commit has two lines flowing into it. The demo has no merge commits, so you cannot see it there, but in real projects merges are routine. To see only the main line, add --first-parent, which means "follow the first parent of each merge commit" — main line only, skipping the commits that were merged in. It is the right option for "what landed on main recently"; for "what did this branch actually change", use --all with a branch name instead. The two options do opposite jobs — do not mix them up.
§3 Filtering by count, time and author: -n, --since, --author
Once history grows long, full output is unreadable, so filtering is a core skill. The demo tries three filters. First, by count: git log --oneline -2 shows only the two newest commits, bc90324 and 53bbcb3, with the oldest cut off. When the question is "what changed recently", -2, -5 or -10 is the handiest form.
Second, by time: git log --oneline --since="2026-11-21" --until="2026-11-22" returns a single line, 53bbcb3 feat: add token constant. Watch the boundary behaviour here: --until="2026-11-22" with a bare date means "up to midnight at the start of that day", so the commit from 22 November at 10:00 is excluded. To include that day, write --until="2026-11-22 23:59" or --until="2026-11-23". That boundary is the easiest detail in this section to get wrong, and it is worth writing down separately.
Third, by author: git log --oneline --author="Demo User" -1 returns the newest commit. In a repository with many collaborators, "what did so-and-so change last week" is exactly this command. --author takes a regular expression, not an exact match, so part of a name matches too; for precision, use an email address. The three filters combine freely — --author="Demo User" --since="2026-11-21" -5 means "this person, this period, the newest five".
§4 Trap 1: --grep searches messages, not code
This is the first genuine trap in this guide. The demo's app.py contains the line TOKEN = "needle-123", added by the second commit. To find "who added the needle line", the instinct is to run git log --oneline --grep=needle. The demo result: empty output, nothing found. Try --grep=TOKEN next, and it is empty again. Twice empty — not because the command is wrong, but because --grep never looks at code at all. It searches only the subject and body of commit messages. The second commit's message is "feat: add token constant", which contains neither "needle" nor an uppercase "TOKEN", so both searches come back empty.
Two lessons hide in this one trap. The first is scope: --grep searches messages; to search code, use -S. git log --oneline -S needle correctly finds 53bbcb3 feat: add token constant in the demo. -S means "find commits that changed the number of occurrences of this string in the code" — the so-called pickaxe, built for tracing who added or removed a particular line. The second lesson is case: --grep=TOKEN found nothing because the message says "token" in lowercase, and --grep is case-sensitive by default. Add -i to ignore case — git log --oneline --grep=token -i finds the same commit in the demo. Remember the pairing: messages with --grep, code with -S, and never the two mixed up.
One more detail that is easy to miss: when --grep finds nothing, the exit code is still 0, not an error. Both empty outputs in the demo are followed by "exit=0". That means a script cannot tell "found nothing" from the exit code — it has to check whether the output is empty. When writing automation around git log, that distinction matters: empty output with exit 0 means "zero matches", not "the command failed".
§5 Trap 2: a branch and a file with the same name confuse log
The second trap is harsher: it fails outright. The demo repository has a branch called app.py, and the working tree also has a file called app.py. Running git log --oneline app.py gets this reply from Git: fatal: ambiguous argument 'app.py': both revision and filename, plus a hint to use -- to separate paths from revisions, and an exit code of 128. In other words, app.py can be read as a branch name or as a filename, Git does not know which you mean, and it refuses to guess.
The fix is to follow the hint and put -- before the path: git log --oneline -- app.py, meaning "everything after -- is a path". In the demo that command correctly lists the two commits that touched app.py — 53bbcb3 and 1b29e08 — while the third commit, which only moved notes.txt, is not listed. The -- separator is a Git-wide convention: revisions before it, paths after it, and it works the same way in git checkout, git diff and other commands.
The demo also contains a control experiment worth reading: git log --oneline notes.txt does not error, and prints all three commits. Why does notes.txt escape the trap when app.py does not? Because the file notes.txt was moved away in the third commit, so no such file exists in the working tree any more — Git can only read it as a branch name, and there is no ambiguity. Ambiguity only appears when the branch and the file exist at the same time. In the real world that is not rare: someone creates a branch called README.md, or a branch called fix that happens to collide with a filename. Remember the rule: whenever a path follows git log, make a habit of adding --, and the ambiguity can never happen.
§6 Following a renamed file: --follow
Renamed files are another classic blind spot for git log. In the demo, notes.txt was moved into docs/ in the third commit. Running git log --oneline -- docs/notes.txt directly returns a single line: bc90324 docs: move notes into docs/ — the rename commit itself. The first commit, which created the file, is missing, because back then the file was called notes.txt and did not live under docs/.
Add --follow and the picture changes: git log --oneline --follow -- docs/notes.txt returns two lines, bc90324 and 1b29e08, recovering the commit from before the rename. --follow means "follow the file's rename history backwards", and Git works out from content similarity that docs/notes.txt and the old notes.txt are the same file. When you want a file's complete history, --follow is mandatory; without it you only see the half after the rename. One limitation: --follow works for a single file only — it does nothing when given several paths or no path at all.
§7 Seeing what one commit changed: --stat and -p
Once you have found a commit, the next step is usually to see what it changed. --stat gives the summary: git log --oneline --stat -1 shows, for the third commit in the demo, "notes.txt => docs/notes.txt | 0, 1 file changed, 0 insertions(+), 0 deletions(-)". The arrow means the file was renamed, and zero insertions and deletions mean the commit only moved the file without changing its content. One glance tells you what kind of commit it was: pure relocation, no logic change.
-p gives the full diff: git log --oneline -p -1 -- app.py | head -20 shows, for the second commit in the demo, the added line +TOKEN = "needle-123" with three lines of context above it. When the question is "exactly which lines did this commit change", -p is the most direct tool. The two options are often used in sequence: --stat first to see which files changed and how much, then -p to see the details. One caution: -p output can be very long — the demo pipes it through head -20 for that reason. In a real repository, a commit that changes hundreds of lines will flood the screen with -p; narrowing it with -- filename is the better habit.
§8 Custom formats: --format and --date
The default output format is not always what you want, and --format lets you build your own. The demo uses git log --format="%h %ad %s" --date=short, producing three lines: bc90324 2026-11-22 docs: move notes into docs/, 53bbcb3 2026-11-21 feat: add token constant, 1b29e08 2026-11-20 feat: v1 greeting. The placeholders each mean something: %h is the short hash, %ad is the author date, %s is the subject line; --date=short renders dates as 2026-11-22.
A few more placeholders are worth memorising: %H is the full hash, %an is the author name, %ae is the author email, %cn is the committer name, %cd is the committer date, %b is the message body. When you process commit history in scripts, --format is the reliable way to do it: the output format is entirely under your control, with no regular expressions needed to pick fields out of the default output. The three lines from §8 of the demo are already in a shape that can go straight into a report.
§9 Three common mistakes
The first mistake is treating git log as a universal search. As shown above, --grep searches messages only and -S searches changes in occurrence counts, each with its own scope. There is one more case: finding when a particular line inside a file was deleted. -S finds the commit where the count changed, but if the line was rewritten rather than deleted, the count never changed and -S finds nothing. That is when to use -G, which matches diff content by regular expression and is broader than -S. The demo does not use -G, but knowing it exists means you will not get stuck when -S comes back empty.
The second mistake is assuming git log can see every commit. It can only see commits that are still reachable in the current repository. A deleted branch, a commit dropped by reset, an old version covered by amend — none of those show up in git log. Those live in git reflog. In the demo all three commits sit on main, so git log sees all of them; but in a real repository, searching git log for a commit on a deleted branch returns empty output. Empty output does not mean "the commit does not exist", only "it is not on this line of history". Searching history and recovering lost work are two different jobs: the first uses git log, the second uses git reflog.
The third mistake is ignoring time zones and dates. In the demo, all three commit dates are fixed in UTC, so the --date=short output — 2026-11-20 and friends — is clean. In a real repository, the author date and the committer date can sit in different time zones, and --date by default shows the committer date in the local time zone. The same commit can display as a different day on different people's machines. Using --date=short together with --date-order, or outputting full ISO timestamps with --format, avoids arguments about which day a commit belongs to. In scripts that process history, always use full timestamps, never short dates.
Cheat sheet
Everything used in this guide, in one table for quick reference:
| What you want | Command |
|---|---|
| List commits, one line each | git log --oneline |
| Branch graph and decoration | git log --oneline --graph --all --decorate |
| Newest N commits only | git log --oneline -N |
| Filter by time | git log --oneline --since="2026-11-21" --until="2026-11-23" |
| Filter by author | git log --oneline --author="name" |
| Search commit messages | git log --oneline --grep="keyword" -i |
| Search code content | git log --oneline -S "code snippet" |
| History of one file | git log --oneline -- filename |
| Follow a renamed file | git log --oneline --follow -- filename |
| Summary of one commit | git log --oneline --stat -1 |
| Diff of one commit | git log --oneline -p -1 -- filename |
| Custom output format | git log --format="%h %ad %s" --date=short |
How this guide relates to the neighbouring guides
This is a dedicated guide to git log — how to read history. The guides it is easiest to confuse it with draw their lines here: the git reflog guide covers recovering commits that are already lost, working from local reflog records; git cherry-pick covers moving a commit from one branch to another; git stash covers shelving changes that have not been committed yet, working on the working tree; git bisect covers locating the commit that introduced a bug by bisection. This guide covers none of those — no recovering, no moving, no shelving, no locating — only one thing: the history is already there, how do you read it properly. Reading history is the basic skill; the others are operations built on top of it.

Comments (0)
Sources
Related articles
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.