Git - how do I view the change history of a method/function?

后端 未结 7 1470
我寻月下人不归
我寻月下人不归 2020-11-28 19:55

So I found the question about how to view the change history of a file, but the change history of this particular file is huge and I\'m really only interested in the changes

7条回答
  •  再見小時候
    2020-11-28 20:13

    The correct way is to use git log -L :function:path/to/file as explained in eckes answer.

    But in addition, if your function is very long, you may want to see only the changes that various commit had introduced, not the whole function lines, included unmodified, for each commit that maybe touch only one of these lines. Like a normal diff does.

    Normally git log can view differences with -p, but this not work with -L. So you have to grep git log -L to show only involved lines and commits/files header to contextualize them. The trick here is to match only terminal colored lines, adding --color switch, with a regex. Finally:

    git log -L :function:path/to/file --color | grep --color=never -E -e "^(^[\[[0-9;]*[a-zA-Z])+" -3
    

    Note that ^[ should be actual, literal ^[. You can type them by pressing ^V^[ in bash, that is Ctrl + V, Ctrl + [. Reference here.

    Also last -3 switch, allows to print 3 lines of output context, before and after each matched line. You may want to adjust it to your needs.

提交回复
热议问题