Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

Inspecting Git History

As your project grows, your Git repository will contain many commits. Git provides powerful commands that help you review what changed, understand when changes were made, and identify who made them.

In this lesson, you will learn how to view your commit history, customize the output of git log, and inspect the changes introduced by a specific commit.

1. View the Commit History

The git log command is one of the most useful commands for exploring the history of a Git repository. It displays commits in reverse chronological order, with the newest commit appearing first.

A standard commit entry includes the commit hash, author, date, and commit message.

$ git log
Tip: If your repository does not contain any commits yet, git log will not display a commit history. Create your first commit before using this command.

2. Understanding the Git Log Output

When you run git log, Git displays information about each commit. Understanding this information makes it easier to track changes and troubleshoot your project.

Information Description
Commit Hash A unique identifier for the commit.
Author The person who created the commit.
Date The date and time when the commit was created.
Commit Message A short description explaining what changed.

3. Useful Options for git log

Large projects can contain hundreds or thousands of commits. Instead of viewing everything, you can use options to customize the output and quickly find the information you need.

  • Show changes with -p
    Displays the actual line-by-line changes introduced by each commit. This is useful when reviewing code changes.
  • Limit commits with -n
    Displays only the specified number of recent commits. For example, git log -2 shows the latest two commits.
  • Compact output with --oneline
    Displays each commit on a single line using a shortened commit hash and commit message.
  • Display file statistics with --stat
    Shows which files were changed and summarizes the number of lines added and deleted.
  • Visualize branches with --graph
    Displays an ASCII graph that helps you understand branches, merges, and the relationship between commits.

4. Try It in the Terminal

The following example shows how a repository's history might look when using the compact --oneline format.

Terminal — Git History
Bash
$ git log --oneline
a84f21c Update student registration API
7c91de4 Add payment validation
35ab821 Create student module
18f5d90 Initial project setup
What does this mean?
Each line represents one commit. The value on the left is the shortened commit hash, followed by the commit message.

5. Git Log Quick Reference

Command What It Shows Best Use
git log Complete commit information including hash, author, date, and message. Detailed History
git log --oneline A compact, one-line summary of each commit. Quick Overview
git log -p Shows the actual line-by-line changes introduced by each commit. Code Review
git log --stat Shows changed files and the number of lines added or removed. Change Summary
git log --graph Displays a visual text-based representation of branches and merges. Branch Visualization
git log -5 Displays only the five most recent commits. Recent Changes

6. Inspect a Specific Commit

Sometimes you already know which commit you want to investigate. Instead of reviewing the entire history, you can use git show with the commit hash.

$ git show [commit-hash]

This command displays detailed information about the selected commit, including the commit message and the changes made to files.

Example:
git show a84f21c
Here, a84f21c represents the commit you want to inspect.

7. Practical Workflow

A simple workflow for inspecting project history is:

  1. Run git log --oneline to get a quick overview.
  2. Identify the commit you want to investigate.
  3. Run git show [commit-hash] to inspect its changes.
  4. Use git log -p when you need to review changes across multiple commits.
  5. Use git log --graph when you need to understand branches and merges.
Key Takeaway

Git history is more than a list of commits. By combining git log, its useful options, and git show, you can understand how your project evolved, investigate previous changes, and quickly find the source of a problem.