30 seconds of code:用 git log 按作者(Author)或提交者(Committer)过滤提交历史

原创2026-09-30 13:26:272,682 阅读
文章标签:教程文档

30 seconds of code:用 git log 按作者(Author)或提交者(Committer)过滤提交历史

本文整理自 30 seconds of code 仓库中的 Git 代码片段 view-commits-by-author-or-committer,讲解如何用 git log --author=<author> 与 git log --committer=<committer> 精确过滤提交历史。阅读本文后,你将掌握按作者/提交者检索提交的完整命令用法,理解 Author 与 Committer 两个身份字段的差异,并学会把这些过滤器与 --oneline、--since/--until 等选项组合,快速定位某位开发者的全部改动。

在浏览一个 Git 仓库的提交历史时,最常见的需求之一就是按作者或提交者过滤提交。无论是想查看某位同事负责了哪些改动、复盘某次发布由谁提交,还是排查某个功能分支的开发记录,git log 都提供了开箱即用的过滤能力。

Author 与 Committer:先分清两个"身份"

在深入命令之前,有必要先理解 Git 提交中的两个身份字段:

  • Author(作者):写这份代码的人。git log 输出中 Author: 一行展示的就是它。
  • Committer(提交者):把代码写入仓库的人。

在绝大多数场景下两者是同一个人,因此在 git log 的输出中默认只展示 Author:。但二者确实可以不同——例如通过邮件或补丁协作时,A 编写补丁、B 负责应用并提交,那么 Author 是 A、Committer 是 B。理解这一区别,才能选对下面的过滤选项。

按特定作者查看提交:git log --author

当你希望查看特定作者提交的历史时,使用 git log --author=<author> 即可按作者过滤提交历史,将 <author> 替换为你想查找的作者名字即可。

# Syntax: git log --author=<author>

git log --author="Duck Quacking"
# commit c191f90c7766ee6d5f24e90b552a6d446f0d02e4
# Author: 30 seconds of code
# Date: Tue Apr 6 11:11:08 2021 +0300
# [...]

命令会列出所有匹配该作者名的提交,每一条都包含完整的 commit 哈希、作者信息与提交时间,随后是本次提交的改动说明(省略号部分)。你可以直接复制上述命令到自己的仓库中运行,替换作者名即可使用。

按特定提交者查看提交:git log --committer

与之对称,若要按特定提交者过滤,则使用 git log --committer=<committer>,将 <committer> 替换为你要查找的提交者名字。

# Syntax: git log --committer=<committer>

git log --committer="Duck Quacking"
# commit c191f90c7766ee6d5f24e90b552a6d446f0d02e4
# Author: 30 seconds of code
# Date: Tue Apr 6 11:11:08 2021 +0300
# [...]

与 --author 相比,--committer 过滤的是"提交者"字段,在 Author 与 Committer 不同的协作工作流中(如补丁转交、CI 机器人代提交)会体现出明显差异。在 30 seconds of code 仓库的 Git 语言配置 中,这两个选项均保留了指向 Git 官方 git-log 文档的引用,说明它们是 git log 的标准过滤参数,适用于所有当前维护中的 Git 版本。

组合过滤:让提交检索更精准

--author 与 --committer 并非孤立使用,它们可以与 git log 的其他过滤选项自由组合。仓库中同属 Git Commit 片段集 的相邻片段提供了大量可直接拼装的组合件:

1. 精简单行输出

git log --oneline 实际是 --pretty=oneline --abbrev-commit 的简写,每个提交只占一行、哈希缩写为 7 位,配合作者过滤能快速得到"某人改了什么"的清单(详见 view-commits-summary):

git log --author="Duck Quacking" --oneline
# d540ba1 Merge network bug fix
# 3050fc0 Fix network bug
# c191f90 Initial commit

2. 排除合并提交

追加 --no-merges 可去掉合并提交,只保留真正的代码改动(同上片段):

git log --author="Duck Quacking" --oneline --no-merges
# 3050fc0 Fix network bug
# c191f90 Initial commit

3. 限定日期范围

配合 --since=<date-from> --until=<date-to> 可以进一步把检索压缩到某个时间段,日期支持 YYYY-MM-DD、MM/DD/YYYY 乃至 yesterday、2 weeks ago 等相对写法(详见 view-commits-in-date-range):

git log --author="Duck Quacking" --since='2 weeks ago' --oneline

4. 可视化提交图

如果还想直观看到提交与分支的关系,可叠加 --pretty=oneline --graph --decorate --all,把过滤后的提交以 ASCII 分支图呈现(详见 view-commit-graph)。

补充:匹配模式与大小写

从 Git 官方文档对 --author、--committer 的定义(仓库 git.yaml 保留了对应文档锚点)可以了解到:这两个选项的值都作为正则表达式模式与对应字段进行匹配,且不区分大小写。这意味着:

  • 即使只记得作者名字的一部分,如 git log --author="Duck",也能匹配到包含该子串的所有作者;
  • 不记得大小写也没关系,"duck quacking" 与 "Duck Quacking" 效果一致;
  • 会正则的话,还可以写出更灵活的模式,如 git log --author="Duck|Quack" 同时匹配两位作者。

延伸:Author 字段从何而来

作者与提交者字段并非凭空产生。在使用 git commit 创建提交时,Git 会读取配置中的用户信息填入 Author 字段——默认取全局配置,也可以用 git config user.name <name>、git config user.email <email> 为单个仓库单独指定(详见 config-user):

# Configure user for current repository
git config user.email "cool.duck@qua.ck"
git config user.name "Duck Quackers"

# Configure global Git user
git config --global user.email "cool.duck@qua.ck"
git config --global user.name "Duck Quackers"

此外,git commit 也支持 --author="<name> <email>" 在提交时临时指定作者,甚至用 git commit --amend --author=... 修正最近一次提交的作者(详见 set-or-amend-commit-author)。当仓库中出现非本人配置的作者名提交时,git log --author 正是把它们筛出来的最直接手段。

小结

  • 按作者过滤:git log --author=<author>;
  • 按提交者过滤:git log --committer=<committer>;
  • 两个选项都接受正则表达式、不区分大小写,可与 --oneline、--no-merges、--since/--until、--graph 等选项任意组合;
  • 理解 Author 与 Committer 的区别,才能选对过滤维度,在协作式提交工作流中快速定位真正的责任人。
登录后查看全文
30-seconds-of-code