Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 78 ยท The Basic Workflow

Viewing History: log, show and Formatting

Master git log and git show: oneline and graph views, filtering by author, date, message and path, custom pretty formats, and ranges like HEAD~2..HEAD.

You make commits to be read later. git log is the command that reads them, and git show looks closely at one. Together they answer the questions every developer asks daily: What happened to this project? Who changed this file and when? What did the last commit actually do? Which commits are on my branch but not on main?

Plain git log is verbose, but it has dozens of options that turn it into a precise search tool. In this lesson you will learn the handful you will use all the time: compact and graph views, limiting by count, date, author, message and file, custom formats, and the revision syntax for ranges. Every example runs against a small repository with a branch and a merge that each TryIt builds for itself.

A history to explore

To make output predictable, every example below first builds this repository with fixed dates (one commit per day in January 2026). The history looks like this:

TEXT
 Add app and README <- Add login form <- Document login <+- Fix login bug (main) <-- Merge feature
                                                         |                                 /
                                                         +- Start feature (feature) ------+

Ada makes most commits, Grace wrote "Document login", and a branch named feature was merged back into main with a merge commit. A tag v1.0 marks the merge.

The default log

Bash git log, git log --oneline and the graph

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The default format shows, for each commit, the full 40-character hash, the author, the date and the message. Commits appear newest first. A line starting with Merge: lists the two parents of a merge commit.

git log --oneline squeezes each commit onto one line: a shortened hash and the subject. This is the view you will use most. The graph view adds the history's shape. Read it from top to bottom, newest at the top: the * marks a commit, |\ shows the history splitting into two lines, |/ shows them joining, and the names in parentheses (HEAD -> main, feature, tag: v1.0) are branches and tags pointing at that commit. The options are worth remembering as a bundle, so many people create an alias for git log --oneline --graph --decorate --all (see Aliases and Everyday Productivity):

Option Effect
--oneline Short hash and subject on one line
--graph Draw the branch structure with ASCII lines
--decorate Show branch and tag names next to commits (the default on a terminal)
--all Start from every branch and tag, not just the current branch
--stat Add a list of changed files with line counts
-p / --patch Add the full diff of each commit
-n, or -3 Show at most that many commits

On a terminal Git sends long output to a pager (usually less): press q to leave, space to page. Our sandbox disables the pager, so you see everything at once.

Seeing what changed: --stat and -p

git log --stat lists the files each commit touched. git log -p shows the whole diff, the same way git diff does. Both are often combined with -n to look at recent work, and with a format that keeps things compact.

Limiting the output

A real project has thousands of commits, so you almost always narrow the list. Filters combine: Git shows commits that satisfy all of them.

Bash Filtering history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

What each filter does:

  • -n 2 (or -2) limits the number of commits.
  • --author=Grace matches the author name or email as a regular expression. --committer= does the same for the committer.
  • --grep=login matches the commit message. Add -i for case-insensitive and --all-match if you give several --grep options and need all of them.
  • --since and --until (synonyms --after and --before) accept dates like 2026-01-03, and also phrases like "2 weeks ago". The range excludes the 5th at 12:00 because --until=2026-01-05 means midnight at the start of that day.
  • -- feature.txt restricts the log to commits that changed that path. The double dash separates revisions from file names, so Git never mistakes a path for a branch. Always use it when a name could be both. Note that the merge commit is not listed: by default Git simplifies history for a path and omits merges that did not themselves bring changes you can attribute.
  • --no-merges hides merge commits and --merges shows only them.

To follow one file through renames use git log --follow -- file, and to search for the commits that added or removed a piece of text use git log -S "text". Both are covered in blame, grep and Searching History.

Choosing your own format

--format (alias --pretty=format:) lets you print exactly what you need using % placeholders. This is how scripts and release tools read history.

Bash Pretty formats and dates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Useful placeholders:

Placeholder Meaning
%H / %h Full / abbreviated commit hash
%an / %ae Author name / email (%cn / %ce for the committer)
%ad / %cd Author / committer date (shaped by --date=)
%ar Relative date such as "3 weeks ago" (changes every time you run it)
%s / %b / %B Subject / body / raw message
%d Branch and tag names (decorations)
%n A newline
%C(yellow)...%C(reset) Colors, shown on a terminal

--date accepts short (2026-01-01), iso, relative, unix, or your own format: string using the usual strftime codes, such as %Y. git shortlog -sn HEAD is a quick summary: commits per author (-s summary, -n sorted by number; naming HEAD stops it from waiting for input on stdin). The built-in names --pretty=short, medium, full, fuller and reference give increasing levels of detail without writing a format.

Colored logs

Git colors its output automatically when it writes to a terminal (color.ui=auto is the default), and git log --color=always forces colors even into a pipe or file. For your own colored format use %C(...) codes: %C(yellow)%h%C(reset) colors the hash, %C(auto)%d%C(reset) colors decorations the way Git does by default, and %C(green)%ar%C(reset) colors the date. A popular everyday alias is:

CONSOLE
$ git log --graph --pretty=format:'%C(yellow)%h%C(reset) %C(auto)%d%C(reset) %s %C(green)(%ar)%C(reset)'

Named colors are black red green yellow blue magenta cyan white, optionally with bold, dim or ul (for example %C(bold red)). In a script you want plain text, so add --no-color. The sandbox output is not a terminal, so these colors are not shown here.

git show: one commit in detail

While log lists many commits, git show displays one object and defaults to the latest commit. For a normal commit it prints the message and the diff against its parent. For a merge commit it shows a compact combined diff. It also accepts tags, trees and files.

Bash git show in its different forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The most useful trick is git show REVISION:path, which prints a file as it existed in any commit, without touching your working tree. The same form :path (just a colon) reads the staged version. --no-patch (or -s) hides the diff, and --stat shows only a summary of which files changed.

Revisions: naming the commits you want

Everything above worked on "the current branch". You can also tell log and show which commits to look at. A few forms cover most needs. Revision Syntax: Naming Commits covers them completely; here is the essential set.

Syntax Meaning
HEAD The commit you are on
HEAD~1 or HEAD~ Its parent; HEAD~2 is the grandparent
HEAD^2 The second parent of a merge commit
main, v1.0, 43ecc21 A branch, tag or abbreviated hash
A..B Commits reachable from B but not from A: "what is new in B"

Note ~ versus ^: HEAD~2 walks back two steps along first parents, while HEAD^2 picks the second parent of a merge commit. They are not the same thing.

Bash Ranges and traversal order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

A..B is the one to remember. Before you open a pull request, git log --oneline main..my-branch lists exactly the commits you are about to propose. --first-parent follows only the first parent at each merge, which gives a clean "what landed on main, in order" view even when feature branches contained many small commits. It is the standard way to read a project that merges branches with merge commits.

Reading history well

  • Start with git log --oneline --graph --decorate --all to get the shape, then zoom in with git show.
  • Narrow before you read: --author, --since, --grep and a path cut thousands of commits down to a handful.
  • git log -p -- path is a file's biography: every change, with the reason in the message.
  • Dates are the author's claim, not a fact; two different clocks can make an older commit appear "newer". Log orders by commit graph, with timestamp as a tie-breaker.
  • git log shows the history reachable from where you point it. Commits on other branches only appear with --all or by naming them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which command shows the history as a compact graph for all branches, with branch names?

    --oneline compacts each commit, --graph draws the shape, --decorate shows names and --all includes every branch and tag.
  2. 2. What does git log main..feature list?

    A..B means reachable from B and not from A. It is the list of commits you would add to main by merging feature.
  3. 3. Why do you write git log -- path with a double dash?

    The double dash removes ambiguity when a name could be both a path and a revision.
  4. 4. How do you print a file as it was in the commit two steps before HEAD, without changing your working directory?

    git show REVISION:path prints the stored content. Checking out or restoring would modify your files.
  5. 5. What is the difference between HEAD~2 and HEAD^2?

    ~N counts generations back, ^N chooses the Nth parent of one commit. Only merge commits have a second parent.

Summary

  • git log lists commits newest first; --oneline --graph --decorate --all is the best overview.
  • Add --stat or -p to see what each commit changed.
  • Filter with -n, --author, --grep, --since/--until, --no-merges and -- path; filters combine.
  • --format with %h %an %ad %s and --date= builds exact output for people and scripts; git shortlog -sn HEAD counts commits per author.
  • git show displays one commit, tag or object; git show REV:path prints a file from history.
  • A..B selects what B has that A lacks; HEAD~N walks back, HEAD^N picks a parent; --first-parent follows the main line.

Next: .gitignore and Untracked Files