git log
Exploring 提交 history 使用 git log — viewing, 筛选, and 格式化 past 提交.
你将学到
- 如何 view 提交 history 使用 git log
- Useful flags: --oneline, --图, --stat
- 筛选通过 author, date, or 数字的 提交
- Viewing details 的单个提交使用 git show
概念讲解
git log 的作用
git log shows the commit history of your repository — every commit in reverse chronological order, with its hash, author, date, and message.
git log
The default output is detailed:
commit a1b2c3d4e5f6... (HEAD -> main)
Author: Ada Lovelace <[email protected]>
Date: Mon Jan 15 10:30:00 2025 +0000
Add user login page with OAuth
commit f6e5d4c3b2a1...
Author: Alan Turing <[email protected]>
Date: Sun Jan 14 16:45:00 2025 +0000
Fix crash when cart is empty
Press q to exit the log viewer (it uses a pager like 更少).
The One-Line Format
The most useful flag is --oneline, which condenses each commit to a single line:
git log --oneline
a1b2c3d Add user login page with OAuth
f6e5d4c Fix crash when cart is empty
d4c3b2a Initial commit
This is the format most developers use day-to-day.
The Graph View
To see branches and merges visually:
git log --oneline --graph --all
* a1b2c3d (HEAD -> main) Add user login page
| * b2c3d4e (feature) Add dark mode
|/
* f6e5d4c Fix crash when cart is empty
* d4c3b2a Initial commit
This shows you how branches diverge and merge — essential for understanding your project's history.
Useful Flags
git log --oneline # compact one-line format
git log --graph --oneline # add branch graph
git log --stat # show files changed in each commit
git log -5 # only the last 5 commits
git log --author="Ada" # only commits by a specific author
git log --since="2 weeks ago" # only recent commits
git log --grep="fix" # commits with "fix" in the message
git log --follow README.md # history of a specific file (including renames)
Viewing a Single Commit
To see the full details of one commit — what files changed and how — use git show:
git show a1b2c3d
This shows the commit metadata plus the full diff (the actual changes made in that commit).
Comparing Commits
git diff a1b2c3d f6e5d4c # changes between two commits
git diff HEAD~1 HEAD # changes in the last commit
Understanding git log and git diff together lets you navigate your project's entire history and understand exactly what changed, when, and why.
示例代码
# View the full history (press q to exit)
git log
# Compact one-line view — the most useful format
git log --oneline
# See the last 5 commits only
git log --oneline -5
# Visual graph with all branches
git log --oneline --graph --all
# Show files changed in each commit
git log --stat -3
# Filter by author
git log --author="Ada" --oneline
# Filter by date
git log --since="2 weeks ago" --oneline
# Search commit messages
git log --grep="fix" --oneline
# View the details of a specific commit
git show HEAD # the most recent commit
git show HEAD~1 # the commit before that
# See the history of a single file
git log --oneline --follow README.md
# Compare two commits
git diff HEAD~2 HEAD --stat
Covers 必要 git log variations: compact, 图, 筛选通过 author/date/消息, and viewing 单个提交使用 git show. --oneline and --图 flags 是 ones 你将 use 大多数.
动手试一试
- Code Diff
Compare two versions — git log and git diff 工作 together 到 show history.
- CI/CD YAML Generator
CI 流水线经常 display git log 输出 — generate one 到 see.
常见错误
错误写法
是 stuck in git log pager and 不 knowing 如何 exit
正确做法
git log uses pager (更少). 按下 q 到 quit, 空间到 page 下, b 到 page 上. Use git log --oneline for 快速 view 那个经常 fits on one screen, or git log -n 到限制输出.
错误写法
不 knowing 关于 --oneline and --图 flags
正确做法
默认 git log 输出是 verbose. git log --oneline gives compact view, and --图 --oneline --所有 shows 分支 visually. 这些 two 格式 cover 90% 的 daily usage.
相关资源
相关代码片段
- Git Log Snippet
高级 git log 格式化 and 筛选示例.
相关速查表
- Git Cheatsheet
git log flags and history 导航.
练习 git log
14 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。