This guide teaches you to explore your commit history with git log. You
will filter by author, by date or by file, search for a word in the changes and
format the output to make it readable. These gestures help you understand how a
project evolved and find the origin of a change.
Prerequisites: a repository with a few commits.
What you will learn
git log: display the commit history- Formatting options:
--oneline,--graph,--pretty - Filters: by author, date, file, number of commits
- Search: find a commit by its content or its message
git show: inspect the detailed content of a commit
The basic history
The git log command displays the list of commits, from the most recent to the
oldest:
git logExpected output:
commit 13b51e7deade1974a855d5e205f983356f89150b (HEAD -> main)Author: Stéphane ROBERT <stephane@example.com>Date: Sat Mar 28 14:30:00 2026 +0100
Addition of the contact page
commit d756a03e9afeeaa3310f84a7f250597af9059226Author: Stéphane ROBERT <stephane@example.com>Date: Sat Mar 28 10:15:00 2026 +0100
Creation of the project structure
commit 4e527538b41cf8cba24d115df64d337f82e1f28aAuthor: Stéphane ROBERT <stephane@example.com>Date: Fri Mar 27 16:00:00 2026 +0100
Initial commitEach commit shows its full SHA, the author, the date and the message. On a
project with many commits, this output scrolls. Press q to quit the pager.
The examples in this guide come from a small test repository (the graph, further
down, from a variant with a branch). Your SHAs, dates and names will be
different. (HEAD -> main) indicates that you are on the main branch and that
this commit is its latest; the section "Refer to a commit with HEAD and HEAD~1"
details HEAD.
Format the output
Compact summary with --oneline
This is the usual reading format: each commit fits on one line.
git log --onelineExpected output:
13b51e7 (HEAD -> main) Addition of the contact paged756a03 Creation of the project structure4e52753 Initial commitThe SHA is abbreviated automatically. The length is not fixed: Git chooses the
number of characters needed for the identifier to stay unique in your
repository, which the core.abbrev setting controls, with a default value of
auto. On a brand-new repository you will see 7 characters; on a repository
with several thousand commits, Git moves to 8 or more by itself. So never
hard-code "7" in a script that compares SHAs.
Branch graph with --graph
To visualize branches and merges in the terminal:
git log --oneline --graph --all --decorateExpected output (project with branches):
* 913676d (HEAD -> main) Merge branch 'feature/contact'|\| * 999dd51 (feature/contact) Addition of the contact form| * 9babff9 Form styles|/* d756a03 Creation of the project structure* 4e52753 Initial commitThe |, \ and / lines draw the branches. The star * marks each commit.
Here, feature/contact split off from main after commit d756a03, then the
merge commit 913676d brought them back together.
Custom format with --pretty
For full control over the display:
git log --pretty=format:"%h %an %ar %s"Expected output:
13b51e7 Stéphane ROBERT 2 hours ago Addition of the contact paged756a03 Stéphane ROBERT 6 hours ago Creation of the project structure4e52753 Stéphane ROBERT 1 day ago Initial commitThe table below lists the most useful substitutions. One distinction comes up
constantly and is worth fixing right away: the a prefix designates the
author, the one who wrote the change, while the c prefix designates the
committer, the one who created the commit object. On an ordinary history the
two are identical, but a rebase or the application of a patch sent by a third
party separates them. If you are looking for "who did this", take %an; if you
are looking for "when did this arrive in this branch", take %cd.
The main placeholders:
| Placeholder | Meaning | Example |
|---|---|---|
%H | Full SHA | 13b51e7deade... |
%h | Abbreviated SHA | 13b51e7 |
%an | Author name | Stéphane ROBERT |
%ae | Author email | stephane@example.com |
%ar | Relative date | 2 hours ago |
%ai | ISO date (with time zone) | 2026-03-28 14:30:00 +0100 |
%s | Message (first line) | Addition of the contact page |
%d | Ref names (branches, tags) | (HEAD -> main) |
See the changed files with --stat
A commit message is sometimes vague, and a full diff sometimes too long to
read. --stat gives you something in between: the list of files touched by
each commit, with their number of lines. It helps to check that a commit did not
touch anything other than what its message announces: a commit titled "typo fix"
that changes 400 lines in 12 files deserves an inspection.
git log --stat --onelineExpected output:
13b51e7 Addition of the contact page contact.html | 25 +++++++++++++++++++++++++ style.css | 8 ++++++++ 2 files changed, 33 insertions(+)--stat displays the list of modified files with the number of lines added
(+) and removed (-).
See the full changes with -p
To display the full diff of each commit:
git log -p -1The -1 option limits the output to a single commit. Without this limit,
git log -p displays the diff of every commit (very verbose on a long
history).
Filter the history
Limit the number of commits
The number is written directly attached to the dash, with no option name. This
filter applies after all the others: git log --author="Alice" -5 returns
Alice's last 5 commits, not Alice's commits among the last 5 of the repository.
The distinction completely changes the result on a repository with several
contributors.
git log -5 # The last 5 commitsgit log --oneline -10 # The last 10, compact formatFilter by author
git log --author="Stéphane"Git looks for the pattern in the author's name and email. You can use regular expressions:
git log --author="alice\|bob" # Commits by Alice OR BobFilter by date
These options accept both an absolute date and an expression in English such as
2 weeks ago, yesterday or last monday. Two clarifications avoid surprises.
Both bounds are inclusive, so a commit timestamped exactly on the date you
pass as an argument does show up in the results. Above all, the filter applies
to the committer date, not the author date: a commit written in January and
then replayed by a rebase in June comes out with --since="2026-05-01" and
stays invisible if you search in the January window.
git log --since="2026-03-01" # Since March 1stgit log --until="2026-03-15" # Until March 15thgit log --since="2 weeks ago" # Last 2 weeksgit log --since="2026-03-01" --until="2026-03-15" # Precise periodFilter by file
To see only the commits that changed a specific file:
git log -- style.cssgit log --oneline -- src/auth/The -- separates the options from the list of paths. It is optional when
there is no ambiguity, but it is a good habit to always put it.
Filter by message
git log --grep="contact"Searches for the pattern "contact" in commit messages. The pattern is treated as a regular expression, not as literal text: a dot or a parenthesis in your search will therefore take its metacharacter meaning.
The search is case-sensitive by default: add -i to ignore case. Several
--grep options combine with a logical OR, unless you add --all-match to
require that all the conditions be met.
Filter by changed content
To find the commit that added or removed a word in the code:
git log -S "function login"The -S option, nicknamed pickaxe, finds the moment when a function, a
variable or a configuration key was introduced or removed. Its exact criterion
is worth knowing: Git does not look for the commits that mention the string,
but those where the number of occurrences changed. A commit that moves a
line without modifying it therefore does not appear, which is precisely the
point of the option. The string is treated literally; for a regular expression,
use -G instead. The lesson
search in Git (in French)
covers -S, -G and -L.
git log -S "API_KEY" --onelineCombine filters
Filters combine naturally:
git log --author="Alice" --since="2026-03-01" --oneline -- src/This command displays Alice's commits since March 1st that touched files in the
src/ folder.
Inspect a commit with git show
To see the detailed content of a specific commit:
git show 13b51e7Expected output:
commit 13b51e7deade1974a855d5e205f983356f89150bAuthor: Stéphane ROBERT <stephane@example.com>Date: Sat Mar 28 14:30:00 2026 +0100
Addition of the contact page
diff --git a/contact.html b/contact.htmlnew file mode 100644index 0000000..b9ece5a--- /dev/null+++ b/contact.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<html>+...git show displays the commit metadata and the full diff of the changes.
You can use an abbreviated SHA: it is enough as long as it stays unique.
To see the modified files and their number of lines, without the full diff:
git show --stat 13b51e7Output obtained with Git 2.47.3:
commit 13b51e7deade1974a855d5e205f983356f89150bAuthor: Stéphane ROBERT <stephane@example.com>Date: Sat Mar 28 14:30:00 2026 +0100
Addition of the contact page
contact.html | 25 +++++++++++++++++++++++++ style.css | 8 ++++++++ 2 files changed, 33 insertions(+)Refer to a commit with HEAD and HEAD~1
HEAD designates the commit you are on, the latest of the current branch.
HEAD~1 designates the commit before it, its parent, and HEAD~2 goes up two
commits. These names save you from copying a SHA: you place them anywhere
git show or git log expects a commit.
On the history of this page, git show --stat HEAD displays commit 13b51e7
and git show --stat HEAD~1 displays the one before:
git show --stat HEAD~1commit d756a03e9afeeaa3310f84a7f250597af9059226Author: Stéphane ROBERT <stephane@example.com>Date: Sat Mar 28 10:15:00 2026 +0100
Creation of the project structure
index.html | 4 ++++ style.css | 10 ++++++++++ 2 files changed, 14 insertions(+)To check that HEAD~1 does point to the previous commit, compare its full SHA,
which git rev-parse gives, with the second line of git log:
git rev-parse HEAD~1git log --oneline -2d756a03e9afeeaa3310f84a7f250597af905922613b51e7 Addition of the contact paged756a03 Creation of the project structureThe full SHA starts with d756a03: it is the commit on the second line, so
HEAD~1 does designate the commit before. For other ways to designate a commit
(HEAD^, A..B ranges), see the lesson
revision selection (in French).
Command summary
This table is read from the left column: start from the need, not from the
option. This is the right reflex with git log, whose options are numerous but
combine freely in a single command line. The first five lines are about the
formatting of the output, the next five about the selection of the
displayed commits, and the last two serve to inspect a commit. You will
often mix the first two families, such as a formatting --oneline with a
selection --author.
| Need | Command |
|---|---|
| Basic history | git log |
| Compact (1 line/commit) | git log --oneline |
| With branch graph | git log --oneline --graph --all |
| Files changed per commit | git log --stat |
| Full diff | git log -p |
| By author | git log --author="name" |
| By date | git log --since="date" --until="date" |
| By file | git log -- path/file |
| By message | git log --grep="word" |
| By code content | git log -S "code" |
| Detail of a commit | git show SHA |
| The commit before | git show HEAD~1 |
Troubleshooting: common problems
A "git log finds nothing" often comes from one of these three causes: the
command searches where you do not think it does, the pattern is not interpreted
as you think, or the local history is not up to date. Before concluding that a
commit has disappeared, run the same search again without any filter: if the
commit appears, then a filter does rule it out, and the table below will tell
you which one.
| Symptom | Likely cause | Solution |
|---|---|---|
git log shows nothing | No commit in the repository | Check with git status then create a first commit |
| The output scrolls endlessly | Pager active (normal) | Press q to quit |
git log --all does not show remote branches | Branches not fetched yet | git fetch --all then run it again |
git log -S does not find the expected result | -S looks for a literal string, metacharacters are not interpreted | Add --pickaxe-regex, or use git log -G "pattern" which takes a regular expression |
--grep does not find a message that is there | Case-sensitive search | Add -i: git log --grep="contact" -i |
--author finds nobody | The name does not match exactly | Try with a fragment: --author="start of the name" |
Key takeaways
git log --onelineis your daily view of the historygit log --graph --allvisualizes branches and merges- Filter by author (
--author), date (--since/--until), file (-- path) or content (-S "code") git show SHAinspects the detailed content of a commitHEADis the latest commit,HEAD~1the one before- Combine filters for precise searches
- Create an alias
git lgfor the daily graph
Knowledge check
Check that you have grasped the essentials of this guide. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: First steps in Python: first program, variables and types
A new full quiz with random questions
Work again only on the questions you missed