
Module 3 Lesson 3: Viewing history with git log
Travel through time. Learn how to use 'git log' to explore your project's history, filter commits by author or date, and visualize the tree of changes.
Module 3 Lesson 3: Viewing history with git log
Once you have a series of commits, you need a way to see them. The git log command is your primary tool for exploring the history of your repository.
In this lesson, we master the various ways to view and filter your logs so you can find exactly what you're looking for.
1. The Basic Log
The simplest command shows all commits in reverse chronological order:
git log
This output includes:
- commit
<SHA-1>: The unique ID. - Author: Who made the change.
- Date: When it was made.
- The Message: Your explanation of the change.
2. Condensing the Log
On large projects, the default log is too long. Use these flags to make it readable:
git log --oneline: Shows each commit on a single line (ID and summary only).git log -n <number>: Shows only the last<number>of commits.git log --stat: Shows which files were changed and how many lines were added/deleted in each commit.
# Example of a clean, readable log
git log --oneline -n 5
3. Visualizing the Tree
Git is a "graph" of commits. You can visualize how your history branches and joins (which we'll study in Module 4) with this power-user command:
git log --oneline --graph --all
Many developers create an alias for this (Module 10) because it's so useful.
graph TD
C3["c8a1b2e - Add footer (HEAD -> main)"]
C2["7f3d9a1 - Fix header typo"]
C1["1a2b3c4 - Initial commit"]
C3 --> C2
C2 --> C1
4. Filtering the History
You can search for specific commits using filters:
- By Author:
git log --author="Sudeep" - By Date:
git log --since="2 weeks ago"orgit log --until="2025-01-01" - By Keyword:
git log --grep="bug"(searches commit messages). - By File:
git log -- index.html(only shows commits that touchedindex.html).
Lesson Exercise
Goal: Navigate a "Dense" Log.
- Go to any repository you cloned in Module 2 (e.g., the Linux kernel or a smaller project).
- Run
git log --oneline -n 10. - Try to find commits only by a specific author.
- Run
git log --grep="fix"and see how many bug fixes appear in the recent history. - Use
git log --staton a single commit to see exactly which files were modified.
Observation: You'll find that the "SHA-1" IDs are the keys. If you want to see exactly what happened in commit abc1234, you can use git show abc1234.
Summary
In this lesson, we established:
git logis the standard tool for viewing history.--onelineand-nmake long logs manageable.--graphhelps visualize the project's structure.- You can filter the log by author, date, keyword, and file path.
Next Lesson: History is great for looking back, but what if you made a mistake? We’ll learn the art of Undoing changes.