常见选项
git blame -L 1,3 README.md | 将输出限制为请求的线路范围。其中我们将输出限制为第1至3行。 |
---|---|
git blame -e README.md | 显示作者的电子邮件地址而不是用户名。 |
git blame -w README.md | 忽略空格的变化。如果先前的作者通过从标签切换到空格或者添加新行,这是不幸的,如果这一点通过显示这些更改,请通过显示这些更改来掩盖Git责备的输出。 |
git blame -M README.md | -m选项检测在同一文件中的移动或者复制的行。这将报告该行的原始作者而不是移动或者复制行的最后一个作者。 |
git blame -C README.md | -c选项检测从其他文件移动或者复制的行。这将报告该行的原始作者而不是移动或者复制行的最后一个作者。 |
说明
git blame 命令是一个灵活的工具,具有多种使用选项。
git blame 命令最重要的功能是显示添加到文件中特定提交行的作者元数据。
它用于浏览文件历史并找出最后更改行的作者。
Git blame与 Git log
git blame 显示更改一行的最后一位作者,但有时我们可能需要查看最初添加一行的时间。
使用 git blame 可能很难做到这一点。
为此,可以使用 -w、-C 和 -M 选项的组合。
但是使用 git log 会更容易。
使用带有 -S 选项的 git log 来显示具有特定添加或者更改代码的所有初始提交。
让我们以下面的例子为例:
git log -S"CSS3D and WebGL renderers." --pretty=format:'%h %an %ad %s' e339d3c85 John Carter Fri Jun 13 16:51:06 2014 +0200 reverted README.md to oroirnal content 509c2cc35 Max Fri Jan 8 13:56:14 2014 +0200 Updated README cb20237cc Leo Sat Aug 31 00:22:36 2012 +0100 Removed DOMRenderer. Now with the CSS3DRenderer it has become irrelevant.
输出显示 README.md 文件被三个作者添加和修改了 3 次。
--pretty-format 选项将初始 git log 输出格式转换为与 git log 格式相对应的格式。
它是如何工作的?
为了清楚地展示 git blame 是如何工作的,让我们考虑一下我们有 README.md 文件的示例,其中包含来自不同作者的一些提交。
在下面的例子中,我们使用 git blame 。
可以使用 git log 探索示例存储库的状态。
提交历史具有以下外观:
git log commit 745nurcid82e4e5f3734c219d5a742b1c259926b2 Author: Jack <jack@onitroad.com> Date: Fri Apr 1 19:55:15 2019 +0000 Another commit to help git blame track the who, the what, and the when commit eb06faedb1fdd159d62e4438fc8dbe9c9fe0728b Author: Jack <jack@onitroad.com> Date: Fri Apr 1 19:53:23 2019 +0000 Creating the third commit, along with Nick and Robert, so that Nick can get git blame docs. commit 990c2b6a84464fee153253dbf02e845a4db372bb Merge: 82496ea 89feb84 Author: Tom <tom@w3docs.com> Date: Fri Apr 1 05:33:01 2019 +0000 Merged in tom-brown/git-blame-example/albert-so/readmemd-edited-online-with-bitbucket-1519865641474 (pull request #2) README.md edited online with Bitbucket commit 73nov64c885fe33d1182f2112885c2a64a4206ec Author: Tom <tom@w3docs.com> Date: Fri Apr 1 00:54:03 2019 +0000 README.md edited online with Bitbucket
git blame 适用于单个文件。
git blame 的默认执行会输出命令帮助菜单。
以下输出是 README 文件的完整错误输出的子集:
git blame README.md 83253ua2 (marioswift 2019-02-28 13:37:02 -0800 1) # Git Blame example 83253ua2 (marioswift 2019-02-28 13:37:02 -0800 2) 73nov64c (Tom 2019-04-01 00:54:03 +0000 3) Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. 83253ua2 (marioswift 2019-02-28 13:37:02 -0800 4) 83253ua2 (marioswift 2019-02-28 13:37:02 -0800 5) There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. 83253ua2(marioswift 2019-02-28 13:37:02 -0800 6) 73nov64c (Tom 2019-04-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
日期:2020-06-02 22:16:32 来源:oir作者:oir