16

if a line is modified back and forth between 2 versions several times, git blame seems to show only the latest commits on that line.

would it be possible to let it show all commits on that line?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
teddy teddy
  • 3,025
  • 6
  • 31
  • 48
  • See http://stackoverflow.com/questions/8435343/retrieve-the-commit-log-for-a-specific-line-in-a-file. Its doable, and quite easy. – Moshe Weitzman Jan 14 '16 at 21:13

5 Answers5

14

git blame can't do that itself (but see below for a workaround).

But git gui has a blame mode that allows you to drill down into commits.

Invoke it with git gui blame <filename> once installed.

Dragon
  • 2,017
  • 1
  • 19
  • 35
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
14

I don't know about showing all commits on that line at the same time, but you can "drill" through each change to the line by using git blame SHA~ -- filename. With each iteration of the blame, just insert the next most "recent" SHA which modified that line.

Example: The first time you run git blame foo.php you see the line was modified by f8e2e89a, so then you exit out and run git blame f8e2e89a~ -- foo.php, git will then show you who modified the line before f8e2e89a. Rinse and repeat as necessary.

Matt Styles
  • 456
  • 7
  • 12
2

You can't do what you want with git blame, but you might get close with a word-diff algorithm or some other custom diff tool. In particular, you could show a line-by-line word diff in your log output like so:

# Show deletions delimited with [- -], and additions with {+ +}.
git log --patch --word-diff=plain

See Also

Extract authorship information from git repository

Community
  • 1
  • 1
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
1

The purpose of git blame is to show which commit most recently modified which lines in a particular file. It does not have an option to show multiple versions of the same line.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • thanks. unfortunately I was looking for something similar to the blame button of perforce , which shows you all commits in history – teddy teddy Jun 14 '12 at 06:49
  • 1
    You can kind of simulate that by doing `git blame HEAD~n -- filename` where `n` starts at 0 and increases. This isn't interactive, but each time you increment the number you'll be looking further back in history. – cdhowie Jun 14 '12 at 15:21
  • hehehe . I used to do something similar in my svn days: perl -e 'for $v ( 0 .. $current_version ) { system(qq(svn diff -r).($v-1).qq( -r$v mysource.java ) } ' – teddy teddy Jun 16 '12 at 20:11
0

Based on the answers already provided here, I created a script called git-rblame in my PATH with following content:

#!/bin/bash

revision="HEAD"

while [ -n "${revision}" ]
do
    result=$(git blame "${revision}" "$@")
    revision="${result%% *}"
    if [[ "${revision}" != [a-z0-9]*[a-z0-9] ]]
    then
        revision=""
    else
        echo "${result}"
        revision="${revision}~"
    fi
done

Then I can call git rblame -L xxx,yyy myfilename and I'll get the full history for the file respective the content given. Given the fact that the line number might change, a meaningful regexp seems to work better.

Eric L.
  • 31
  • 3