如何找到哪个提交在 git 中引入了特定行?或者有更好的选择

  • 本文关键字:或者 选择 更好 何找 提交 git git
  • 更新时间 :
  • 英文 :


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

如果没有,除了每次提交以查找我特别感兴趣的行集之外,是否有其他选择?

泰。

特定的代码行?就像你知道提前知道这条线是什么?当然有可能。事实上,这很容易。只需使用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.

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

git log -S"public class Foo"

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

git log -S"public class Foo" -- Foo.java

通常,请使用以下内容:

git log -S<string> [-- <file>]
您可以使用

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

如果想知道上次编辑行的时间,可以使用 git blame

相关内容

最新更新