git log: How to View Commit History

By
Bosko Marijan
Published:
June 3, 2025
Topics:

The git log command allows users to view commit history and helps manage and maintain code.

Git commit history is essential for tracking changes, understanding who made them and why, and maintaining a clear record of a project's evolution.

In this tutorial, you will learn to use the git log command to view commit history.

git log: how to view commit history - a tutorial.

Prerequisites

What Is git log?

The git log command is the primary tool for inspecting a repository's commit history. It traces the commit history starting from one or more specified commits (usually the latest on a branch) and following their history backward. You can exclude commits by adding a caret (^) in front of them. Git includes all commits from the starting points, removes the ones reachable from the excluded commits, and displays the rest in reverse chronological order.

By default, each entry in the output shows the commit's SHA-1 hash, author name and email, commit date, and commit message. Beyond its basic output, git log supports filtering by author, date ranges, message content, and file paths, allowing the user to zero in on specific changes or contributions.

Thus, git log provides a precise, chronological reference of every recorded change in the repository. It serves as a retrospective view of project evolution and sets a foundation for more advanced inspection and analysis workflows, such as bisecting regressions or generating release notes.

How to Navigate git log?

The git log command pipes the output through the less pager interface by default, so navigation works identically across all platforms and operating systems. Use the following keys to navigate the log with less:

  • Scroll down one line: j or โ†“
  • Scroll up one line: k or โ†‘
  • Scroll down one page: Spacebar or Page Down
  • Scroll up one page: b or Page Up
  • Quit: q

For a visual representation of the repository's history, use a GUI tool called gitk. A visual representation helps navigate and understand the structure and flow of a project's history by turning raw data into a more intuitive and interactive experience.

Invoke gitk to open a window displaying a commit graph, commit messages, authors, timestamps, and diffs. The interface facilitates exploring branches, merges, and individual commits more intuitively.

To run gitk, type the command name in the terminal:

gitk
Example of the gitk tool on Windows.

The example above shows the gitk UI on Windows.

How to Use git log?

The git log is most commonly run on the current branch. The output shows a list of past commits in reverse chronological order.

The command syntax is:

git log [options] [revision range] [[--] pathโ€ฆ]
  • [options] - Optional flags and arguments that customize the output and control how much information is shown and how it is formatted.
  • [revision range] - Defines which commits to include in the log. You can specify a single branch (e.g., main), a specific commit hash, or a range like HEAD~10..HEAD to show the last 10 commits. It also accepts exclusion (^commit) to subtract commits from the list.
  • [path] - Adding a file or directory path filters the log to show only commits that affected the specified file or path.

Run git log without any arguments to show the current branch's full history:

git log
Using git log without any options.

The output lists each commit's SHA-1 hash, author, date, and commit message. It is a quick way to inspect how the project has evolved over time. You can also use revision ranges or specify file paths to narrow the scope.

For more refined control, Git offers a variety of options, which we explore in the next section.

git log Options

While the default format already provides essential details, like commit hashes, authors, dates, and messages, the git log command also supports numerous options that customize and filter the output.

The key options are described in the table below:

OptionDescription
-n [number]Show only the last [number] commits.
--since=[date]Show commits more recent than a specific date.
--until=[date]Show commits older than a specific date.
--author=[pattern]Filter commits by author name or email.
--grep=[pattern]Filter commits by commit message content.
--allShow commits from all references (branches, tags, etc.).
--onelineCondense each commit to a single line.
--graphDisplay an ASCII graph of the branch and merge history.
--decorateShow reference names (e.g., branches, tags) next to commit hashes.
--statShow statistics for files modified in each commit.
--patch or -pShow the patch (diff) introduced in each commit.
--name-onlyShow only the names of files changed in each commit.
--name-statusShow names and status (added, modified, deleted) of files changed.
--abbrev-commitShow only the abbreviated commit hash.
--relative-dateShow dates relative to the current time (e.g., "2 weeks ago").
--pretty=[format]Customize the log output format.
--reverseDisplay commits in chronological order (oldest first).
--followContinue listing the history of a file beyond renames (works with a single file).
-S[string]Show commits that add or remove a given string.
-L[start],[end]:[file]Trace the evolution of a line range or function in a file.

These options can be combined to tailor the git log output to your specific needs. For a comprehensive list and more details on each option, refer to the official Git log documentation.

Limit the Number of Commits: -n [number]

The -n option provides a quick overview of the most recent activity without going through the entire history.

The option is especially useful during code reviews or when you have just committed a few changes and need to confirm they are all present.

For example:

git log -n 3
Limiting the number of commits in git log output.

The example above produces three commits, each with its full SHA-1 hash, author, date, and commit message. If your branch has dozens of commits but you only care about the last few, the command provides exactly that.

Filter by Date: --since and --until

The --since and --until options narrow down the repository history to a particular timeframe. The options are ideal for generating monthly reports, recent change audit, or auditing the commits that lead up to a release.

For example:

git log --since="2025-05-01" --until="2025-05-31"
Filtering git commits by date.

The output shows only commits dated between May 1, 2025 and May 31, 2025. Each entry still shows all its metadata, but there are no commits before or after that window, making it easy to focus on the specific timeframe.

Search for a Specific Change: -S[string]

Use the -S option to find exactly which commit introduced or removed a particular piece of code (for example, a function name or configuration key). It scans each diff for the given string and returns only the commits whose patch contains it.

For example, the following command finds the commits where the word "Hello" was used in the code:

git log -S"Hello" --oneline
Searching for a specific string in Git commits.

Each returned line in the output is a commit whose diff added or removed "Hello". Combine the option with --oneline for a concise list of SHA-1 abbreviations and messages.

Filter by Author: --author=[pattern]

Use --author to show only those commits authored by a specific person or matching a name/email pattern. The option is handy when you need to review or attribute work done by a particular team member.

For example, to show only the commits authored by "bosko-pnap", run:

git log --author="bosko-pnap" --oneline
Filtering git commits by author.

Only commits whose author field contains "bosko-pnap" are shown. Combined with --oneline, you get a concise list of matching commits, making it easy to audit or collect contributions from that author.

Search Commit Messages: --grep=[pattern]

Use --grep to filter commits whose commit message matches a given regular expression. The option helps find commits related to a bug ID, feature keyword, or any standardized tag in your messages.

For example, to search for Python-related commits, run:

git log --grep="python" --pretty=oneline
Searching for a pattern in Git commit messages.

Only commits with messages containing the string "python" appear. The --pretty=oneline format gives a brief overview of those fixes or references.

Condense Output: --oneline

The --oneline option collapses each commit into a single line, shows an abbreviated hash, and the commit message. The option is perfect when you need a high-level overview of many commits at once.

For example:

git log --oneline -n 10
Condensing the git log output.

The example above displays the ten most recent commits in a compact form, which reduces clutter and makes it easier to scan through large histories.

Visualize History: --graph

Add the --graph option to git log to include an ASCII art representation of the branch and merge structure alongside each commit entry. The graph is invaluable for understanding complex branching workflows.

The command below provides an ASCII graphical representation of the commit history:

git log --oneline --graph --all
Visualizing a Git repository history.

Lines and characters (e.g., *, |, /) connect commits, showing where branches split and rejoin. The graph thus provides immediate insight into project topology.

Display Full Diffs: -p or --patch

When you need to inspect exactly what changed, use the -p option to show the full diff introduced by each commit. The option is essential during code reviews or when tracing a bug to its origin.

For example:

git log -p -1
Displaying full diffs with git log.

The example above shows the patch for the most recent commit, with + and โ€“ markers indicating added and removed lines, respectively.

Follow Renames: --follow

When inspecting the history of a single file that may have been renamed, --follow continues tracking across renames. That way, you see the file's complete history even if its path has changed.

For example, to inspect the history of README.md in our repository, we run the command below:

git log --follow -- README.md
Inspecting the history of a single file in Git.

The output shows all commits that touched README.md, including those before it was renamed or moved, and provides an uninterrupted history.

Custom Formatting: --pretty=[format]

The --pretty option allows users to define a custom output template by specifying placeholders like %h (abbreviated hash), %an (author name), %ad (author date), and %s (subject). The option tailors the log to your needs, whether it is for reporting or scripting.

For example:

git log --pretty=format:"%h - %an, %ar : %s" -n 5
Using custom formatting to display git log history.

In the example above, each of the last five commits is printed as "hash - author, relative-date : message."

Conclusion

This article explained how to use the git log command for inspecting a Git repository history. The command enables developers to review changes, identify contributors, and trace issues. With powerful filtering, formatting, and visualization options, git log transforms raw commit data into actionable insights.

Next, understand the differences between git reset, git revert, and git checkout, or see how to use git clone to copy repositories locally.

Was this article helpful?
YesNo