How-to: Find Who Wrote A Line in git Repository
I’m building on a previous article
where I categorize all the rubocop issues.
My goal is to find out who committed the infraction. Not to point
fingers, just to see if there are problems I can solve further up in
the development process.
I review two ways to figure out how to get who wrote a line of code in
a git repository using
git blame, one using
grep and another using
git blame syntax:
Understanding what built-in options to a program are can solve
problems better than working around it with external tools.
This article will take you less than four minutes to read.
source and more information
I would just like a command that tells me who wrote a specific line of
Is that too much to ask for?? :-)
If the version control of the code I am investigating uses
For example from my TDD
The format of the output of
git blame is:
This is good, it has what I want: for a given filename and line
number, return who wrote the code for a specific line.
It’s great to get details of the whole file. How can I narrow this
Let’s try both and see how they work.
One way to do this is to use grep for the specific line number you want:
For example, line 25:
What if I want line 33?
Oh, it’s easy to just grep for
What if I escape it:
Better, not exactly what I want and it would produce false positives
or require additional filtering, definitely possible with
Is there a better way? Let’s try the next option:
git blame -L
git blame has a feature to list a specific range of lines of a file:
Hmm, too much output. I wonder if I can just specify the beginning to
be the same as the end.
What about line 33? That failed miserably using
Nice! Exactly the output expected. No additional processing needed.
Figuring out who wrote a specific line of code is best using the
Which returns the output in the following format:
I am glad I dug a bit more into
git documentation as using
would have been an OK solution, overlapping numbers in the code
section would require additional (surprising) work.