Using git to find first introduction of token on a specific line of a file

前端 未结 5 1033
星月不相逢
星月不相逢 2021-01-04 07:36

Let\'s say I have a file A.cpp, and I notice an error on line 15 of the file. Let\'s say the error is a \"const\" on a function that returns a pointer to a member variable,

5条回答
  •  悲&欢浪女
    2021-01-04 07:40

    The change may not have always been on line 15 of A.cpp, so use the surrounding context. Say it was a definition of const int foobar:

    git grep 'const *int *foobar' \
      $(git log --reverse --pretty=format:%H -- A.cpp) -- \
      A.cpp | head -1
    

    This searches forward in time through all commits on the current branch that touch A.cpp and finds the first one that contains the offending pattern. The output will be the commit's SHA-1 and the matching line in its revision of A.cpp.

    Once you know the commit, use git show to learn the author.

提交回复
热议问题