git: blame the previous version of each line changed in a given commit
Asked Answered
S

2

11

For a given commit, I need to run blame on the previous version of each line changed but said commit.

Having a commit that changes lines 2 and 3 on file a, I want a way to see the output of git blame commit^ limited to the preamble and the lines 2 and 3.

How can I do that?

EDIT

Seems like I have to explicitly specify that I'm looking for a way to do the above programmatically, and not examining the commit with my own eyes, determining which lines of which files where changed, and then manually run git blame for each single changed file.

Sexton answered 28/6, 2013 at 23:43 Comment(1)
possible duplicate of Git blame -- prior commits?Harwilll
I
0

You can try:

git blame <commit>^ -- filename | head -3 | tail -2
Incudes answered 3/7, 2013 at 19:51 Comment(2)
This does not actually do what the question asks -- it simply shows the previous commit, regardless of which lines were changed in this one. See some discussion on why this might be difficult here: git.661346.n2.nabble.com/…Buddy
That link is now broken.Functionary
G
0

You can use git log to trace the evolution of a range of lines. So if you already know you need to trace lines 2 through 3 on file a you can use git log -L2,3:a that's the format -L:<start>,<end>:<file>

git log will then only show commits that have touched those lines, and will automatically show you the patch. You can specify -L as many times as you need.

For more information, see the -L option on git log

Gastric answered 9/2 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.