Reading history with log and diff
Use git log, git diff and git show to see what changed, when and why, and learn the three comparisons git diff makes: unstaged, staged and between commits.
Reading history with log and diff
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- git log lists commits newest first, and --oneline, --stat and -p change how much detail each entry shows.
- git diff compares the working directory to the staging area, git diff --staged compares the staging area to the last commit, and git diff A B compares two commits.
- HEAD is the commit you are on, and HEAD~1 and HEAD~2 mean one and two commits before it.
- git show displays one commit, or a file as it was in a commit with git show <commit>:<path>.
- A path after -- limits git log to commits that touched that path.
Two questions history answers
Once a project has a few commits, you keep asking two things. What happened here? (git log) and what exactly changed? (git diff and git show). Both only read, so you can run them as often as you like.
This lesson continues in tinynotes after two commits. I made two more: one to number the lines show prints, one to add a clear command. All the output below is from real runs, with my hashes.
git log and its useful options
$ git log --oneline
8d1773c Add a clear command
b6062b1 Number the notes in show
ea12739 Add a README
7740572 Add notes.py with add and show
--oneline squeezes each commit to a shortened hash and its message. It is the view you will use most. The plain git log shows the full hash, author and date. Both open in a pager when the output is long, so press q to leave it.
The commit message is the reason to write them well. This list is a table of contents for the project, and a vague message makes it useless. The next group covers writing good ones.
Add --stat to see which files each commit touched:
$ git log --oneline --stat
8d1773c Add a clear command
notes.py | 2 ++
1 file changed, 2 insertions(+)
b6062b1 Number the notes in show
notes.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
ea12739 Add a README
README.md | 6 ++++++
1 file changed, 6 insertions(+)
Add -p for the full patch of each commit. -1 limits how many commits to show, so git log -p -1 is the last commit in full:
$ git log -p -1
commit 8d1773c95b873a2ac44e727314036308d4092775
Author: Ada Example <ada@example.com>
Date: Thu Jan 8 10:00:00 2026 +0000
Add a clear command
diff --git a/notes.py b/notes.py
index 6dad463..8458c7d 100644
--- a/notes.py
+++ b/notes.py
@@ -19,3 +19,5 @@ if __name__ == "__main__":
add(" ".join(sys.argv[2:]))
elif sys.argv[1] == "show":
show()
+ elif sys.argv[1] == "clear":
+ open(NOTES, "w").close()
You can also filter. git log --oneline --author=Ada --since=2026-01-07 showed two of my commits. git log --oneline -- README.md showed only the commit that touched the README. The -- separates options from paths, so Git doesn’t mistake a file name for something else.
Reading a diff
The format in that patch is worth learning once. A line starting with - was removed, a line starting with + was added, and a line with neither is unchanged context. The @@ -19,3 +19,5 @@ header says the hunk starts at line 19 in the old version, runs three lines there, and in the new version starts at line 19 and runs five. The index 6dad463..8458c7d line shows the short IDs of the file’s content before and after. Those IDs come back in the lesson on how Git stores history.
The three comparisons git diff makes
git diff always compares two things. Which two depends on what you type. Here I edited show in notes.py but had not yet staged it:
$ git diff
diff --git a/notes.py b/notes.py
index 19f69ec..6dad463 100644
--- a/notes.py
+++ b/notes.py
@@ -10,7 +10,8 @@ def add(text):
def show():
with open(NOTES) as f:
- print(f.read(), end="")
+ for number, line in enumerate(f, start=1):
+ print(f"{number}. {line}", end="")
if __name__ == "__main__":
git diffcompares the working directory with the staging area, so it shows unstaged edits.git diff --stagedcompares the staging area with the last commit, so it shows what the next commit will contain.git diff <a> <b>compares any two commits.
After git add notes.py, I ran plain git diff again and it printed nothing at all, while git diff --staged showed the same patch. This is the single most common diff surprise. Empty output doesn’t mean the change vanished, only that nothing is left unstaged.
For the third form, HEAD is the commit you’re on and HEAD~1 is its parent, HEAD~2 its grandparent:
$ git diff HEAD~2 HEAD --stat
notes.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
That is the combined effect of the last two commits. --stat summarises instead of printing the patch.
git show for one commit
$ git show --stat HEAD
commit 8d1773c95b873a2ac44e727314036308d4092775
Author: Ada Example <ada@example.com>
Date: Thu Jan 8 10:00:00 2026 +0000
Add a clear command
notes.py | 2 ++
1 file changed, 2 insertions(+)
git show is the log entry plus its patch for a single commit. With a colon it reads a file from the past without touching your files:
$ git show HEAD~1:README.md
# tinynotes
A tiny command-line notes tool.
python3 notes.py add "buy milk"
python3 notes.py show
What the video covers
The embed plays the “Basics” chapter of MIT’s Missing Semester lecture 5, “Version Control and Git” (2026), from 38:37 to 48:24, about ten minutes of a 70-minute lecture. The creator’s chapter list names it “Basics”, and the matching lecture notes list the commands it covers: git init, git status, git add, git commit, git log, git log --all --graph --decorate, git diff <filename>, git diff <revision> <filename> and git checkout <revision>. So it covers both log and diff, though it opens with the commands you already know before reaching them. I couldn’t read a transcript, so I haven’t checked how much time each command gets. The lecture is in English, judged from the title and description. A shot of code’s video on git diff and Boot.dev’s 73-second tour of git log options are linked as focused extras.
Where this fits
You can now read history. Next, how Git actually stores the thing you’ve been reading: how Git stores history.
Check yourself
You staged a change and then ran git diff, which printed nothing. Is the change lost?
No. Plain git diff shows only unstaged changes, and yours are staged. Run git diff --staged to see them.
What does git diff HEAD~2 HEAD compare?
The state of the project two commits ago against the latest commit, showing everything that changed across those two commits.
How do you list only the commits that touched README.md?
git log -- README.md, with the path after a double dash.
How do you read what a file looked like in an older commit, without changing your files?
git show <commit>:<path>, for example git show HEAD~1:README.md. It prints the file and leaves your working directory alone.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.