如何在git中找到引入特定行的提交?还是有更好的选择吗?

Pum*_*kin 5 git

想象一下git项目中的一个类,它经历了1000次提交,一遍又一遍地重新访问.我有没有机会检查何时(在哪个提交中)特定的代码行被引入该类?

如果没有,是否有另一种方法可以找到我特别感兴趣的行集?

TY.

Chr*_*her 10

一行特定的代码?就像你知道这条线提前了吗?当然有可能.事实上,它很容易愚蠢.只需使用pickaxe搜索选项git log:

-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)

假设该类是public class Foo {,您可以找到触及该字符串的每个提交:

git log -S"public class Foo"
Run Code Online (Sandbox Code Playgroud)

如果要将其限制为特定文件,只需使用标准--语法:

git log -S"public class Foo" -- Foo.java
Run Code Online (Sandbox Code Playgroud)

一般来说,使用这个:

git log -S<string> [-- <file>]
Run Code Online (Sandbox Code Playgroud)


Wil*_*der 3

您可以git bisect在引入某些代码时使用回溯(请参阅http://git-scm.com/book/en/Git-Tools-Debugging-with-Git),并且可以使用此技术每次检查代码,然后看看这条线是否还存在。这使得搜索的时间复杂度为 O(log n) 而不是 O(n),这可以节省你很多时间......

如果您想知道最后编辑某行的时间,可以使用git blame

  • 有一种方法可以在 git 中搜索特定的字符串历史记录。这实际上很容易。检查我的答案以获取更多详细信息。 (2认同)