Search code examples
gitgit-blame

How to get source line numbers in a specific revision of a file?


It's possible to see source/original line numbers of lines with git blame But it shows line numbers according to the last commit that made a modification in the line

I want to do the same for a specific commit/revision of a file.

Example,

File: file.ext (xyz11 is the revision/commit of the file we're currently reviewing)

Content:

Line 1 (**abc11** is the last commit changed this line)
Line 2 (**abc12** is the last commit changed this line)
Line 3 (**abc13** is the last commit changed this line)

I want to get "3" for "Line 3". Git blame will show this info according to the line's commit (abc13) commit. But, since xyz11 and abc13 revisions contain different contents, actual line number in the xyz11 may be different.

So how can I get line numbers in a specific revision of a file?

Note: I said "source/original line number" Because I want to get correct line numbers even if document is dirty (has uncommited changes) It is possible with git blame

My scenario is, I'll use these line numbers in API request to add inline comments

So, suppose I've modified the file.ext

Line 1
Line 2
Uncommited Line 
Uncommited Line
Line 3

I should get still "3" for "Line 3" instead of "5", otherwise comment will go to wrong line. As I said, its possible with git blame but it shows this info according to the line's commit

Thanks


Solution

  • There is an option rev for git blame so, you can specify the commit/revision you want to blame:

    git blame <rev> file
    

    Example

    git blame xyz11 file.txt
    

    More info in the docs