Viewing history

Topic 4 of 5 in Your first repository6 min readBeginnerFree course

Overview

Every commit you make is kept. git log and git show let you explore that history: what changed, when, by whom and why. You’ll use them to understand unfamiliar code, to find when something broke, and to check your own work before sharing it.

git log

Terminal
git log
Text
commit c91e5a0f3b8d4e2a1c7f9b6d5e4a3c2b1f0e9d8c (HEAD -> main)
Author: Asha Rao <asha@example.com>
Date:   Fri Dec 4 10:12:44 2026 +0530

    Add pricing page

commit 7d02b4e1c3f0a9e8d7c6b5a4f3e2d1c0b9a8f7e6
Author: Asha Rao <asha@example.com>
Date:   Thu Dec 3 17:40:02 2026 +0530

    Style the header

Newest first. Each entry shows the commit’s ID, author, date and message. (HEAD -> main) marks where you are: you’re on branch main, which points at this commit.

When the output is longer than your screen, Git shows it in a pager: use the arrow keys or Space to scroll, / to search, and q to quit.

Seeing more, or less

Reading git log. The command git log –oneline –graph prints three commits: c91e5a0 (HEAD -> main) Add pricing, 7d02b4e Style the header, and a1f3e9c Create home page. Drawn as a graph, a1 ← 7d ← c9, with main and HEAD pointing at c9; the newest commit is on the right and arrows point to parents. Useful variations: git log for full details, git log –oneline for one line each, git log -p with the changes, git log –stat for the files changed, git log -n 5 for the last five, and git show c91e5a0 for one commit.

Terminal
git log --oneline                 # one line per commit
git log --oneline --graph --all   # draw branches and merges, all branches
git log --stat                    # which files changed, and how much
git log -p                        # the full changes (patch) of every commit
git log -n 5                      # only the last five (or: git log -5)

git log --oneline --graph --all is worth memorising — or better, save it as an alias:

Terminal
git config --global alias.lg "log --oneline --graph --all --decorate"
git lg

Filtering

Terminal
git log --author="Ben"                  # author name or email contains "Ben"
git log --since="2 weeks ago"           # also: --until, --since="2026-11-01"
git log --grep="discount" -i            # message contains "discount" (case-insensitive)
git log -- prices.json                  # commits that changed this file
git log -- src/                         # …or anything in this folder
git log main..feature                   # commits on feature that aren't on main

The -- separates options from file paths. It’s optional when the name can’t be confused with a branch, but it’s a good habit.

Filters combine: git log --author=Ben --since="1 month ago" -- src/ lists Ben’s changes to src/ in the last month.

Looking at one commit: git show

Terminal
git show c91e5a0          # message plus the full diff
git show --stat c91e5a0   # message plus a list of changed files
git show HEAD             # the latest commit

Naming commits

You can refer to a commit in several ways:

Name Meaning
c91e5a0f3b8d… the full 40-character ID
c91e5a0 an abbreviated ID — fine as long as it’s unique in the repository
HEAD the commit you’re on now
HEAD~1 (or HEAD~) its parent — one commit back
HEAD~3 three commits back
main the commit the main branch points to
v1.0 a tag

(HEAD^ also means “the parent”. The difference between ~ and ^ only matters for merge commits, which have two parents; you’ll meet them in Merging branches.)

Old versions of a file

To see a file as it was in an earlier commit — without changing anything in your folder:

Terminal
git show HEAD~2:prices.json
git show v1.0:src/app.js > /tmp/old-app.js    # save it somewhere to compare

The path is from the top of the repository. To actually bring an old version back into your working directory, use git restore --source=HEAD~2 prices.json (you’ll practise that in Restore and reset).

Who changed this line?

git blame annotates each line of a file with the commit and author that last changed it:

Terminal
git blame prices.json
Text
b63f15cc (Ben Ito 2026-12-04 10:12:44 +0530 1) {"tea": 2, "coffee": 4}

It’s how you find the commit — and so the message and pull request — behind a confusing line of code. Searching history in chapter 11 covers it, and other detective tools, in depth.

Graphical history

Git’s command-line log is powerful, but a graphical view helps for complex histories. gitk comes with Git on most systems, VS Code and other editors show history and blame inline, and GitHub has a history view for every file. They all show the same commits — the commands in this lesson are what they run underneath.

Habit: before starting work in an unfamiliar repository, run git log --oneline -20 to see what’s been happening recently.

Exercise

Investigate a project’s history

The script builds a small history with two authors. Answer these questions with Git commands: (1) list all commits on one line each; (2) show only Ben’s commits; (3) show the commits that touched prices.json; (4) show which files each commit changed; (5) show the full content of prices.json as it was two commits ago, without changing your files; (6) find the commit whose message mentions “discount”.

Terminal
mkdir shop && cd shop && git init -b main
c() { git add -A && GIT_AUTHOR_NAME="$1" GIT_AUTHOR_EMAIL="$2" git commit -qm "$3"; }

echo '{"tea": 3}' > prices.json;                c "Asha Rao" asha@example.com "Add price list"
echo "# Shop" > README.md;                      c "Ben Ito" ben@example.com "Write README"
echo '{"tea": 3, "coffee": 4}' > prices.json;   c "Asha Rao" asha@example.com "Add coffee"
echo '{"tea": 2, "coffee": 4}' > prices.json;   c "Ben Ito" ben@example.com "Apply tea discount"

# Your commands here

Summary

  • git log lists commits newest first; --oneline, --graph, --stat and -p change how much detail you see.
  • Filter history by author, date, message (--grep) or path (-- file).
  • git show <commit> displays one commit and its changes; git show <commit>:<path> prints a file as it was then.
  • HEAD is the current commit; HEAD~1, HEAD~2 are its parent and grandparent.
  • Short commit IDs (the first 7 or so characters) work anywhere a full ID does, as long as they’re unique.