git - 如何在文件的特定修订版中获取源代码行号?

标签 git git-blame

git blame可以看到source/original行号但是显示的是last的行号在行中进行修改的提交

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

例子,

文件: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""Line 3"。 Git blame 将根据行的提交 (abc13) 提交显示此信息。但是,由于 xyz11abc13 修订包含不同的内容,因此 xyz11 中的实际行号可能不同。

那么如何获取文件特定修订版中的行号呢?

注意:我说“源/原始行号”因为我想获得正确的行号,即使文档是脏的(有未提交的更改)git blame

我的方案是,我将在 API 请求中使用这些行号来添加内嵌评论

所以,假设我修改了 file.ext

Line 1
Line 2
Uncommited Line 
Uncommited Line
Line 3

对于 "Line 3" 我应该仍然是 "3" 而不是 "5",否则评论会转到错误的行。正如我所说,它可能与 git blame 但它根据行的提交显示此信息

谢谢

最佳答案

如果我没理解错的话,你有一个包含未提交更改的文件和你的 git blame看起来像这样。

$ 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 blame <file> 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

关于git - 如何在文件的特定修订版中获取源代码行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998810/

相关文章:

git - 如何将特定分支发布到azure应用程序服务?

git - --no-ff merge 如何打破平分和指责?

git - 如何搜索有人用 git 更改的字符串?

git - 使用代码格式化程序时如何逐行保留 git 作者?

python - Setup.py 重新安装已经安装的用户编写的包

git - 如何在 Git 中创建一个没有提交的补丁?

Android Studio - 从本地创建远程分支

git - 通过cURL删除Bitbucket存储库

git - 如何在git中找到所有更改行的先前作者?

git - SourceTree 中的 git-blame 在哪里