代码之家  ›  专栏  ›  技术社区  ›  zadam

在Git中查找第一行代码[重复]

  •  12
  • zadam  · 技术社区  · 12 年前

    我正在尝试跟踪一段代码(仅一行),以确定其提交。

    使用“责备”只会将我指向一个提交,其中这一行是作为提取方法风格重构的一部分移动的。

    如果代码库中第一次存在特定的文本,我可以使用哪些技术进行提交?

    3 回复  |  直到 11 年前
        1
  •  15
  •   Community Mohan Dere    9 年前

    如中所述 this answer git: finding a commit that introduced a string ,您可以简单地使用

    git log -S <line>
    # or
    git log -G <regex>
    

    查找存储库中第一行代码。

    official Linux kernel Git documentation for log :

    -S<string>
    

    查找引入或删除实例的差异 <string> 注意,这不同于仅在diff输出中出现的字符串;请参见 报关进口 gitdiffcore(7) 了解更多详情。

    -G<regex>
    

    查找添加或删除的行与给定行匹配的差异 <regex> .

        2
  •  0
  •   michas    11 年前

    我通常这么做 git log -p filename 和搜索(按 / )对于给定文本。

    通过这种方式,您可以注意到该行的更改,如果文本在过去发生了更改,则可以继续搜索(稍微)不同的文本。

        3
  •  -1
  •   Chandrayya G K    12 年前

    我可能会满足于:

    function allcommits { 
        git rev-list --reverse HEAD
    }
    
    
    for i in `allcommits` ;do  
        git co -f $i 2>/dev/null ; 
        echo commit:$i ;
        git grep -q 'that line of code' && break
    done
    

    从第一个提交开始,检查每个提交,并在每个步骤中扫描所有文件,直到找到匹配项。