How do you view the commit history in Git and what options are useful?
Quick Answer
Use git log to view commit history. Key options include --oneline for condensed output, --graph for visual branch structure, --author to filter by developer, and --since/--until for date ranges. git log -p shows the actual diff for each commit.
Detailed Answer
Think of git log as a time machine's control panel. By default, it shows you every stop the machine has made (every commit), but the real power comes from the filters and dials. You can zoom in on a specific time period, filter by who was driving, or see exactly what changed at each stop. The more filters you master, the faster you can navigate through a project's history.
The git log command reads the commit objects from Git's object database, starting from the current HEAD and following parent pointers backwards through the commit graph. By default, it displays each commit's SHA-1 hash, author name and email, date, and commit message in reverse chronological order. For repositories with thousands of commits, the output can be overwhelming, which is why the various formatting and filtering options are essential. The --oneline flag compresses each commit to a single line showing the abbreviated hash and first line of the commit message.
Under the hood, git log traverses a directed acyclic graph (DAG) of commit objects. Each commit points to its parent commit(s), forming chains. Merge commits have multiple parents, and the --first-parent option follows only the first parent at each merge, which is useful for seeing the main branch history without merge noise. The --graph flag adds ASCII art showing how branches diverge and merge. The traversal is highly optimized: Git uses the commit-graph file (.git/objects/info/commit-graph) to speed up history walks without unpacking every commit object.
In production debugging scenarios, git log becomes indispensable. When a deployment breaks, you might use git log --since='2 hours ago' --oneline to see recent changes. To find who modified a specific file, git log --follow -- src/billing/invoice-generator.rb tracks the file even through renames. For auditing purposes, git log --format='%H %an %ae %ad %s' produces machine-parseable output that security teams pipe into compliance tools. The git shortlog -sn command generates a leaderboard of commits per author, useful for understanding team contribution patterns during sprint retrospectives.
A common gotcha: git log by default only shows commits reachable from the current branch. If you're on main and want to see commits on a feature branch, you need git log feature/search-improvements or switch to that branch first. Another pitfall is confusing git log with git reflog. While git log shows the commit history, git reflog shows every position HEAD has pointed to, including commits that might have been orphaned by a reset or rebase. This distinction is crucial for recovering lost work. Also, the default date format can vary between systems. Use --date=iso for consistent, unambiguous timestamps.