Git & GitHub ยท Chapter 10 of 42

git log

The `git log` command shows the commit history of a repository, listing commits from newest to oldest. Each entry includes the commit hash, author, date, and message.

Git log has many useful options for filtering and formatting history, such as showing a compact one-line summary per commit or filtering by author or date range.

Syntax
git log
git log --oneline
git log --graph --oneline

Basic log output

By default, git log shows the full hash, author, date, and message for every commit, which can be long. Press 'q' to exit the paginated view.

Useful log options

`git log --oneline` shows a compact summary. `git log --graph` visualizes branches. `git log -n 5` limits output to the last 5 commits.

Example 1 (bash)
git log --oneline
Output
9c2e1aa Fix crash when cart is empty
4d1f0bb Refactor auth module
a3f5c9e Add login validation

Shows each commit as a short hash and summary message on a single line.

Example 2 (bash)
git log -n 2
Output
commit 9c2e1aa...
Author: Alex Smith
Date: ...

    Fix crash when cart is empty

commit 4d1f0bb...
...

Limits the output to the two most recent commits with full details.

Key points

  • git log shows commit history, newest first.
  • --oneline compresses each commit to a single summary line.
  • -n <number> limits how many commits are shown.
  • --graph visually shows how branches diverged and merged.
๐Ÿ’ก Note: Combine flags like `git log --oneline --graph --all` for a compact visual overview of the whole project's history.

๐Ÿ“ Quick Quiz

1. What does git log display?

2. Which flag shows a compact one-line summary per commit?

3. Which flag limits git log to a specific number of commits?