5

I would like to find out all the authors that have changed a line of code. Git-log can do it on the file level. I read the man page and it seems like git log cannot do it on the line level. Is there a way to use git command to do it or I need to write a script for myself?

For my purpose, I would like to do this to all lines of code. I am doing this to produce authorship training data for my research.

user1440858
  • 115
  • 5
  • 2
    You can blame/annotate to get get the info on a per-revision level. "Who changed a line" starts becoming a fuzzy question once you bring in multiple revisions. – Sean McSomething Jun 18 '12 at 18:43
  • @Tomasz: It's quite useful to search history for a particular line, particularly when you're looking to see how long a bug's existed in the codebase or who's touched a particular function. – Christopher Jun 18 '12 at 21:30

1 Answers1

3

You want to combine the -S and --pretty flags on git log.

From the git log man page, the -S pickaxe search:

   -S<string>
       Look for differences that introduce or remove an instance of <string>. Note that this is different than the string simply appearing in diff output;
       see the pickaxe entry in gitdiffcore(7) for more details.

This flag looks for all changes to a particular string. Using an example from another SO answer, git log -g -Ssearch_for_this would search the history for all changes to "search_for_this".

Combine it with --pretty to get better results. For example, this command would search through the history for changes to 'foo' and return the shortened hash, followed by the author, two dashes, the first line of the message, two dashes, and the date:

$ git log -Sfoo --pretty=format:'%h %an -- %s -- %ad'
bc34134 Sally Developer -- Fixed all bugs and made all clients happy forever -- Tue Jan 31 17:41:17 2025 -0500
Community
  • 1
  • 1
Christopher
  • 42,720
  • 11
  • 81
  • 99
  • Do you mean by putting the content of the line after the -S option? This can help me find out the commit changing the string, but I still need to find out the previous form of the line and keep using the method until no previous version of the line? – user1440858 Jun 19 '12 at 16:15
  • Hmmm. Yes. This solution will only get you to the point where was introduced. If you want to track past that change, it won't work. Maybe this? https://github.com/renard/git-line-history Found via google. No idea if it works but it leverages `git blame`. – Christopher Jun 19 '12 at 16:25