Inspecting Commit History with git log and Formatting Flags0%

Inspecting Commit History with git log and Formatting Flags

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Inspecting Commit History with git log and Formatting Flags

As software evolves over months and years, inspecting historical commits becomes crucial for tracking features, conducting post-mortems on bugs, and reviewing peer contributions. The git log command is Git's primary time machine.


1. Basic History Inspection: git log

Executing git log lists the commits made in that repository in reverse chronological order (newest commit first):

Bash / Terminal
git log

Standard Output Format:

Output
commit 7b3f81e28d4c5a9b0c2d4e6f8a0b1c2d3e4f5a6b (HEAD -> main)
Author: Sumit Sharma <sumit@mskinstitute.com>
Date: Mon Sep 15 10:30:00 2026 +0530
 
feat(auth): add JWT token expiration check
 
commit 4a2c91d8f1e3a5b7c9d0e2f4a6b8c0d2e4f6a8b0
Author: Neha Gupta <neha@mskinstitute.com>
Date: Sun Sep 14 16:15:22 2026 +0530
 
fix(css): fix mobile navbar z-index overflow
Note
[!NOTE] When the log output exceeds your terminal height, Git pages the output using less. - Press Spacebar to scroll down one page. - Press b to scroll up. - Press q to quit and return to the command prompt.

2. Essential Power Flags for git log

A. Compact Single-Line View (--oneline)

Condenses each commit into its short 7-character hash and commit title:

Bash / Terminal
git log --oneline

Output:

Output
7b3f81e (HEAD -> main) feat(auth): add JWT token expiration check
4a2c91d fix(css): fix mobile navbar z-index overflow
9f1e82b docs: update project README

B. Limit Output Count (-n <number>)

View only the last $N$ commits:

Bash / Terminal
git log -n 5 --oneline

C. Show Changed Files and Stats (--stat)

Displays the files modified, number of lines inserted (+), and deleted (-):

Bash / Terminal
git log --stat -n 2

D. Show Full Line-by-Line Code Patches (-p)

Shows the actual line diff introduced by each commit:

Bash / Terminal
git log -p -n 1

3. Visual Branch Graph (--graph --oneline --all)

When multiple developers are branching and merging, visualizing the branch tree topology directly in your terminal is invaluable:

Bash / Terminal
git log --graph --oneline --all --decorate

Terminal Output:

Output
* 7b3f81e (HEAD -> main) Merge branch 'feature-payment'
|\
| * 3a89e12 (feature-payment) feat: integrate Stripe checkout API
| * 2c510fa feat: add webhook listener
* | 4a2c91d fix: update landing page typography
|/
* 9f1e82b Initial commit

Pro Tip: Create an Alias for Visual Log

Save this command permanently as an alias:

Bash / Terminal
git config --global alias.lg "log --graph --oneline --all --decorate"
 
# Now simply run:
git lg

4. Filtering History

Git lets you filter commit history with precision:

Bash / Terminal
# 1. Filter by author
git log --author="Sumit"
 
# 2. Filter by date range
git log --since="2 weeks ago" --until="yesterday"
 
# 3. Filter by search keyword in commit messages
git log --grep="bugfix"
 
# 4. Filter history of a specific single file
git log -p src/utils/auth.js

Interactive Quiz

Practice Quiz

Question 1
What is the default ordering of commits displayed by git log?
Question 2
Which key allows you to exit the Git log pager and return to the terminal prompt?
Question 3
Which flag formats the output of git log into a clean single-line representation per commit?
Question 4
Which combination of flags renders an ASCII graphical tree of all branches and merge topologies in the terminal?
Question 5
How can you view the commit history for changes that specifically affected only src/db.js?
Next Lesson

Comparing Changes with git diff (Working Tree vs Staged vs HEAD)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext