Can Git show history for selected lines?
Asked Answered
H

6

21

I have a large file where, somewhere in the middle, there is a function that I know has been modified several times. git annotate or blame will show the most recent commit for each line but if I'm right, it will show only the most recent one, not a list of other commits that affected that line before.

So is there a command in Git where I could say something like show me commits that affected lines 250..260 in file XYZ?

Homophone answered 30/5, 2012 at 12:44 Comment(4)
not line, but git log <paths>… - show only commits that affect the specified paths. maybe it helpsMarybelle
Possible duplicate of how do I view the change history of a method/function?Bibliofilm
Thanks @CharlesB, I found some good advice there and posted it as an answer here.Homophone
possible duplicate of Retrieve the commit log for a specific line in a file?Higgledypiggledy
H
18

As suggested in one of the comments in Git - how do I view the change history of a method/function?, doing

git gui blame <file>

and then right-clicking a line and selecting Blame Parent Commit does what I need.

Homophone answered 30/5, 2012 at 13:6 Comment(0)
M
18

phpStorm can do this. Select the lines -> right-click -> Git -> Show History for Selection

Maharaja answered 21/3, 2013 at 6:25 Comment(2)
what an awesome feature! Thanks for bringing it to my attention!!Enjambement
intellij does it tooAdne
M
7
git show $(git blame XYZ -L 250,260 | awk '{print $1}')

Every line of code is always documented. via @mislav

Miler answered 28/2, 2014 at 8:10 Comment(1)
If only the results were chronological.Instrumentalism
B
2

I think that's not possible. You will need to write a script around git blame. You can find a bash example of such a script on GitHub.

Beaty answered 30/5, 2012 at 12:50 Comment(0)
C
1

Select line -> Right-click->Git->Annotate

Clemens answered 21/3, 2014 at 7:55 Comment(1)
this doesnt answer the question as it will only show the most recent commit to affect a single line.Adne
C
1

this fonctionnality made me switch to IntelliJ Idea. With IntelliJ you can select lines, right clic -> git -> Show history for selection enter image description here

Castaneda answered 19/4, 2022 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.