TEC-WEB46-05 · Lesson 4 · 12 clock hours · demo
Viewing and Navigating History
Objective. Students use git log filtering options, git show, and git diff to locate specific commits, compare versions of a file, and interpret the output of each command accurately.
⚠ Pending professional review
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Viewing and Navigating History in Git
One of Git's most powerful features is its complete, permanent record of every change made to a project. Learning to read that record accurately lets you answer questions like: Who changed this file? When did this bug appear? What exactly changed between version A and version B? The commands `git log`, `git show`, and `git diff` are your primary tools for that investigation.
git log — Reading the Commit History
Running `git log` alone prints every commit on the current branch, newest first. Each entry shows the commit hash, author, date, and message. Because real projects can have thousands of commits, Git provides flags to filter and format the output.
- `--oneline` condenses each commit to one line: a short hash and the first line of the commit message. Use this for a quick overview.
- `--graph` draws an ASCII graph of branch and merge history to the left of the log. Combine it with `--oneline` for a readable branch diagram.
- `--all` shows commits from every branch and tag, not just the one you have checked out. This is essential when you need to find a commit that lives on a different branch.
- `--author="Name"` limits results to commits by a specific author. The value is matched as a case-insensitive substring, so `--author="maria"` matches "Maria Gonzalez".
- `--since="2024-01-01"` (and its counterpart `--until`) filters by date. You can use plain dates, relative expressions such as `"2 weeks ago"`, or ISO 8601 format.
- `--grep="keyword"` searches commit messages for the given text. This is useful for finding the commit that implemented a particular feature or fixed a named bug.
- `--follow` tracks a single file's history across renames. Without it, `git log -- oldname.js` stops at the point the file was renamed; adding `--follow` continues the search under the previous name.
Flags can be combined. For example:
```
git log --oneline --graph --all
git log --oneline --author="chen" --since="3 months ago"
```
git show — Inspecting a Single Commit
`git show <commit-hash>` displays the full metadata for one commit (author, date, message) followed by the patch — the exact lines added and removed. You can use the full 40-character hash or the short form shown by `--oneline`. If you omit the hash, Git shows the most recent commit on the current branch.
Reading the patch output uses the same conventions as `git diff` (explained below). `git show` is the right tool when you already know which commit you want to examine in depth.
git diff — Comparing Versions
`git diff` compares two states of the repository and reports what changed. The most common forms you will use are:
- `git diff` — compares the working directory to the staging area (what you have changed but not yet staged).
- `git diff --staged` — compares the staging area to the last commit.
- `git diff <commitA> <commitB>` — compares any two commits.
- `git diff HEAD~2 HEAD -- filename` — compares a specific file two commits ago (`HEAD~2`) to the current commit (`HEAD`). The `--` separator tells Git unambiguously that what follows is a file path, not a branch name.
Reading Unified Diff Output
Unified diff format is the standard output format for `git diff` and `git show`. Key lines to recognize:
- Lines beginning with `---` identify the older version of the file.
- Lines beginning with `+++` identify the newer version.
- A hunk header like `@@ -12,7 +14,9 @@` means the hunk starts at line 12 in the old file (showing 7 lines) and line 14 in the new file (showing 9 lines).
- Lines beginning with `-` (red in most terminals) were removed.
- Lines beginning with `+` (green) were added.
- Lines with no prefix are context lines — unchanged lines shown for reference.
When you write a plain-language summary of a diff, describe what the change accomplishes, not just the mechanics. For example: "The `validateEmail` function was updated to also reject addresses longer than 254 characters" is more useful than "one line was removed and two were added."
Locating a Commit with grep
When you do not know the hash, combine `git log --all --oneline` with the shell pipe and `grep` to search output:
```
git log --all --oneline | grep "login"
```
Alternatively, use Git's built-in `--grep` flag directly, which searches only commit messages:
```
git log --all --oneline --grep="login"
```
Once you have the short hash, pass it to `git show` for the full picture.
Accuracy and Safe Habits
Version control history is a professional record. Never alter published history on shared branches — commands like `git rebase` and `git commit --amend` rewrite commits and can cause serious problems for teammates if used on branches others depend on. In this course you will only read history, not rewrite it.
When working at the command line, verify you are in the correct repository directory before running any Git command (`pwd` on Linux/macOS, `cd` on Windows). Running `git log` in the wrong directory produces confusing results or an error, not data corruption, but developing the habit of confirming your location prevents mistakes when you later use commands that do modify state.
Key terms8
commit hashA unique identifier (SHA-1 checksum) assigned to every commit in a Git repository; can be referenced in full (40 characters) or by a unique short prefix.
HEADA special Git pointer that refers to the currently checked-out commit, typically the latest commit on the active branch; HEAD~2 means two commits before HEAD.
unified diff formatThe standard output format used by git diff and git show; lines prefixed with + were added, lines prefixed with - were removed, and unprefixed lines are unchanged context.
hunkA contiguous block of changed lines within a unified diff, introduced by a header line beginning with @@; each hunk shows the starting line numbers and line counts for the old and new file versions.
--follow flagA git log option that traces a file's history across renames, allowing you to see commits made under the file's previous name.
git showA Git command that displays the metadata and patch (diff) of a single specified commit, or of HEAD if no commit is given.
--grep flagA git log option that filters the commit list to only commits whose commit message contains the specified text string.
context linesUnchanged lines surrounding a changed block in unified diff output; they provide surrounding context to help the reader understand where in the file the change occurred.
In class: Instructor demonstrates git log with flags --oneline, --graph, --all, --author, --since, --grep, and --follow on a prepared sample repository; students follow along on their own terminals and answer a guided worksheet about what each flag does. · Students use git diff HEAD~2 HEAD -- filename to compare a file across two commits, interpret the unified diff output (+ and - lines), and write a short plain-language summary of what changed between those two commits. · Use git show <commit-hash> to inspect a specific commit's metadata and patch, then use git log --all --oneline to locate a commit that introduced a specific keyword using grep piped output; record findings in the lab log.
Materials: Prepared sample Git repository (provided by instructor), terminal, printed unified-diff reference card, lab log
Check: Students correctly answer 8 of 10 questions on a worksheet requiring them to interpret git log and git diff output from printed screenshots; lab log entry signed off.
Practice check8
Answer each question, then check it. Nothing is recorded; this is your own practice.
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.