Browsing History With git log: Formatting, Graphs, Filtering and Searching
Find anything in a project's history with git log: one-line and graph views, showing patches and changed files, custom formats and dates, limiting by count, date, author and message, finding when code was added with -S and -G, the history of one file or a range of lines, and comparing branches.
kvi5hnuCode Sprout team member9 min readOn this page
git log lists commits. With no options it shows every commit reachable from where you are, newest first, but its real power is in its options: it can draw the branch structure, show what each commit changed, and find the one commit that added a particular line, among thousands. This article covers the options you'll reach for most.
All the examples use the same small history: a few commits on main, a side branch lake with two commits by Ravi, a merge, and a later change by Mei. Each commit gets its own date by setting GIT_AUTHOR_DATE and GIT_COMMITTER_DATE before committing, since the sandbox clock is otherwise fixed.
The default view, and a compact one
git init -q trail-guide && cd trail-guide
at() { export GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1"; }
at 2026-02-02T09:00; echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
at 2026-02-03T10:00; echo "Ridge loop: 8 km" > trails.txt; git add .; git commit -q -m "Add ridge loop" --author="Ana Diaz <ana@example.com>"
at 2026-02-05T11:00; git switch -q -c lake; echo "Lake path: 5 km" >> trails.txt; git commit -q -am "Add lake path" --author="Ravi Kumar <ravi@example.com>"
at 2026-02-06T12:00; echo "MAX_GROUP=12" > config.ini; git add .; git commit -q -m "Add group size limit" --author="Ravi Kumar <ravi@example.com>"
at 2026-02-07T09:30; git switch -q main; echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
at 2026-02-09T15:00; git merge -q --no-ff lake -m "Merge branch 'lake'"
at 2026-02-10T08:00; sed -i 's/MAX_GROUP=12/MAX_GROUP=10/' config.ini; git commit -q -am "Lower group size limit to 10" --author="Mei Chen <mei@example.com>"
echo "=== git log -2"
git log -2
echo "=== git log --oneline"
git log --oneline
echo "=== git log --oneline --graph --all"
git log --oneline --graph --all
=== git log -2
commit 42e783c9c06f0f2829765d3d77416d199ca64765
Author: Mei Chen <mei@example.com>
Date: Tue Feb 10 08:00:00 2026 +0000
Lower group size limit to 10
commit 30b8733ea1bef8cbb82736663ea70f278f145784
Merge: 42950bf 01a6dbb
Author: Sam Lee <sam@example.com>
Date: Mon Feb 9 15:00:00 2026 +0000
Merge branch 'lake'
=== git log --oneline
42e783c Lower group size limit to 10
30b8733 Merge branch 'lake'
42950bf Add stylesheet
01a6dbb Add group size limit
be7ef2e Add lake path
c467a68 Add ridge loop
76e8e08 Add README
=== git log --oneline --graph --all
* 42e783c Lower group size limit to 10
* 30b8733 Merge branch 'lake'
|\
| * 01a6dbb Add group size limit
| * be7ef2e Add lake path
* | 42950bf Add stylesheet
|/
* c467a68 Add ridge loop
* 76e8e08 Add README
git logshows each commit's full ID, author, date and message. A merge commit adds aMerge:line with the short IDs of its two parents.-2, or-n 2, limits the output to the newest two commits.--onelineshows one line per commit: the short ID and the summary line. In a terminal, Git also adds decorations such as(HEAD -> main, lake)that show where branches and tags point. The sandbox isn't a terminal, so they're missing above:--decorateadds them anywhere, as the next example shows with%d.--graphdraws the branch structure on the left: each*is a commit, and the lines show parents. Here thelakecommits sit on a side line that rejoinsmainat the merge.--allincludes every branch and tag, not just the current branch's history.git log main lakeshows the history of the branches you name.
In a terminal, long output opens in a pager: space pages down, /text searches, and q quits. git --no-pager log prints straight to the terminal instead.
What each commit changed
| Option | Shows for each commit |
|---|---|
--stat | the changed files, with a +/- bar and counts |
--shortstat | only the totals line |
--name-only / --name-status | file names, or names with A, M, D, R status letters |
-p (--patch) | the full diff |
These work with any other options, so git log -p -2 shows the full changes of the last two commits, and git log --stat --author=Ravi shows which files Ravi's commits touched. Merge commits show no diff by default. Add -m, --cc or --first-parent to see one (article 12).
Your own format
--format (or --pretty=format:) takes a template with placeholders:
| Placeholder | Meaning |
|---|---|
%H / %h | full / short commit ID |
%P / %p | parent IDs, full / short |
%an, %ae, %ad | author name, email, date |
%cn, %ce, %cd | committer name, email, date |
%ar, %cr | author / committer date, relative, such as "3 days ago" |
%s | subject (summary line) |
%b / %B | body / whole message |
%d / %D | branch and tag names, with / without the surrounding brackets |
%n | a new line |
%C(yellow), %C(auto), %Creset | colours |
--date chooses how dates appear: short (2026-02-10), iso, iso-strict, relative, human, local, or a pattern such as --date=format:'%d %b %Y'. Ready-made formats are available with --pretty=short, medium (the default), full (adds the committer), fuller (adds both dates) and oneline.
git init -q trail-guide && cd trail-guide
at() { export GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1"; }
at 2026-02-02T09:00; echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
at 2026-02-03T10:00; echo "Ridge loop: 8 km" > trails.txt; git add .; git commit -q -m "Add ridge loop" --author="Ana Diaz <ana@example.com>"
at 2026-02-05T11:00; echo "Lake path: 5 km" >> trails.txt; git commit -q -am "Add lake path" --author="Ravi Kumar <ravi@example.com>"
git tag v1.0
echo "=== short ID, date, author and subject"
git log --format='%h %ad %an: %s' --date=short
echo "=== a custom date format and decorations"
git log -1 --format='%h%d on %ad' --date=format:'%A %d %B %Y'
echo "=== --pretty=fuller shows author and committer separately"
git log -1 --pretty=fuller
=== short ID, date, author and subject
be7ef2e 2026-02-05 Ravi Kumar: Add lake path
c467a68 2026-02-03 Ana Diaz: Add ridge loop
76e8e08 2026-02-02 Sam Lee: Add README
=== a custom date format and decorations
be7ef2e (HEAD -> main, tag: v1.0) on Thursday 05 February 2026
=== --pretty=fuller shows author and committer separately
commit be7ef2ea313fdbb9d6313f255d59e7a121fc7120
Author: Ravi Kumar <ravi@example.com>
AuthorDate: Thu Feb 5 11:00:00 2026 +0000
Commit: Sam Lee <sam@example.com>
CommitDate: Thu Feb 5 11:00:00 2026 +0000
Add lake path
A popular graph view combines these. Many developers save it as an alias (article 34):
git log --graph --all --format='%C(auto)%h%d %s %C(green)(%ar) %C(blue)<%an>%Creset'
Filtering commits
| Option | Keeps commits |
|---|---|
-n 5, -5 | the newest 5 |
--since=2026-02-06, --after=... | committed on or after a date |
--until=2026-02-09, --before=... | committed on or before a date |
--author=Ravi, --committer=... | whose author or committer matches a regular expression (name or email) |
--grep=limit | whose message matches a regular expression |
-i | with --grep and --author: ignore case |
--all-match | with several --grep: matching all of them, not any |
--invert-grep | whose message does not match the --grep patterns |
--merges / --no-merges | merge commits only / excluding merges |
--first-parent | following only the first parent of merges: the branch's own line of commits |
-- path | that changed the given files or folders |
git init -q trail-guide && cd trail-guide
at() { export GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1"; }
at 2026-02-02T09:00; echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
at 2026-02-03T10:00; echo "Ridge loop: 8 km" > trails.txt; git add .; git commit -q -m "Add ridge loop" --author="Ana Diaz <ana@example.com>"
at 2026-02-05T11:00; git switch -q -c lake; echo "Lake path: 5 km" >> trails.txt; git commit -q -am "Add lake path" --author="Ravi Kumar <ravi@example.com>"
at 2026-02-06T12:00; echo "MAX_GROUP=12" > config.ini; git add .; git commit -q -m "Add group size limit" --author="Ravi Kumar <ravi@example.com>"
at 2026-02-07T09:30; git switch -q main; echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
at 2026-02-09T15:00; git merge -q --no-ff lake -m "Merge branch 'lake'"
at 2026-02-10T08:00; sed -i 's/MAX_GROUP=12/MAX_GROUP=10/' config.ini; git commit -q -am "Lower group size limit to 10" --author="Mei Chen <mei@example.com>"
echo "=== 6 to 9 February, with explicit times"
git log --oneline --since="2026-02-06 00:00" --until="2026-02-09 23:59"
echo "=== by Ravi"
git log --oneline --author=Ravi
echo "=== message mentions 'limit' (any case)"
git log --oneline -i --grep=LIMIT
echo "=== no merges, and only main's own line (first parent)"
git log --oneline --no-merges -3
git log --oneline --first-parent
echo "=== commits that changed config.ini"
git log --oneline -- config.ini
=== 6 to 9 February, with explicit times
30b8733 Merge branch 'lake'
42950bf Add stylesheet
01a6dbb Add group size limit
=== by Ravi
01a6dbb Add group size limit
be7ef2e Add lake path
=== message mentions 'limit' (any case)
42e783c Lower group size limit to 10
01a6dbb Add group size limit
=== no merges, and only main's own line (first parent)
42e783c Lower group size limit to 10
42950bf Add stylesheet
01a6dbb Add group size limit
42e783c Lower group size limit to 10
30b8733 Merge branch 'lake'
42950bf Add stylesheet
c467a68 Add ridge loop
76e8e08 Add README
=== commits that changed config.ini
42e783c Lower group size limit to 10
01a6dbb Add group size limit
Dates need care. A date without a time, such as --until=2026-02-09, means that day at the current time of day, not at midnight. So when run before 15, --until=2026-02-09 would leave out the merge at 15 that day. Write explicit times, as above, when the boundary matters. Relative forms such as --since="2 weeks ago" and --since=yesterday also work, measured from now. Date filters use the committer date, which can differ from the author date shown by default, for example after a rebase.
--first-parent is especially useful on a main branch where work arrives through merges: it shows one line per merge, the branch's own story, without the individual commits of every merged branch.
Searching the code's history: -S and -G
Two options search the changes, not the messages. They answer questions like "when was this function added?" or "who removed this setting?":
-S<string>, the "pickaxe", finds commits that changed the number of occurrences of the string: commits that added or removed it.-G<regex>finds commits whose diff contains an added or removed line matching the regular expression, including lines that were only edited.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
echo "MAX_GROUP=12" > config.ini; git add .; git commit -q -m "Add group size limit"
echo "SHOW_MAP=true" >> config.ini; git commit -q -am "Add map setting"
sed -i 's/MAX_GROUP=12/MAX_GROUP=10/' config.ini; git commit -q -am "Lower group size limit to 10"
sed -i '/SHOW_MAP/d' config.ini; git commit -q -am "Remove map setting"
echo "=== -S MAX_GROUP: commits that added or removed the string"
git log --oneline -S MAX_GROUP
echo "=== -G 'MAX_GROUP': commits that changed a line containing it"
git log --oneline -G MAX_GROUP
echo "=== -S SHOW_MAP, with the patch"
git log -S SHOW_MAP --format='--- %h %s' -p
=== -S MAX_GROUP: commits that added or removed the string
f4e3862 Add group size limit
=== -G 'MAX_GROUP': commits that changed a line containing it
67d2414 Lower group size limit to 10
f4e3862 Add group size limit
=== -S SHOW_MAP, with the patch
--- e83867d Remove map setting
diff --git a/config.ini b/config.ini
index 8e08bd4..119027e 100644
--- a/config.ini
+++ b/config.ini
@@ -1,2 +1 @@
MAX_GROUP=10
-SHOW_MAP=true
--- a9ac306 Add map setting
diff --git a/config.ini b/config.ini
index 00d6137..8bf2b6e 100644
--- a/config.ini
+++ b/config.ini
@@ -1 +1,2 @@
MAX_GROUP=12
+SHOW_MAP=true
Changing MAX_GROUP=12 to MAX_GROUP=10 didn't change how many times MAX_GROUP appears, so -S doesn't list that commit, but -G does, because a line containing it was removed and another added. Add --pickaxe-regex to make -S take a regular expression, and -p to see the matching changes. These searches read every commit's changes, so they can take a while in big repositories: limit them with a path, such as git log -S MAX_GROUP -- config.ini.
The history of one file, or of some lines
git log -- path shows the commits that changed a file. If the file was renamed, the history stops at the rename. --follow continues past renames, for a single file:
git init -q trail-guide && cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\n' > ridge.txt
git add .; git commit -q -m "Add ridge notes"
sed -i 's/240 m/260 m/' ridge.txt; git commit -q -am "Correct climb"
mkdir trails; git mv ridge.txt trails/ridge.txt; git commit -q -m "Move notes into trails/"
sed -i 's/8 km/8.5 km/' trails/ridge.txt; git commit -q -am "Correct distance"
echo "=== without --follow: the history stops at the rename"
git log --oneline -- trails/ridge.txt
echo "=== with --follow"
git log --oneline --follow -- trails/ridge.txt
echo "=== -L: the history of line 3 only"
git log -L 3,3:trails/ridge.txt --format='--- %h %s'
=== without --follow: the history stops at the rename
12ff2a1 Correct distance
9e91c24 Move notes into trails/
=== with --follow
12ff2a1 Correct distance
9e91c24 Move notes into trails/
2d2d131 Correct climb
80ed344 Add ridge notes
=== -L: the history of line 3 only
--- 2d2d131 Correct climb
diff --git a/ridge.txt b/ridge.txt
index 1f1753e..7d7e79a 100644
--- a/ridge.txt
+++ b/ridge.txt
@@ -3,1 +3,1 @@
-Climb: 240 m
+Climb: 260 m
--- 80ed344 Add ridge notes
diff --git a/ridge.txt b/ridge.txt
new file mode 100644
index 0000000..1f1753e
--- /dev/null
+++ b/ridge.txt
@@ -0,0 +3,1 @@
+Climb: 240 m
-L narrows the history to a range of lines and shows how they changed in each commit. -L 3,3:file is line 3, and -L 10,+5:file is 5 lines from line 10. -L '/Climb/',+1:file starts at the first line matching a regular expression. For code, -L :function_name:file follows a whole function, found by its name. -L follows renames automatically.
Commits on one branch but not another
git log A..B lists commits reachable from B but not from A: on a feature branch, "what does feature have that main doesn't?". Three dots, A...B, lists commits on either side but not both. With --left-right, each is marked < for A's side or > for B's:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake path"
echo "Boats for hire." >> lake.txt; git commit -q -am "Mention boat hire"
git switch -q main
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
echo "=== main..lake: on lake, not on main"
git log --oneline main..lake
echo "=== lake..main: on main, not on lake"
git log --oneline lake..main
echo "=== main...lake: on either, not both"
git log --oneline --left-right main...lake
echo "=== the same as main..lake, written with ^"
git log --oneline lake ^main
=== main..lake: on lake, not on main
99c82af Mention boat hire
4d8783b Add lake path
=== lake..main: on main, not on lake
d7549b1 Add stylesheet
=== main...lake: on either, not both
< d7549b1 Add stylesheet
> 99c82af Mention boat hire
> 4d8783b Add lake path
=== the same as main..lake, written with ^
99c82af Mention boat hire
4d8783b Add lake path
lake ^main, or lake --not main, is the general form: include commits reachable from the first, exclude those reachable from the second. You can list several of each, as in git log feature1 feature2 ^main. git rev-list takes the same arguments and prints only commit IDs, which is useful in scripts: git rev-list --count main..lake prints 2. Article 29 explains ranges in detail.
Ordering
By default, git log shows commits in reverse chronological order of committer date, without promising that a commit comes before its parents when dates are out of order. Other orders:
--reverse: oldest first.--topo-order: keep each branch's commits together, instead of interleaving them by date. It's often easier to read with--graph, which uses it automatically.--date-orderand--author-date-order: by committer or author date, still never showing a parent before its child.
What's current
- Decorations (
HEAD -> main, branch names) appear automatically in a terminal (log.decorate=auto). Setlog.decorate=shortto always show them. git log --remerge-diffshows how a merge's result differs from what Git would have produced automatically: how the conflicts were resolved (article 13).- Commit-graph files, written automatically by
git maintenanceandgit gc, makegit log --graphand other history walks much faster in large repositories (article 44).
Common questions
How do I see the history of a deleted file?
Use a path with --, as in git log --oneline -- old/config.ini. The -- tells Git it's a path even though the file no longer exists. The first (newest) commit listed is the one that deleted it, and git show <commit>^:old/config.ini shows the file's last version.
What's the difference between --author and --committer?
The author wrote the change and the committer created the commit object (article 6). They differ after rebases, cherry-picks and applied patches. --author is usually what you want.
Why do some of my commits not appear in git log?
git log shows the history of the current branch. Commits on other branches appear with --all or by naming the branch. Commits that no branch points to any more, for example after a reset, are found with git reflog (article 26).
How do I search for text in commit messages on all branches?
git log --all --grep="text". Add -i to ignore case.
How can I see what changed in a specific commit?
git show <commit>, covered in the next article, or git log -p -1 <commit>.
Summary
git loglists commits newest first.--oneline,--graphand--allgive a compact picture of the branches.--stat,--name-statusand-pshow what each commit changed.--formatand--datebuild your own view.- Filter with
-n,--since/--until(give explicit times),--author,--grep,--no-merges,--first-parentand paths. -Sfinds commits that added or removed a string, and-Gfinds commits that changed lines matching a regular expression.--followtracks one file across renames, and-Lfollows a range of lines or a function.A..Blists commits inBbut notA, andA...Bwith--left-rightshows both sides.
Practice questions
You need every commit by Ana in March 2026 that touched files under trails/, one line each. Write the command.
Answer
git log --oneline --author=Ana --since="2026-03-01 00:00" --until="2026-03-31 23:59" -- trails/
The explicit times avoid the "current time of day" behaviour of bare dates. Note that --since and --until filter on the committer date. --author matches part of the name or email.
A setting TIMEOUT=30 was changed to TIMEOUT=60 at some point. Why might git log -S TIMEOUT not find that commit, and what finds it?
Answer
-S TIMEOUT lists commits that changed the number of occurrences of TIMEOUT. Editing the value leaves exactly one occurrence, so that commit isn't listed. git log -G 'TIMEOUT=' lists commits with an added or removed line matching the pattern, so it finds the edit. Or search for the specific value: git log -S 'TIMEOUT=60' finds the commit that introduced it.
What's the difference between git log main..feature and git log feature..main?
Answer
main..feature lists commits on feature that aren't on main, the work a merge would bring in. feature..main lists commits on main that feature doesn't have yet, which is what feature would gain by merging or rebasing onto main.
Previous: Ignoring Files: .gitignore Patterns, Global and Local Ignores, and Untracking Files
Next: Inspecting Commits and Files: git show, diff-tree and Files at Any Revision

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.