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.
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
git logcommit 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 listThis 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:
git log --oneline --graph --decorate* 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:
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.txtshows only commits that changed that path. The double dash separates paths from other arguments.--authormatches the author name or email.--sinceand--untilaccept dates such as2026-11-01or phrases like2 weeks ago.--grepsearches 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 onlyb7ee981 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:
git log --format="%h %an %s" -36c3b130 Asha Rao List Austen and mark Dune as paperback
390ecf5 Asha Rao Start the authors list
b7ee981 Asha Rao Add Emma to the catalogueNaming 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
git show --stat HEADcommit 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:
git show HEAD:books.txtDune (paperback)
Emmagit 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:
git diff HEAD~2 HEAD --stat 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
git blame books.txt6c3b1301 (Asha Rao 2026-11-10 10:00:00 +0000 1) Dune (paperback)
b7ee9818 (Asha Rao 2026-11-10 10:00:00 +0000 2) EmmaEach 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 diffandgit diff --staged. An empty plain diff aftergit addis 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
- In a repository with at least four commits, list them as one line each with a graph.
- Show only the commits that changed one particular file.
- Show the full patch of the commit before the latest one.
- Compare the first and last commits and show only the names of changed files.
Show one possible answer
git log --oneline --graph --decorate
git log --oneline -- books.txt
git show HEAD~1
git diff HEAD~3 HEAD --name-onlyAdjust HEAD~3 to match how many commits your repository has: it should point to the first one.
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.