LinuxCommandLibrary

git-log

View commit history

TLDR

Show the sequence of commits starting from the current one, in reverse chronological order of the Git repository in the current working directory

$ git log
copy

Show the history of a particular file or directory, including differences
$ git log [[-p|--patch]] [path/to/file_or_directory]
copy

Show an overview of which file(s) changed in each commit
$ git log --stat
copy

Show a graph of commits in the current branch using only the first line of each commit message
$ git log --oneline --graph
copy

Show a graph of all commits, tags and branches in the entire repo
$ git log --oneline --decorate --all --graph
copy

Show only commits with messages that include a specific string, ignoring case
$ git log [[-i|--regexp-ignore-case]] --grep [search_string]
copy

Show the last N number of commits from a certain author
$ git log [[-n|--max-count]] [number] --author "[author]"
copy

Show commits between two dates (yyyy-mm-dd)
$ git log --before "[2017-01-29]" --after "[2017-01-17]"
copy

SYNOPSIS

git log [] [] [[--] ...]

Explanation:

  • <options>: Flags that modify the command's behavior (e.g., formatting, filtering).
  • <revision-range>: Specifies the range of commits to display (e.g., HEAD~5..HEAD, branch-A..branch-B). If omitted, it shows the history reachable from HEAD.
  • <path>...: Restricts the log output to commits that modified the specified files or directories. The -- separator is used to distinguish paths from revisions or options, especially if a path might resemble an option or revision.

PARAMETERS

--patch, -p
    Show the patch (diff) for each commit.

--stat
    Show diffstat (number of files changed, insertions, deletions).

--oneline
    Display each commit on a single line, showing only the SHA-1 hash and commit message.

--graph
    Draw an ASCII art graph of the commit history, illustrating merges and branches.

--pretty[=<format>]
    Format the output using predefined styles (e.g., oneline, short, full) or a custom format string.

-n <count>, --max-count=<count>
    Limit the number of commits to output.

--author=<pattern>
    Show commits whose author matches the specified pattern.

--grep=<pattern>
    Show commits whose commit message matches the specified pattern.

--since=<date>, --until=<date>
    Limit commits to those made after or before a specified date.

--all
    Show all branches, remote-tracking branches, and tags.

--follow
    Continue listing commits along rename detection when a single path is given.

DESCRIPTION

The git log command is a fundamental tool for examining the history of a Git repository. It provides a chronological list of commits, displaying essential information such as the commit SHA-1 hash, author, date, and commit message. By default, commits are listed in reverse chronological order, showing the most recent commits first.

git log is highly versatile, offering numerous options to filter, format, and visualize the repository's history. Users can narrow down results by author, committer, date range, commit message content, or specific files/directories. It can also display the actual changes (diffs) introduced by each commit, show statistics, or even draw an ASCII art graph representing the branch and merge history. This command is indispensable for understanding how a project has evolved, identifying when and by whom specific changes were introduced, debugging issues, and preparing for code reviews.

CAVEATS

For very large repositories with extensive history, running git log without filters can be slow and produce overwhelming output. Using options like --max-count, --oneline, or specific path filters is recommended.

Understanding the various revision-range syntaxes (e.g., A..B for commits reachable from B but not A, A...B for commits unique to either A or B) is crucial for precise history exploration.

The --graph output can become complex and difficult to interpret in repositories with highly intricate branching and merging strategies.

DEFAULT PAGER

The output of git log is typically piped through a pager program (e.g., less on Linux/macOS, more on Windows), allowing users to scroll through large outputs, search, and navigate. You can exit the pager by pressing 'q'.

CUSTOM FORMATTING WITH <I>--PRETTY=FORMAT</I>

This powerful option allows users to define their own output format using placeholders. For example, --pretty=format:"%h %an %ad %s" would show a short hash, author name, author date, and subject (commit message first line) for each commit. This offers unparalleled flexibility for custom scripts or specialized views.

HISTORY

git log has been a core command in Git since its inception by Linus Torvalds in 2005. It was designed as the primary interface for users to inspect and navigate the repository's history. Over the years, its functionality has been continuously expanded with more sophisticated filtering, formatting, and visualization options, such as --graph and highly customizable --pretty=format, to meet the evolving needs of developers managing complex codebases. It remains one of the most frequently used Git commands.

SEE ALSO

git show(1): Display various types of objects (commits, tags, trees, blobs)., git diff(1): Show changes between commits, commit and working tree, etc., git reflog(1): Manage and show reference log., git blame(1): Show what revision and author last modified each line of a file., git rev-list(1): Lists commit objects in reverse chronological order.

Copied to clipboard