Git:blame 命令

Oct 30, 2021

目录


ref: https://git-scm.com/docs/git-blame


命令


1git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental]
2          [-L <range>] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>]
3          [--ignore-rev <rev>] [--ignore-revs-file <file>]
4          [--progress] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>..<rev>]
5          [--] <file>

git-blame - Show what revision and author last modified each line of a file


描述


用来自最后一次修改该行的修改版本的信息标注指定文件中的每一行。 可以指定特定的修改版本来进行标注。

此命令的输出信息中不包含已经删除的行和替换掉的行的信息。


选项


  • -b

为边界提交显示空白 SHA-1。 也可以通过配置选项 blame.blankBoundary 控制。

  • --root

不要将根提交视为边界。 也可以通过配置选项 blame.showRoot 控制。

  • --show-stats

blame 输出的末尾包括额外的统计数据。

  • -L <start>, <end>
  • -L :<funcname>

仅仅标注处于 <start>, <end> 之间的行,或者标注通过函数名正则表达式 **<funcname>**匹配的范围。

可以多次使用此选项,运行指定的范围之间重合。

<start><end> 选项是可选的。 -L <start>或者 -L <start>, 将涵盖从 <start> 到文件结束的行。 -L , <end> 将涵盖从文件的第一行到 <end> 的行。

<start><end> 可以是以下格式:

number
    指定特定的行号(行号从 1 开始)

/regex/
    此格式将使用与给定 POSIX 正则表达式匹配的第一行。
    如果 <start> 是正则表达式,他将从文件开始搜索。 如果当前 `-L` 指令之前还有其他的 `-L` 指令,那么当前 `-L` 指定将从上一个指定的末尾位置开始搜索。
    如果 <start> 是 `^/regex/`, 他将从文件开始搜索。
    如果 <end> 是正则表达式, 它将从 <start> 指定的行开始搜索。

+offset 或者 -offset
    这种格式只能用于 <end>。 用于指定从 <start> 开始往前或者往后的行数。

如果使用 :<funcname> 格式指定,那么涵盖的范围是:从第一个匹配当前 funcname的位置到下一个匹配的位置。 如果此选项之前指定了其他的 -L 指定,那么当前指定从上一个指定的末尾位置开始搜索,否则,从文件的第一行开始搜索。

The function names are determined in the same way as git diff works out patch hunk headers (see Defining a custom hunk-header in gitattributes[5]).

  • -l

显示 long rev,默认关闭。

  • -t

显示 raw timestamp, 默认关闭。

  • -S <revs-file>

Use revisions from revs-file instead of calling git-rev-list[1].

  • --reverse <rev>..<rev>

Walk history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revision like START..END where the path to blame exists in START. git blame –reverse START is taken as git blame –reverse START..HEAD for convenience.

  • --first-parent

Follow only the first parent commit upon seeing a merge commit. This option can be used to determine when a line was introduced to a particular integration branch, rather than when it was introduced to the history overall.

  • -p, --porcelain

显示供机器使用的格式。

  • --line-porcelain

显示供机器使用的格式, 显示每行的提交信息,而不是仅仅最后一次提交的信息。 隐含地指定了 --porcelain.

  • --incremental

以供机器使用的格式递增的显示。

  • --encoding=<encoding>

指定用于显示作者姓名和提交信息的字符编码。 当指定为 none 时,git 对输出不做任何编码,原样输出。

  • --contents <file>

When <rev> is not specified, the command annotates the changes starting backwards from the working tree copy. This flag makes the command pretend as if the working tree copy has the contents of the named file (specify - to make the command read from the standard input).

  • --date <format>

指定用于显示时间的格式。

  • --[no-]progress

Progress status is reported on the standard error stream by default when it is attached to a terminal. This flag enables progress reporting even if not attached to a terminal. Can’t use –progress together with –porcelain or –incremental.

  • M[<num>]

检测文件中移动或复制的行。

When a commit moves or copies a block of lines (e.g. the original file has A and then B, and the commit changes it to B and then A), the traditional blame algorithm notices only half of the movement and typically blames the lines that were moved up (i.e. B) to the parent and assigns blame to the lines that were moved down (i.e. A) to the child commit. With this option, both groups of lines are blamed on the parent by running extra passes of inspection.

<num> is optional but it is the lower bound on the number of alphanumeric characters that Git must detect as moving/copying within a file for it to associate those lines with the parent commit. The default value is 20.

  • C[<num>]

In addition to -M, detect lines moved or copied from other files that were modified in the same commit. This is useful when you reorganize your program and move code around across files. When this option is given twice, the command additionally looks for copies from other files in the commit that creates the file. When this option is given three times, the command additionally looks for copies from other files in any commit.

<num> is optional but it is the lower bound on the number of alphanumeric characters that Git must detect as moving/copying between files for it to associate those lines with the parent commit. And the default value is 40. If there are more than one -C options given, the argument of the last -C will take effect.

  • -e, --show-email

显示作者邮件信息,取代作者名称。 默认关闭。 也可以通过配置选项 blame.showEmail 控制。

  • --ignore-rev <rev>

忽略指定的版本号。

  • --ignore-revs-file <file>

忽略在 file 中列举出来的版本。 这个文件的格式必须和 fsck.skipList 相同。 这个选项可以多次使用。

  • -c

使用和 git-annodate 相同的输出模式。

  • --score-debug

Include debugging information related to the movement of lines between files (see -C) and lines moved within a file (see -M). The first number listed is the score. This is the number of alphanumeric characters detected as having been moved between or within files. This must be above a certain threshold for git blame to consider those lines of code to have been moved.

  • -f
  • --show-name

显示原始文件的名称。

  • -n,
  • --show-number

显示在原始文件中的行号,默认关闭。

  • -s

不显示作者名称和时间戳

  • -w

在比较父版本和子版本以查找行来自何处时,忽略空格。

  • --abbrev=<n>

不使用默认的7+1十六进制数字作为缩写对象名,而是使用 <m>+1 位数字,这里 <m> 是最后 <n> 个数字。 但是 git 会确保这对象名可以唯一表示该对象。


机器格式 (PORCELAIN FORMAT)


在这种格式中,每行都会有一个头,这个头至少包含一行:

  • 当前行的提交的 40 字节的 SHA-1 值
  • 当前行在原始文件中的行号
  • 当前行在最终文件中的行号
  • on a line that starts a group of lines from a different commit than the previous one, the number of lines in this group. On subsequent lines this field is absent.

紧跟着每个头,会包含至少一组如下信息:

  • 作者名称(“author”),作者邮箱(“email”),作者时间(“author-time”), 和时区(“author-tz”)
  • 提交时文件的名称
  • 提交信息的第一行 (“summary”)

行的实际内容在上述头之后显示,该行会有一个 TAB 前缀。


END!!!


Tags