git log 命令用法实例 (2)

使用 git log 命令查看提交记录时,默认打印commit hash值、作者、提交日期、和提交信息。如果想要查看更多内容,可以提供不同的参数来指定查看的信息。具体实例说明如下。

在git log中显示committer信息

git log 命令默认显示的里面只有author,没有committer,类似于下面的信息:

$ git log
commit b932a847f5xxxxx
Author: John <[email protected]>
Date:   Mon Oct 21 16:18:09 2019 +0800

    hello release

如果要显示committer的信息,可以使用 --pretty=full 选项。例如下面显示的信息:

$ git log --pretty=full
commit b932a847f5xxxxx
Author: John <[email protected]>
Commit: John <[email protected]>

    hello release

查看 man git-log 对 --pretty 选项说明如下:

--pretty[=<format>], --format=<format>
Pretty-print the contents of the commit logs in a given format, where <format> can be one of oneline, short, medium, full, fuller, email, raw and format:<string>.

默认的 medium 格式样式如下:

    medium
        commit <sha1>
        Author: <author>
        Date:   <author date>

        <title line>

        <full commit message>

可以显示 committer 信息的 full 格式样式如下:

    full
        commit <sha1>
        Author: <author>
        Commit: <committer>

        <title line>

        <full commit message>

这里的 author 和 committer 的区别是,author 是进行这个修改的人,而 committer 是把这个修改提交到git仓库的人。
一般来说,我们自己修改代码,然后执行 git commit,那么既是 author,又是 committer。
如果别人用 git format-patch 生成 git patch,在patch文件里面会包含修改者的名称和邮箱信息。例如:

From 033abaaecdxxxx Mon Sep 17 00:00:00 2001
From: Jobs <[email protected]>
Date: Mon, 21 Oct 2019 16:18:09 +0800
Subject: [PATCH] hello release

我们拿到这个patch文件,用 git am 命令把patch合入本地仓库,那么 author 是这个patch文件的修改者 Jobs,而 committer 是我们自己。

只查看某个人的提交历史

使用 git log --author=<pattern> 命令来查看某个作者的提交历史。
使用 git log --committer=<pattern> 命令来查看某个提交者的提交历史。
查看 man git-log 对这两个选项的说明如下:

--author=<pattern>, --committer=<pattern>
Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression). With more than one --author=<pattern>, commits whose author matches any of the given patterns are chosen (similarly for multiple --committer=<pattern>).

即,所给的 pattern 参数可以用正则表达式来匹配特定模式。举例如下:
使用 git log --author=John 查看 John 的上库信息,如果有多个人名都带有 John,会匹配到多个人的提交历史。
使用 git log [email protected] 来查看 [email protected] 这个邮箱的提交历史。
使用 git log [email protected] 来查看 @xxxx.com 这个邮箱后缀的提交历史。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章