Steven Lu
Steven Lu

Reputation: 43427

Git: Is there a way to look up the changes made to a specific section of a file?

Use case:

I find a piece of code that I do not understand at all. I want to find the temporal context in which this code was written, and all of the changes that this part of the code underwent.

Basically look up the history of this file but only the parts of that history which intersect with [lines 34 thru 38 (as an example)].

Is there a command for this that is more useable than git log -p <file> | grep <some snippet matching mysterious code> and then going back to the log -p output to look at the context in the diff there.

What's sad is that I often find the need for this kind of functionality when reviewing code that I wrote.

Upvotes: 1

Views: 97

Answers (1)

Op De Cirkel
Op De Cirkel

Reputation: 29463

git blame <path to file> will give you list of commits with context (changes done at at that commit point in that file). Read git help blame for all the powers it has.

Then you can use git show <commit>:<path> to see the whole file at that commit point or git diff <commit> <commit> -- <path> to see the difference

Upvotes: 1

Related Questions