Git Basics | git log

The git log command

git log displays committed snapshots. You can use it to filter the commit history and search for specific changes. While git status shows the state of the working directory and staging area, git log focuses only on committed history.

Git Tutorial: git status and git log

Log output can range from a simple filtered view of commit history to a completely customized format. The following examples show commonly used git log options.

Usage

git log

Displays the full commit history in the default format. If the output spans more than one page, press Space to scroll. Press q to quit.

git log -n <limit>

Limits the number of displayed commits to <limit>. For example, git log -n 3 displays three commits.

git log --oneline

Compresses each commit into one line. This is useful when you want to scan the commit history quickly.

git log --stat

In addition to the normal git log information, displays modified files and the number of added and deleted lines.

git log -p

Displays the patch for each commit. This provides the most detailed information available from the commit history: the full diff for each commit.

git log --author="<pattern>"

Searches for commits created by a specific author. <pattern> can be plain text or a regular expression.

git log --grep="<pattern>"

Searches for commits whose commit message matches <pattern>, which can be plain text or a regular expression.

git log <since>..<until>

Displays only commits between <since> and <until>. These two arguments can be commit IDs, branch names, HEAD, or other version references.

git log <file>

Displays only commits that include the specified file. This is useful when you want to inspect the history of a particular file.

git log --graph --decorate --oneline

This combines several useful options. --graph draws a text-based graph of the commit history to the left of the commit messages. --decorate adds branch and tag names to the displayed commits. --oneline compresses each commit into one line, which makes it useful for getting an overview of the history.

Additional notes

git log is the basic tool for inspecting commit history in Git. It helps you find specific versions of a project, understand what will change when you merge a feature branch, and even identify where a problematic change came from.

commit 3157ee3718e180a9476bf2e5cab8e3f1e78a73b7
Author: John Smith

Most of this information is intuitive, but the first line deserves explanation. The 40-character string after commit is the SHA-1 checksum of the commit contents. It has two purposes. First, it guarantees commit integrity: if the commit is damaged, a different checksum is generated. Second, it acts as the unique ID for the commit.

You can use this ID in commands such as git log <since>..<until> to specify a particular commit. For example, git log 3157e..5ab91 displays all commits between the IDs 3157e and 5ab91. In addition to checksums, common ways to specify individual commits include branch names and the HEAD keyword. HEAD always points to the current commit, whether that is on a branch or a specific detached commit.

The ~ character is used to refer to parent commits. For example, 3157e~1 refers to the parent commit immediately before 3157e, and HEAD~3 means the commit three commits before the current commit.

These reference methods exist because many Git operations are performed against a specific commit. git log gives you a way to find the commit you want to operate on, so it is often the starting point for that kind of work.

Examples

The usage section introduced many git log examples, but note that you can combine multiple options in a single command.

git log --author="John Smith" -p hello.py

This command displays every diff for changes that John Smith made to hello.py.

The .. syntax is often used to compare branches. The following example displays all commits that exist on the some-feature branch but not on the master branch.

git log --oneline master..some-feature