Reading History

Table of Contents

  1. git log: The List of Snapshots
  2. Commit Hashes
  3. HEAD: You Are Here
  4. git diff: What Changed?
  5. git show: Look Inside One Commit
  6. Finding Things in History
  7. Writing Good Commit Messages
  8. Try It Yourself

1. git log: The List of Snapshots

Inside my-site, run:

git log
commit 8b1e4d07c2f5a9e3b6d1c0f4a7e2b9d8c3f6a1e5 (HEAD -> main)
Author: Dara Sok <dara@example.com>
Date:   Mon Oct 5 10:24:13 2026 +0700

    Add .gitignore

commit c47a2e91d3b8f6a0e5c2d7b4a9f1e3c8d6b0a2f7
Author: Dara Sok <dara@example.com>
Date:   Mon Oct 5 10:18:40 2026 +0700

    Add where I live

commit 3f2a1c95e8d7b4a6c0f3e1d9b2a8c7f5e4d6b1a3
Author: Dara Sok <dara@example.com>
Date:   Mon Oct 5 10:11:02 2026 +0700

    Add home page

The newest commit is at the top. Each one shows its ID, author, date, and message. (Your IDs and dates will be different, and you may have a few more commits from the last chapter's exercises.)

If the list is long, it opens in the pager — scroll with the arrow keys and press q to quit.

For a compact view, one line per commit:

git log --oneline
8b1e4d0 (HEAD -> main) Add .gitignore
c47a2e9 Add where I live
3f2a1c9 Add home page

You'll use --oneline far more often than the full version.

2. Commit Hashes

That long ID — 8b1e4d07c2f5a9e3b6d1c0f4a7e2b9d8c3f6a1e5 — is the commit's hash. It's 40 hexadecimal digits (the base-16 numbers you met in Bits and Bytes), calculated from everything in the commit: the file contents, the author, the date, the message, and the commit that came before it.

That has two useful consequences:

  • Every commit has a unique ID. No two commits, anywhere in the world, share one in practice.
  • History can't change silently. If anyone changed even one letter of an old commit, its hash would change — and so would the hash of every commit after it. Git would notice immediately.

You never need to type all 40 digits. The first 7 or so are enough for Git to know which commit you mean, which is why --oneline shows 8b1e4d0.

3. HEAD: You Are Here

In the log you saw (HEAD -> main). HEAD is Git's "you are here" marker: it points to the commit you're currently on, which is usually the newest one on your current branch.

HEAD is handy for referring to commits without copying hashes:

You typeIt means
HEADThe commit you're on now
HEAD~1One commit before that
HEAD~2Two commits before

You'll use these in the next chapter to undo things.

4. git diff: What Changed?

Open index.html and add a line under "I live in Phnom Penh":

    <p>My favorite food is num banh chok.</p>

Save it, but don't stage it yet. Now ask Git what changed:

git diff
diff --git a/index.html b/index.html
index 5d2f1a3..9c8e4b7 100644
--- a/index.html
+++ b/index.html
@@ -7,5 +7,6 @@
     <h1>Hi, I'm Dara</h1>
     <p>I'm learning Git.</p>
     <p>I live in Phnom Penh.</p>
+    <p>My favorite food is num banh chok.</p>
   </body>
 </html>

How to read it:

  • --- a/ and +++ b/ — the old version and the new version of index.html.
  • @@ -7,5 +7,6 @@ — where in the file this is: starting at line 7, the old version had 5 lines here and the new one has 6.
  • Lines starting with + were added (green in most terminals).
  • Lines starting with - were removed (red). There are none here.
  • Lines with no sign are unchanged context, shown so you can see where the change is.

A changed line shows up as a removed line followed by an added one.

Now stage the change and run git diff again:

git add index.html
git diff

Nothing! That surprises everyone the first time. Plain git diff shows only changes that are not staged yet. To see what's in the staging area — what the next commit will contain — use:

git diff --staged

Reading git diff --staged before every commit is a great habit. It's the last chance to spot a leftover debug line or a file you didn't mean to include. Go ahead and commit:

git commit -m "Add favorite food"

You can also compare any two commits:

git diff 3f2a1c9 HEAD     # everything that changed since the first commit

5. git show: Look Inside One Commit

To see what a single commit changed, use git show with its hash:

git show c47a2e9

It prints the commit's author, date, and message, followed by its diff. git show on its own shows the latest commit (HEAD).

6. Finding Things in History

A few more ways to ask questions about the past:

git log --oneline -5          # only the last 5 commits
git log --oneline index.html  # only commits that changed index.html
git log -p                    # every commit with its full diff
git log --author="Dara"       # only commits by one person
git log --grep="food"         # commits whose message mentions "food"

And one of the most useful commands in a team:

git blame index.html

It shows every line of the file next to the commit and author that last changed it. When you find a confusing line of code, git blame tells you who wrote it and which commit to read for the reason why. (GitHub has the same view: open any file and click Blame.)

7. Writing Good Commit Messages

History is only as useful as its messages. Compare these two logs:

a1b2c3d fix
e4f5a6b stuff
b7c8d9e changes
f0a1b2c wip
a1b2c3d Fix broken link to about page
e4f5a6b Add contact email to footer
b7c8d9e Make heading smaller on phones
f0a1b2c Add about page

The second one tells the story of the project. Six months from now, you'll be able to find exactly the commit you're looking for.

Simple rules most teams follow:

  1. Describe what the commit does, as a command: "Add about page", not "Added about page" or "Adding about page". A good test: the message should finish the sentence "If applied, this commit will…".
  2. Keep the first line short — around 50 characters — so it fits in git log --oneline and on GitHub.
  3. One commit, one idea. If your message needs the word "and", it might be two commits.
  4. Explain why when it isn't obvious. For that, write the message in your editor instead of using -m: run git commit, put the short summary on the first line, leave a blank line, then write as much as you need.
Remove autoplay from the intro video

Autoplaying video uses a lot of mobile data, and several visitors
on phones said the page felt slow. The video now starts only when
clicked.

8. Try It Yourself

  1. Read your history with git log, then git log --oneline. Press q if you end up in the pager.
  2. Find HEAD. Which commit has (HEAD -> main) next to it?
  3. Use the staging-area diffs. Change your <h1> heading. Run git diff (you see the change), git add index.html, git diff (empty), then git diff --staged (there it is). Commit it with a good message.
  4. Look inside an old commit. Pick the hash of "Add where I live" from git log --oneline and run git show with it.
  5. Ask who wrote what. Run git blame index.html. Every line is you, for now — that changes when you work with someone else.
  6. Write a multi-line message. Make a small change, stage it, and run git commit without -m. Write a short summary, a blank line, and a sentence explaining why. Then view it with git log.

Now that you can read history, you can use it to undo mistakes.

Buy Me a Coffee

Found this tutorial helpful? Consider buying me a coffee to support more free content!