3

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

Schwern
  • 153,029
  • 25
  • 195
  • 336
user3790180
  • 433
  • 5
  • 12

2 Answers2

3

If I understand you correctly, you have a file with uncommitted changes and your git blame looks like this.

$ git blame foo

^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 3) Uncommitted Line
00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 4) Uncommitted Line
^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3

Use -n to show what line it is in the original commit.

$ git blame -n foo

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 3 (Not Committed Yet  2019-01-01 12:58:47 -0800 3) Uncommitted Line
00000000 4 (Not Committed Yet  2019-01-01 12:58:47 -0800 4) Uncommitted Line
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3

To ignore all uncommitted and unstaged changes, use git blame <file> HEAD. HEAD is the last commit. This will look for all changes to the file from HEAD backwards. Because intervening commits will also throw the line numbers off, you'll still want -n to get the line number in that commit. For example.

$ git blame -n foo

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
00000000 3 (Not Committed Yet  2019-01-01 13:03:06 -0800 3) Uncommitted line
4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 4) Line 2.5
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3

$ git blame -n foo HEAD

^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 3) Line 2.5
^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 4) Line 3
Schwern
  • 153,029
  • 25
  • 195
  • 336
1

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

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16