stackyyflow stackyyflow - 1 year ago 42
Git Question

Git blame commit changes for specific file of specific range of code

I would like to find out the commits for a specific file from line x to y. The following command outputs the commit changes but I would like to know how to retrieve just the first 8 characters of the commit changes using git blame?

git blame -L 50,60 filename.txt

Sample output:

Also, what are the differences when I use the command below vs the command above in terms of number of commits for a specific file from line x to y?

git log -L 50,60:filename.txt

Answer Source

To get just the short commit from git blame you can pipe it through awk like so:

git blame -L 50,60 filename.txt | awk '{print $1}'

This will print the first field from the blame command, which in this case is the commit.

For your second question:

git log will show you all the commits in which those lines have changed.

git blame will only show you who changed those lines and in which commit they were most recently changed. You will never get any history of the changes.

You can pass additional parameters into the git log command to trim your output to exactly what you want or you can pipe it through other commands to get your results.