Himanshu SharmaSoftware engineer
Let’s talk

Git and GitHub · Lesson 5 of 30

Reading project history with git log, diff and show

Read a project's history with git log, compare versions with git diff, inspect one commit with git show, and filter by author, date, message or path.

14 minYour first commitsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonFind who changed what and when, using git log options, git diff between any two points, git show and git blame.

What you can do after this lesson

You will be able to answer the everyday questions about a project: what happened recently, who changed this file, what exactly did that commit do, and how does today's version differ from last month's. The examples use a small fictional bookshop catalogue repository with four commits. Hashes in your own repository will differ.

Setting up a small history

If you want to follow along, create a repository and make four commits like these.

bash
git init
echo "Dune" >> books.txt && git add books.txt && git commit -m "Add Dune to the catalogue"
echo "Emma" >> books.txt && git add books.txt && git commit -m "Add Emma to the catalogue"
echo "Herbert" >> authors.txt && git add authors.txt && git commit -m "Start the authors list"

A fourth commit lists a second author and renames the Dune entry. The exact contents matter less than having a few commits that touch two files.

git log: the list of commits

bash
git log
text
commit 6c3b130113b6277fffbf1a05d2116e93eb5041fb
Author: Asha Rao <asha@example.com>
Date:   Tue Nov 10 10:00:00 2026 +0000

    List Austen and mark Dune as paperback

commit 390ecf594f9c520cf18e9f798142d524c24b7768
Author: Asha Rao <asha@example.com>
Date:   Tue Nov 10 10:00:00 2026 +0000

    Start the authors list

This is example output; hashes and dates differ for you. Newest commit first. If the history is long, Git uses a pager: press space to scroll and q to quit. The full version is verbose, so most people use the compact one:

bash
git log --oneline --graph --decorate
text
* 6c3b130 (HEAD -> main) List Austen and mark Dune as paperback
* 390ecf5 Start the authors list
* b7ee981 Add Emma to the catalogue
* 30b7680 Add Dune to the catalogue

--graph draws lines when history has branches, which you will see in later lessons. --decorate shows labels such as the branch main and where HEAD points. Add --stat for a summary of files changed, or -p (patch) for the full diff of each commit. git log -2 --stat limits the output to two commits.

Filtering the log

These options narrow the list. Each is verified in the practice repository:

bash
git log --oneline -- authors.txt
git log --author="Asha" --oneline
git log --since="2026-11-01" --oneline
git log --grep="Dune" --oneline
git log -S"Emma" --oneline
  • -- authors.txt shows only commits that changed that path. The double dash separates paths from other arguments.
  • --author matches the author name or email.
  • --since and --until accept dates such as 2026-11-01 or phrases like 2 weeks ago.
  • --grep searches commit messages.
  • -S"text" finds commits where the number of occurrences of the text changed, which locates when something was added or removed. In the example it returns only b7ee981 Add Emma to the catalogue.

You can combine them. For custom layouts, --format accepts placeholders such as %h for the short hash, %an for the author name and %s for the subject:

bash
git log --format="%h %an %s" -3
text
6c3b130 Asha Rao List Austen and mark Dune as paperback
390ecf5 Asha Rao Start the authors list
b7ee981 Asha Rao Add Emma to the catalogue

Naming commits: HEAD, ~ and hashes

You can refer to a commit by a hash (the first seven or so characters are usually enough), by a branch or tag name, or relative to another point. HEAD is where you are. HEAD~1 is its parent, HEAD~2 the grandparent, and so on. Learn these; they appear in almost every Git command.

git show: one commit

bash
git show --stat HEAD
text
commit 6c3b130113b6277fffbf1a05d2116e93eb5041fb
Author: Asha Rao <asha@example.com>
Date:   Tue Nov 10 10:00:00 2026 +0000

    List Austen and mark Dune as paperback

 authors.txt | 1 +
 books.txt   | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

Without --stat, git show prints the message and the complete patch. You can also show a file as it was in an old commit:

bash
git show HEAD:books.txt
text
Dune (paperback)
Emma

git diff: comparing two points

git diff with no arguments compares the working tree with the staging area. With --staged it compares the staging area with the last commit. Given two commits it compares them:

bash
git diff HEAD~2 HEAD --stat
text
 authors.txt | 2 ++
 books.txt   | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

Add -- books.txt to restrict the comparison to one file, --name-only to list just the changed paths, or two branch names to compare branches. If you want to compare two pieces of text outside Git, the text diff tool shows the same added and removed lines in your browser.

git blame: who last touched each line

bash
git blame books.txt
text
6c3b1301 (Asha Rao 2026-11-10 10:00:00 +0000 1) Dune (paperback)
b7ee9818 (Asha Rao 2026-11-10 10:00:00 +0000 2) Emma

Each line is labelled with the commit that last changed it. The name is a pointer to a commit, which you then examine with git show, not a verdict on who is responsible. Treat blame as a way to find the reason for a line, which usually lives in that commit's message.

Getting comfortable

The best way to learn these commands is to use them on a project you did not write. Clone any small public repository, run git log --oneline to see its story, pick one commit and read it with git show, then run git blame on a file you find interesting. Reading other people's history teaches you what good commits and messages look like, and it makes your own habits better.

Common mistakes

  • Reading log without the pager. If the screen seems stuck, press q.
  • Forgetting -- before a path. If a file name matches a branch name, Git may guess wrong; the double dash removes the guess.
  • Confusing git diff and git diff --staged. An empty plain diff after git add is normal.
  • Using blame to point fingers. Use it to find context.
  • Copying hashes with the wrong length. Too few characters can match several commits and Git will say the hash is ambiguous.

Exercise

  1. In a repository with at least four commits, list them as one line each with a graph.
  2. Show only the commits that changed one particular file.
  3. Show the full patch of the commit before the latest one.
  4. Compare the first and last commits and show only the names of changed files.
Show one possible answer
bash
git log --oneline --graph --decorate
git log --oneline -- books.txt
git show HEAD~1
git diff HEAD~3 HEAD --name-only

Adjust HEAD~3 to match how many commits your repository has: it should point to the first one.

Next: Keeping files out of Git with .gitignore.

Key points

  • git log lists commits newest first; --oneline, --graph, --stat and -p change how much detail you see.
  • git log can filter by path, author, date range, message text, or by when a string was added or removed with -S.
  • git diff compares any two commits, branches or the working tree; git show displays one commit or one file at a commit.
  • HEAD~1 means one commit before HEAD, and HEAD~3 means three back.
  • git blame shows which commit last changed each line of a file.

Check yourself

1. Which command gives one compact line per commit?
2. What does HEAD~2 refer to?
3. You want every commit that touched authors.txt. Which command is right?
4. What does git diff HEAD~3 HEAD compare?
5. Which command finds the commit that added or removed the word Emma in the content?

Free tools for this lesson