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

时间:2012-06-18 18:40:57

标签: git

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

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

1 个答案:

答案 0 :(得分:3)

您希望合并-S上的--prettygit 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.

此标志查找特定字符串的所有更改。 Using an example from another SO answer, 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
相关问题