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

前端 未结 5 1037
星月不相逢
星月不相逢 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:50

    There are a few possible ways of doing it.

    • git blame, or better graphical blame tool (like git gui blame or the blame view in git instaweb / gitweb) to browse history of a line, going back in history until you find appropriate commit.

    • so called "pickaxe search", i.e. git log -S with appropriate token / regexp, to find (list) all commits where number of given tokens changed (which usually means where given token was added or deleted), e.g.:

      git log --reverse -p -S'const int foobar' -- A.cpp
      
    • git bisect where "bad" commit would mean the one with 'const' where there it shouldn't be (testing using e.g. grep).

提交回复
热议问题