Git Log

The git log command displays the history of commits in the current repository. It shows a list of all the snapshots that have been saved, starting from the most recent and going back to the very first commit.

Think of git log like a project diary — it shows every saved entry, who wrote it, when it was written, and what the entry was about.

Basic Syntax

git log

Default Output Format

Running git log shows output like this:

commit a3f92b1c8d4e5f6789abcdef1234567890abcdef
Author: Ravi Kumar <ravi@example.com>
Date:   Mon Oct 9 14:32:10 2023 +0530

    Add contact form with email validation

commit b7c3d45ef12345678901234567890abcdef12345
Author: Priya Sharma <priya@example.com>
Date:   Sun Oct 8 11:15:00 2023 +0530

    Update homepage layout for mobile screens

commit c1a2b3c4d5e6f7890123456789abcdef01234567
Author: Ravi Kumar <ravi@example.com>
Date:   Sat Oct 7 09:00:00 2023 +0530

    Initial commit

Each commit entry shows the full hash, the author, the date, and the commit message.

Useful git log Options

One Line Per Commit

git log --oneline

Output:

a3f92b1 Add contact form with email validation
b7c3d45 Update homepage layout for mobile screens
c1a2b3c Initial commit

This is the most commonly used format — compact and easy to read.

Limit Number of Commits

git log -5

Shows only the last 5 commits.

Show Changes in Each Commit

git log -p

Shows the full diff (what lines were added or removed) for each commit. Useful for reviewing what was actually changed.

Show Brief Statistics

git log --stat

Output example:

commit a3f92b1
Author: Ravi Kumar <ravi@example.com>
Date:   Mon Oct 9 14:32:10 2023

    Add contact form

 contact.html | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Show a Visual Branch Graph

git log --oneline --graph --all

Output example:

* a3f92b1 (HEAD -> main) Merge feature branch
|\
| * d1e2f3g Add new feature
|/
* b7c3d45 Update homepage
* c1a2b3c Initial commit

This shows a visual diagram of the commit history with branch and merge points. Very helpful in multi-branch projects.

Show Commits by a Specific Author

git log --author="Ravi Kumar"

Search Commits by Keyword in Message

git log --grep="login"

Shows all commits where the commit message contains the word "login".

Show Commits Within a Date Range

git log --after="2023-10-01" --before="2023-10-31"

Show Commits That Changed a Specific File

git log -- index.html

Shows only commits that involved changes to index.html.

Viewing a Single Commit in Detail

To see the full details and changes of a specific commit, use the commit hash:

git show a3f92b1

Or to see the most recent commit:

git show HEAD

Customizing the Log Format

A completely custom format for the log output can be defined:

git log --pretty=format:"%h - %an, %ar : %s"

Output:

a3f92b1 - Ravi Kumar, 2 days ago : Add contact form
b7c3d45 - Priya Sharma, 3 days ago : Update homepage

Format Placeholders

PlaceholderMeaning
%hShort commit hash
%HFull commit hash
%anAuthor name
%aeAuthor email
%arAuthor date, relative (e.g., "3 days ago")
%sCommit subject (message)

Navigating the git log Output

When git log produces a long output, it opens in a pager (usually less on Linux/macOS).

  • Press Space to scroll down one page
  • Press b to scroll up one page
  • Press q to quit and return to the terminal

A Powerful Combined Command

This single command gives a beautiful and informative history view:

git log --oneline --graph --all --decorate

It shows the branch graph, all branches, decorations (branch and tag names), and each commit on one line. This is a command worth memorizing.

Summary

git log is the command for viewing the full history of commits in a repository. The default output is detailed, but --oneline gives a compact view. Use --graph to visualize branches, --author to filter by person, and --grep to search by keyword. The log is one of the most valuable tools for understanding a project's history.

Leave a Comment

Your email address will not be published. Required fields are marked *