Git & GitHub Fundamentals · Module 04 of 12

History, diff and object inspection

Read repository history efficiently and compare commits, branches and files using Git object identifiers.

Learning objectives

By the end of this module, you should be able to:

  • Use log, show and diff for different questions.
  • Understand that commit IDs identify immutable commit objects.
  • Compare two points in history without modifying the repository.

History is a graph

Commits reference parent commits, forming a directed history graph. Branch names are movable references to commits; the commits themselves are addressed by object IDs.

Choose the inspection tool

git log summarizes history, git show inspects an object (commonly one commit), and git diff compares content between states. These commands are read-only unless combined with another write operation.

Readable log formats

--oneline --graph --decorate --all provides a useful compact graph when branches diverge or merge. Avoid assuming chronological display alone explains ancestry.

Worked examples

See the idea in practice.

Inspect two commits

git log --oneline --decorate --graph --all
git show --stat HEAD
git diff HEAD~1..HEAD
  • git log answers where commits are.
  • git show answers what one commit contains.
  • git diff answers how two states differ.
Hands-on lab

Lab — Trace how a file changed

Goal: Create three commits and identify exactly which commit changed a line.

Before you start

  • Git 2.23 or newer is recommended so the labs can use git switch and git restore.
  • Run labs in a disposable working directory; the included validator creates and removes its own temporary repository.
STEP 1

Create repository and first commit

mkdir -p ~/gnu-git-labs/module04 && cd ~/gnu-git-labs/module04
git init -b main
git config user.name "GNU Learner"
git config user.email "learner@example.invalid"
printf "port=8080\n" > service.conf
git add service.conf && git commit -m "Add service configuration"
STEP 2

Change twice

printf "port=8081\n" > service.conf
git commit -am "Change service port"
printf "threads=4\n" >> service.conf
git commit -am "Add worker setting"
STEP 3

Read the graph

git log --oneline --graph --decorate
STEP 4

Inspect recent changes

git show --stat HEAD
git diff HEAD~2..HEAD

Verify

  • Three commits exist.
  • The diff across the last two transitions includes the port and threads changes.

Expected outcome

  • Learners can locate a change without resetting or checking out old commits.

If it fails

  • If HEAD~2 is invalid, confirm all three commits succeeded.
  • Use git log -- service.conf to limit history to a path.
Real-world connection

During incidents, engineers often inspect what changed between a known-good deployment commit and the current release.

Avoid these traps

Common mistakes

  • Using reset when only inspection is needed.
  • Treating short commit IDs as sequential numbers.
  • Assuming log display order is always the same as ancestry.
Knowledge check

Can you explain it without looking back?

Open each answer only after you have tried to answer the question yourself.

1What does a branch name point to?

A commit reference that normally moves when new commits are added.

2Which command is ideal for inspecting one commit?

git show.

3Does git diff HEAD~1..HEAD modify files?

No; it compares states.