Git Log

Purpose

git log displays the commit history of a repository. It is the primary tool for understanding what changed, when, and why — and for locating specific commits.

Architecture

Each commit in the log contains:

  • Commit hash (SHA-1) — unique 40-character identifier; typically abbreviated to 7 characters
  • Author — name + email of who wrote the change
  • Date — when the commit was authored
  • Commit message — subject line + optional body

Git walks the commit graph from HEAD backwards through parent pointers to produce the log.

Implementation Notes

Basic log:

git log                                # full log, oldest last
git log --oneline                      # one line per commit: short hash + subject

Visualise branch structure:

git log --oneline --decorate --graph          # ASCII branch graph
git log --oneline --decorate --graph --all    # include all branches

Useful flags:

FlagDescription
--onelineShort hash + subject only
--decorateShow branch/tag refs next to commits
--graphASCII graph of branch topology
--allInclude all branches and remotes
--parentsShow parent commit hashes
-n <N>Limit to last N commits
--author=<name>Filter by author
--since=<date>Filter by date (e.g. --since="2 weeks ago")
--grep=<pattern>Filter by commit message
--follow <file>Follow file renames
-pShow diff for each commit (patch)
--statShow file change statistics per commit

Log of a remote branch:

git log origin/main

Search commit content:

git log -S "function_name"             # commits that added/removed string (pickaxe)
git log -G "regex"                     # commits where diff matches regex

Referencing commits:

HEAD~1        # one commit before HEAD
HEAD~3        # three commits before HEAD
<hash>        # any commit by (abbreviated) SHA

Trade-offs

  • --graph --all --oneline --decorate is verbose but invaluable for understanding complex merge/rebase histories.
  • git log -p produces a lot of output; pipe to less or use git show <hash> to inspect a single commit.
  • --follow is necessary when a file was renamed; without it, log stops at the rename.

References