git blame throws a bad revision error with the -L <n,m> option

2.6k Views Asked by At

One of the git blame options processes a line range. The manual says:

-L Process only line range n,m, counting from 1

Now, I have a file that has over 100 lines. When I run git blame -L 5,15 myFile.txt, git complains:

fatal: bad revision '15'

Interestingly, git does not complain when I run git blame -L 5 myFile.txt.

What's going on?

3

There are 3 best solutions below

1
On BEST ANSWER

Your command looks correct in that case.

I have checked that problem with my composer.json file, and it's working well. When I try to access more lines than are inside the file, I get an error "file composer.json has only 87 lines".

You get this error only if you have a space before the second value.

git blame -L 10, 200 composer.json 

fatal: bad revision '200'

So I think that's the problem.

Note that PowerShell and/or Posh-Git might inject a space after the comma. Try using the command prompt.

0
On

Note: with git 2.19 (Q3 2018), parsing of -L[<N>][,[<M>]] parameters "git blame" and "git log" take has been tweaked.

That should avoid certain case of fatal: bad revision '15', especially when the file has less than 15 lines:

See commit 7f81c00, commit 96cfa94 (15 Jun 2018) by Isabella Stephens (``).
(Merged by Junio C Hamano -- gitster -- in commit 6566a91, 02 Aug 2018)

blame: prevent error if range ends past end of file

If the -L option is used to specify a line range in git blame, and the end of the range is past the end of the file, git will fail with a fatal error. This commit prevents such behavior - instead we display the blame for existing lines within the specified range.

This commit also fixes two corner cases.

  • Blaming -L n,-(n+1) now blames the first n lines of a file rather than from n to the end of the file.
  • Blaming -L ,-n will be treated as -L 1,-n and blame the first line of the file, rather than blaming the whole file.
0
On

if you quote the lines it will work

git blame -L '10,200' composer.json