git可以找出所有曾经改变过一行代码的作者吗?

use*_*858 5 git

我想找出所有改变了一行代码的作者.Git-log可以在文件级别上执行.我阅读了手册页,似乎git log无法在行级别上执行.有没有办法使用git命令来做或我需要为自己编写脚本?

出于我的目的,我想对所有代码行执行此操作.我这样做是为了为我的研究提供作者培训数据.

Chr*_*her 3

您想要组合-S--pretty上的标志git log

git log手册页中,-S镐搜索:

   -S<string>
       Look for differences that introduce or remove an instance of <string>. Note that this is different than the string simply appearing in diff output;
       see the pickaxe entry in gitdiffcore(7) for more details.
Run Code Online (Sandbox Code Playgroud)

该标志查找对特定字符串的所有更改。使用另一个 SO 答案中的示例, git log -g -Ssearch_for_this将搜索历史记录以查找“search_for_this”的所有更改。

结合起来--pretty可以获得更好的效果。例如,此命令将在历史记录中搜索“foo”的更改并返回缩短的哈希值,后跟作者、两个破折号、消息的第一行、两个破折号和日期:

$ git log -Sfoo --pretty=format:'%h %an -- %s -- %ad'
bc34134 Sally Developer -- Fixed all bugs and made all clients happy forever -- Tue Jan 31 17:41:17 2025 -0500
Run Code Online (Sandbox Code Playgroud)