Viewing history
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
git logcommit 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 headerNewest 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
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:
git config --global alias.lg "log --oneline --graph --all --decorate"
git lgFiltering
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 mainThe -- 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
git show c91e5a0 # message plus the full diff
git show --stat c91e5a0 # message plus a list of changed files
git show HEAD # the latest commitNaming 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:
git show HEAD~2:prices.json
git show v1.0:src/app.js > /tmp/old-app.js # save it somewhere to compareThe 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:
git blame prices.jsonb63f15cc (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 -20to see what’s been happening recently.
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”.
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 heremkdir 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"
# 1. One line per commit
git log --oneline
# 2. Only Ben's commits
git log --oneline --author="Ben"
# 3. Commits that touched prices.json
git log --oneline -- prices.json
# 4. Which files each commit changed
git log --stat
# 5. prices.json as it was two commits ago
git show HEAD~2:prices.json # {"tea": 3}
# 6. The commit whose message mentions "discount"
git log --oneline --grep="discount" -iSummary
git loglists commits newest first;--oneline,--graph,--statand-pchange 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.HEADis the current commit;HEAD~1,HEAD~2are 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.