Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

By updated

Improve this page

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:

Terminal window
git log

Expected 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 d756a03e9afeeaa3310f84a7f250597af9059226
Author: Stéphane ROBERT <stephane@example.com>
Date: Sat Mar 28 10:15:00 2026 +0100
Creation of the project structure
commit 4e527538b41cf8cba24d115df64d337f82e1f28a
Author: Stéphane ROBERT <stephane@example.com>
Date: Fri Mar 27 16:00:00 2026 +0100
Initial commit

Each 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.

Terminal window
git log --oneline

Expected output:

13b51e7 (HEAD -> main) Addition of the contact page
d756a03 Creation of the project structure
4e52753 Initial commit

The 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:

Terminal window
git log --oneline --graph --all --decorate

Expected 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 commit

The |, \ 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:

Terminal window
git log --pretty=format:"%h %an %ar %s"

Expected output:

13b51e7 Stéphane ROBERT 2 hours ago Addition of the contact page
d756a03 Stéphane ROBERT 6 hours ago Creation of the project structure
4e52753 Stéphane ROBERT 1 day ago Initial commit

The 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:

PlaceholderMeaningExample
%HFull SHA13b51e7deade...
%hAbbreviated SHA13b51e7
%anAuthor nameStéphane ROBERT
%aeAuthor emailstephane@example.com
%arRelative date2 hours ago
%aiISO date (with time zone)2026-03-28 14:30:00 +0100
%sMessage (first line)Addition of the contact page
%dRef 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.

Terminal window
git log --stat --oneline

Expected 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:

Terminal window
git log -p -1

The -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.

Terminal window
git log -5 # The last 5 commits
git log --oneline -10 # The last 10, compact format

Filter by author

Terminal window
git log --author="Stéphane"

Git looks for the pattern in the author's name and email. You can use regular expressions:

Terminal window
git log --author="alice\|bob" # Commits by Alice OR Bob

Filter 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.

Terminal window
git log --since="2026-03-01" # Since March 1st
git log --until="2026-03-15" # Until March 15th
git log --since="2 weeks ago" # Last 2 weeks
git log --since="2026-03-01" --until="2026-03-15" # Precise period

Filter by file

To see only the commits that changed a specific file:

Terminal window
git log -- style.css
git 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

Terminal window
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:

Terminal window
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.

Terminal window
git log -S "API_KEY" --oneline

Combine filters

Filters combine naturally:

Terminal window
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:

Terminal window
git show 13b51e7

Expected output:

commit 13b51e7deade1974a855d5e205f983356f89150b
Author: 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.html
new file mode 100644
index 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:

Terminal window
git show --stat 13b51e7

Output obtained with Git 2.47.3:

commit 13b51e7deade1974a855d5e205f983356f89150b
Author: 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:

Terminal window
git show --stat HEAD~1
commit d756a03e9afeeaa3310f84a7f250597af9059226
Author: 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:

Terminal window
git rev-parse HEAD~1
git log --oneline -2
d756a03e9afeeaa3310f84a7f250597af9059226
13b51e7 Addition of the contact page
d756a03 Creation of the project structure

The 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.

NeedCommand
Basic historygit log
Compact (1 line/commit)git log --oneline
With branch graphgit log --oneline --graph --all
Files changed per commitgit log --stat
Full diffgit log -p
By authorgit log --author="name"
By dategit log --since="date" --until="date"
By filegit log -- path/file
By messagegit log --grep="word"
By code contentgit log -S "code"
Detail of a commitgit show SHA
The commit beforegit 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.

SymptomLikely causeSolution
git log shows nothingNo commit in the repositoryCheck with git status then create a first commit
The output scrolls endlesslyPager active (normal)Press q to quit
git log --all does not show remote branchesBranches not fetched yetgit fetch --all then run it again
git log -S does not find the expected result-S looks for a literal string, metacharacters are not interpretedAdd --pickaxe-regex, or use git log -G "pattern" which takes a regular expression
--grep does not find a message that is thereCase-sensitive searchAdd -i: git log --grep="contact" -i
--author finds nobodyThe name does not match exactlyTry with a fragment: --author="start of the name"

Key takeaways

  • git log --oneline is your daily view of the history
  • git log --graph --all visualizes branches and merges
  • Filter by author (--author), date (--since/--until), file (-- path) or content (-S "code")
  • git show SHA inspects the detailed content of a commit
  • HEAD is the latest commit, HEAD~1 the one before
  • Combine filters for precise searches
  • Create an alias git lg for 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

10 questions
6 min
70% to pass

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