Viewing Git Logs and Tracking History

Commit history records all changes made to a project and is the core value of version control. By reviewing this history, you can understand the project’s evolution, trace the source of issues, and analyze the development trajectory.

1. Basic Concepts of Logging

(1) What Is Commit History?

The commit history is a chronological record of all commits and includes:

100%
graph TB
    A[Latest Submissions<br/>HEAD] --> B[SubmitC3<br/>a1b2c3d]
    B --> C[SubmitC2<br/>d4e5f6g]
    C --> D[SubmitC1<br/>h7i8j9k]
    D --> E[Initial Submission<br/>l0m1n2o]
    
    style A fill:#d4edda
    style E fill:#fff3cd

(2) The Role of History

(3) Git log command

git log is the main command for viewing commit history and supports a wide range of options to customize the output.


2. Viewing Basic Logs

(1) View Full History

▶ Example: View commit history

BASH
# View Full History
git log

# Output Format:
# commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
# Author: Zhang San <zhangsan@example.com>
# Date:   Mon Jan 1 10:00:00 2026 +0800
#
#     feat: Add User Login Functionality
#
# commit d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2
# Author: Li Si <lisi@example.com>
# Date:   Sun Dec 31 15:30:00 2025 +0800
#
#     fix: Fix the login authentication error

(2) Compact Mode

▶ Example: Concise Display

BASH
# One commit per line
git log --oneline

# Output:
# a1b2c3d feat: Add User Login Functionality
# d4e5f6g fix: Fix the login authentication error
# h7i8j9k docs: UpdateREADME
# l0m1n2o Initial commit

# Abbreviated form
git log --oneline --decorate

# Output:
# a1b2c3d (HEAD -> main) feat: Add User Login Functionality
# d4e5f6g (origin/main) fix: Fix the login authentication error

(3) Limit the number of items displayed

▶ Example: Limiting the Number of Items Displayed

BASH
# Show Recent3Next Submission
git log -3

# Or
git log -n 3

# Simplified Mode: Show Recent 5
git log --oneline -5

# Output:
# a1b2c3d feat: Add User Login Functionality
# d4e5f6g fix: Fix the login authentication error
# h7i8j9k docs: UpdateREADME
# k9l0m1n style: Code Formatting
# n2o3p4q refactor: Optimize Query Logic

3. Formatted Log Output

(1) Formatting Options

Option Description Purpose
--oneline Simplified Mode Quick Browse History
--stat Show Statistics View File Change Statistics
-p Show differences View specific changes
--graph Graphical Display View Branch Merge History
--decorate Show references View branch and tag references

▶ Example: Displaying Statistics

BASH
# Display File Change Statistics
git log --stat

# Output:
# commit a1b2c3d4e5f6...
# Author: Zhang San <zhangsan@example.com>
# Date:   Mon Jan 1 10:00:00 2026 +0800
#
#     feat: Add User Login Functionality
#
#  src/auth/login.js | 25 +++++++++++++++++++++++++
#  src/auth/index.js  |  3 ++-
#  2 files changed, 26 insertions(+), 2 deletions(-)

(2) Show Full Differences

▶ Example: Viewing commit differences

BASH
# Show the full diff for each commit
git log -p

# Show the differences from the most recent commit
git log -1 -p

# Output:
# commit a1b2c3d4e5f6...
# Author: Zhang San <zhangsan@example.com>
# Date:   Mon Jan 1 10:00:00 2026 +0800
#
#     feat: Add User Login Functionality
#
# diff --git a/src/auth/login.js b/src/auth/login.js
# new file mode 100644
# index 0000000..abc1234
# --- /dev/null
# +++ b/src/auth/login.js
# @@ -0,0 +1,25 @@
# +function login(username, password) {
# +  // Validation Logic
# +}

(3) Custom Formats

▶ Example: Custom Output Format

BASH
# Use a preset format
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
git log --pretty=fuller

# Custom Format
git log --pretty=format:"%h - %an, %ar : %s"

# Output:
# a1b2c3d - Zhang San, 2 hours ago : feat: Add User Login Functionality
# d4e5f6g - Li Si, 1 day ago : fix: Fix the login authentication error

# A more detailed format
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)%an%C(reset), %C(blue)%ar%C(reset) : %s"

# Table Format
git log --pretty=format:"%h | %an | %ad | %s" --date=short

# Output:
# a1b2c3d | Zhang San | 2026-01-01 | feat: Add User Login Functionality
# d4e5f6g | Li Si | 2025-12-31 | fix: Fix the login authentication error

(4) Format Placeholders

Placeholder Description Sample Output
%H Full hash a1b2c3d4e5f6...
%h Short Hash a1b2c3d
%an Author's Name Zhang San
%ae Author Email zhangsan@example.com
%ad Author Date Mon Jan 1 10:00:00 2026
%ar Author Date (Relative) 2 hours ago
%s Submit Information feat: Add a Feature
%d Reference Name (HEAD -> main)

4. Log Filtering and Querying

(1) Filter by time

▶ Example: Filtering by Time Range

BASH
# After the specified date
git log --since="2026-01-01"

# Before the specified date
git log --until="2026-01-31"

# Relative Time
git log --since="1 week ago"
git log --since="2 months ago"
git log --after="yesterday"

# Time Range
git log --since="2026-01-01" --until="2026-01-31"

# Combined Use
git log --oneline --since="1 week ago" --until="yesterday"

(2) Filter by Author

▶ Example: Author Filter

BASH
# By Author Name
git log --author="Zhang San"

# By author's email address
git log --author="zhangsan@example.com"

# Fuzzy Matching
git log --author="Zhang"

# Combined Filtration
git log --author="Zhang San" --since="1 week ago"

(3) Filter by Submitted Information

▶ Example: Search by Submission Information

BASH
# Search and Submit Information
git log --grep="fix"

# Multiple conditions(or relationship)
git log --grep="fix" --grep="feat"

# Regular Expressions
git log --grep="feat\|fix"

# Ignore case
git log --grep="FIX" -i

# Combined Filtration
git log --author="Zhang San" --grep="feat"

(4) Filter by File

▶ Example: File History Query

BASH
# View the history of a specific file
git log -- README.md

# View Multiple Files
git log -- file1.js file2.js

# View Catalog History
git log -- src/auth/

# Show File Differences
git log -p -- README.md

# Display File Change Statistics
git log --stat -- README.md

# Show only commits that modified this file
git log --oneline -- README.md

# Output:
# a1b2c3d docs: UpdateREADME
# h7i8j9k docs: Add Installation Instructions
# l0m1n2o Initial commit

(5) Filter by submission scope

▶ Example: Submitting a range query

BASH
# View the history between two commits
git log a1b2c3d..d4e5f6g

# View the history following a specific commit
git log a1b2c3d..

# View the history leading up to a specific commit
git log ..d4e5f6g

# View Branch Differences
git log main..feature

# View the differences between the two branches
git log main...feature

5. Graphical Display

(1) Display the branch diagram

▶ Example: Graphical Log

BASH
# Display the branch merge diagram
git log --graph

# Common Combinations
git log --graph --oneline --all

# Output:
# *   a1b2c3d Merge branch 'feature'
# |\
# | * d4e5f6g Add feature
# * | b7c8d9e Fix bug
# |/
# * c0d1e2f Initial commit

# With decorative details
git log --graph --oneline --decorate --all

# Color Output
git log --graph --oneline --all --pretty=format:"%C(red)%h%C(reset) - %C(green)%s%C(reset)"

(2) Visualizing Branch History

100%
gitGraph
    commit id: "Initial commit"
    commit id: "Add feature A"
    branch feature
    checkout feature
    commit id: "Feature work"
    checkout main
    commit id: "Fix bug"
    merge feature id: "Merge feature"
    commit id: "Release v1.0"

(3) View Branch History

▶ Example: Branch History

BASH
# View the history of all branches
git log --graph --oneline --all --decorate

# View the history of a specific branch
git log --graph --oneline feature

# View Branch Fork Points
git log --graph --oneline --simplify-by-decoration

# View the merged commit
git log --merges --oneline

# View Unmerged Commits
git log --no-merges --oneline

6. Advanced Logging Techniques

Use git bisect to pinpoint the commit that introduced the issue:

▶ Example: Submission for the Binary Search Problem

BASH
# Start the binary search
git bisect start

# Mark the current commit as problematic
git bisect bad

# Mark a commit as normal
git bisect good v1.0.0

# GitIt will automatically switch to the middle submission
# Marked as good or bad after testing
git bisect good
# Or
git bisect bad

# After finding and submitting a bug
# a1b2c3d is the first bad commit

# End Binary Search
git bisect reset

(2) View the file change history

▶ Example: File History Analysis

BASH
# View the complete change history for the file
git log --follow -p -- filename

# View the revision history for each line of the file
git blame filename

# Output:
# a1b2c3d (Zhang San 2026-01-01 10:00:00  1) function login() {
# d4e5f6g (Li Si 2025-12-31 15:30:00  2)   // Validation Logic
# h7i8j9k (Zhang San 2025-12-30 09:00:00  3) }

# View the history for a specific row
git log -L 10,20:filename

# View Function History
git log -L :functionName:filename

(3) Citation Log

▶ Example: Viewing the citation log

BASH
# ViewHEADMovement History
git reflog

# Output:
# a1b2c3d HEAD@{0}: commit: feat: Add Feature
# d4e5f6g HEAD@{1}: checkout: moving from feature to main
# h7i8j9k HEAD@{2}: commit: Feature work
# l0m1n2o HEAD@{3}: checkout: moving from main to feature

# View the history of a specific citation
git reflog show HEAD
git reflog show main

# Restore to the previous state
git reset HEAD@{5}

❓ FAQ

Q How do I view the revision history of a file?
A Use git log -- <file> to view the file's commit history, git log -p -- <file> to view detailed changes, and git blame <file> to view the changes for each line.
Q How do I view the differences between two commits?
A Use git log <commit1>..<commit2> to view the history between two commits, and use git diff <commit1> <commit2> to view specific file differences.
Q How do I find the commit that introduced a particular bug?
A Use git bisect to perform a binary search. Mark one commit that you know is fine and one that you know has a problem, and Git will automatically pinpoint the commit that introduced the issue.
Q What is the difference between log and reflog?
A git log displays the commit history, which is a record of the project's evolution; git reflog displays the history of branch movements, including all operations such as commits, resets, and checkouts—even if a commit is discarded, it can still be found in the reflog.
Q How do I view the history of deleted files?
A Use git log --all --full-history -- <file> to view the complete history of deleted files, including the deletion actions.

📖 Summary


📝 Exercises

  1. Basic Exercise: Use the various options of git log to view project history, including compact mode, statistics, and graphical displays, and try customizing the output format.

  2. Advanced Exercise: Use filter options to find commits that meet specific criteria, such as: finding commits from the past week, finding all commits by a specific author, or finding commits whose commit message contains "fix."

  3. Challenge: Use git bisect to locate the commit that introduced a specific issue in a project with multiple commits, and experience the power of binary search.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏