'git blame' 是做什么的?

What does 'git blame' do?

看到很多关于使用方法的问题git blame,但是我不是很懂

我在 GitHub 界面的文件顶部看到一个 Blame 按钮。单击它后,它会在左侧栏中显示用户名的一些差异。这说明什么?

除了GitHub,为什么实际使用git blame

From GitHub:

The blame command is a Git feature, designed to help you determine who made changes to a file.

Despite its negative-sounding name, git blame is actually pretty innocuous; its primary function is to point out who changed which lines in a file, and why. It can be a useful tool to identify changes in your code.

基本上,git-blame 用于显示文件每一行最后修改的版本和作者。就像查看一个文件的发展历史。

来自git-blame:

Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision.

When specified one or more times, -L restricts annotation to the requested lines.

示例:

johndoe@server.com:~# git blame .htaccess
...
^e1fb2d7 (John Doe 2015-07-03 06:30:25 -0300  4) allow from all
^72fgsdl (Arthur King 2015-07-03 06:34:12 -0300  5)
^e1fb2d7 (John Doe 2015-07-03 06:30:25 -0300  6) <IfModule mod_rewrite.c>
^72fgsdl (Arthur King 2015-07-03 06:34:12 -0300  7)     RewriteEngine On
...

请注意,git blame 不显示按时间顺序排列的每行修改历史记录。 它仅显示在 HEAD 中的最后一次提交之前谁是最后一个更改文档行的人。

也就是说,为了查看完整的 history/log 文档行,您需要 运行 对 [=14= 中的每个提交进行 git blame path/to/file ].

就是要弄清楚是哪位同事写了具体的行或者毁了项目,所以你可以责怪他们:)

git blame命令用于知道who/whichcommit负责对文件所做的最新更改。每行的author/commit也可以看到

git blame filename(提交对代码中所有行的更改负责)

git blame filename -L 0,10(提交负责从“0”行到“10”行的更改)

还有许多其他的责备选项,但通常这些可能有所帮助。

git blame 命令使用来自最后修改该行的修订版的信息来注释行,并且...使用 Git 2.22(2019 年第二季度),将这样做 更快,因为围绕“git blame”进行了性能修复,尤其是在线性历史中(这是我们应该优化的规范)。

参见 commit f892014 (02 Apr 2019) by David Kastrup (fedelibre)(由 Junio C Hamano -- gitster -- in commit 4d8c4da 合并,2019 年 4 月 25 日)

blame.c: don't drop origin blobs as eagerly

When a parent blob already has chunks queued up for blaming, dropping the blob at the end of one blame step will cause it to get reloaded right away, doubling the amount of I/O and unpacking when processing a linear history.

Keeping such parent blobs in memory seems like a reasonable optimization that should incur additional memory pressure mostly when processing the merges from old branches.

git blame命令用于逐行检查文件内容,查看每行的最后修改时间以及修改者是谁。

If there was a bug in code,use it to identify who cased it,then you can blame him. Git blame is get blame(d).

如果你需要知道一行代码的历史,用git log -S"code here",比git怪简单。

git log vs git blame