Search code examples
gitgit-log

Get all the commits which have some piece of code (added/updated/deleted) for a given author


I want to check for a given author, how many commits has he committed when he was playing around with some given piece of code?

E.g:- Let us say an author, A has added Hello World! somewhere in file in the git repository and commited it. Then, he moved that line to another file and commited it. And finally, he completely removed it from git ecosystem. I want all 3 commits in my output.

I ran into this use-case while back. I googled and did some experiments after reading some online git log documentations. Therefore, I thought of sharing the knowledge with a bigger audience. Suggestions are welcome!


Solution

  • One can use the following code:

    git log --all -S "<Piece of code>" --author="<Author's Name/Email>" 
    

    Here, -S "<Piece of code>" will ask git to search for the commit logs with piece of code input, and then --author="<Author's Name/Email>" will apply an additional filter to check for desired author.

    Note: input to --author is case sensitive. One can resolve this issue by adding -i to above command.

    git log --all -S "<Piece of code>" -i --author="<Author's Name/Email>"