Determine when a line was modified in a file

1.8k views Asked by At

In a git repository used by many people, I found some code code I'm interested in, but it's not fully fleshed out.

I have no way of knowing whether this is a recent or old change, so I hesitate to just browse back through all commits on the file.

Is it possible find when the line of code appeared in the repository so I can talk to the original person who posted it?

1

There are 1 answers

4
Gabriele Petronella On BEST ANSWER

This is the perfect case for git blame.

As per its man page

Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision.

You can use it like follows

git blame -- filename

Optionally (advisable, I'd say) you can output the result on a file

git blame -- filename > blame.out

and open the file with any editor supporting the git blame syntax. I use for instance Sublime Text 2.

Other tips from comments:

git gui blame -- filename

will start a basic gui interface, which can be helpful.

Also

git blame -L 42,+1 -- filename

will show only line 42, in case you are interested in a specific line.