Programming and IT
git log
git log shows history: which commits brought the repository to its current state. Without options the output is wordy and nearly useless, but a few flags turn it into a search tool — by author, by date, by file and even by a line of code that has disappeared.
In this article
The default output#
Four lines per commit, newest first. The output opens in a pager: scroll with the
arrow keys and space, quit with q. This detail trips people up more than any
other: it looks as if the terminal has frozen, but it is just waiting for q.
This format tells you whose commits these are and when they were made. It does not tell you the shape of the history — for that you need other formats.
Compact formats#
The last line is what people usually mean by "pretty" output:
|\
|
|
|
|
The left column shows branch lines, an asterisk is a commit. You can see that
report-pdf split off from a common ancestor, someone fixed rounding in main
in the meantime, and the top commit joined them.
You can shape the format precisely with --pretty:
Here %h is the short hash, %ad the author date, %an the name and %s the
message subject. There is no need to type this every time — set up an alias:
After that, git lg is enough.
Searching history#
Filters combine with each other, and that is where the command's power lies.
-S deserves a separate word: it does not search message text but the content of
the changes. It finds commits where the number of occurrences of a string
changed — literally the moment a variable was introduced or removed.
The two-dot notation (main..feature-pdf) reads as "what the second ref can
reach but the first cannot". It is handy before pushing a branch: you see exactly
the commits you are about to propose.
Example: finding when something broke#
A line in a report started being calculated wrong, and nobody knows when. First, narrow it down by file and size of changes:
|
Then look at the whole commit:
git show prints the message and changes of a single commit — the same format as
git log -p, but for one entry. If after reviewing it you need to undo the
commit, the options are collected in
how to undo a commit.
What git log will not show you#
History is made of commits. Anything not committed does not appear in it: you
look at uncommitted edits with git status and git diff, and at stashed work
with git stash list — more in the guide to git stash.
The second exception is commits that no branch can reach. After a branch reset or
a rebase they drop out of git log, but they stay in the repository; you find
them in the reference log, git reflog.
The third is remote branches and other people's commits you do not have yet.
git log does not go to the network; run git fetch origin first, then
git log origin/main.
Common mistakes#
Searching code with --grep. That filter only looks at commit messages. To
search the content of changes, use -S (by number of occurrences) or -G (by a
regular expression in the diff text).
File history stops at a rename. Without --follow, Git shows only what
happened at the current path. The flag makes it keep searching under earlier
names.
Expecting git log to show every branch. By default the command prints the
history of the current branch. To see all of them, add --all; to see a specific
one, pass its name: git log feature-pdf.
Mixing up the order in a range. git log feature..main and
git log main..feature give different answers. On the left is what gets
subtracted, on the right what gets shown. Quick check: if the output is empty,
swap them.
Step-by-step plan
- See the shape of historygit log --graph --oneline --decorate --all; remember that q quits the pager.
- Set up an aliasgit config --global alias.lg with your favourite set of flags.
- Narrow the selection--author, --since, --grep and a path after two dashes all combine.
- Find a change by codegit log -S"string" -- path: the commits where the string appeared or vanished.
- Examine what you foundgit show <hash> in full; if needed, compare a range such as main..branch.
Start learning this in your own space
The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.
Check yourself
1.Which git log flag finds commits where the number of occurrences of a line of code changed?
2.What does git log main..feature show?
3.A file's history stops where it was renamed. Which flag fixes that?
Sources
-
git log referenceEvery filter and output formatfree
-
Pro Git book“Git Basics” — the section on viewing the commit historyfree
-
git show referenceA detailed look at a single commitfree
Was this helpful?