View git history of specific line
GitGit Problem Overview
I want to view the commit history related to the specific line in the file. I know we can get the history of the file. Are there any commands available to sort only the commits related to one specific line?
Git Solutions
Solution 1 - Git
bomz gave the right option but with wrong syntax. Fixed line would look like this:
git log -L '/the line from your file/,+1:path/to/your/file.txt'
The meaning of argument to -L
is "find the first occurrence of regex /the line from your file/
, in path/to/your/file.txt
and show the log regarding one line range starting at this point (meaning, just this line, but you could say +5 instead)".
The caveat is, if the line contains characters with special meaning in regex, you need to escape them.
However, it's likely much simpler to use line number, like this:
git log -L15,+1:'path/to/your/file.txt'
(for line 15 of file path/to/your/file.txt
)
In both cases +1
can be replaced with bigger number to get more line, or with regex to match the end of selected range.
Detailed description from the docs:
> -L
Solution 2 - Git
You could use git log https://git-scm.com/docs/git-log
git log -L'the line from your file' -- path/to/your/file.txt
Solution 3 - Git
The other answers here cover the question as asked very well, but sometimes you want to see this history for another branch without first checking out that branch.
git log branch-name -L '/regex/',+1:path/to/your/file.txt
works very well for these cases. Or you already know the line number:
git log branch-name -'L15,+1':'path/to/your/file.txt'