Git 如何在文件的特定版本中获取源行号?

Git 如何在文件的特定版本中获取源行号?,git,git-blame,Git,Git Blame,可以查看带有git bull的源代码/原始代码行的行号,但它会根据对行进行修改的上次提交显示行号 我想对文件的特定提交/修订执行相同的操作 例如 文件:File.ext(xyz11是我们当前正在审阅的文件的修订/提交) 内容: Line 1 (**abc11** is the last commit changed this line) Line 2 (**abc12** is the last commit changed this line) Line 3 (**abc13** is the

可以查看带有
git bull
源代码/原始代码行的行号,但它会根据对行进行修改的上次提交显示行号

我想对文件的特定提交/修订执行相同的操作

例如

文件:
File.ext
xyz11是我们当前正在审阅的文件的修订/提交)

内容:

Line 1 (**abc11** is the last commit changed this line)
Line 2 (**abc12** is the last commit changed this line)
Line 3 (**abc13** is the last commit changed this line)
我想为“第3行”获取“3”。Git将根据行的提交(abc13)提交显示此信息。但是,由于xyz11abc13版本包含不同的内容,因此xyz11中的实际行号可能不同

那么,如何才能在文件的特定版本中获得行号呢

注意:我之所以说“源代码/原始行号”是因为我想得到正确的行号,即使文档是脏的(有未限制的更改),也可以使用
git bull

我的场景是,我将在API请求中使用这些行号来添加内联注释

因此,假设我修改了
文件.ext

Line 1
Line 2
Uncommited Line 
Uncommited Line
Line 3
对于“第3行”,我应该保持“3”,而不是“5”,否则评论将转到错误的行。正如我所说的,它可以通过
git-bull
实现,但是它会根据行的提交来显示这些信息


谢谢

如果我理解正确,您有一个包含未提交更改的文件,您的
git bull
如下所示

$ git blame foo

^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 3) Uncommitted Line
00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 4) Uncommitted Line
^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3
使用
-n
显示原始提交中的行

$ git blame -n foo

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 3 (Not Committed Yet  2019-01-01 12:58:47 -0800 3) Uncommitted Line
00000000 4 (Not Committed Yet  2019-01-01 12:58:47 -0800 4) Uncommitted Line
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3
要忽略所有未提交和未老化的更改,请使用
git-bull-HEAD
HEAD
是最后一次提交。这将从
HEAD
向后查找对文件的所有更改。因为中间的提交也会抛出行号,所以您仍然需要
-n
在该提交中获取行号。比如说

$ git blame -n foo

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 3 (Not Committed Yet  2019-01-01 13:03:06 -0800 3) Uncommitted line
4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 4) Line 2.5
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3

$ git blame -n foo HEAD

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 3) Line 2.5
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 4) Line 3

有一个选项rev用于git dull,因此,您可以指定要责备的提交/修订:

git blame <rev> file

更多信息请参见

这似乎是我想要的。Thanks@user3790180请,如果这对您有效,请将其标记为已接受(绿色复选标记✓) 向上投票!!提前Thnx!!这个(
-n
参数)和@JosuéCortina的答案的组合似乎就是我想要的。谢谢
git blame xyz11 file.txt