git log

View the history of commits in your repository, showing who changed what and when.

You need to see what changed in the last week, or track down when a specific feature was added, or understand how a file evolved over time. git log shows your repository's commit history, displaying commits in reverse chronological order with their messages, authors, and dates. It's your window into the repository's past, helping you understand what happened and why.

Running git log by itself shows the full history of the current branch. Each commit gets a display showing its SHA hash, author, date, and commit message. The most recent commit appears first, and you can scroll through older commits. Press q to exit the log viewer. This default view is comprehensive but can be overwhelming for repositories with thousands of commits.

The --oneline flag provides a compact view with one commit per line: git log --oneline. Each line shows the abbreviated commit hash and the first line of the commit message. This condensed format makes it easy to scan recent history quickly or see how many commits are in a range. It's probably the most commonly used variant of log because it gives you enough information without cluttering your terminal.

Filtering and Searching History

You can limit the number of commits shown with -n, like git log -5 for the five most recent commits. This is useful when you just want to see recent activity without scrolling through years of history. Combining this with --oneline gives you a quick snapshot: git log --oneline -10 shows the last ten commits in compact form.

To see history for a specific file, add the filename: git log -- filename.js. This shows only commits that modified that file, which is invaluable for tracking down when a bug was introduced or understanding how a piece of code evolved. You can also see the actual changes with git log -p -- filename.js, which shows the diff for each commit affecting that file.

Date ranges help narrow your search to specific time periods. Running git log --since="2 weeks ago" shows commits from the past two weeks, while git log --since="2024-01-01" --until="2024-06-30" shows commits from the first half of 2024. You can use natural language like "yesterday," "last week," or specific dates in various formats.

Visualizing Branch History

The --graph flag draws an ASCII representation of branch structure alongside the commits: git log --graph --oneline --all. This visualization shows where branches split and merge, making it clear how parallel work came together. It's especially useful for understanding complex histories with multiple feature branches merging into main.

Adding --all shows commits from all branches, not just the current one. Without this flag, you only see commits reachable from your current branch, which can hide work happening on other branches. Combining --graph, --oneline, and --all gives you a comprehensive overview of all activity across all branches in compact form.

Searching for Specific Changes

The -S flag searches for commits that added or removed specific text: git log -S "function calculateTotal". This is incredibly useful for finding when a function was introduced or deleted. Unlike grepping through current files, this searches through every version in history. If you're debugging and want to know when a particular method appeared in the codebase, this finds it.

You can also search commit messages with --grep: git log --grep="fix bug" shows commits whose messages contain "fix bug." This works well if your team uses consistent commit message conventions, like including ticket numbers or keywords. Combine with -i for case-insensitive searching: git log --grep="JIRA-123" -i.

Formatting and Customization

The --pretty flag lets you customize output format. Using --pretty=format:"%h - %an, %ar : %s" shows abbreviated hash, author name, relative date, and subject in a custom layout. Many developers create Git aliases for their preferred log formats, so they can type git lg and get their customized view without remembering complex format strings.

For visual learners, many GUI tools like GitKraken, SourceTree, or the GitHub Desktop app provide graphical log views with clickable commits and visual branch diagrams. These can be easier to navigate than terminal output, though understanding the command-line version helps when you're working on servers or in terminals.

Pull Request Context

In pull request workflows, log helps you review what's about to be merged. Before approving a PR, you might check out the branch and run git log main..feature-branch to see all commits that are in the feature branch but not in main. This shows exactly what the PR will add to the codebase.

After merging PRs, log helps you generate changelogs or release notes. You can review all commits since the last release with git log v1.0.0..HEAD, then summarize the changes for your users. Many teams automate this with tools that parse commit messages following conventions like Conventional Commits.

Understanding log means understanding that your repository's history tells a story. Each commit is a chapter explaining what changed and why. Good commit messages turn that history into documentation that helps future developers (including future you) understand decisions, trace bugs, and learn how the codebase evolved. Log is how you read that story.