git blame

git blame 命令是一个多功能的故障排除实用程序,具有广泛的使用选项。git blame 的高级功能是显示附加到文件中特定提交行的作者元数据。该功能用于检查文件历史记录的特定点,并获取最后修改该行代码的作者相关背景信息。它还用于探索特定代码的历史记录并回答有关将代码添加到存储库的内容、方式和原因的问题。

Git blame 通常用于 GUI 显示。像 Bitbucket 这样的在线 Git 托管网站提供 blame 视图,这些视图是 git blame 的用户界面封装。这些视图在围绕拉取请求和提交的协作讨论中引用。此外,大多数集成了 Git 的 IDE 也有动态 blame 视图。

工作原理

为了演示 git blame,我们需要一个包含一些历史记录的存储库。我们将使用开源项目 git-blame-example。这个开源项目是一个简单的存储库,其中包含一个 README.md 文件,其中包含一些来自不同作者的提交。我们的 git blame 用法示例的第一步是 git clone 示例存储库。

git clone https://kevzettler@bitbucket.org/kevzettler/git-blame-example.git && cd git-blame-example

现在我们有了示例代码的副本,我们可以使用 git blame 开始探索了。示例代码存储库的状态可以使用 git log 进行检查。提交历史记录应如下所示:

$ git log
    commit 548dabed82e4e5f3734c219d5a742b1c259926b2
    Author: Juni Mukherjee <jmukherjee@atlassian.com>
    Date:   Thu Mar 1 19:55:15 2018 +0000

        Another commit to help git blame track the who, the what, and the when

    commit eb06faedb1fdd159d62e4438fc8dbe9c9fe0728b
    Author: Juni Mukherjee <jmukherjee@atlassian.com>
    Date:   Thu Mar 1 19:53:23 2018 +0000

        Creating the third commit, along with Kev and Albert, so that Kev can get git blame docs.

    commit 990c2b6a84464fee153253dbf02e845a4db372bb
    Merge: 82496ea 89feb84
    Author: Albert So <aso@atlassian.com>
    Date:   Thu Mar 1 05:33:01 2018 +0000

        Merged in albert-so/git-blame-example/albert-so/readmemd-edited-online-with-bitbucket-1519865641474 (pull request #2)

        README.md edited online with Bitbucket

    commit 89feb84d885fe33d1182f2112885c2a64a4206ec
    Author: Albert So <aso@atlassian.com>
    Date:   Thu Mar 1 00:54:03 2018 +0000

        README.md edited online with Bitbucket

git blame 仅对单个文件起作用。任何有用的输出都需要文件路径。git blame 的默认执行只会输出命令帮助菜单。在本示例中,我们将对 README.MD 文件进行操作。在 git 存储库的根目录中包含 README 文件作为项目的文档源是一种常见的开源软件做法。

git blame README.MD

执行上述命令将为我们提供第一个 blame 输出样本。以下输出是 README 的完整 blame 输出的子集。此外,此输出是静态的,反映了撰写本文时代码存储库的状态。

$ git blame README.md
    82496ea3 (kevzettler     2018-02-28 13:37:02 -0800  1) # Git Blame example
    82496ea3 (kevzettler     2018-02-28 13:37:02 -0800  2)
    89feb84d (Albert So      2018-03-01 00:54:03 +0000  3) This repository is an example of a project with multiple contributors making commits.
    82496ea3 (kevzettler     2018-02-28 13:37:02 -0800  4)
    82496ea3 (kevzettler     2018-02-28 13:37:02 -0800  5) The repo use used elsewhere to demonstrate `git blame`
    82496ea3 (kevzettler     2018-02-28 13:37:02 -0800  6)
    89feb84d (Albert So      2018-03-01 00:54:03 +0000  7) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod TEMPOR incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
    89feb84d (Albert So      2018-03-01 00:54:03 +0000  8)
    eb06faed (Juni Mukherjee 2018-03-01 19:53:23 +0000  9) Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision.
    eb06faed (Juni Mukherjee 2018-03-01 19:53:23 +0000 10)
    548dabed (Juni Mukherjee 2018-03-01 19:55:15 +0000 11) Creating a line to support documentation needs for git blame.
    548dabed (Juni Mukherjee 2018-03-01 19:55:15 +0000 12)
    548dabed (Juni Mukherjee 2018-03-01 19:55:15 +0000 13) Also, it is important to have a few of these commits to clearly reflect the who, the what and the when. This will help Kev get good screenshots when he runs the git blame on this README.

这是 README.md 文件前 13 行的示例。为了更好地理解这个输出,我们来分解一行。下表显示第 3 行的内容,表中的列表示列内容。

ID

作者

时间戳

行号

行内容

89feb84d

Albert So

2018-03-01 00:54:03 +0000

3

此存储库是一个由多个贡献者提交的项目的示例。

如果我们查看 blame 输出列表,可以得出一些观察结果。列出了三位作者,除了该项目的维护人员 Kev Zettler 外,还列出了 Albert So 和 Juni Mukherjee。作者通常是 git blame 输出中最有价值的部分,时间戳列也很有用。行内容列显示了变更内容。

常用选项

git blame -L 1,5 README.md

-L 选项会将输出限制在请求的行范围内。在这里,我们将输出限制在第 1 行到第 5 行。

git blame -e README.md

-e 选项显示作者的电子邮件地址而不是用户名。

git blame -w README.md

-w 选项忽略空格变更。不幸的是,如果以前的作者通过从制表符切换到空格或添加新行来修改文件的间距,则会通过显示这些变更来掩盖 git blame 的输出。

git blame -M README.md

-M 选项可检测同一文件中移动或复制的行。该选项会报告代码行的原作者,而非最后移动或复制这些代码行的作者。

git blame -C README.md

-C 选项可检测从其他文件中移动或复制过来的行。该选项会报告代码行的原作者,而非最后移动或复制这些代码行的作者。

git blame 与 git log

虽然 git blame 会显示最后修改行的作者,但通常您需要知道该行最初是在什么时候添加的。使用 git blame 来实现这可能很麻烦。它需要 -w-C-M 选项的组合。使用 git log 命令可能要方便得多。

要列出所有添加或修改了特定代码段的原始提交,请执行带 -S 选项的 git log。将 -S 选项附加到您要查找的代码中。我们以上方 README 输出中的一行为例。我们从 README 输出的第 12 行中提取文本 "CSS3D and WebGL renderers"。

$ git log -S"CSS3D and WebGL renderers." --pretty=format:'%h %an %ad %s'
    e339d3c85 Mario Schuettel Tue Oct 13 16:51:06 2015 +0200 reverted README.md to original content
    509c2cc35 Daniel Tue Sep 8 13:56:14 2015 +0200 Updated README
    cb20237cc Mr.doob Mon Dec 31 00:22:36 2012 +0100 Removed DOMRenderer. Now with the CSS3DRenderer it has become irrelevant.

此输出向我们显示,README 中的内容由 3 位不同的作者添加或修改 3 次。它最初是由 Mr.doob 在 cb20237cc 提交中添加。在本示例中,git log 前面还加上了 --pretty-format 选项。此选项将 git log 的默认输出格式转换为与 git log 格式相匹配的输出格式。有关用法和配置选项的更多信息,请访问 git log 页面。

摘要

git blame 命令用于逐行检查文件内容,查看每行最后一次修改的时间以及修改的作者是谁。git blame 的输出格式可以通过各种命令行选项进行更改。像 Bitbucket 这样的在线 Git 托管解决方案提供注解视图,相比命令行形式的 git blame 使用,它能带来更优质的用户体验。git blame 和 git log 可以结合使用来帮助发现文件内容的历史记录。git log 命令也有一些类似的注解功能,要了解更多信息,请访问 git log 概述页面。

为您推荐

Bitbucket 博客

DevOps 学习路径

了解有关 Git 的更多信息

在此中心查找更多 Git 指南和资源。